发布包含adx和ema过滤器的策略
条目:所有三个超趋势都变为正值。如果应用了ADX和EMA过滤器,还应检查ADX是否高于所选水平,close是否高于EMA 退出:当第一个超趋势变为负值时
短条目相反
提供一个过滤器,用于在同一侧采取或避免再次进入。例如,在长时间退出后,如果在触发短单之前很长时间内再次满足进入条件,则如果选中,则需要重新进入。
回测测试
/*backtest
start: 2021-07-01 09:00:00
end: 2022-04-24 15:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
args: [["v_input_int_4",60],["v_input_int_5",35],["v_input_int_6",20],["v_input_float_4",12],["v_input_bool_1",true],["v_input_int_1",2],["v_input_int_2",2],["v_input_int_3",3],["ContractType","i888",360008]]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ©kunjandetroja
//@version=5
strategy('Triple Supertrend with EMA and ADX', overlay=true)
m1 = input.float(1,"ATR 系数",minval = 1,maxval= 6,step=0.5,group='ST 1')
m2 = input.float(2,"ATR 系数",minval = 1,maxval= 6,step=0.5,group='ST 2')
m3 = input.float(3,"ATR 系数",minval = 1,maxval= 6,step=0.5,group='ST 3')
p1 = input.int(10,"ATR 系数",minval = 5,maxval= 25,step=1,group='ST 1')
p2 = input.int(15,"ATR 系数",minval = 5,maxval= 25,step=1,group='ST 2')
p3 = input.int(20,"ATR 系数",minval = 5,maxval= 25,step=1,group='ST 3')
len_EMA = input.int(200,"EMA 周期",minval = 5,maxval= 250,step=1)
len_ADX = input.int(14,"ADX 周期",minval = 1,maxval= 25,step=1)
len_Di = input.int(14,"Di 周期",minval = 1,maxval= 25,step=1)
adx_above = input.float(25,"adx 过滤",minval = 1,maxval= 50,step=0.5)
var bool long_position = false
adx_filter = input.bool(false, "Add Adx & EMA 过滤")
renetry = input.bool(true, "允许重入")
f_getColor_Resistance(_dir, _color) =>
_dir == 1 and _dir == _dir[1] ? _color : na
f_getColor_Support(_dir, _color) =>
_dir == -1 and _dir == _dir[1] ? _color : na
[superTrend1, dir1] = ta.supertrend(m1, p1)
[superTrend2, dir2] = ta.supertrend(m2, p2)
[superTrend3, dir3] = ta.supertrend(m3, p3)
EMA = ta.ema(close, len_EMA)
[diplus,diminus,adx] = ta.dmi(len_Di,len_ADX)
// ADX Filter
adxup = adx > adx_above and close > EMA
adxdown = adx > adx_above and close < EMA
sum_dir = dir1 + dir2 + dir3
dir_long = if(adx_filter == false)
sum_dir == -3
else
sum_dir == -3 and adxup
dir_short = if(adx_filter == false)
sum_dir == 3
else
sum_dir == 3 and adxdown
Exit_long = dir1 == 1 and dir1 != dir1[1]
Exit_short = dir1 == -1 and dir1 != dir1[1]
// BuySignal = dir_long and dir_long != dir_long[1]
// SellSignal = dir_short and dir_short != dir_short[1]
// if BuySignal
// label.new(bar_index, low, 'Long', style=label.style_label_up)
// if SellSignal
// label.new(bar_index, high, 'Short', style=label.style_label_down)
longenter = if(renetry == false)
dir_long and long_position == false
else
dir_long
shortenter = if(renetry == false)
dir_short and long_position == true
else
dir_short
if longenter
long_position := true
if shortenter
long_position := false
strategy.entry('BUY', strategy.long, when=longenter)
strategy.entry('SELL', strategy.short, when=shortenter)
strategy.close('BUY', Exit_long)
strategy.close('SELL', Exit_short)
//buy1 = ta.barssince(dir_long)
//sell1 = ta.barssince(dir_short)
//colR1 = f_getColor_Resistance(dir1, color.red)
//colS1 = f_getColor_Support(dir1, color.green)
//colR2 = f_getColor_Resistance(dir2, color.orange)
//colS2 = f_getColor_Support(dir2, color.yellow)
//colR3 = f_getColor_Resistance(dir3, color.blue)
//colS3 = f_getColor_Support(dir3, color.maroon)
//plot(superTrend1, 'R1', colR1, linewidth=2)
//plot(superTrend1, 'S1', colS1, linewidth=2)
//plot(superTrend2, 'R1', colR2, linewidth=2)
//plot(superTrend2, 'S1', colS2, linewidth=2)
//plot(superTrend3, 'R1', colR3, linewidth=2)
//plot(superTrend3, 'S1', colS3, linewidth=2)
// // Intraday only
// var int new_day = na
// var int new_month = na
// var int new_year = na
// var int close_trades_after_time_of_day = na
// if dayofmonth != dayofmonth[1]
// new_day := dayofmonth
// if month != month[1]
// new_month := month
// if year != year[1]
// new_year := year
// close_trades_after_time_of_day := timestamp(new_year,new_month,new_day,15,15)
// strategy.close_all(time > close_trades_after_time_of_day)