-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStrategy - Reverse RSI.pine
252 lines (201 loc) · 11.4 KB
/
Strategy - Reverse RSI.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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © handikogesang
//@version=4
strategy("Strategy - Reverse RSI", overlay=true, currency = currency.USD, pyramiding = 1, initial_capital = 50000)
//==============================================================================
//FOREX EXCHANGE - Currency Conversion
//==============================================================================
//Select quote currency
quoteCurrency = syminfo.currency //gets quote currency automatically by looking using syminfo function - thank you Rodenwild
//Get currency pair rate quoted against USD (base currency)
usdUSDrate = security("USDUSD", "D", close[1])
gbpUSDrate = security("GBPUSD", "D", close[1])
audUSDrate = security("AUDUSD", "D", close[1])
nzdUSDrate = security("NZDUSD", "D", close[1])
cadUSDrate = security("CADUSD", "D", close[1])
chfUSDrate = security("CHFUSD", "D", close[1])
eurUSDrate = security("EURUSD", "D", close[1])
jpyUSDrate = security("JPYUSD", "D", close[1])
//Fuction to get currency rate into variable
cr_function(source) =>
if quoteCurrency == "USD"
1
else
if quoteCurrency == "GBP"
gbpUSDrate
else
if quoteCurrency == "AUD"
audUSDrate
else
if quoteCurrency == "NZD"
nzdUSDrate
else
if quoteCurrency == "CAD"
cadUSDrate
else
if quoteCurrency == "CHF"
chfUSDrate
else
if quoteCurrency == "EUR"
eurUSDrate
else
jpyUSDrate
//Put currency rate function into variable
cr_multi = cr_function(tr(true))
//==============================================================================
//==============================================================================
//BACKTEST DATE RANGE - Select Dates
//==============================================================================
//Input for date window
startDay = 1 //input(defval = 1, title = "Start Day", type = input.integer, minval = 1, maxval = 31)
startMonth = 1 //input(defval = 1, title = "Start Month", type = input.integer, minval = 1, maxval = 12)
startYear = 1970 //input(defval = 1970, title = "Start Year", type = input.integer, minval = 1970 )
endDay = 31 //input(defval = 31, title = "End Day", type = input.integer, minval = 1, maxval = 31)
endMonth = 12 //input(defval = 12, title = "End Month", type = input.integer, minval = 1, maxval = 12)
endYear = 2222 //input(defval = 2222, title = "End Year", type = input.integer, minval = 1970 )
//Submit date window
startDate = timestamp(startYear, startMonth, startDay, 00, 00, 00) // backtest start date
endDate = timestamp(endYear, endMonth, endDay, 23, 59, 59) // backtest end date
dateRange() => time >= startDate and time <= endDate ? true : false // specify where date range is true
// Remove EurChf from testing
EURCHFCrashDate = syminfo.currency == "CHF" or syminfo.basecurrency == "CHF" ? time >= timestamp(2015,01,01) and time <= timestamp(2015,01,18) ? false : true : true
//==============================================================================
//==============================================================================
//INDICATOR 1 - Trigger
//==============================================================================
//Indicator RSI
// inputs
rsi_length = input(title="RSI Length", defval=20)
rsi_ma_length = input(title="RSI SMA Length", defval=5)
src = input(title="Source",defval=close)
upper_limit = input(title="Upper Limit", defval=55)
lower_limit = input(title="Lower Limit", defval=49)
// calculating
up = rma(max(change(src), 0), rsi_length)
down = rma(-min(change(src), 0), rsi_length)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsi_ma = sma(rsi, rsi_ma_length)
Ind_1_L = crossunder(rsi_ma, lower_limit)
Ind_1_S = crossover(rsi_ma, upper_limit)
//==============================================================================
//==============================================================================
//INDICATOR 2
//==============================================================================
//EMA Ribbon
emaLen1 = input(title="Ema 1 - Period", defval=4)
emaLen2 = input(title="Ema 2 - Period", defval=10)
emaLen3 = input(title="Ema 3 - Period", defval=15)
emaLen4 = input(title="Ema 4 - Period", defval=19)
emaLen5 = input(title="Ema 5 - Period", defval=25)
ema1 = ema(src, emaLen1)
ema2 = ema(src, emaLen2)
ema3 = ema(src, emaLen3)
ema4 = ema(src, emaLen4)
ema5 = ema(src, emaLen5)
//Long and Short Orders (Confirmation Conditions)
Ind_2_L = src < ema1 and ema1 < ema2 and ema2 < ema3 and ema3 < ema4 and ema4 < ema5
Ind_2_S = src > ema1 and ema1 > ema2 and ema2 > ema3 and ema3 > ema4 and ema4 > ema5
//==============================================================================
//==============================================================================
//INDICATOR 3
//==============================================================================
//Time Window
i_sess = input(title="Session",defval="1600-1900",type=input.session, options=["1300-1600","1400-1700","1500-1800","1600-1900", "1700-2000","1800-2100","1900-2200","2000-2300","0200-0500","0300-0600"])
t = time(timeframe.period, i_sess)
bgcolor(time == t ? color.green : na)
//Long and Short Orders (Confirmation Conditions)
Ind_3_L = time == t ? true : false
Ind_3_S = time == t ? true : false
//==============================================================================
//==============================================================================
//INDICATOR 4
//==============================================================================
//Indicator Name
//Long and Short Orders (Confirmation Conditions)
Ind_4_S = true
Ind_4_L = true
//==============================================================================
//==============================================================================
//INDICATOR 5
//==============================================================================
//Indicator Name
//Long and Short Orders (Confirmation Conditions)
Ind_5_L = true
Ind_5_S = true
//==============================================================================
//==============================================================================
//INDICATOR 6
//==============================================================================
//Indicator Name
//Long and Short Orders (Confirmation Conditions)
Ind_6_L = true
Ind_6_S = true
//==============================================================================
//==============================================================================
//MONEY MANAGEMENT - Pivot high low
//==============================================================================
//Entrer intial capital and percentage risk inputs
atr_length = input(title="ATR Length", defval=14)
atr_mult = input(title="ATR Multiple", defval = 2)
atrValue = atr(atr_length)
percentRisk = input(title = "Risk Per Trade", defval = 0.01, minval = 0.001, maxval =0.1)
//Enter inputs
Multi_Profit = input(title = "Profit Multiple", type=input.float, defval = 1.5)
Multi_SL = input(title = "SL Distance Multiple", type=input.float, defval = 1.0)
RiskDollar = atrValue * atr_mult * Multi_SL
//Get position size
posSize = round(((strategy.initial_capital * percentRisk) / (RiskDollar))/cr_multi)
//==============================================================================
//==============================================================================
//ENTRY CONDITIONS - Submit Orders
//==============================================================================
//Long and short strategy conditions
//entry_long = strategy.position_size<=0 and dateRange() and EURCHFCrashDate and Ind_1_L and Ind_2_L and Ind_3_L and Ind_4_L and Ind_5_L and Ind_6_L
//entry_short = strategy.position_size>=0 and dateRange() and EURCHFCrashDate and Ind_1_S and Ind_2_S and Ind_3_S and Ind_4_S and Ind_5_S and Ind_6_S
entry_long = strategy.position_size==0 and dateRange() and EURCHFCrashDate and Ind_1_L and Ind_2_L and Ind_3_L and Ind_4_L and Ind_5_L and Ind_6_L
entry_short = strategy.position_size==0 and dateRange() and EURCHFCrashDate and Ind_1_S and Ind_2_S and Ind_3_S and Ind_4_S and Ind_5_S and Ind_6_S
plotshape(entry_long, color=color.lime, style=shape.arrowup, location = location.belowbar, text="Buy")
plotshape(entry_short, color=color.red, style=shape.arrowdown, location = location.abovebar, text="Sell")
//Store ATR and Price upon entry of trade.
entry_RiskDollar = float(0.0) //set float
entry_price = float(0.0) //set float
entry_RiskDollar := strategy.position_size == 0 or entry_long or entry_short ? RiskDollar : entry_RiskDollar[1]
entry_price := strategy.position_size == 0 or entry_long or entry_short ? close : entry_price[1]
//Submit long and short orders based on entry conditions
if(entry_long)
strategy.entry(id="Long Entry 1", long=true, qty=posSize)
if(entry_short)
strategy.entry(id="Short Entry 1", long=false, qty=posSize)
//==============================================================================
//==============================================================================
//TAKE PROFIT & STOP LOSS VALUES
//==============================================================================
//Calculate stop loss and take profit distance (in price)
nLoss = entry_RiskDollar
nProfit = entry_RiskDollar * Multi_Profit
//Find long take profit and stop loss levels
long_profit_level = float(0.0) //set float
long_stop_level = float(0.0) //set float
long_profit_level := entry_price + nProfit
long_stop_level := entry_price - nLoss
//Find short take profit and stop loss levels
short_profit_level = float(0.0) //set float
short_stop_level = float(0.0) //set float
short_profit_level := entry_price - nProfit
short_stop_level := entry_price + nLoss
//Plot stop loss and profit level on graph; hide levels when no trade
plot(strategy.position_size <= 0 or entry_long or entry_short ? na : long_stop_level, color = color.red, style = plot.style_linebr, linewidth = 2)
plot(strategy.position_size <= 0 or entry_long or entry_short ? na : long_profit_level, color = color.green, style = plot.style_linebr, linewidth = 2)
plot(strategy.position_size >= 0 or entry_long or entry_short ? na : short_stop_level, color = color.red, style = plot.style_linebr, linewidth = 2)
plot(strategy.position_size >= 0 or entry_long or entry_short ? na : short_profit_level, color = color.green, style = plot.style_linebr, linewidth = 2)
//==============================================================================
//==============================================================================
//EXIT CONDITIONS - Submit Orders
//==============================================================================
//Submit exit orders on static profit and loss
strategy.exit("TP/SL 1", "Long Entry 1", stop = long_stop_level, limit = long_profit_level)
strategy.exit("TP/SL 1", "Short Entry 1", stop = short_stop_level, limit = short_profit_level)
//Submit exit orders on exit indicator - Exit 1
//strategy.close(id="Long Entry 1", comment = "Exit 1 L1", when = Ind_1_S)
//strategy.close(id="Short Entry 1", comment = "Exit 1 S1", when = Ind_1_L)
//==============================================================================