Files
conversation-history/Trading/MT5/OrdersEA Smart Grid v3.4 Fix.md
T

154 lines
4.9 KiB
Markdown

# OrdersEA Smart Grid v3.4 Fix
## Master Switch - Clarified Behavior
**When Master=false (default):**
- Scope: Per-symbol only
- Each chart manages only its own symbol's grids
- EURUSD chart only sees/counts/places/cancels EURUSD orders
- GBPUSD and XAUUSD grids are completely isolated
- Sharing MagicNum 333 across symbols is safe — no disadvantages
**When Master=true:**
- Scope: Global (all symbols)
- One chart shuts down every OrdersEA grid on the account
- Used as emergency kill switch only
- Does NOT affect other algorithms (e.g., MultiSignal 777777)
## Control Matrix
| Scenario | Use This | Scope |
|----------|----------|-------|
| Normal grid trading | Master=false + GetOut="N" | Per-symbol |
| Stop one symbol's grid | GetOut="A" on that chart | Single symbol |
| Emergency: stop ALL OrdersEA grids | Master=true on any chart | Global (all symbols) |
## Why This Design Works
The EA's normal operations already filter by `_Symbol` in addition to `MagicNum`. So:
- EURUSD chart with MagicNum 333 only touches EURUSD orders
- GBPUSD chart with MagicNum 333 only touches GBPUSD orders
- No cross-symbol interference during normal operation
Master=true is the only exception — it intentionally bypasses the symbol filter for emergency shutdown.
---
## Code Diff Summary
### New Functions Added
#### `HandleGetOut()` — Closes positions + cancels orders for current symbol
```cpp
void HandleGetOut()
{
bool getOutLongs = (GetOut=="L") || (GetOut=="l") || (GetOut=="A") || (GetOut=="a") || (GetOut=="X") || (GetOut=="x");
bool getOutShorts = (GetOut=="S") || (GetOut=="s") || (GetOut=="A") || (GetOut=="a") || (GetOut=="X") || (GetOut=="x");
// Close positions (symbol-specific, MagicNum-filtered)
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket <= 0) continue;
if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue;
if(PositionGetInteger(POSITION_MAGIC) != MagicNum) continue;
long posType = PositionGetInteger(POSITION_TYPE);
if(posType == POSITION_TYPE_BUY && getOutLongs)
trade.PositionClose(ticket);
else if(posType == POSITION_TYPE_SELL && getOutShorts)
trade.PositionClose(ticket);
}
// Cancel pending orders
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
ulong ticket = OrderGetTicket(i);
if(ticket <= 0) continue;
if(OrderGetString(ORDER_SYMBOL) != _Symbol) continue;
if(OrderGetInteger(ORDER_MAGIC) != MagicNum) continue;
trade.OrderDelete(ticket);
}
gridPlaced = false;
}
```
#### `GlobalShutdown()` — Emergency global shutdown for MagicNum only
```cpp
void GlobalShutdown()
{
// Close ALL positions for this MagicNum across ALL symbols
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket <= 0) continue;
if(PositionGetInteger(POSITION_MAGIC) != MagicNum) continue;
trade.PositionClose(ticket);
}
// Cancel ALL pending orders for this MagicNum across ALL symbols
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
ulong ticket = OrderGetTicket(i);
if(ticket <= 0) continue;
if(OrderGetInteger(ORDER_MAGIC) != MagicNum) continue;
trade.OrderDelete(ticket);
}
gridPlaced = false;
}
```
### OnInit() — Added Validation
```cpp
bGetOutOK = (GetOut=="L") || (GetOut=="l") || (GetOut=="A") || (GetOut=="a") ||
(GetOut=="S") || (GetOut=="s") || (GetOut=="N") || (GetOut=="n") ||
(GetOut=="X") || (GetOut=="x");
bOpenNewTradesOK = (OpenNewTrades=="Y") || (OpenNewTrades=="y") || (OpenNewTrades=="L") || (OpenNewTrades=="l") ||
(OpenNewTrades=="N") || (OpenNewTrades=="n") || (OpenNewTrades=="S") || (OpenNewTrades=="s");
bEnableLongs = (OpenNewTrades=="Y") || (OpenNewTrades=="y") || (OpenNewTrades=="L") || (OpenNewTrades=="l");
bEnableShorts = (OpenNewTrades=="Y") || (OpenNewTrades=="y") || (OpenNewTrades=="S") || (OpenNewTrades=="s");
```
### OnTick() — Added Guards
```cpp
// Handle Master mode - global shutdown for this EA only (all symbols)
if(Master)
{
GlobalShutdown();
return;
}
// Handle GetOut mode - close trades and cancel orders, don't open new ones
if(GetOut != "N" && GetOut != "n")
{
HandleGetOut();
return;
}
```
### Grid Placement — Direction Filtering
```cpp
// Only place if valid distance from current price AND direction enabled
if(bEnableLongs && buyLevel < currentPrice - (Entry * 2 * point))
{
if(PlaceBuyLimit(buyLevel, i)) buyCount++;
}
if(bEnableShorts && sellLevel > currentPrice + (Entry * 2 * point))
{
if(PlaceSellLimit(sellLevel, i)) sellCount++;
}
```
### OpenNewTrades Block
```cpp
// Check if new trades are allowed
if(OpenNewTrades == "N" || OpenNewTrades == "n")
return;
if(!bEnableLongs && !bEnableShorts)
return;
```