-
Notifications
You must be signed in to change notification settings - Fork 2
/
3 EMA Stochastik RSI ATR SL TP only LONG.pine
350 lines (346 loc) · 9.2 KB
/
3 EMA Stochastik RSI ATR SL TP only LONG.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
Script Name: 3 EMA Stochastik RSI ATR SL TP only LONG
Author: TradingStrategyCheck
Description: Hey there!
Here i will show you the 3 EMA RSI Stochastic Crossover strategy with an ATR SL and TP.
The strategy works as follows:
For long positions, the EMA's must be in the following order:
The 8 E M A must be above the 14 E M A and the 14 E M A must be above the 50 E M A.
The buy signal is given to us by the Stochastic RSI indicators.
The K line must cross...
PineScript code:
Pine Script™ strategy
3 EMA Stochastik RSI ATR SL TP only LONG
Copy code
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
//@version=4
strategy(title="Stoch RSI Crossover Strat + EMA", shorttitle="Stoch RSI Cross + EMA Strat", overlay = true, pyramiding = 0, max_bars_back = 3000, calc_on_order_fills = false, commission_type = strategy.commission.percent, commission_value = 0.075, default_qty_type = strategy.percent_of_equity, default_qty_value = 1, initial_capital=10000, currency=currency.USD)
// Time Range
FromMonth=input(defval=1,title="FromMonth",minval=1,maxval=12)
FromDay=input(defval=1,title="FromDay",minval=1,maxval=31)
FromYear=input(defval=2020,title="FromYear",minval=2017)
ToMonth=input(defval=1,title="ToMonth",minval=1,maxval=12)
ToDay=input(defval=1,title="ToDay",minval=1,maxval=31)
ToYear=input(defval=9999,title="ToYear",minval=2017)
start=timestamp(FromYear,FromMonth,FromDay,00,00)
finish=timestamp(ToYear,ToMonth,ToDay,23,59)
window()=>time>=start and time<=finish?true:false
// See if this bar's time happened on/after start date
afterStartDate = time >= start and time<=finish?true:false
enablelong = input(true, title="Enable Long?")
enableshort = input(true, title="Enable Short?")
enableatrexit = input(true, title="Enable ATR Exit?")
enablelowexit = input(false, title="Enable Lowest Low/ Highest High Exit?")
//STOCH RSI
smoothK = input(3, minval=1)
smoothD = input(3, minval=1)
lengthRSI = input(14, minval=1)
lengthStoch = input(14, minval=1)
src = input(close, title="RSI Source")
rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
//MACD
fast_length = input(title="Fast Length", type=input.integer, defval=12)
slow_length = input(title="Slow Length", type=input.integer, defval=24)
srcmacd = input(title="MACD Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 8)
sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=true)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)
zeroline = 0
// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00
// Calculating
fast_ma = sma_source ? sma(srcmacd, fast_length) : ema(srcmacd, fast_length)
slow_ma = sma_source ? sma(srcmacd, slow_length) : ema(srcmacd, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal
//plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )
//plot(macd, title="MACD", color=col_macd, transp=0)
//plot(signal, title="Signal", color=col_signal, transp=0)
//plot(zeroline, title="Zero Line", color=color.black, transp=0)
///////////////////////////LONG////////////////////////////////////////////////////////////////////
//ATR
lengthATR = input(title="ATR Length", defval=11, minval=1)
atr = atr(lengthATR)
//MULTI EMA
emasrc = close,
len1 = input(8, minval=1, title="EMA 1")
len2 = input(15, minval=1, title="EMA 2")
len3 = input(50, minval=1, title="EMA 3")
ema1 = ema(emasrc, len1)
ema2 = ema(emasrc, len2)
ema3 = ema(emasrc, len3)
col1 = color.lime
col2 = color.blue
col3 = color.orange
//EMA Plots
plot(ema1, title="EMA 1", linewidth=1, color=col1)
plot(ema2, title="EMA 2", linewidth=1, color=col2)
plot(ema3, title="EMA 3", linewidth=1, color=col3)
//LONG
macdunderhis = macd < zeroline
macdcrossup = macd > signal
crossup = k[0] > d[0] and k[1] <= d[1]
emaup = ema1 > ema2 and ema2 > ema3 and close > ema1
barbuy = crossup and emaup and macdcrossup and macdunderhis
//SHORT
macdoverhis = macd > zeroline
macdcrosunder = macd < signal
crossdown = k[0] < d[0] and k[1] >= d[1]
emadown = ema1 < ema2 and ema2 < ema3 and close < ema1
barsell = crossdown and emadown and macdcrosunder and macdoverhis
//plotshape(crossup, style=shape.triangleup, location=location.belowbar, color=color.white)
plotshape(barbuy, style=shape.triangleup, location=location.belowbar, color=color.green)
plotshape(barsell, style=shape.triangledown, location=location.abovebar, color=color.red)
longloss = sma(open, 1)
//plot(longloss, color=color.red)
//Buy and Sell Factors
profitfactorl = input(title="Profitfactor Long", type=input.float, step=0.1, defval=1.5)
stopfactorl = input(title="ATR Stopfactor Long", type=input.float, step=0.1, defval=2)
profitfactors = input(title="Profitfactor Short", type=input.float, step=0.1, defval=1.5)
stopfactors = input(title="ATR Stopfactor Short", type=input.float, step=0.1, defval=2)
bought = strategy.position_size[1] < strategy.position_size
sold = strategy.position_size[1] > strategy.position_size
longcondition = barbuy
shortcondition = barsell
/////ENTRY/////
//LONG ENTRY
if (enablelong == true) and (longcondition) and (afterStartDate) and strategy.opentrades < 1
strategy.entry("Long", strategy.long)
//SHORT ENTRY
if (enableshort == true) and (shortcondition) and (afterStartDate) and strategy.opentrades < 1
strategy.entry("Short", strategy.short)
//PLOTT ATR TP SL//
//Long
plot_profit_level_long = strategy.position_avg_price + (atr*profitfactorl)
plot_stop_level_long = strategy.position_avg_price - (atr*stopfactorl)
plot(plot_profit_level_long, title="PL Long", linewidth=1, color=color.green)
plot(plot_stop_level_long, title="SL Long", linewidth=1, color=color.red)
//Short
plot_profit_level_short = strategy.position_avg_price - (atr*profitfactors)
plot_stop_level_short = strategy.position_avg_price + (atr*stopfactors)
plot(plot_profit_level_short, title="PL Long", linewidth=1, color=color.green)
plot(plot_stop_level_short, title="SL Long", linewidth=1, color=color.red)
////////////////ATR EXIT/////////////////
//LONG EXIT
if (afterStartDate) and (enableatrexit == true) and strategy.opentrades > 0
barsbought = barssince(bought)
profit_level = strategy.position_avg_price + (atr*profitfactorl)
stop_level = strategy.position_avg_price - (atr*stopfactorl)
strategy.exit("Take Profit/ Stop Loss", "Long", stop=stop_level[barsbought], limit=profit_level[barsbought])
//SHORT EXIT
if (afterStartDate) and (enableatrexit == true) and strategy.opentrades > 0
barsbought = barssince(sold)
profit_level = strategy.position_avg_price - (atr*profitfactors)
stop_level = strategy.position_avg_price + (atr*stopfactors)
strategy.exit("Take Profit/ Stop Loss", "Short", stop=stop_level[barsbought], limit=profit_level[barsbought])
//Lowest Low / Highest High Exit
//////LOWEST LOW//////
//Lowest Low LONG
loLen = input(title="Lowest Low Lookback", type=input.integer,
defval=30, minval=2)
stop_level_long = lowest(low, loLen)
barsboughtlong = barssince(bought)
if (afterStartDate) and (enablelowexit == true) and strategy.opentrades > 0
profit_level_long = strategy.position_avg_price + ((strategy.position_avg_price - stop_level_long[barsboughtlong])*profitfactorl)
strategy.exit(id="TP/ SL", stop=stop_level_long[barsboughtlong], limit=profit_level_long)
//Lowest Low SHORT
highLen = input(title="highest high lookback", type=input.integer,
defval=26, minval=2)
stop_level_short = highest(high, highLen)
barsboughtshort = barssince(sold)
if (afterStartDate) and (enablelowexit == true) and strategy.opentrades > 0
profit_level_short = strategy.position_avg_price - ((stop_level_short[barsboughtshort] - strategy.position_avg_price)*profitfactors)
strategy.exit(id="TP/ SL", stop=stop_level_short[barsboughtshort], limit=profit_level_short)
Expand (182 lines)