//+------------------------------------------------------------------+ //| EdgerySync.mq5 - sends your MetaTrader 5 trades to Edgery | //| Open positions (live P/L) every few seconds, closed positions | //| with lots, open/close time and price, SL/TP, commission, swap. | //| Read-only: this EA never opens, changes or closes a trade. | //| Setup: Tools > Options > Expert Advisors > Allow WebRequest for | //| https://edgery.app then attach to any one chart and paste your | //| sync key from Edgery > Live sync. | //+------------------------------------------------------------------+ #property copyright "Edgery" #property link "https://edgery.app" #property version "1.00" input string EdgeryKey = ""; // Sync key from Edgery > Live sync input int IntervalSeconds = 5; // How often to send (seconds) input int HistoryDays = 30; // Closed trades to send on the first run (days) input string EdgeryUrl = "https://edgery.app/api/bridge/mt"; // Do not change #define EA_VERSION "mt5-1.00" datetime g_lastClosedSent = 0; // server time of the newest closed position already sent int g_tick = 0; //--- seconds to add to a trade-server time to get UTC long ServerToUtc() { return (long)(TimeGMT() - TimeTradeServer()); } string Esc(string s) { StringReplace(s, "\\", "\\\\"); StringReplace(s, "\"", "\\\""); return s; } string Num(double v, int digits) { return DoubleToString(v, digits); } int OnInit() { if(StringLen(EdgeryKey) != 48) { Alert("EdgerySync: paste your 48-character sync key from Edgery > Live sync."); return(INIT_PARAMETERS_INCORRECT); } EventSetTimer(MathMax(2, IntervalSeconds)); Print("EdgerySync started. Account ", AccountInfoInteger(ACCOUNT_LOGIN)); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); } //--- open positions with live profit string PositionsJson() { string out = ""; long off = ServerToUtc(); for(int i = PositionsTotal() - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if(ticket == 0) continue; string sym = PositionGetString(POSITION_SYMBOL); long type = PositionGetInteger(POSITION_TYPE); int dg = (int)SymbolInfoInteger(sym, SYMBOL_DIGITS); string row = "{\"ticket\":\"" + IntegerToString(PositionGetInteger(POSITION_IDENTIFIER)) + "\"" + ",\"symbol\":\"" + Esc(sym) + "\"" + ",\"type\":\"" + (type == POSITION_TYPE_BUY ? "buy" : "sell") + "\"" + ",\"lots\":" + Num(PositionGetDouble(POSITION_VOLUME), 2) + ",\"open_time\":" + IntegerToString(PositionGetInteger(POSITION_TIME) + off) + ",\"open_price\":" + Num(PositionGetDouble(POSITION_PRICE_OPEN), dg) + ",\"price\":" + Num(PositionGetDouble(POSITION_PRICE_CURRENT), dg) + ",\"sl\":" + Num(PositionGetDouble(POSITION_SL), dg) + ",\"tp\":" + Num(PositionGetDouble(POSITION_TP), dg) + ",\"profit\":" + Num(PositionGetDouble(POSITION_PROFIT), 2) + ",\"swap\":" + Num(PositionGetDouble(POSITION_SWAP), 2) + ",\"contract_size\":" + Num(SymbolInfoDouble(sym, SYMBOL_TRADE_CONTRACT_SIZE), 4) + "}"; out += (out == "" ? "" : ",") + row; } return "[" + out + "]"; } //--- closed positions, built from the deal history (entry deals + exit deals of one position id) string ClosedJson(datetime &newest) { datetime from = g_lastClosedSent > 0 ? g_lastClosedSent - 60 : TimeTradeServer() - HistoryDays * 86400; newest = g_lastClosedSent; if(!HistorySelect(from - 90 * 86400, TimeTradeServer() + 86400)) return "[]"; // wide window: the entry deal can be older long off = ServerToUtc(); int total = HistoryDealsTotal(); string out = ""; int count = 0; for(int i = 0; i < total && count < 500; i++) { ulong d = HistoryDealGetTicket(i); if(d == 0) continue; long entry = HistoryDealGetInteger(d, DEAL_ENTRY); if(entry != DEAL_ENTRY_OUT && entry != DEAL_ENTRY_OUT_BY) continue; datetime ct = (datetime)HistoryDealGetInteger(d, DEAL_TIME); if(ct < from) continue; long pos = HistoryDealGetInteger(d, DEAL_POSITION_ID); string sym = HistoryDealGetString(d, DEAL_SYMBOL); double inVol = 0, outVol = 0, inPrice = 0, outPrice = 0, profit = 0, swap = 0, comm = 0; datetime ot = 0, lastOut = 0; long ptype = -1; bool laterOut = false; for(int j = 0; j < total; j++) { ulong k = HistoryDealGetTicket(j); if(k == 0 || HistoryDealGetInteger(k, DEAL_POSITION_ID) != pos) continue; long e = HistoryDealGetInteger(k, DEAL_ENTRY); double v = HistoryDealGetDouble(k, DEAL_VOLUME), p = HistoryDealGetDouble(k, DEAL_PRICE); profit += HistoryDealGetDouble(k, DEAL_PROFIT); swap += HistoryDealGetDouble(k, DEAL_SWAP); comm += HistoryDealGetDouble(k, DEAL_COMMISSION) + HistoryDealGetDouble(k, DEAL_FEE); datetime t = (datetime)HistoryDealGetInteger(k, DEAL_TIME); if(e == DEAL_ENTRY_IN) { if(ot == 0 || t < ot) ot = t; if(inVol + v > 0) inPrice = (inPrice * inVol + p * v) / (inVol + v); inVol += v; ptype = (HistoryDealGetInteger(k, DEAL_TYPE) == DEAL_TYPE_BUY) ? 0 : 1; } else if(e == DEAL_ENTRY_OUT || e == DEAL_ENTRY_OUT_BY) { if(outVol + v > 0) outPrice = (outPrice * outVol + p * v) / (outVol + v); outVol += v; if(t > lastOut) lastOut = t; if(j > i) laterOut = true; } } if(laterOut) continue; // send the position once, at its last exit deal if(ptype < 0 || inVol <= 0 || outVol < inVol - 1e-8) continue; // still partly open, or entry not in history int dg = (int)SymbolInfoInteger(sym, SYMBOL_DIGITS); if(dg <= 0) dg = 5; string row = "{\"ticket\":\"" + IntegerToString(pos) + "\"" + ",\"symbol\":\"" + Esc(sym) + "\"" + ",\"type\":\"" + (ptype == 0 ? "buy" : "sell") + "\"" + ",\"lots\":" + Num(inVol, 2) + ",\"open_time\":" + IntegerToString((long)ot + off) + ",\"open_price\":" + Num(inPrice, dg) + ",\"close_time\":" + IntegerToString((long)lastOut + off) + ",\"close_price\":" + Num(outPrice, dg) + ",\"profit\":" + Num(profit, 2) + ",\"swap\":" + Num(swap, 2) + ",\"commission\":" + Num(comm, 2) + ",\"contract_size\":" + Num(SymbolInfoDouble(sym, SYMBOL_TRADE_CONTRACT_SIZE), 4) + "}"; out += (out == "" ? "" : ",") + row; count++; if(lastOut > newest) newest = lastOut; } return "[" + out + "]"; } void OnTimer() { g_tick++; datetime newest = g_lastClosedSent; bool withHistory = (g_lastClosedSent == 0 || g_tick % 6 == 0); // closed trades: first run, then every 6th send string closed = withHistory ? ClosedJson(newest) : "[]"; string body = "{\"key\":\"" + EdgeryKey + "\",\"ea_version\":\"" + EA_VERSION + "\"" + ",\"account\":{\"login\":\"" + IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN)) + "\"" + ",\"server\":\"" + Esc(AccountInfoString(ACCOUNT_SERVER)) + "\"" + ",\"company\":\"" + Esc(AccountInfoString(ACCOUNT_COMPANY)) + "\"" + ",\"platform\":\"MT5\"" + ",\"currency\":\"" + AccountInfoString(ACCOUNT_CURRENCY) + "\"" + ",\"balance\":" + Num(AccountInfoDouble(ACCOUNT_BALANCE), 2) + ",\"equity\":" + Num(AccountInfoDouble(ACCOUNT_EQUITY), 2) + ",\"margin\":" + Num(AccountInfoDouble(ACCOUNT_MARGIN), 2) + ",\"free_margin\":" + Num(AccountInfoDouble(ACCOUNT_MARGIN_FREE), 2) + "}" + ",\"positions\":" + PositionsJson() + ",\"closed\":" + closed + "}"; char data[], result[]; string resHeaders; int n = StringToCharArray(body, data, 0, WHOLE_ARRAY, CP_UTF8); if(n > 0) ArrayResize(data, n - 1); // drop the terminating zero ResetLastError(); int code = WebRequest("POST", EdgeryUrl, "Content-Type: application/json\r\n", 10000, data, result, resHeaders); if(code == -1) { int err = GetLastError(); if(err == 4014) Print("EdgerySync: allow WebRequest for https://edgery.app in Tools > Options > Expert Advisors."); else Print("EdgerySync: connection error ", err); return; } if(code == 200 && withHistory) g_lastClosedSent = newest > 0 ? newest : TimeTradeServer() - HistoryDays * 86400; else Print("EdgerySync: Edgery answered ", code, " ", CharArrayToString(result, 0, WHOLE_ARRAY, CP_UTF8)); } //+------------------------------------------------------------------+