//+------------------------------------------------------------------+ //| CROZAX Sync — sends your closed MetaTrader 5 trades to your free | //| CROZAX trading journal (crozax.com/journal). | //| It only READS your trade history. It never opens, changes or | //| closes trades, and your MT5 password never leaves your computer. | //| | //| Setup: | //| 1. MT5: Tools > Options > Expert Advisors > tick "Allow WebRequest | //| for listed URL" and add https://crozax.com | //| 2. Copy this file to MQL5\Experts (File > Open Data Folder), | //| open it in MetaEditor and press Compile (F7). | //| 3. Drag "CROZAX-Sync" onto any chart and paste your sync key | //| from crozax.com/journal into the SyncKey input. | //+------------------------------------------------------------------+ #property copyright "CROZAX — crozax.com" #property link "https://crozax.com/journal" #property version "1.10" #property description "Sends closed trades to your free CROZAX trading journal. Read-only: it never places or changes trades." input string SyncKey = ""; // Your sync key from crozax.com/journal input int DaysBack = 30; // On the first run, send trades closed in the last N days input int EveryMinutes = 2; // How often to check for newly closed trades string URL = "https://crozax.com/api/journal/ea"; string GV; // terminal global variable that remembers the last synced close time datetime lastSent = 0; int OnInit() { if(StringLen(SyncKey) < 24) { Alert("CROZAX Sync: paste your sync key from crozax.com/journal into the SyncKey input."); return(INIT_PARAMETERS_INCORRECT); } GV = "CROZAX_SYNC_" + IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN)); if(GlobalVariableCheck(GV)) lastSent = (datetime)GlobalVariableGet(GV); else lastSent = TimeCurrent() - DaysBack * 86400; EventSetTimer(MathMax(1, EveryMinutes) * 60); Sync(); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); } void OnTimer() { Sync(); } string JsonEsc(string s) { StringReplace(s, "\\", "\\\\"); StringReplace(s, "\"", "\\\""); return(s); } string Num(double v, int digits) { return(DoubleToString(v, digits)); } //--- one closed position as a JSON object, built from all of its deals string PositionJson(long pos_id, datetime &closeTime) { if(!HistorySelectByPosition(pos_id)) return(""); string sym = "", type = ""; double vol = 0, openPrice = 0, closePrice = 0, profit = 0, commission = 0, swap = 0, fee = 0, sl = 0, tp = 0, outVol = 0, outValue = 0; datetime openTime = 0; closeTime = 0; int digits = 5; for(int i = 0; i < HistoryDealsTotal(); i++) { ulong d = HistoryDealGetTicket(i); if(d == 0) continue; long entry = HistoryDealGetInteger(d, DEAL_ENTRY); long dtype = HistoryDealGetInteger(d, DEAL_TYPE); if(dtype != DEAL_TYPE_BUY && dtype != DEAL_TYPE_SELL) continue; sym = HistoryDealGetString(d, DEAL_SYMBOL); digits = (int)SymbolInfoInteger(sym, SYMBOL_DIGITS); profit += HistoryDealGetDouble(d, DEAL_PROFIT); commission += HistoryDealGetDouble(d, DEAL_COMMISSION); swap += HistoryDealGetDouble(d, DEAL_SWAP); fee += HistoryDealGetDouble(d, DEAL_FEE); if(entry == DEAL_ENTRY_IN) { if(openTime == 0) { openTime = (datetime)HistoryDealGetInteger(d, DEAL_TIME); openPrice = HistoryDealGetDouble(d, DEAL_PRICE); type = (dtype == DEAL_TYPE_BUY ? "buy" : "sell"); } vol += HistoryDealGetDouble(d, DEAL_VOLUME); } else if(entry == DEAL_ENTRY_OUT || entry == DEAL_ENTRY_OUT_BY || entry == DEAL_ENTRY_INOUT) { double v = HistoryDealGetDouble(d, DEAL_VOLUME); outVol += v; outValue += v * HistoryDealGetDouble(d, DEAL_PRICE); datetime t = (datetime)HistoryDealGetInteger(d, DEAL_TIME); if(t > closeTime) closeTime = t; } } if(type == "" || closeTime == 0 || outVol < vol - 0.0000001) return(""); // still open (or partly open): send it once fully closed closePrice = outValue / outVol; for(int k = 0; k < HistoryOrdersTotal(); k++) // last stop-loss / take-profit set on the position's orders { ulong o = HistoryOrderGetTicket(k); if(o == 0) continue; if(HistoryOrderGetDouble(o, ORDER_SL) > 0) sl = HistoryOrderGetDouble(o, ORDER_SL); if(HistoryOrderGetDouble(o, ORDER_TP) > 0) tp = HistoryOrderGetDouble(o, ORDER_TP); } // v1.10: MAE / MFE — the furthest price moved against / in favour of the position while it was open (M1 bars) double mae = 0, mfe = 0, hi[], lo[]; int nh = CopyHigh(sym, PERIOD_M1, openTime, closeTime, hi), nl = CopyLow(sym, PERIOD_M1, openTime, closeTime, lo); if(nh > 0 && nl > 0) { double H = hi[ArrayMaximum(hi)], L = lo[ArrayMinimum(lo)]; if(type == "buy") { mae = MathMax(0, openPrice - L); mfe = MathMax(0, H - openPrice); } else { mae = MathMax(0, H - openPrice); mfe = MathMax(0, openPrice - L); } } return("{\"position\":\"" + IntegerToString(pos_id) + "\",\"symbol\":\"" + JsonEsc(sym) + "\",\"type\":\"" + type + "\",\"volume\":" + Num(vol, 2) + ",\"openTime\":\"" + TimeToString(openTime, TIME_DATE | TIME_MINUTES) + "\",\"openPrice\":" + Num(openPrice, digits) + ",\"closeTime\":\"" + TimeToString(closeTime, TIME_DATE | TIME_MINUTES) + "\",\"closePrice\":" + Num(closePrice, digits) + ",\"sl\":" + Num(sl, digits) + ",\"tp\":" + Num(tp, digits) + ",\"commission\":" + Num(commission, 2) + ",\"fee\":" + Num(fee, 2) + ",\"swap\":" + Num(swap, 2) + ",\"profit\":" + Num(profit, 2) + ",\"mae\":" + Num(mae, digits) + ",\"mfe\":" + Num(mfe, digits) + "}"); } void Sync() { if(!HistorySelect(lastSent - 3600, TimeCurrent() + 86400)) return; long ids[]; int n = 0; for(int i = 0; i < HistoryDealsTotal(); i++) // positions that had a closing deal since the last sync { ulong d = HistoryDealGetTicket(i); if(d == 0) continue; long entry = HistoryDealGetInteger(d, DEAL_ENTRY); if(entry != DEAL_ENTRY_OUT && entry != DEAL_ENTRY_OUT_BY && entry != DEAL_ENTRY_INOUT) continue; long pid = HistoryDealGetInteger(d, DEAL_POSITION_ID); bool seen = false; for(int j = 0; j < n; j++) if(ids[j] == pid) { seen = true; break; } if(!seen) { ArrayResize(ids, n + 1); ids[n++] = pid; } } if(n == 0) return; string items = ""; int count = 0; datetime newest = lastSent; for(int j = 0; j < n; j++) { datetime ct = 0; string js = PositionJson(ids[j], ct); if(js == "") continue; items += (count > 0 ? "," : "") + js; count++; if(ct > newest) newest = ct; if(count == 100) { if(!Send(items)) return; items = ""; count = 0; } } if(count > 0 && !Send(items)) return; lastSent = newest; GlobalVariableSet(GV, (double)lastSent); } bool Send(string items) { string body = "{\"account\":\"" + IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN)) + "\",\"trades\":[" + items + "]}"; char data[], result[]; string headers; StringToCharArray(body, data, 0, WHOLE_ARRAY, CP_UTF8); ArrayResize(data, ArraySize(data) - 1); // drop the trailing zero ResetLastError(); int code = WebRequest("POST", URL + "?key=" + SyncKey, "Content-Type: application/json\r\n", 15000, data, result, headers); if(code == -1) { Print("CROZAX Sync: add https://crozax.com in Tools > Options > Expert Advisors > Allow WebRequest. Error ", GetLastError()); return(false); } if(code != 200) { Print("CROZAX Sync: server answered ", code, " ", CharArrayToString(result)); return(false); } Print("CROZAX Sync: ", CharArrayToString(result)); return(true); } //+------------------------------------------------------------------+