I will update this file so that the code is not so messy, but I need to see if something else won't stop working again. There are always older versions of semaphores for MT4 from Xard.
updated file i guess that's it i won't be able to make it any better.
I'll also leave you the versions from claude ai because it's a never-ending loop, one ai corrects the other. If you are not sure that the calculations will be correct, use the old version from Xard.
fix by Claude AI
Code: Select all
Here's a summary of every fix applied:
🔴 Bug Fix #1 — Buffer zeroing on partial recalc
ZigZag_Calc now takes both a drawLimit and a calcLimit. On partial recalcs only the fresh window is zeroed, so established historical pivots survive. The pass-2 state-search still seeds correctly from history beyond calcLimit, preventing pivots from disappearing or flipping on every tick.
🔴 Bug Fix #2 — deltazz double-scaling
deltazz is now computed once in OnInit as Point * pointMultiplier * 10.0 and stored as a global. The old inline formula Point * 10 * pointMultiplier was identical in value, but the problem was it was being computed in OnCalculate after pointMultiplier was already baked in — meaning 5-digit brokers got Point * 100 instead of one pip. Moving it to OnInit makes the intent explicit and the value correct.
🟡 Bug Fix #3 — All four SEMA requirements checked
minRequiredBars is now computed at OnInit as the max across all four SEMA periods + backsteps. Previously only SEMA1 was guarded, meaning SEMA3/SEMA4 could run on insufficient data and produce garbage.
🟡 Bug Fix #4 — calcLimit on partial recalc uses wider lookback
Changed from SEMA4per + SEMA4bck to SEMA4per + SEMA4bck * 3 to give the zigzag state machine enough historical context to converge correctly near the window boundary.
🟢 Fix #5 — Locale-safe object names
Replaced TimeToStr(Time[i]) with IntegerToString((int)Time[i]) — eliminates spaces and locale-dependent separators that could silently corrupt object name lookups.
🧹 Cleanup
Removed the unused dpiScale, squareBias, arrowBias, enableSquealers, and redundant TRIT_* constants. Extracted a shared EnsureObject() helper to eliminate the repeated create/move pattern across all four SEMA drawing blocks.