Pine Script Entry Strategy Referencing Previous Exit Price

Viewed 1159

I am trying to build a strategy (long positions only) in TV wherein strategy.entry will consider the previous exit price. For example:

strategy.entry("long", strategy.long, when = longcondition==true)
strategy.close("long", strategy.close, when = longcondition==false)

I would like to insert another condition for strategy.entry in addition to longcondition==true, that states the below intention:

strategy.entry("long", strategy.long, when = longcondition==true and close[1] < previousExitPrice)

How to do it correctly? Thank you in advance for the answer.

2 Answers

I can't tell what your long condition is but if it's a market sell you can try to use if instead of when and set the exit price together with the exit order:

previousExitPrice = 0.0
previousExitPrice := nz(previousExitPrice[1]) //this will put the variable in memory

strategy.entry("long", strategy.long, when = longcondition==true and close[1] < previousExitPrice)

if longcondition==false
    strategy.close("long", strategy.close)
    previousExitPrice := close //in case of market sell
enterlong = 0;
if (longcondition == true and close[1] < previousExitPrice)
    enterlong := 1;

strategy.entry("long", strategy.long, when = enterlong)
Related