this is how to fix your logic (while making the code close to how you have it)
void ScanAllChart() { int totalBars = Bars - SwingPeriod - 50; for(int i = totalBars; i >= SwingPeriod + 2; i--) { FindSwingsUpTo(i); int highCount = ArraySize(swingHighs); int lowCount = ArraySize(swingLows); if(ArraySize(swingHighs) < 2 || ArraySize(swingLows) < 2) { lastTrendDetected = 0; continue; } double ema8 = iMA(NULL,0,EMAPeriod,0,MODE_EMA,PRICE_CLOSE,i); double lastSwingLow = swingLows[lowCount-1].price; double lastSwingHigh = swingHighs[highCount-1].price; // --- Determine current bar's trend --- int currentTrend = 0; bool bullishCondition = (Close[i] > ema8 && Low[i] > lastSwingLow); bool bearishCondition = (Close[i] < ema8 && High[i] < lastSwingHigh); if(bullishCondition) currentTrend = 1; if(bearishCondition) currentTrend = -1; bool SignalGivenInThisTrend = false; if(currentTrend != 0 && currentTrend != lastTrendDetected) { SignalGivenInThisTrend = true; } if(SignalGivenInThisTrend) { string name = "SWING_" + (currentTrend==1?"BUY_":"SELL_") + IntegerToString(i) + "_" + IntegerToString(Time[i]); if(currentTrend == 1) DrawArrow(name, Time[i], Low[i] - 15*Point, BuyColor, 233, "BUY"); else if(currentTrend == -1) DrawArrow(name, Time[i], High[i] + 15*Point, SellColor, 234, "SELL"); } // --- Update the last trend detected --- if(currentTrend != 0) lastTrendDetected = currentTrend; } }
You can also do it that way:
void ScanAllChart() { int totalBars = Bars - SwingPeriod - 50; for(int i = totalBars; i >= SwingPeriod + 2; i--) { FindSwingsUpTo(i); int highCount = ArraySize(swingHighs); int lowCount = ArraySize(swingLows); if(ArraySize(swingHighs) < 2 || ArraySize(swingLows) < 2) { lastTrendDetected = 0; continue; } double ema8 = iMA(NULL,0,EMAPeriod,0,MODE_EMA,PRICE_CLOSE,i); double lastSwingLow = swingLows[lowCount-1].price; double lastSwingHigh = swingHighs[highCount-1].price; // --- Determine current bar's trend --- int currentTrend = 0; bool bullishCondition = (Close[i] > ema8 && Low[i] > lastSwingLow); bool bearishCondition = (Close[i] < ema8 && High[i] < lastSwingHigh); if(bullishCondition) currentTrend = 1; if(bearishCondition) currentTrend = -1; bool SignalGivenInThisTrend = false; if(currentTrend != 0 && currentTrend != lastTrendDetected) { SignalGivenInThisTrend = true; lastTrendDetected = currentTrend; } if(SignalGivenInThisTrend) { string name = "SWING_" + (currentTrend==1?"BUY_":"SELL_") + IntegerToString(i) + "_" + IntegerToString(Time[i]); if(currentTrend == 1) DrawArrow(name, Time[i], Low[i] - 15*Point, BuyColor, 233, "BUY"); else if(currentTrend == -1) DrawArrow(name, Time[i], High[i] + 15*Point, SellColor, 234, "SELL"); } } }
I would make this a global variable:
int lastTrendDetected = 0;
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi all,
I’m working on a custom MQL4 swing-based indicator that should generate only one clean BUY signal when a bullish trend is confirmed, and only one SELL signal when a bearish trend is confirmed — then stay silent until the trend actually reverses.
My Signal Rules (exactly as coded):
BUY Signal Conditions
→ Candle color is ignored (only Close vs EMA matters)
SELL Signal Conditions
The Problem (in practice):
Even though I use:
...the indicator still plots multiple arrows in the same trend phase.
Root Cause (I suspect):
My logic scans the chart backward ( for i = totalBars; i >= ... ) and rebuilds all swing points up to each historical bar using FindSwingsUpTo(i) .
This causes the set of detected swings — and therefore the trend state — to change artificially at different historical bars, tricking the system into thinking the trend "restarted" and triggering extra signals.
For example:
But in reality, the trend never reversed — it’s just an artifact of inconsistent swing detection across time.
I believe the solution involves:
Any guidance or code pattern would be greatly appreciated!
Thanks for your time! 🙏