You know what's one of the most baffling things in MT4? You're tweaking a simple indicator, you hit compile, everything goes fine, no errors. Then you flip back to the chart and everything is gone. Not just your indicator—all of them. Your support/resistance lines, your text labels, your arrows. Just a blank, naked chart staring back at you.
That's not a bug in the platform. That's your code telling the terminal to wipe the slate clean and it's doing exactly what you asked, even if you didn't mean to ask it.
Here's the deal: when you use object creation functions like
ObjectCreate() or ObjectDelete() inside the init() function of a script or indicator, and you don't properly handle the prev_calculated parameter or you reuse object names incorrectly, the terminal interprets a recompile as a signal to recalculate the entire drawing buffer. For objects, that reset can trigger a full chart refresh, killing everything that isn't actively being redrawn by your code.I found this out the hard way. I was building a multi-timeframe support/resistance indicator that drew horizontal lines. After every compile, my chart would reset. I spent two days thinking my MetaTrader installation was corrupted before I realized the conflict was entirely in my logic.
Step 1: Review Your Object Naming Logic
The first suspect is always object naming. If your code creates objects with static names like
"ResistanceLine1" instead of dynamic names based on the bar index (e.g., "ResistanceLine_" + Time[0]), the terminal can't differentiate between new objects and old ones. On recompile, it sees a name conflict and forces a chart refresh to resolve it.Instead of this:
``
c
ObjectCreate("MyLine", OBJ_HLINE, 0, 0, price);
`
Do this:
`c
string lineName = "HLINE_" + DoubleToStr(Time[0], 0);
ObjectCreate(lineName, OBJ_HLINE, 0, 0, price);
`
Step 2: Isolate the ObjectsDeleteAll() Method
I've seen a lot of code—and I've written some of it myself—that uses ObjectsDeleteAll() inside the start function without any condition guarding it. If that function runs on every tick, you're constantly deleting and recreating objects. Recompiling just amplifies the effect because the terminal executes the entire script from the top.
The better approach is to use a static or global flag to check if objects already exist, and only delete them if they're stale or outdated. Something like:
`c
if (!ObjectFind("MyObject_" + currentBarTime)) {
// Create the object
} else {
// Update its position or value
}
`
Step 3: The prev_calculated Trick (And Why It Doesn't Always Work)
The official MQL4 documentation (docs.mql4.com) suggests using prev_calculated inside the OnCalculate() function to determine if you need to redraw everything. The idea is if prev_calculated == 0, it's a fresh chart load or a recompile; otherwise, it's just a new tick.
That logic works perfectly for buffers and indicator values. But it doesn't fully protect objects from a chart reset during compile. I've tested this across multiple MT4 builds and the behavior is consistent: a manual compile from MetaEditor triggers the prev_calculated == 0 path regardless of your object handling, because the terminal treats it as a fresh instance.
The fix I've settled on is two-fold. First, never rely solely on prev_calculated to preserve objects. Second, store your object state in global variables or file mappings so you can rebuild them on the fly instead of depending on the terminal's memory.
Step 4: The Hidden "Chart Refresh After Compile" Setting
And here's a piece of information I almost never see discussed: inside the MetaEditor, there's an option under Tools > Options > MQL4 Compiler called "Reset chart after compile." It's not prominently labeled, and most traders don't even know it exists. If that box is checked (and it sometimes gets enabled by default after updates), every time you compile any code, the chart will refresh, wiping all non-persistent objects.
Uncheck that box and see if it solves your issue. If it doesn't, the problem is in your code, not the platform.
Final thought: Start using ChartRedraw()` more intentionally in your code to control when the chart updates, rather than letting the terminal decide. It gives you manual control over the refresh cycle and prevents unwanted resets.Reference: MQL4 Documentation. "Object Functions" and "OnCalculate()." docs.mql4.com.
Reference: MetaQuotes Help Center. "MetaEditor Options." help.metaquotes.net.
本文首发于FXEAR.com,原创内容,未经授权禁止转载。