如何从指定日期回测策略
How to backtest a strategy from a specified date
我正在尝试从指定日期回测策略,例如 2020-01-01。
代码运行正常,但我收到警告
line 12: The function 'ta.crossover' should be called on each calculation for consistency. It is recommended to extract the call from this scope.
如何解决?
//@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)
我正在尝试从指定日期回测策略,例如 2020-01-01。
代码运行正常,但我收到警告
line 12: The function 'ta.crossover' should be called on each calculation for consistency. It is recommended to extract the call from this scope.
如何解决?
//@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)