首页 > 解决方案 > 带有错误 ID 的 Pine 脚本和交易视图关闭条目

问题描述

我是第一次测试 pine 编辑器和策略测试器,但我试图理解和意外的行为。

下面是我正在测试的一个简单策略的代码。(基本上我想了解 strategy.close 和 strategy.exit 之间的区别)所以为了做到这一点,我只需在周一买入并在周五平仓。为了真正测试平仓,我在周三输入了一个新仓位,我希望代码永远不会关闭周三的仓位。但是,如果我检查“策略测试器”的“交易列表”,我可以看到在交易编号 2 中,周三头寸被周一平仓订单平仓。(见附图)。

有人可以澄清为什么会这样吗?我还注意到星期一的买入信号发生在星期二,即使我激活了 process_orders_on_close(否则它会在星期三发生),并且在所有工作日都是如此。

谢谢你的帮助。干杯。

//@version=4
strategy("My Strategy", overlay=true, process_orders_on_close=true, pyramiding=2)


testStartYear = input(2021, "Backtest Start Year")
testStartMonth = input(06, "Backtest Start Month")
testStartDay = input(06, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear, testStartMonth, testStartDay, 0, 0)

// Stop us from entering two wednesday positions when pyramiding is on
// This would prevent us entering a monday position.
wedEntered = bool(na)
wedEntered := nz(wedEntered[1], false)

monCondition = time >= testPeriodStart and dayofweek == dayofweek.monday
wedCondition = time >= testPeriodStart and dayofweek == dayofweek.wednesday and (not wedEntered[1])

if (monCondition)

    strategy.entry("Monday", strategy.long)
    // strategy.exit("Monday TP", "Monday", limit=high)
    
if (wedCondition)
    wedEntered := true
    strategy.entry("Wednesday", strategy.long)

strategy.close("Monday", when=dayofweek == dayofweek.friday)
// strategy.close_all(when=dayofweek == dayofweek.friday)

策略测试器

标签: pine-scripttradingview-api

解决方案


您需要像这样指定close_entries_rule参数:

strategy("My Strategy", overlay=true, process_orders_on_close=true, pyramiding=2, close_entries_rule="ANY")

推荐阅读