Philayyy
Philayyy

Reputation: 1927

pinescript strategy.exit() function sometimes not executed when Take Profit (limit) target is reached?

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

Upvotes: 1

Views: 1055

Answers (2)

Pencilcheck
Pencilcheck

Reputation: 2932

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

Upvotes: 0

marcq
marcq

Reputation: 483

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()

Upvotes: 0

Related Questions