应该是世界上最简单的编码策略,但我似乎无法弄清楚。我只想要一个简单的波段交易测试仪,我在收盘时买入,第二天早上开盘时卖出。这就是我所拥有的。
//@version=4
strategy(title = "Buy the Close, Sell the Open", default_qty_value=100, overlay=true)
strategy.entry(id = "Entry at Close", when = close, long = true)
strategy.close(id = "Exit at Open", when = open)
我也试过这个
//@version=4
strategy(title = "Buy the Close, Sell the Open", shorttitle = "BtCStO", default_qty_value=100, overlay=true)
timeResolution = input("D", type=input.resolution)
priceClose = security(syminfo.tickerid, timeResolution, close[1])
priceOpen = security(syminfo.tickerid, timeResolution, open[1])
strategy.entry(id = "Entry at Close", when = priceClose, long = true)
strategy.close(id = "Exit at Open", when = priceOpen)
编辑:我已经进去并添加了一些 if 语句,我认为对其进行了一些简化,并添加了一条情节线以确保数据确实被提取。我一路回到图表上,发现它确实进入了一个位置,尽管它似乎是在开盘时进入,而不是在关闭时进入,然后它就永远不会退出该位置。我怀疑它很困惑,因为它试图在同一天同时做这两件事,而不是在第 1 天买入收盘价并在第 2 天卖出,然后在第 2 天重复买入收盘价,然后在第 3 天卖出开盘价,依此类推.
更新的代码示例:
//@version=4
strategy(title = "Buy the Close, Sell the Open", shorttitle = "BtCStO", default_qty_value=100, overlay=true)
if close
strategy.entry(id = "Entry at Close", long = true)
if open
strategy.close(id = "Exit at Open")
plot(close)
plot(open)
这是我所看到内容的快速屏幕截图。
答案 0 :(得分:1)
我花了一整天的时间才弄明白,但答案确实很简单,我没有匹配 id,因为关闭需要匹配打开。由于我输入了错误的ID,它从未关闭初始位置,因此从未打开新位置。
strategy(title = "Buy the Close, Sell the Open", shorttitle = "BtCStO", default_qty_value=100, overlay=true)
if close
strategy.entry(id = "BtCStO", long = true
if open
strategy.close(id = "BtCStO")