1

I'm creating a strategy with the following code, and whilst manually verifying the orders I have encountered a couple of instances where the strategy.exit() function is not executed when the Take Profit (limit) target is reached, despite there being a candle on the chart where price has wicked past the 2% take profit target

See screenshot 1 where there were 2 candles that wicked past the TP target, but the strategy did not exit.

I've noticed this only a few times, mostly the order is executed (see screenshot 2)

Is there a genuine reason for this, a defect in my code, or a bug on trading views side?

OWRTPLong = strategy.position_avg_price * (1 + (2/100))  //2% above entry price
OWRTPShort = strategy.position_avg_price * (1 - (2/100)) //2% below entry price

//Enter Long Position
if analysisType == "Obtain Win Rate" and ta.crossover(line1, 99)
    strategy.order("OWR Long", strategy.long, comment = "Enter Long")

//Take first profits on Long Position
if strategy.position_size > 0
    strategy.exit("Long Exit", from_entry = "OWR Long", limit = OWRTPLong, comment = "Exit Long")

//Close Long Position
if analysisType == "Obtain Win Rate" and (ta.crossover(line2, 99) or ta.crossunder(line1, 1))
    strategy.close("OWR Long", comment="Close Long")
//---

enter image description here

enter image description here

Philayyy
  • 1,767
  • 4
  • 16
  • 22
  • Did you set calc_on_every_tick to true from the strategy() function? Source: https://www.tradingview.com/pine-script-reference/v5/#fun_strategy – Dave Oct 23 '22 at 12:49
  • @Daveatt yes re-calculate on every tick is set – Philayyy Oct 24 '22 at 03:20
  • ok that's weird, does it still happen now? – Dave Oct 24 '22 at 12:18
  • yes still happening :/ – Philayyy Oct 30 '22 at 10:30
  • Just happened to me now, wick past well over the target limit but the trade was not closed. Did you found a workaround or the reason why @Philayyy? – marcq Feb 07 '23 at 01:24
  • still happening, I have a strategy.exit with limit, and funnily when I plot it it highlight when I should sell correctly, but somehow in strategy tester it doesn't show. What is going on?? Stop order also doesn't work. Limit order also doesn't work. – Pencilcheck Aug 29 '23 at 23:07

2 Answers2

0

According to @vitruvius which gave me the solution :

You can refer to the wick with either high or low

wick_high_cross = ta.crossover(high, green_line_price)

if (wick_high_cross)
    strategy.close()
marcq
  • 477
  • 3
  • 12
0

If you have multiple strategy.exit, then it look like the limit and stop cannot overlap.

Meaning if your first strategy.exit has limit on it, then your next one can't use limit otherwise it wouldn't work.

My solution is to run the second strategy.exit after checking that the first strategy.exit condition has been executed (I look at the close price in relation to the previous exit limit)


However in your case, you should check if your limit value is correct by plotting on the chart to see

Pencilcheck
  • 2,664
  • 3
  • 25
  • 14