// L/S variables Position = input.string('0', title='1 / 2', options=['0', '1', '2'], group='???') is_Long = Position == '1' ? na : true is_Short = Position == '2' ? na : true //L/S variables var bool longCond = na var bool shortCond = na longCond := nz(longCond[1]) shortCond := nz(shortCond[1]) var int CondIni_long = 0 var int CondIni_short = 0 CondIni_long := nz(CondIni_long[1]) CondIni_short := nz(CondIni_short[1]) var bool Final_longCondition = na var bool Final_shortCondition = na Final_longCondition := nz(Final_longCondition[1]) Final_shortCondition := nz(Final_shortCondition[1]) var bool BT_Final_longCondition = na var bool BT_Final_shortCondition = na BT_Final_longCondition := nz(BT_Final_longCondition[1]) BT_Final_shortCondition := nz(BT_Final_shortCondition[1]) var float last_open_longCondition = na var float last_open_shortCondition = na var int last_longCondition = na var int last_shortCondition = na var int nLongs = na var int nShorts = na nLongs := nz(nLongs[1]) nShorts := nz(nShorts[1])
//STRATEGY L_1 = RS_Long_condt and Volume_Breakouts_condt and L_adx and Long_MA S_1 = RS_Short_condt and Volume_Breakouts_condt and S_adx and Short_MA L_2 = L_VAP and L_sar S_2 = S_VAP and S_sar L_3 = L_rmi and L_sar and Long_MA S_3 = S_rmi and S_sar and Short_MA Final_Long_Condt = L_1 or L_2 or L_3 Final_Short_Condt = S_1 or S_2 or S_3 longCond := Final_Long_Condt shortCond := Final_Short_Condt CondIni_long := longCond[1] ? 1 : shortCond[1] ? -1 : nz(CondIni_long[1]) CondIni_short := longCond[1] ? 1 : shortCond[1] ? -1 : nz(CondIni_short[1]) longCondition = longCond[1] and nz(CondIni_long[1]) == -1 shortCondition = shortCond[1] and nz(CondIni_short[1]) == 1
对照解读
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
L_1 = RS_Long_condt and Volume_Breakouts_condt and L_adx and Long_MA S_1 = RS_Short_condt and Volume_Breakouts_condt and S_adx and Short_MA L_2 = L_VAP and L_sar S_2 = S_VAP and S_sar L_3 = L_rmi and L_sar and Long_MA S_3 = S_rmi and S_sar and Short_MA //此段是根据之前得指标结果,穿插使用结合出三对开单指条件
//TP_1 tp = input.float(0.8, 'TP-1 [%]', step=0.1, group='Backtesting') var bool long_tp = na var bool short_tp = na var int last_long_tp = na var int last_short_tp = na var bool Final_Long_tp = na var bool Final_Short_tp = na Final_Long_tp := nz(Final_Long_tp[1]) Final_Short_tp := nz(Final_Short_tp[1]) long_tp := is_Long and high > last_open_longCondition * (1 + tp / 100) and in_longCondition short_tp := is_Short and low < last_open_shortCondition * (1 - tp / 100) and in_shortCondition last_long_tp := long_tp ? time : nz(last_long_tp[1]) last_short_tp := short_tp ? time : nz(last_short_tp[1]) Final_Long_tp := long_tp and last_longCondition > nz(last_long_tp[1]) and last_longCondition > nz(last_long_sl[1]) Final_Short_tp := short_tp and last_shortCondition > nz(last_short_tp[1]) and last_shortCondition > nz(last_short_sl[1]) l_1_h = Final_Long_tp ? last_open_longCondition * (1 + tp / 100) : na s_1_h = Final_Short_tp ? last_open_shortCondition * (1 - tp / 100) : na //TP_2 tp2 = input.float(0.9, 'TP-2 [%]', step=0.1) var bool long_tp2 = na var bool short_tp2 = na var int last_long_tp2 = na var int last_short_tp2 = na var bool Final_Long_tp2 = na var bool Final_Short_tp2 = na Final_Long_tp2 := nz(Final_Long_tp2[1]) Final_Short_tp2 := nz(Final_Short_tp2[1]) long_tp2 := is_Long and high > last_open_longCondition * (1 + tp2 / 100) and in_longCondition short_tp2 := is_Short and low < last_open_shortCondition * (1 - tp2 / 100) and in_shortCondition last_long_tp2 := long_tp2 ? time : nz(last_long_tp2[1]) last_short_tp2 := short_tp2 ? time : nz(last_short_tp2[1]) Final_Long_tp2 := long_tp2 and last_longCondition > nz(last_long_tp2[1]) and last_longCondition > nz(last_long_sl[1]) Final_Short_tp2 := short_tp2 and last_shortCondition > nz(last_short_tp2[1]) and last_shortCondition > nz(last_short_sl[1]) l_2_h = Final_Long_tp2 ? last_open_longCondition * (1 + tp2 / 100) : na s_2_h = Final_Short_tp2 ? last_open_shortCondition * (1 - tp2 / 100) : na //TP_3 tp3 = input.float(2.1, ' TP-3 [%]', step=0.1) var bool long_tp3 = na var bool short_tp3 = na var int last_long_tp3 = na var int last_short_tp3 = na var bool Final_Long_tp3 = na var bool Final_Short_tp3 = na Final_Long_tp3 := nz(Final_Long_tp3[1]) Final_Short_tp3 := nz(Final_Short_tp3[1]) long_tp3 := is_Long and high > last_open_longCondition * (1 + tp3 / 100) and in_longCondition short_tp3 := is_Short and low < last_open_shortCondition * (1 - tp3 / 100) and in_shortCondition last_long_tp3 := long_tp3 ? time : nz(last_long_tp3[1]) last_short_tp3 := short_tp3 ? time : nz(last_short_tp3[1]) Final_Long_tp3 := long_tp3 and last_longCondition > nz(last_long_tp3[1]) and last_longCondition > nz(last_long_sl[1]) Final_Short_tp3 := short_tp3 and last_shortCondition > nz(last_short_tp3[1]) and last_shortCondition > nz(last_short_sl[1]) l_3_h = Final_Long_tp3 ? last_open_longCondition * (1 + tp3 / 100) : na s_3_h = Final_Short_tp3 ? last_open_shortCondition * (1 - tp3 / 100) : na // SL sl = input.float(7.7, 'SL [%]', step=0.1) var int CondIni_long_sl = 0 var int CondIni_short_sl = 0 var bool Final_Long_sl0 = na var bool Final_Short_sl0 = na Final_Long_sl0 := nz(Final_Long_sl0[1]) Final_Short_sl0 := nz(Final_Short_sl0[1]) var bool Final_Long_sl = na var bool Final_Short_sl = na Final_Long_sl := nz(Final_Long_sl[1]) Final_Short_sl := nz(Final_Short_sl[1]) long_sl = is_Long and low <= (1 - sl / 100) * last_open_longCondition and not(open < (1 - sl / 100) * last_open_longCondition) short_sl = is_Short and high >= (1 + sl / 100) * last_open_shortCondition and not(open > (1 + sl / 100) * last_open_shortCondition) Final_Long_sl0 := Position == 'BOTH' ? long_sl and nz(CondIni_long_sl[1]) == -1 and not Final_Long_tp and not shortCondition : long_sl and nz(CondIni_long_sl[1]) == -1 and not Final_Long_tp Final_Short_sl0 := Position == 'BOTH' ? short_sl and nz(CondIni_short_sl[1]) == -1 and not Final_Short_tp and not longCondition : short_sl and nz(CondIni_short_sl[1]) == -1 and not Final_Short_tp last_long_sl := Final_Long_sl ? time : nz(last_long_sl[1]) last_short_sl := Final_Short_sl ? time : nz(last_short_sl[1]) Final_Long_sl := Final_Long_sl0 and last_longCondition > nz(last_long_tp[1]) and last_longCondition > nz(last_long_sl[1]) Final_Short_sl := Final_Short_sl0 and last_shortCondition > nz(last_short_tp[1]) and last_shortCondition > nz(last_short_sl[1]) CondIni_long_sl := Final_Long_tp or Final_Long_sl or Final_shortCondition ? 1 : Final_longCondition ? -1 : nz(CondIni_long_sl[1]) CondIni_short_sl := Final_Short_tp or Final_Short_sl or Final_longCondition ? 1 : Final_shortCondition ? -1 : nz(CondIni_short_sl[1])
// Backtest if long strategy.entry('L_1', strategy.long, when=ACT_BT and testPeriod) strategy.entry('L_2', strategy.long, when=ACT_BT and testPeriod) strategy.entry('L_3', strategy.long, when=ACT_BT and testPeriod)
if short strategy.entry('S_1', strategy.short, when=ACT_BT and testPeriod) strategy.entry('S_2', strategy.short, when=ACT_BT and testPeriod) strategy.entry('S_3', strategy.short, when=ACT_BT and testPeriod)