JustSander98
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: December 18th, 2023
Contact: TradingView Profile

Buy/Sell signals firing twice?

Hi TradingView community :)

I wrote a simple script using 3 simple moving averages and a smooth Heikin-Ashi, but for some reason my buy and sell alerts go double?

The idea is to Long when trends are up, and when the trend is getting weaker to sell, and same for short.
For some reason it now gets out of the long and immediately enters a short, or gets out of the short and immediately enters the long.

The entry moments seem to be correct, but the exit's should be triggered sooner since it has the same statements, but instead of "And" it has "Or".
It should trigger as soon as 1 of the satements is true, instead of when all of them are true.

Does anyone know how to fix this problem?
Here is my script :
// Define Moving Averages
ma1 = ta.sma(close, 1)
ma5 = ta.sma(close, 5)
ma10 = ta.sma(close, 10)

// Define Smoothed Heikin Ashi
length = 20
ha_open = ta.sma(ohlc4, length)
ha_close = ta.sma(close, length)
ha_high = ta.sma(high, length)
ha_low = ta.sma(low, length)
ha_color = ha_open < ha_close ? color.green : color.red

// Plot Heikin Ashi candles
plotcandle(ha_open, ha_high, ha_low, ha_close, color=ha_color, title="Heikin Ashi")

// Buy Conditions Long
buy_conditionLong = ma1 > ma5 and ma5 > ma10 and ha_color == color.green

// Sell Conditions Long
sell_conditionLong = ma1 < ma5 or ma5 < ma10 or ha_color == color.red or ta.crossover(ha_close, ha_open)

//execute trades Long
strategy.entry("Buy Long", strategy.long, when=buy_conditionLong)
strategy.close("Sell Long", when=sell_conditionLong)

// Buy Conditions Short
buy_conditionShort = ma1 < ma5 and ma5 < ma10 and ha_color == color.red

// Sell Conditions Short
sell_conditionShort = ma1 > ma5 or ma5 > ma10 or ha_color == color.green or ta.crossover(ha_open, ha_close)

//execute trades Short
strategy.entry("Buy Short", strategy.short, when=buy_conditionShort)
strategy.close("Sell Short", when=sell_conditionShort)

Return to “General Trading Discussions”