6

I'm having an odd issue modifying an open position in TradingView's strategy tester. Let me explain the context first:
The following line opens an order based on my entry condition, entryLong.

strategy.entry("ID", strategy.long, comment="L_Entry", when = entryLong)

This works great, however, you can't make money until you close an order ;) thus:

strategy.exit( "L_STOP", "ID", loss = fixedSL * 10)

This line modifies the open order to add a stop loss at a price fixedSL below the entry position. At this point, my only exit condition is price hitting my stop loss, which will always result in a losing strategy. To address this, I include:

if (exitLong) strategy.exit("L_TRAIL", "ID", trail_points = fixedTP * 10, trail_offset = trailSL * 10)

Which then adds a trailing stop loss after reaching a set profit. This way, I can safely lock in profit while still leaving room for growth. Here lies the problem. Each exit condition has an ID - L_STOP and L_Trail (L stands for Long, bc this is a buy). I reference these IDs on my charts to help in debugging and only the L_STOP ever appears to close the order. This leads me to believe that the L_TRAIL exit condition is either never met (unlikely) or is never set. I know that the bool, exitLong, is set to True and the line should be executing.

I could avoid this issue entirely by setting the trailing stop and stop loss in a singlestrategy.exit call, but it is extremely helpful to see L_STOP or L_TRAIL printed on the screen to tell what caused the exit of a trade. Only the ID of an order is printed when its condition is met, so with one call it would only be L_STOP for instance, which doesn't give much information on the trigger to exiting.

Any and all feedback is helpful! I can also include screencaps of the charts if necessary.

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
Dasan
  • 61
  • 1
  • 2

2 Answers2

6

strategy.exit( "L_STOP", "ID", loss = fixedSL * 10)

...

strategy.exit("L_TRAIL", "ID", trail_points = fixedTP * 10, trail_offset = trailSL * 10)

...

I reference these IDs on my charts to help in debugging and only the L_STOP ever appears to close the order. This leads me to believe that the L_TRAIL exit condition is either never met (unlikely) or is never set.

The problem here is that you use the strategy.exit() function twice, both times to set a stop loss (fixed stop and a trailing stop loss).

But that is not how strategy.exit() can work. TradingView's reference says:

"If you use a stop loss and a trailing stop, their order type is 'stop', so only one of them is placed (the one that is supposed to be filled first)."

This would explain why your second stop loss (the trail one) is not set.

What you'll need to do instead is rewrite your strategy code so that there's one stop send by strategy.exit(). Without seeing the full code I can't offer much practical advice. But perhaps you can give your trailing stop loss the trail_offset value that your normal stop would be set to?

I can't tell from your question what values the variables hold, but perhaps this would work:

stopPrice = exitLong ? strategy.position_avg_price - (trailSL * 10) :
     strategy.position_avg_price - (fixedSL * 10)
     
strategy.exit("L_STOP", "ID", stop=stopPrice)
Community
  • 1
  • 1
Jos
  • 1,732
  • 3
  • 19
  • 39
  • Ahh, thank you so much! I guess I missed that bit on order types. I can easily get around the problem by setting a loss and trailing stop in a single strategy.exit() call, however I wanted to see exactly what causes the exit - price touching my original stop loss or touching the trailing stop. I guess if I made profit, it should be obvious that it touched the trailing stop. Thank you for the response! You helped clear up a very dumb issue for me – Dasan Aug 02 '18 at 02:55
2

This worked for me. Do a "strategy.cancel()" before the "strategy.exit()".

Now i get the id correctly plotted on the chart.

Example:

if (strategy.position_size > 0) //long position exists
    if strategy.position_avg_price < open //price is lower than my entry_price
        strategy.cancel(id="StopLoss")
        strategy.exit(id="TakePrft", limit= longLimit, stop = stopPrice)
    else if strategy.position_avg_price > open //price is higher than my entry_price
        strategy.cancel(id="TakePrft")
        strategy.exit(id="StopLoss", limit= longLimit, stop = stopPrice)
Penquin
  • 81
  • 1
  • 5