Summary: EA won’t close positions? This guide reveals the hidden logic traps in MQL4, including how non-zero values are interpreted as ‘true’, and the OrderSend/OrderModify timing flaw that leaves trades unprotected.




Step 1: Identify the “EA Won‘t Close” Symptom

Your EA opens trades normally but positions remain open even when take profit or stop loss conditions are met. Check the ‘Experts’ tab in Toolbox (Ctrl+T) for hidden errors. Screenshot: Toolbox window with Experts tab showing no visible red errors but positions still open on chart.

Step 2: Understand the If-Statement Trap – Non-Zero Equals True

In MQL4, an if-condition treats ANY non-zero number as ‘true’. Zero is the ONLY value treated as ‘false’. This means `if(1)`, `if(-5)`, and `if(0.5)` all execute as true[citation:3]. Screenshot: MetaEditor showing if(OrderSelect()) code example.

Step 3: Check Your Function Return Value Assumptions

Many EA developers assume a function returns 1 for success and 0 for failure. However, some MQL4 functions return different values. For example, `OrderSend()` returns the ticket number (always positive on success). Using `if(OrderSend(...))` works correctly. But if a custom function returns 0 on success and -1 on failure, `if(MyFunction())` will treat the successful 0 as FALSE[citation:3]. Screenshot: Code comparison showing correct vs incorrect return value handling.

Step 4: Examine the OrderSend → OrderModify Timing Flaw

A common hidden error: your EA sends an order without SL/TP, then immediately calls `OrderModify()` to add them. There is a tiny delay between these two operations. If price moves during that millisecond, your calculated SL/TP may become invalid (e.g., buy stop loss above market price). OrderModify fails silently, leaving your trade as a ‘naked’ position with no protection[citation:3]. Screenshot: Code example showing the flawed two-step approach.

Step 5: Fix by Setting SL/TP Directly in OrderSend()

Instead of two separate calls, calculate your stop loss and take profit BEFORE sending the order. Pass them directly into `OrderSend()` as parameters. This makes the operation atomic – the trade opens with full protection in one server request[citation:3]. Screenshot: Corrected code showing OrderSend() with pre-calculated SL and TP parameters.

Step 6: Add Proper Error Checking After Every Order Function

After any trading function, check the return value and use `GetLastError()` to capture the specific error code. Print these to the Experts tab for debugging. Example: `if(ticket<0){ Print(”Order failed. Error: “,GetLastError()); }`[citation:3]. Screenshot: Error checking code block with Print statements.

Step 7: Test Your Fixed EA on Demo First

Always run the corrected EA on a demo account for at least one week. Monitor the Experts tab for any new error messages. Confirm that all trades close properly at your specified SL/TP levels. Screenshot: Demo chart showing EA running with smiley face and closed trade history.

Reference: MQL4 Documentation; FM Studio EA Logic Guide 2025[citation:3].