forked from lth-elm/TradingView-Webhook-Trading-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick-Mean-Reversion-Strat.pine
149 lines (109 loc) · 6.6 KB
/
Quick-Mean-Reversion-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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © lth_elm
//@version=4
strategy("Mean reversion strategy", overlay=true, margin_long=100, margin_short=100)
// ----- INPUTS -----
// EMA
ema_len = input(200, minval=1, title="EMA Length")
ema = ema(close, ema_len)
plot(ema, title="EMA", color=color.blue)
// RSI
rsi_len = input(14, minval=1, title="RSI Length")
r = rsi(close, rsi_len)
// ATR
tp_sl_mul = input(false, "──── TP/SL multiplicator")
risk = input(title="Risk (%)", type=input.float, minval=0, defval=1)
risk := risk/100
atr_bars = input( title="ATR Bars", defval=14, minval=1)
stop_mult = input(title=" ▪ Stoploss Multiplicator", type=input.float, defval=3)
tp_mult = input(title=" ▪ Takeprofit Multiplicator", type=input.float, defval=5)
tp1_mult = input(title=" ▪ TP1 Multiplicator", type=input.float, defval=0.75)
tp2_mult = input(title=" ▪ TP2 Multiplicator", type=input.float, defval=1.5)
tp_close = input(title="► TPx close (%)", type=input.float, defval=15)
atr = atr(atr_bars)
plot(tp_close, title="TP Close", display=display.none)
plot(tp1_mult, title="TP1 Mult", display=display.none)
plot(tp2_mult, title="TP2 Mult", display=display.none)
// ----- CONDITIONS -----
bullishEng = open <= close[1] and close > open[1] and open[1] > close[1]
bearishEng = open >= close[1] and close < open[1] and open[1] < close[1]
goLong = bullishEng and close < ema and r[1] < 30
goShort = bearishEng and close > ema and r[1] > 70
// ------ GET IN/OUT A TRADE ------
inLongPosition = strategy.position_size > 0
inShortPosition = strategy.position_size < 0
notInTrade = strategy.position_size == 0
// ----- MANAGE TP/SL -----
// For long
float long_stop_level = na
float long_profit_level = na
long_stop_level := (goLong and notInTrade) ? close - atr * stop_mult : long_stop_level[1]
long_profit_level := (goLong and notInTrade) ? close + atr * tp_mult : long_profit_level[1]
// For short
float short_stop_level = na
float short_profit_level = na
short_stop_level := (goShort and notInTrade) ? close + atr * stop_mult : short_stop_level[1]
short_profit_level := (goShort and notInTrade) ? close - atr * tp_mult : short_profit_level[1]
// Execute buy or sell order
if goLong and notInTrade
size = (strategy.equity*risk) / abs(close-long_stop_level)
strategy.entry("long", strategy.long, qty=size, alert_message="entry")
// strategy.exit("exit", "long", stop=long_stop_level, limit=long_profit_level, alert_message="exit")
inLongPosition := true
notInTrade := false
else if goShort and notInTrade
size = (strategy.equity*risk) / abs(close-short_stop_level)
strategy.entry("short", strategy.short, qty=size, when=notInTrade, alert_message="entry")
// strategy.exit("exit", "short", stop=short_stop_level, limit=short_profit_level, alert_message="exit")
inShortPosition := true
notInTrade := false
// ----- MULTIPLE TP -----
// Set multiple takeprofit and the stoploss
ticksConversion(x) =>
strategy.position_size != 0 ? (x / syminfo.mintick) : float(na)
long_sl = abs(strategy.position_avg_price-long_stop_level)
long_tp = abs(strategy.position_avg_price-long_profit_level)
long_tp1 = tp1_mult * long_sl
long_tp2 = tp2_mult * long_sl
if not notInTrade[1] and goLong[1]
strategy.exit("tp1", "long", qty_percent = tp_close, profit = ticksConversion(long_tp1), loss = ticksConversion(long_sl), alert_message="tp1_breakeven")
strategy.exit("tp2", "long", qty_percent = tp_close, profit = ticksConversion(long_tp2), loss = ticksConversion(long_sl), alert_message="tp2")
strategy.exit("exit", "long", qty_percent = 100, profit = ticksConversion(long_tp), loss = ticksConversion(long_sl), alert_message="exit")
short_sl = abs(strategy.position_avg_price-short_stop_level)
short_tp = abs(strategy.position_avg_price-short_profit_level)
short_tp1 = tp1_mult * short_sl
short_tp2 = tp2_mult * short_sl
if not notInTrade[1] and goShort[1]
strategy.exit("tp1", "short", qty_percent = tp_close, profit = ticksConversion(short_tp1), loss = ticksConversion(short_sl), alert_message="tp1_breakeven")
strategy.exit("tp2", "short", qty_percent = tp_close, profit = ticksConversion(short_tp2), loss = ticksConversion(short_sl), alert_message="tp2")
strategy.exit("exit", "short", qty_percent = 100, profit = ticksConversion(short_tp), loss = ticksConversion(short_sl), alert_message="exit")
// Exit or breakeven
longbreakstop = strategy.position_avg_price + 2 * (strategy.position_avg_price*0.001)
shortbreakstop = strategy.position_avg_price - 2 * (strategy.position_avg_price*0.001)
if inLongPosition
if close > ema // or we can keep our atr multiplicator first objective
strategy.close("long", alert_message="exit")
breakeven_level = strategy.position_avg_price + long_tp1 // change if breakeven tpx
if high > breakeven_level
strategy.exit("exit", "long", qty_percent = 100, stop=longbreakstop, limit=long_profit_level, alert_message="exit")
strategy.exit("tp2", "long", qty_percent = tp_close, stop=longbreakstop, profit = ticksConversion(long_tp2), alert_message="tp2")
if inShortPosition
if close < ema // or we can keep our atr multiplicator first objective
strategy.close("short", alert_message="exit")
breakeven_level = strategy.position_avg_price - short_tp1 // change if breakeven tpx
if low < breakeven_level
strategy.exit("exit", "short", qty_percent = 100, stop=shortbreakstop, limit=short_profit_level, alert_message="exit")
strategy.exit("tp2", "short", qty_percent = tp_close, stop=shortbreakstop, profit = ticksConversion(short_tp2), alert_message="tp2")
// ----- Plot TP/SL -----
plot(longbreakstop, color=color.yellow, title="Long Breakeven", display=display.none)
plot(shortbreakstop, color=color.yellow, title="Short Breakeven", display=display.none)
invisible = color.new(color.white, 100)
p_price = plot(strategy.position_avg_price, color=invisible)
p_long_stop = plot(long_stop_level, color=invisible, style=plot.style_linebr, linewidth=1, title="Long SL")
p_long_profit = plot(long_profit_level, color=invisible, style=plot.style_linebr, linewidth=1, title="Long TP")
p_short_stop = plot(short_stop_level, color=invisible, style=plot.style_linebr, linewidth=1, title="Short SL")
p_short_profit = plot(short_profit_level, color=invisible, style=plot.style_linebr, linewidth=1, title="Short TP")
fill(p_long_stop, p_price, color = inLongPosition ? color.red : na)
fill(p_long_profit, p_price, color = inLongPosition ? color.green : na)
fill(p_short_stop, p_price, color = inShortPosition ? color.red : na)
fill(p_short_profit, p_price, color = inShortPosition ? color.green : na)