This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathich_trailing_stop_strat.pine
264 lines (229 loc) · 14.2 KB
/
ich_trailing_stop_strat.pine
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//@version=3
strategy(title="Pleo_Ross Ichimoku & Trailing Stop V1 Strategy", shorttitle="PleoIchTSV1Strategy", overlay=true, default_qty_value=1, commission_type=strategy.commission.percent, commission_value=0.075, backtest_fill_limits_assumption=1)
trailPoints = input(140, "Trail Points (in ticks)", type=integer)
trailOffset = input(100, "Trail Offset (in ticks)", type=integer)
testType = input('Trail Points', "Test Points or Price", options=['Trail Points', 'Trail Price'])
allowsStrongSignal = input(true, "Enables strong signals", type=bool)
allowsMediumSignal = input(true, "Enables medium signals", type=bool)
allowsWeakSignal = input(true, "Enables weak signals", type=bool)
usingCustomTrailingStop = input(true, "Uses custom trailing stop", type=bool)
// === INPUT BACKTEST RANGE ===
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromYear = input(defval = 2017, title = "From Year", minval = 2017)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToYear = input(defval = 9999, title = "To Year", minval = 2017)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"
//Ichimoku input Logic
conversionPeriods = input(9, minval=1, title="Conversion Line Periods"),
basePeriods = input(26, minval=1, title="Base Line Periods")
laggingSpan2Periods = input(52, minval=1, title="Lagging Span 2 Periods"),
displacement = input(26, minval=1, title="Displacement")
//Ichimoku function Logic
donchian(len) => avg(lowest(len), highest(len))
//Ichimoku line Logic
tenkanLine = donchian(conversionPeriods)
kijunLine = donchian(basePeriods)
leadLine1 = avg(tenkanLine, kijunLine)
leadLine2 = donchian(laggingSpan2Periods)
isAggressiveStrategy = input(false, title="Use aggressive closing of positions")
// store state of open position (whether long or short)
lastOpenPosition = 0.0
lastOpenPosition := lastOpenPosition[1]
price = 0.0
price := na
// Chikou Logic
isAboveCloud = open > leadLine1[displacement] and open > leadLine2[displacement]
isBelowCloud = open < leadLine1[displacement] and open < leadLine2[displacement]
isChikouAboveCloud = high > leadLine1[displacement * 2] and high > leadLine2[displacement * 2]
isChikouBelowCloud = low < leadLine1[displacement * 2] and low < leadLine2[displacement * 2]
isChikouAboveAllLines = isChikouAboveCloud and high > tenkanLine[displacement] and high > kijunLine[displacement]
isChikouBelowAllLines = isChikouBelowCloud and low < tenkanLine[displacement] and low < kijunLine[displacement]
isChikouInClearSpace = (high > (high[displacement] + 5.00) and isChikouAboveAllLines) or (low < (low[displacement] - 5.00) and isChikouBelowAllLines)
useChikou = input(false, title="Use Chikou Clear Space")
// Logic for open positions
inOpenPosition = lastOpenPosition[1] != 0
inPreviousBuy = lastOpenPosition[1] > 0
inPreviousSell = lastOpenPosition[1] < 0
isBuySignal = crossover(tenkanLine, kijunLine) and not inPreviousBuy
isSellSignal = crossunder(tenkanLine, kijunLine) and not inPreviousSell
isStrongBuySignal = isBuySignal and isAboveCloud and (useChikou ? isChikouInClearSpace : true)
isStrongSellSignal = isSellSignal and isBelowCloud and (useChikou ? isChikouInClearSpace : true)
isWeakBuySignal = isBuySignal and isBelowCloud and (useChikou ? isChikouInClearSpace : true)
isWeakSellSignal = isSellSignal and isAboveCloud and (useChikou ? isChikouInClearSpace : true)
isMediumSellSignal = (isSellSignal and not isBelowCloud and not isAboveCloud) or (isSellSignal and isBelowCloud and (useChikou ? isChikouInClearSpace : true))
isMediumBuySignal = (isBuySignal and not isBelowCloud and not isAboveCloud) or (isBuySignal and isAboveCloud and (useChikou ? isChikouInClearSpace : true))
// Logic for exit positions
isAboveAllLines = isAboveCloud and close > tenkanLine and close > kijunLine
isBelowAllLines = isBelowCloud and close < tenkanLine and close < kijunLine
isBuyExit = inPreviousBuy and (isAggressiveStrategy ? isBelowAllLines or crossunder(close, kijunLine) : isSellSignal) and not isSellSignal
isSellExit = inPreviousSell and (isAggressiveStrategy ? isAboveAllLines or crossover(close, kijunLine) : isBuySignal) and not isBuySignal
// Visualise the Opening positions
plotshape(isWeakBuySignal, style=shape.arrowup, location=location.abovebar, color=inPreviousBuy ? green : gray, size=size.small, text="wb")
plotshape(isMediumBuySignal, style=shape.arrowup, location=location.abovebar, color=inPreviousBuy ? green : gray, size=size.normal, text="Mb")
plotshape(isStrongBuySignal, style=shape.arrowup, location=location.abovebar, color=inPreviousBuy ? green : gray, size=size.large, text="SB")
plotshape(isWeakSellSignal, style=shape.arrowdown, location=location.belowbar, color=inPreviousSell ? red : gray, size=size.small, text="ws")
plotshape(isMediumSellSignal, style=shape.arrowdown, location=location.belowbar, color=inPreviousSell ? red : gray, size=size.normal, text="Ms")
plotshape(isStrongSellSignal, style=shape.arrowdown, location=location.belowbar, color=inPreviousSell ? red : gray, size=size.large, text="SS")
// Visualise the Exit positions
plotshape(isBuyExit, style=shape.xcross, location=location.abovebar, color=green, size=size.small)
plotshape(isSellExit, style=shape.xcross, location=location.belowbar, color=red, size=size.small)
if isBuyExit
strategy.close("buy")
lastOpenPosition := 0
price := close
if isSellExit
strategy.close("sell")
lastOpenPosition := 0
price := close
if (isBuySignal)
strategy.close("sell")
price := close
if isStrongBuySignal and allowsStrongSignal
lastOpenPosition := 3
strategy.order("buy", strategy.long, 3, comment="Strong buy", when=window())
strategy.exit("Trailing Stop (buy)", "buy", trail_points= trailPoints, trail_offset= trailOffset, when= testType == 'Trail Points' and window() and not usingCustomTrailingStop)
else
if isMediumBuySignal and allowsMediumSignal
lastOpenPosition := 2
strategy.order("buy", strategy.long, 2, comment="Medium buy", when=window())
strategy.exit("Trailing Stop (buy)", "buy", trail_points= trailPoints, trail_offset= trailOffset, when= testType == 'Trail Points' and window() and not usingCustomTrailingStop)
else
if isWeakBuySignal and allowsWeakSignal
lastOpenPosition := 1
strategy.order("buy", strategy.long, 1, comment="Weak buy", when=window())
strategy.exit("Trailing Stop (buy)", "buy", trail_points= trailPoints, trail_offset= trailOffset, when= testType == 'Trail Points' and window() and not usingCustomTrailingStop)
if (isSellSignal)
strategy.close("buy")
price := close
if isStrongSellSignal and allowsStrongSignal
lastOpenPosition := -3
strategy.order("sell", strategy.short, 3, comment="Strong sell", when=window())
strategy.exit("Trailing Stop (sell)", "sell", trail_points= trailPoints, trail_offset= trailOffset, when= testType == 'Trail Points' and window() and not usingCustomTrailingStop)
else
if isMediumSellSignal and allowsMediumSignal
lastOpenPosition := -2
strategy.order("sell", strategy.short, 2, comment="Medium sell", when=window())
strategy.exit("Trailing Stop (sell)", "sell", trail_points= trailPoints, trail_offset= trailOffset, when= testType == 'Trail Points' and window() and not usingCustomTrailingStop)
else
if isWeakSellSignal and allowsWeakSignal
lastOpenPosition := -1
strategy.order("sell", strategy.short, 1, comment="Weak sell", when=window())
strategy.exit("Trailing Stop (sell)", "sell", trail_points= trailPoints, trail_offset= trailOffset, when= testType == 'Trail Points' and window() and not usingCustomTrailingStop)
readyToEvalTrigger = not isBuySignal and not isSellSignal and not isSellExit and not isBuyExit
////////////////
// TRAILING STOP STRATEGY 1
////////////////
offset = input(defval=2, title="Offset %", type=float, minval=0.01, maxval=100, step=0.1)
buyMaxTrigger = input(title="Buy Max trigger", type=source, defval=high)
sellMaxTrigger = input(title="Sell Max trigger", type=source, defval=low)
srcTrigger = input(title="Source trigger", type=source, defval=close)
highest1 = buyMaxTrigger
highest1 := highest1[1]
if inPreviousBuy
if buyMaxTrigger > highest1[1]
highest1 := buyMaxTrigger
else
highest1 := buyMaxTrigger
lowest1 = sellMaxTrigger
lowest1 := lowest1[1]
if inPreviousSell
if sellMaxTrigger < lowest1[1]
lowest1 := sellMaxTrigger
else
lowest1 := sellMaxTrigger
buyTsl = highest1 * (1-(offset/100))
plot(highest1, style=line, color=inPreviousBuy ? olive : white, title="Highest Position", linewidth=1, transp=70)
plot(buyTsl, style=line, color=inPreviousBuy ? olive : white, title="Highest Trailing Stop Position", linewidth=2)
sellTsl = lowest1 * (1+(offset/100))
plot(lowest1, style=line, color=inPreviousSell ? orange : white, title="Lowest Position", linewidth=1, transp=70)
plot(sellTsl, style=line, color=inPreviousSell ? orange : white, title="Lowest Trailing Stop Position", linewidth=2)
hasTriggeredBuyStop = inPreviousBuy and crossunder(srcTrigger, buyTsl) and usingCustomTrailingStop and readyToEvalTrigger
hasTriggeredSellStop = inPreviousSell and crossover(srcTrigger, sellTsl) and usingCustomTrailingStop and readyToEvalTrigger
if hasTriggeredBuyStop
strategy.close("buy")
lastOpenPosition := 0
price := 0
if hasTriggeredSellStop
strategy.close("sell")
lastOpenPosition := 0
price := 0
plotshape(hasTriggeredBuyStop, style=shape.xcross, location=location.abovebar, color=olive, size=size.small, text="Trailing\nStop")
plotshape(hasTriggeredSellStop, style=shape.xcross, location=location.abovebar, color=orange, size=size.small, text="Trailing\nStop")
// ////////////////
// // TRAILING STOP STRATEGY 2
// ////////////////
// last_high = buyMaxTrigger
// last_low = sellMaxTrigger
// last_high := not inPreviousBuy ? na : inPreviousBuy and (na(last_high[1]) or buyMaxTrigger > nz(last_high[1])) ? buyMaxTrigger : nz(last_high[1])
// last_low := not inPreviousSell ? na : inPreviousSell and (na(last_low[1]) or sellMaxTrigger < nz(last_low[1])) ? sellMaxTrigger : nz(last_low[1])
// buyTsl = last_high * (1-(offset/100))
// plot(last_high, style=line, color=inPreviousBuy ? olive : white, title="Highest Position", linewidth=1, transp=70)
// plot(buyTsl, style=line, color=inPreviousBuy ? olive : white, title="Highest Trailing Stop Position", linewidth=2)
// sellTsl = last_low * (1+(offset/100))
// plot(last_low, style=line, color=inPreviousSell ? orange : white, title="Lowest Position", linewidth=1, transp=70)
// plot(sellTsl, style=line, color=inPreviousSell ? orange : white, title="Lowest Trailing Stop Position", linewidth=2)
// long_ts = not na(last_high) and srcTrigger <= buyTsl and usingCustomTrailingStop and readyToEvalTrigger
// short_ts = not na(last_low) and srcTrigger >= sellTsl and usingCustomTrailingStop and readyToEvalTrigger
// if long_ts
// strategy.close("buy")
// lastOpenPosition := 0
// price := 0
// if short_ts
// strategy.close("sell")
// lastOpenPosition := 0
// price := 0
// plotshape(long_ts, style=shape.xcross, location=location.abovebar, color=olive, size=size.small, text="Trailing\nStop")
// plotshape(short_ts, style=shape.xcross, location=location.abovebar, color=orange, size=size.small, text="Trailing\nStop")
// ////////////////
// // TRAILING STOP STRATEGY 3
// ////////////////
// sumbars = input(defval=6, title="Use last x bars for calculation", type=integer, minval=1)
// tsl = (sum(close,sumbars)/sumbars)*(1-(offset/100))
// plot(tsl, color=(srcTrigger<tsl)?red:green)
// buyStopCondition = crossunder(srcTrigger,tsl) and inPreviousBuy and readyToEvalTrigger and usingCustomTrailingStop
// sellStopCondition = crossover(srcTrigger,tsl) and inPreviousSell and readyToEvalTrigger and usingCustomTrailingStop
// plotshape(buyStopCondition, text="Buy\nStop", style=shape.circle, color=olive)
// plotshape(sellStopCondition, text="Sell\nStop", style=shape.circle, color=red)
// alertcondition(buyStopCondition, "Stop alert", "SELL")
// alertcondition(sellStopCondition, "Stop alert", "BUY")
// if (buyStopCondition)
// strategy.close("buy")
// lastOpenPosition := 0
// price := 0
// if (sellStopCondition)
// strategy.close("sell")
// lastOpenPosition := 0
// price := 0
////////////////
// TRAILING STOP STRATEGY 4
////////////////
// longoffset = input(defval=100, title="Long Trailing Stop Size", type=float, minval=0.5, maxval=1000, step=0.5)
// shortoffset = input(defval=100, title="Short Trailing Stop Size ", type=float, minval=0.5, maxval=1000, step=0.5)
// hiprice = security(tickerid, "1", high)
// loprice = security(tickerid, "1", low)
// price1 = security(tickerid, "1", close)
// trailstop = price1
// trailstop := (loprice <= trailstop[1] and loprice[1] >= trailstop[2]) ? price1 + shortoffset : ((hiprice >= trailstop[1] and hiprice[1] <= trailstop[2]) ? price1 - longoffset : (hiprice > trailstop[1] ? max(hiprice - longoffset, trailstop[1]) : (loprice < trailstop[1] ? min(loprice + shortoffset, trailstop[1]) : price1)))
// trailcol = trailstop > price1 ? red : green
// plot(trailstop, color=trailcol)
// longCondition = (trailcol == green) and inPreviousSell and readyToEvalTrigger and usingCustomTrailingStop
// alertcondition(longCondition, "Long Stop alert", "BUY")
// plotshape(longCondition, text="Sell\nStop", style=shape.circle, color=red)
// if (longCondition)
// strategy.close("buy")
// lastOpenPosition := 0
// price := 0
// shortCondition = (trailcol == red) and inPreviousBuy and readyToEvalTrigger and usingCustomTrailingStop
// alertcondition(shortCondition, "Short alert", "SELL")
// plotshape(shortCondition, text="Buy\nStop", style=shape.circle, color=olive)
// if (shortCondition)
// strategy.close("sell")
// lastOpenPosition := 0
// price := 0
// Visualise price change for open positions
bgcolor(lastOpenPosition > 0 ? green : na, transp=95)
bgcolor(lastOpenPosition < 0 ? red : na, transp=95)