I can't seem to figure out the syntax for exit a trade from a certain time interval from the trade entry. Any help would be greatly appreciated.
if (crossover(delta, 0))
strategy.entry("Long", strategy.long, comment="Long")
strategy.exit("Exit", "Long", when = 15)
The code above I want to exit
the long position after 15 days. But it doesn't seem to work.
Try 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)
I figured it out one solution. I created another if
statement but offset by 15 days to trigger. I also set the crossover to a variable. See code:
buy = (crossover(delta, 0))
if (buy)
strategy.entry("Long", strategy.long, comment="Long")
if (buy[15])
strategy.close("Long")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.