| #ifndef TRADE_SYNC_MQH
|
| #define TRADE_SYNC_MQH
|
| // ---------------------------------------------------------------------------
|
| // TradeSync.mqh — v7.26 (2026-11-09). Called by the rotation leader. Sends every position of this EA
|
| // (MagicNumber) closed in the last Trade_Sync_Hours as one report_trade batch; the gateway upserts on
|
| // (account, ticket) so re-sending is harmless. Values come from the deal history (exact P/L incl. swap
|
| // and commission, open/close time) and the opening order (initial SL/TP).
|
| // ---------------------------------------------------------------------------
|
| void TradeSyncTick()
|
| {
|
| if(Trade_Sync_Minutes <= 0 || !GW_Initialized) return;
|
| static datetime last = 0;
|
| if(last == 0) last = TimeLocal() - Trade_Sync_Minutes * 60 + Leader_Start_Delay_Minutes * 60; // v7.27: first run after the startup burst
|
| if(TimeLocal() - last < Trade_Sync_Minutes * 60) return;
|
| last = TimeLocal();
|
|
|
| datetime to = TimeCurrent() + 60;
|
| datetime from = to - Trade_Sync_Hours * 3600;
|
| if(!HistorySelect(from - 7 * 86400, to)) return; // a week earlier so long trades still find their entry
|
|
|
| ulong pid[]; string sym[]; string cmt[]; long dtype[];
|
| double ein[]; double vol[]; double pnl[]; long t_in[]; long t_out[];
|
| int n = 0;
|
| int total = HistoryDealsTotal();
|
| for(int i = 0; i < total; ++i)
|
| {
|
| ulong d = HistoryDealGetTicket(i);
|
| if(d == 0 || HistoryDealGetInteger(d, DEAL_MAGIC) != MagicNumber) continue;
|
| long entry = HistoryDealGetInteger(d, DEAL_ENTRY);
|
| ulong p = (ulong)HistoryDealGetInteger(d, DEAL_POSITION_ID);
|
| int k = -1;
|
| for(int j = 0; j < n; ++j) if(pid[j] == p) { k = j; break; }
|
| if(k < 0)
|
| {
|
| k = n++;
|
| ArrayResize(pid, n); ArrayResize(sym, n); ArrayResize(cmt, n); ArrayResize(dtype, n); ArrayResize(ein, n);
|
| ArrayResize(vol, n); ArrayResize(pnl, n); ArrayResize(t_in, n); ArrayResize(t_out, n);
|
| pid[k] = p; sym[k] = HistoryDealGetString(d, DEAL_SYMBOL); cmt[k] = ""; dtype[k] = -1;
|
| ein[k] = 0.0; vol[k] = 0.0; pnl[k] = 0.0; t_in[k] = 0; t_out[k] = 0;
|
| }
|
| if(entry == DEAL_ENTRY_IN)
|
| {
|
| dtype[k] = HistoryDealGetInteger(d, DEAL_TYPE); ein[k] = HistoryDealGetDouble(d, DEAL_PRICE);
|
| vol[k] = HistoryDealGetDouble(d, DEAL_VOLUME); t_in[k] = HistoryDealGetInteger(d, DEAL_TIME);
|
| cmt[k] = HistoryDealGetString(d, DEAL_COMMENT);
|
| }
|
| else if(entry == DEAL_ENTRY_OUT || entry == DEAL_ENTRY_INOUT || entry == DEAL_ENTRY_OUT_BY)
|
| {
|
| pnl[k] += HistoryDealGetDouble(d, DEAL_PROFIT) + HistoryDealGetDouble(d, DEAL_SWAP) + HistoryDealGetDouble(d, DEAL_COMMISSION);
|
| t_out[k] = MathMax(t_out[k], HistoryDealGetInteger(d, DEAL_TIME));
|
| }
|
| }
|
|
|
| string items = ""; int sent = 0;
|
| for(int k = 0; k < n; ++k)
|
| {
|
| if(t_out[k] < (long)from || t_in[k] == 0 || dtype[k] < 0) continue; // closed in the window, entry known
|
| if(PositionSelectByTicket(pid[k])) continue; // still open (partial close)
|
| double sl = 0.0, tp = 0.0;
|
| if(HistoryOrderSelect(pid[k])) { sl = HistoryOrderGetDouble(pid[k], ORDER_SL); tp = HistoryOrderGetDouble(pid[k], ORDER_TP); }
|
| int dg = (int)SymbolInfoInteger(sym[k], SYMBOL_DIGITS);
|
| string setup = cmt[k];
|
| if(StringLen(setup) == 0) setup = "auto";
|
| if(sent > 0) items += ",";
|
| items += "{\"symbol\":\"" + GWJsonEscape(sym[k]) + "\",\"direction\":\"" + (dtype[k] == DEAL_TYPE_BUY ? "BUY" : "SELL") +
|
| "\",\"volume\":" + DoubleToString(vol[k], 2) + ",\"entry\":" + DoubleToString(ein[k], dg) +
|
| ",\"sl\":" + DoubleToString(sl, dg) + ",\"tp\":" + DoubleToString(tp, dg) +
|
| ",\"profit\":" + DoubleToString(pnl[k], 2) + ",\"ticket\":" + IntegerToString((long)pid[k]) +
|
| ",\"open_time\":" + IntegerToString(t_in[k]) + ",\"close_time\":" + IntegerToString(t_out[k]) +
|
| ",\"duration_min\":" + IntegerToString((int)((t_out[k] - t_in[k]) / 60)) +
|
| ",\"setup\":\"" + GWJsonEscape(setup) + "\"}";
|
| sent++;
|
| }
|
| if(sent == 0) return;
|
| string payload = "{\"action\":\"report_trade\",\"source\":\"reconcile\",\"account\":\"" + IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN)) +
|
| "\",\"server\":\"" + GWJsonEscape(AccountInfoString(ACCOUNT_SERVER)) + "\",\"trades\":[" + items + "]}";
|
| string resp;
|
| if(GatewayRequest("report_trade", payload, resp))
|
| Print("TradeSync: sent ", sent, " closed positions (last ", Trade_Sync_Hours, "h) for reconciliation");
|
| else
|
| Print("TradeSync: gateway request failed - will retry in ", Trade_Sync_Minutes, " min");
|
| }
|
| #endif
|