5

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.

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
emotheraphy
  • 97
  • 11

2 Answers2

2

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
galki
  • 8,149
  • 7
  • 50
  • 62
-2
enterlong = 0;
if (longcondition == true and close[1] < previousExitPrice)
    enterlong := 1;

strategy.entry("long", strategy.long, when = enterlong)
Nick Friskel
  • 2,369
  • 2
  • 20
  • 33
  • How do you define "previousExitPrice" in that case? I know there is a built-in parameter 'strategy.position_avg_price' which refers to previous entry price. But there is no parameter to refer to previous exit price. – emotheraphy Oct 24 '18 at 07:01