Avoid Re entry in same direction

Viewed 64

I am creating a Pinescript with Supertrend Indicator.

So entry when Supertrend says uptrend and take profit at 100 pips.

Short at downward Supertrend and take profit at 100 pips.

Problem is when it takes profit it renters again as Supertrend is still green or red.

I want to avoid re-entry in same direction and wait for next signal reverse signal.

Can anyone guide me in right directions.

1 Answers

you can use the strategy.position_size to know if the last trade was long or short. strategy.position_size is a positive value if you're long, and it's negative if you're short. So check like this:

if ta.barssince(strategy.position_size > 0) > ta.barssince(strategy.position_size < 0)

if this condition is true it means you last trade was short because it has been more time since your position was positive than it was last time it was negative.

Related