Pine Script策略的反向测试结果从V2到V4各不相同



pine脚本反测试结果从V2到V4 各不相同

我尝试将Pine脚本V2转换为V4。我认为证券功能从V2到V4有很多变化。回测结果的变化。如果有人知道解决方案,请帮助我

这是V2 pine脚本策略

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © suryaobulareddy
//@version=2
strategy("Strategy1", overlay=true)
tim=input('50')
//160
isSession  = input(defval = true, title = "Apply Trading Session", type = bool)
sess = input(defval = "0935-1500", title="Trading Session")
t = time(period, sess)
sessionOpen = isSession ? (na(t) ? false : true):false
startYear   = input(defval = 2020, title = "From Year",     type = integer)
startMonth  = input(defval = 1,    title = "From Month",    type = integer )
startDay    = input(defval = 1,    title = "From Day",      type = integer)
endYear     = input(defval = 2112, title = "To Year",       type = integer)
endMonth    = input(defval = 1,    title = "To Month",      type = integer)
endDay      = input(defval = 1,    title = "To Day",        type = integer)
showDate  = input(defval = true, title = "Show Date Range", type = bool)
start     = timestamp(startYear, startMonth, startDay, 00, 00)        // backtest start window
finish    = timestamp(endYear, endMonth, endDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false       // create function "within window of time"
out1 = security(tickerid, tim, open)
out2 = security(tickerid, tim, close)
//plot(out1,color=color.red)
//plot(out2,color=color.green)
longCondition = (crossover(security(tickerid, tim, close),security(tickerid, tim, open)) and sessionOpen and window())
shortCondition = (crossunder(security(tickerid, tim, close),security(tickerid, tim, open)) and sessionOpen and window())
val = 0
if (longCondition)
val := 1
strategy.entry("long", strategy.long)
if (shortCondition)
val := -1
strategy.entry("short", strategy.short)
if(not sessionOpen)
val := -3
strategy.close_all(when =  not sessionOpen)
plot_stoploss_short= plot(val, title="type", color=red)

这是V4 pine脚本策略

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © suryaobulareddy
//@version=4
strategy("Strategy1", overlay=true)
tim=input('50')
timopen=input('50')
//160
isSession  = input(defval = true, title = "Apply Trading Session", type = input.bool)
sess = input(defval = "0935-1500", title="Trading Session")
t = time(timeframe.period, sess)
sessionOpen = isSession ? (na(t) ? false : true):false 
startYear   = input(defval = 2020, title = "From Year",     type = input.integer)
startMonth  = input(defval = 1,    title = "From Month",    type = input.integer )
startDay    = input(defval = 1,    title = "From Day",      type = input.integer)
endYear     = input(defval = 2112, title = "To Year",       type = input.integer)
endMonth    = input(defval = 1,    title = "To Month",      type = input.integer)
endDay      = input(defval = 1,    title = "To Day",        type = input.integer)
showDate  = input(defval = true, title = "Show Date Range", type = input.bool)
start     = timestamp(startYear, startMonth, startDay, 00, 00)        // backtest start window
finish    = timestamp(endYear, endMonth, endDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false       // create function "within window of time"
out1 = security(syminfo.tickerid, tim, open)
out2 = security(syminfo.tickerid, tim, close)
longCondition = (crossover(security(syminfo.tickerid, tim, close),security(syminfo.tickerid, timopen, open)) and sessionOpen and window())
shortCondition = (crossunder(security(syminfo.tickerid, tim, close),security(syminfo.tickerid, timopen, open)) and sessionOpen and window())
val = 0
if (longCondition)
val := 1
strategy.entry("long", strategy.long)
if (shortCondition)
val := -1
strategy.entry("short", strategy.short)
if(not sessionOpen)
val := -3
strategy.close_all(when =  not sessionOpen)
plot_stoploss_short= plot(val, title="type", color=color.red)

是的,从v3开始,security()的行为方式发生了重要变化。它在v3之前使用了未来展望,而默认设置不再使用。

因此,您通过v4回溯测试得到的结果将更加可靠。

最新更新