您如何才能仅在一天内的特定时间窗口内对您的策略进行反向测试?
我正在测试MACD和BB的组合,时间框架为1分钟。但我只想在上午10点到14点30分和1600点到2000点之间在UTC+2建立多头或空头头寸。我想在进入窗口外的X分钟内退出任何开放的职位。
以一种简化的方式,我的代码目前如下所示:
longentry = crossover(macd, signal) and src <= lowerBB
shortentry = crossunder(macd, signal) and src >= upperBB
longclose = crossunder(src, lowerBB)
shortclose = crossover(src, upperBB)
if longentry
strategy.entry("id=LONG", strategy.long)
if shortentry
strategy.entry("id=SHORT", strategy.short)您的协助将不胜感激。
感谢并致以最良好的问候,
Bas
发布于 2021-09-29 20:42:07
这是一个如何为您的交易设置时间窗口的示例脚本。下面你会看到几个输入。一个是打开和关闭过滤器的布尔值,另一个是我们想要在其间进行交易的时间。然后我们创建一个条件来检查我们是否超出了我们的时间,并且我们已经检查了过滤交易。然后我们使用"and not timeFilter“将其添加到我们的条目中。我添加了一些背景颜色,这样你就可以看到豁免在哪里。
//@version=4
strategy("My Strategy", overlay=true, margin_long=100, margin_short=100)
useTimeFilter = input(false, "Use Time Session Filter", input.bool, group="Filters")
timeSession = input("0000-0300", "Time Session To Trade In", input.session, group="Filters")
timeFilter = na(time(timeframe.period, timeSession + ":1234567", "GMT-2")) == true and useTimeFilter
longentry = crossover(sma(close, 14), sma(close, 28))
shortentry = crossunder(sma(close, 14), sma(close, 28))
longclose = crossunder(sma(close, 14), sma(close, 28))
shortclose = crossover(sma(close, 14), sma(close, 28))
if longentry and not timeFilter
strategy.entry("LONG", strategy.long)
if longclose and not timeFilter
strategy.entry("SHORT", strategy.short)
strategy.close("LONG", when=longclose, comment="Long Close")
strategy.close("SHORT", when=shortclose, comment="Short Close")
bgcolor(timeFilter ? color.red : na)万事如意,
比约古姆
https://stackoverflow.com/questions/69382539
复制相似问题