如何从指定日期对策略进行回溯测试



我正试图从指定日期对策略进行回溯测试,例如2020-01-01。

代码正在工作,但我收到警告

第12行:为了一致性,应该在每次计算中调用函数"ta.cocrossover"。建议从此作用域提取调用。

如何修复?

//@version=5
strategy(title="GOLDEN",  overlay=true)
sma20 = ta.sma(close,20)
sma60 = ta.sma(close,60)
plot(sma20,color=color.orange)
plot(sma60,color=color.blue)
backtest_year = input.int(2020,"Y")
backtest_month = input.int(1,"M",minval=1,maxval=12,step = 1)
backtest_day = input.int(1,"D",minval=1,maxval=31,step = 1)
backtest_startDate = timestamp(backtest_year,backtest_month,backtest_day,0,0,0)
if (time>= backtest_startDate)
to_long = ta.crossover(sma20,sma60)
to_close = ta.crossunder(sma20,sma60)
strategy.entry("golden",strategy.long,qty=1000, when = to_long)
strategy.close("golden",qty = 1000, when = to_close)

您可以在input()函数中使用timestamp,而不是这样做,因此您需要一个输入作为开始时间,一个输入用于结束时间,这看起来很酷:(

注意你收到的警告。这是因为您在本地作用域中具有该函数。试着把它放在全球范围内。

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vitruvius
//@version=5
strategy(title="GOLDEN",  overlay=true)
in_start_time = input(defval=timestamp("01 Jan 2021 00:00 +0000"), title="Start Time", group="Time Settings")
in_end_time = input(defval=timestamp("31 Dec 2031 00:00 +0000"), title="End Time", group="Time Settings")
in_window = time >= in_start_time and time <= in_end_time
sma20 = ta.sma(close,20)
sma60 = ta.sma(close,60)
plot(sma20,color=color.orange)
plot(sma60,color=color.blue)
to_long = ta.crossover(sma20,sma60)
to_close = ta.crossunder(sma20,sma60)
if (in_window and to_long)
strategy.entry("golden",strategy.long,qty=1000)
if (in_window and to_close)
strategy.close("golden",qty = 1000, when = to_close)

最新更新