-
Notifications
You must be signed in to change notification settings - Fork 51
/
Failure Swing (stop hunting) Strategy
90 lines (77 loc) · 4.07 KB
/
Failure Swing (stop hunting) Strategy
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
//@version=3
//
// GitHub: https://github.com/Heavy91/TradingView_Indicators
//
// TradingView: https://www.tradingview.com/u/Heavy91/#published-scripts
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// If you like my work, you can support me with a donation:
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// €: http://pinescripts.fetchapp.com/sell/d15d4740
//
// $: http://pinescripts.fetchapp.com/sell/0f9153a5
//
// BTC: bc1q8e4wav3t55plp6ar0xc7gh4uqrzlxc7g97ywrg
//
// LTC: ltc1qagcys3pyluke3xdlq94qx8p7fd3z3mj2w32grs
//
// ----------------------------------------------------------------------------
// Failure Swing Strategy
// ----------------------------------------------------------------------------
// This strategy is a first attempt to countertrade the false break of a key support/resistance.
// If a candle breaks the level, but it comes back before the close, it will trigger an order.
// The Stop Loss is in %, the Take Profit is near the EMA.
// The volatility filter is used to block new orders when the price is near the EMA.
// The volatility adjustment coefficients calibrate Stop Loss and Take Profit values according to the current volatility.
strategy("Failure Swing Strategy V1", overlay=true, initial_capital = 1000, default_qty_type = strategy.percent_of_equity , pyramiding=0,default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.2 )
period = input( 84, minval=1, title='Support & Resistance' )
EMAp = input( 120, minval=1, title='EMA' )
Threshold = input( 5, minval=1, title='VolatilityFilter' )
coefficientTP = input( 0.05,title='TP_VolatilityAdjustment', type=float , step=0.01)
coefficientSL = input( 0.02,title='SL_VolatilityAdjustment', type=float , step=0.01)
sl_inp = input(2, title='Stop Loss %', type=float)/100
//STATEGY SETTINGS
// === BACKTEST RANGE ===
FromMonth = input(defval = 8, title = "From Month", minval = 1)
FromDay = input(defval = 1, title = "From Day", minval = 1)
FromYear = input(defval = 2018, title = "From Year", minval = 2014)
ToMonth = input(defval = 1, title = "To Month", minval = 1)
ToDay = input(defval = 1, title = "To Day", minval = 1)
ToYear = input(defval = 9999, title = "To Year", minval = 2014)
AllowShort = input(type=bool, defval=true, title="Allow Short")
strategy.risk.allow_entry_in(AllowShort ? strategy.direction.all : strategy.direction.long)
resistance = highest(high[1], period)
support = lowest(low[1], period)
plot(resistance, color=gray, linewidth=1)
plot(support, color=gray, linewidth=1)
emaBase = ema(close,EMAp)
plot(emaBase, color=yellow, linewidth=1)
volatility = (((open-emaBase)*sign(open-emaBase))/emaBase)
ema = emaBase*(1+volatility*coefficientTP*sign(open-emaBase))
plot(ema, color=green, linewidth=2, style=circles)
stop_level_L = strategy.position_avg_price * (1 - sl_inp-volatility*coefficientSL)
stop_level_S = strategy.position_avg_price * (1 + sl_inp + volatility*coefficientSL)
//STRATEGY
if( (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)) )
//OPEN SHORT
if(volatility>(Threshold/100))//filter volatility
if(resistance[1]==resistance[2])//filter resistance
if((high>=resistance[1])and(close<resistance[1]))//failed swing
strategy.entry("Short", strategy.short,comment="Short")
//OPEN LONG
if(volatility>(Threshold/100))//filter volatility
if(support[1]==support[2])//filter support "piatto"
if((low<=support[1])and(close>support[1]))//failed swing
strategy.entry("Long", strategy.long,comment="Long")
//STOP LOSS
if((high>stop_level_S) or (low<ema))
strategy.close("Short")
if((low<stop_level_L) or (high>ema))
strategy.close("Long")
//TAKE PROFIT
if(low<ema)
strategy.close("Short")
if(high>ema)
strategy.close("Long")
plot((strategy.position_size < 0)?stop_level_S:na, color=red, style=circles, linewidth=2)
plot((strategy.position_size > 0)?stop_level_L:na, color=red, style=circles, linewidth=2)