-
Notifications
You must be signed in to change notification settings - Fork 2
/
QuantLoop.r
158 lines (121 loc) · 6.1 KB
/
QuantLoop.r
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
library('quantmod')
#library('zoo')
#library('xts')
#Rules & Settings ------------------------------------------------------------------------
mySymbol <- AUY
dayRange <- 90
daysBodyRule <- .02
daysRangeRule <- .08
daysUpperShadowRule <- .03
daysLowerShadowRule <- .08
prevDaysBodyRule <- .02
prevDaysRangeRule <- .08
prevDaysUpperShadowRule <- .02
prevDaysLowerShadowRule <- .02
#end rules --------------------------------------------------------------------
# ETF List: http://en.wikipedia.org/wiki/List_of_American_exchange-traded_funds#Small-cap_ETFs
smallCapETFs <- c("IWM","IWN","IWO","IJR","IJT","IJS","VIOO","VIOG","VIOV","VB","VBK","VBR","VTWO","VTWG","VTWV","VXF")
majorETFs <- c("SPY","DIA","IOO","IVV","VOO","VXF","VONE","VTWO","IWM","OEF","QQQ","WXSP")
testList <- c("SPY","AUY","DIA")
# end stock lists -------------------------------------------------------------
# Load Stock Data
loadSymbols <- testList
# add Try/Catch logic
getSymbols(loadSymbols,src="yahoo")
#My Indicators
#Today's Numbers -----------------------------------------------------------
daysBodyAbs <- abs(Cl(mySymbol) - Op(mySymbol))/Cl(mySymbol) # Body Size
daysBody <- (Cl(mySymbol) - Op(mySymbol))/Cl(mySymbol) # Body Size
daysRange <- (Hi(mySymbol) - Lo(mySymbol))/Hi(mySymbol) # Low to High
daysUpShadow <- ifelse(Cl(mySymbol) > Op(mySymbol),
(Hi(mySymbol) - Cl(mySymbol))/Hi(mySymbol),
(Hi(mySymbol) - Op(mySymbol))/Hi(mySymbol)) # Upper Shadow Size
daysLoShadow <- ifelse(Cl(mySymbol) > Op(mySymbol),
(Op(mySymbol) - Lo(mySymbol))/Op(mySymbol),
(Cl(mySymbol) - Lo(mySymbol))/Cl(mySymbol)) # Lower Shadow Size
daySummary <- daysBodyAbs + daysRange + daysUpShadow + daysLoShadow
shadowLength <- daysRange - daysBodyAbs
#----------------------------------------------------------
#Yesterday's Numbers ----------------------------------------------------
prevDaysBodyAbs <- abs(Lag(Cl(mySymbol)) - Lag(Op(mySymbol)))/Lag(Cl(mySymbol)) # Body Size
prevDaysBody <- (Lag(Cl(mySymbol)) - Lag(Op(mySymbol)))/Lag(Cl(mySymbol)) # Body Size
prevDaysRange <- (Lag(Hi(mySymbol)) - Lag(Lo(mySymbol)))/Lag(Hi(mySymbol)) # Low to High
prevDaysUpShadow <- ifelse(Lag(Cl(mySymbol)) > Lag(Op(mySymbol)),
(Lag(Hi(mySymbol)) - Lag(Cl(mySymbol))) / Lag(Hi(mySymbol)),
(Lag(Hi(mySymbol)) - Lag(Op(mySymbol)))/Lag(Hi(mySymbol))) # Upper Shadow Size
prevDaysLoShadow <- ifelse(Lag(Cl(mySymbol)) > Lag(Op(mySymbol)),
(Lag(Op(mySymbol)) - Lag(Lo(mySymbol))) / Lag(Op(mySymbol)),
(Lag(Cl(mySymbol)) - Lag(Lo(mySymbol))) / Lag(Cl(mySymbol))) # Lower Shadow Size
prevDaySummary <- prevDaysBodyAbs + prevDaysRange + prevDaysUpShadow + prevDaysLoShadow
prevDayShadowLength <- prevDaysRange - prevDaysBodyAbs
#----------------------------------------------------------
### Today's Decisions ----------------------------------------------------
daysBodyDecision <- ifelse(daysBodyAbs > daysBodyRule,1,0)
daysRangeDecision <- ifelse(daysRange > daysRangeRule,1,0)
daysUpShadowDecision <- ifelse(daysUpShadow > daysUpperShadowRule,1,0)
daysLoShadowDecision <- ifelse(daysLoShadow < daysLowerShadowRule,1,0)
todayScore <- (daysBodyDecision +
daysRangeDecision +
daysUpShadowDecision +
daysLoShadowDecision)
#-------------------------------------------------------------
### Prev Day's Decisions ----------------------------------------------------
prevDaysBodyDecision <- ifelse(prevDaysBodyAbs > prevDaysBodyRule,1,0)
prevDaysRangeDecision <- ifelse(prevDaysRange > prevDaysRangeRule,1,0)
prevDaysUpShadowDecision <- ifelse(prevDaysUpShadow > prevDaysUpperShadowRule,1,0)
prevDaysLoShadowDecision <- ifelse(prevDaysLoShadow < prevDaysLowerShadowRule,1,0)
prevDayScore <- (prevDaysBodyDecision +
prevDaysRangeDecision +
prevDaysUpShadowDecision +
prevDaysLoShadowDecision)
#-------------------------------------------------------------
#--Bullish Engulfing Pattern --------#Rules are in negative order -----------
BullishEngulfing <- ifelse(
prevDaysBody > -0.001, 0,
ifelse(prevDaysBody < -.02, 0,
ifelse(daysBody < prevDaysBodyAbs, 0,
ifelse(Op(mySymbol)>Lag(Op(mySymbol)), 0,
ifelse(Op(mySymbol)>Lag(Cl(mySymbol)), 0,
ifelse(Cl(mySymbol)<Lag(Op(mySymbol)), 0,
ifelse(Cl(mySymbol)<Lag(Op(mySymbol)), 0, 1
)))))))
#-------------------------------------------------------------
overallScore <- todayScore + prevDayScore
## --------------------------------------
## Charting -------------------------------------------------------------------------
candleChart(last(mySymbol,dayRange),dn.col="black",up.col="white",theme="white")
addBBands()
addMACD()
addTA(last(BullishEngulfing,dayRange), col="blue") # test
# addTA(last(daysBodyDecision,dayRange), col="blue")
# addTA(last(daysRangeDecision,dayRange), col="brown")
# addTA(last(daysUpShadowDecision,dayRange), col="green")
# addTA(last(daysLoShadowDecision,dayRange), col="red")
addTA(last(overallScore,dayRange))
#
#
# addTA(last(daysBody,dayRange), col="blue")
# addTA(last(daysRange,dayRange), col="brown")
# addTA(last(daysUpShadow,dayRange), col="green")
# addTA(last(daysLoShadow,dayRange), col="red")
# addTA(last(daySummary,dayRange), col="purple")
# addTA(last(shadowLength,dayRange))
#
# # Cross Correlation
# #ccf(last(drop(SPY$SPY.Open), 90),last(drop(daysRange), 90))
# #ccf(last(drop(SPY$SPY.Open), 400),last(drop(GLD$GLD.Open), 400))
# ccf(last(drop(daysRange),dayRange),last(drop(ClCl(SPY)),dayRange))
#
#
# #Plot various metrics...
# #plot(last(daysRange,90),col="blue")
# #lines(last(daysBody,90),col="red")
#
#
# plot(last(dailyReturn(SPY),dayRange))
# lines(last(OpCl(SPY),dayRange),col="blue")
# daySummary
# par(bg="gray")
# plot(last(daysUpShadow,dayRange),lwd=2,col="green")
# lines(last(ClCl(SPY),dayRange)*100,lwd=2)
# lines(last(daysLoShadow,dayRange),lwd=2, col="red")