如何在`strategy.entry`之后的某些天退出

时间:2018-04-14 19:22:51

标签: trading stocks tradingview-api pine-script

我似乎无法弄清楚从交易条目的特定时间间隔退出交易的语法。任何帮助将不胜感激。

if (crossover(delta, 0))
    strategy.entry("Long", strategy.long, comment="Long")
    strategy.exit("Exit", "Long", when = 15)

上面的代码我希望在15天后exit排长位。但它似乎没有用。

2 个答案:

答案 0 :(得分:0)

我想出了一个解决方案。我创建了另一个if语句,但是错误地触发了15天。我还将交叉设置为变量。见代码:

buy = (crossover(delta, 0))

if (buy)
    strategy.entry("Long", strategy.long, comment="Long")
if (buy[15])
    strategy.close("Long")

答案 1 :(得分:0)

尝试 barssince

// Example - Buy when the price closes below 22

myEntry = close  < 22

strategy.entry(id= "sample", long = strategy.long, when= myEntry)

// Close 10 bar periods after the condition that triggered the entry

strategy.close(id = "sample", when = barssince(myEntry) >= 10)