3

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.

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
Mike Mann
  • 528
  • 4
  • 18

2 Answers2

5

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)
Robotronic
  • 472
  • 1
  • 6
  • 13
4

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")
Mike Mann
  • 528
  • 4
  • 18