//+------------------------------------------------------------------+ //| EdgerySync.mq4 - sends your MetaTrader 4 trades to Edgery | //| Open trades (live P/L) every few seconds, closed trades 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" #property strict 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 "mt4-1.00" datetime g_lastClosedSent = 0; // server time of the newest closed trade already sent int g_tick = 0; long ServerToUtc() { return (long)(TimeGMT() - TimeCurrent()); } // TimeCurrent = last known server time 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 ", AccountNumber()); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); } string Row(bool closed, long off) { string sym = OrderSymbol(); int dg = (int)MarketInfo(sym, MODE_DIGITS); if(dg <= 0) dg = 5; string r = "{\"ticket\":\"" + IntegerToString(OrderTicket()) + "\"" + ",\"symbol\":\"" + Esc(sym) + "\"" + ",\"type\":\"" + (OrderType() == OP_BUY ? "buy" : "sell") + "\"" + ",\"lots\":" + Num(OrderLots(), 2) + ",\"open_time\":" + IntegerToString((long)OrderOpenTime() + off) + ",\"open_price\":" + Num(OrderOpenPrice(), dg) + ",\"sl\":" + Num(OrderStopLoss(), dg) + ",\"tp\":" + Num(OrderTakeProfit(), dg) + ",\"profit\":" + Num(OrderProfit(), 2) + ",\"swap\":" + Num(OrderSwap(), 2) + ",\"commission\":" + Num(OrderCommission(), 2) + ",\"contract_size\":" + Num(MarketInfo(sym, MODE_LOTSIZE), 4); if(closed) r += ",\"close_time\":" + IntegerToString((long)OrderCloseTime() + off) + ",\"close_price\":" + Num(OrderClosePrice(), dg); else r += ",\"price\":" + Num(OrderClosePrice(), dg); // for an open trade: the current price return r + "}"; } string PositionsJson() { string out = ""; long off = ServerToUtc(); for(int i = OrdersTotal() - 1; i >= 0; i--) { if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if(OrderType() != OP_BUY && OrderType() != OP_SELL) continue; // pending orders are not trades out += (out == "" ? "" : ",") + Row(false, off); } return "[" + out + "]"; } string ClosedJson(datetime &newest) { datetime from = g_lastClosedSent > 0 ? g_lastClosedSent - 60 : TimeCurrent() - HistoryDays * 86400; newest = g_lastClosedSent; string out = ""; int count = 0; long off = ServerToUtc(); for(int i = OrdersHistoryTotal() - 1; i >= 0 && count < 500; i--) { if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue; if(OrderType() != OP_BUY && OrderType() != OP_SELL) continue; // balance, credit and cancelled orders if(OrderCloseTime() < from) continue; out += (out == "" ? "" : ",") + Row(true, off); count++; if(OrderCloseTime() > newest) newest = OrderCloseTime(); } return "[" + out + "]"; } void OnTimer() { g_tick++; datetime newest = g_lastClosedSent; bool withHistory = (g_lastClosedSent == 0 || g_tick % 6 == 0); string closed = withHistory ? ClosedJson(newest) : "[]"; string body = "{\"key\":\"" + EdgeryKey + "\",\"ea_version\":\"" + EA_VERSION + "\"" + ",\"account\":{\"login\":\"" + IntegerToString(AccountNumber()) + "\"" + ",\"server\":\"" + Esc(AccountServer()) + "\"" + ",\"company\":\"" + Esc(AccountCompany()) + "\"" + ",\"platform\":\"MT4\"" + ",\"currency\":\"" + AccountCurrency() + "\"" + ",\"balance\":" + Num(AccountBalance(), 2) + ",\"equity\":" + Num(AccountEquity(), 2) + ",\"margin\":" + Num(AccountMargin(), 2) + ",\"free_margin\":" + Num(AccountFreeMargin(), 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 == 4060 || 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 : TimeCurrent() - HistoryDays * 86400; else if(code != 200) Print("EdgerySync: Edgery answered ", code, " ", CharArrayToString(result, 0, WHOLE_ARRAY, CP_UTF8)); } //+------------------------------------------------------------------+