-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtechnicalsLoop.R
More file actions
97 lines (75 loc) · 3.22 KB
/
Copy pathtechnicalsLoop.R
File metadata and controls
97 lines (75 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
##########################################
## Loop thru files in stockData and compute technicals for each
## technicals computed are: SMA10, EMA10, ATR, RSI, Delta-1, Delta-5
##########################################
##########################################
technicalsLoop <- function(){
library(quantmod)
sourcePath = "/Users/ehren/Documents/StockAnalysis/stockData/"
destinationPath = "/Users/ehren/Documents/StockAnalysis/stockTechnicals/"
errorPath = "/Users/ehren/Documents/StockAnalysis/errors/"
theFiles = list.files(path=sourcePath,pattern=".csv")
errorTable <- data.frame(TIME=0,SYMBOL=0, ERROR=0)
k=0 # error counter for insufficient data
fileCount = 0
startTime = Sys.time()
for (ii in theFiles){
#data = read.csv(paste(sourcePath,ii,sep=""))
## read .csv file as zoo
data <- read.zoo(paste(sourcePath,ii,sep=""), header = TRUE, sep = ",")
#print(paste(ii," loaded", sep=""))
if (NROW(data) > 14) {
## Compute the Simple Moving Average (SMA) with a 10 day period
data$SMA10 <- SMA(data$close, n=10)
#print(paste("SMA10 calculated for ", ii, sep=""))
## Compute the Exponential Moving Average (EMA) with a 10 day period
data$EMA10 <- EMA(data$close,n=10,wilder=FALSE, ratio=NULL)
#print(paste("EMA10 calculated for ", ii, sep=""))
## compute ATR with a 14 day period
data <- cbind(data, ATR(data[,c("high","low","close")], n=14))
## Compute RSI for the CLOSE price w/ period of 14
data$RSI <- RSI(data$close, n = 14)
## Compute the relative "Delt" in the price over last 1 and 5 days
data <- cbind(data, Delt(data$close, k= c(1,5), type = "arithmetic"))
}
## Compute the MACD data with standard parameters
if (nrow(data)>=30){
data <- cbind(data,MACD(data$close, nFast = 26, nSlow = 12, maType = "EMA", percent = FALSE))
# print(paste(fileCount,": ", ii," MACD"))
# data <- cbind(data,MACD(data$volume, nFast = 26, nSlow = 12, percent = TRUE))
} #end MACD check
else {
k=k+1
data$SMA10 <- NA
data$EMA10 <- NA
data$atr <- NA
data$RSI <- NA
data$RSI <- NA
data$Delt.1.arithmetic <- NA
data$Delt.5.arithmetic <- NA
data$macd <- NA
data$signal <- NA
# error logging functions
errorTable[k,] <- NA
errorTable$TIME[k] <- paste(Sys.time())
errorTable$SYMBOL[k] <- ii
errorTable$ERROR[k] <- "Insufficient data"
#print(paste("Insufficient data for: ", ii, sep=""))
}
## remove the "tr", "trueLow", and "trueHigh" columns from ATR calculation
data <- data[,! colnames(data) %in% c("tr","trueLow","trueHigh")]
# rename ATR
names(data)[names(data) == "atr"] <- "ATR"
names(data)[names(data) == "macd"] <- "MACD"
names(data)[names(data) == "signal"] <- "MACD Signal"
#### INSERT OTHER TECHNICALS ####
## REMOVE EXCESS COLUMNS FROM PRICE DATA
data <- data[,! colnames(data) %in% c("open","high","low","volume","adj.")]
## Write the technicals to a .csv file at destintionPath location
write.zoo(data,paste(destinationPath, ii,sep=""),sep=",",row.names=FALSE)
fileCount = fileCount + 1
}
write.table(errorTable,paste(errorPath,Sys.Date(),"_tech_errors.csv", sep=""),sep=",",row.names=FALSE)
print(Sys.time()-startTime)
print(fileCount)
} #end function loop