//+------------------------------------------------------------------+ //| JTVertexJournal.mq5 | //| Auto-syncs your closed MT5 trades to your JTVertex Journal. | //| https://jtvertex.com.au · Education only. Capital at risk. | //+------------------------------------------------------------------+ #property copyright "JTVertex" #property link "https://jtvertex.com.au" #property version "1.00" //--- paste these two from the "Connect MT4" panel in your journal --- input string ServerURL = "https://journal.jtvertex.com.au/api/ingest"; // Server URL input string Token = ""; // Your private token input int SyncEverySeconds = 15; // How often to check input bool SyncOnAttach = true; // Backfill history on start string sent = "|"; bool ready = false; //+------------------------------------------------------------------+ int OnInit() { if(StringLen(Token) < 5) { Alert("JTVertex Journal: paste your Token into the EA inputs."); Comment("JTVertex Journal — NO TOKEN. Open EA inputs and paste your token."); return(INIT_SUCCEEDED); } ready = true; EventSetTimer(MathMax(5, SyncEverySeconds)); Comment("JTVertex Journal — connected. Syncing closed trades to " + ServerURL); if(SyncOnAttach) SyncClosed(); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); Comment(""); } void OnTimer() { if(ready) SyncClosed(); } void OnTrade() { if(ready) SyncClosed(); } //+------------------------------------------------------------------+ //| Walk closed positions via history deals (group by position id) | //+------------------------------------------------------------------+ void SyncClosed() { if(!HistorySelect(0, TimeCurrent())) return; int deals = HistoryDealsTotal(); for(int i = 0; i < deals; i++) { ulong ticket = HistoryDealGetTicket(i); if(ticket == 0) continue; if((ENUM_DEAL_ENTRY)HistoryDealGetInteger(ticket, DEAL_ENTRY) != DEAL_ENTRY_OUT) continue; // closing deal only long posId = HistoryDealGetInteger(ticket, DEAL_POSITION_ID); string tag = "|" + (string)posId + "|"; if(StringFind(sent, tag) >= 0) continue; if(SendPosition(posId, ticket)) sent += (string)posId + "|"; } } //+------------------------------------------------------------------+ bool SendPosition(long posId, ulong outTicket) { // aggregate the position's open/close from its deals string symbol = HistoryDealGetString(outTicket, DEAL_SYMBOL); double closePrice = HistoryDealGetDouble(outTicket, DEAL_PRICE); datetime closeTime = (datetime)HistoryDealGetInteger(outTicket, DEAL_TIME); double netProfit = 0.0, openPrice = 0.0, lots = 0.0; datetime openTime = 0; int isSell = 0; int n = HistoryDealsTotal(); for(int i = 0; i < n; i++) { ulong d = HistoryDealGetTicket(i); if(HistoryDealGetInteger(d, DEAL_POSITION_ID) != posId) continue; netProfit += HistoryDealGetDouble(d, DEAL_PROFIT) + HistoryDealGetDouble(d, DEAL_SWAP) + HistoryDealGetDouble(d, DEAL_COMMISSION); ENUM_DEAL_ENTRY entry = (ENUM_DEAL_ENTRY)HistoryDealGetInteger(d, DEAL_ENTRY); if(entry == DEAL_ENTRY_IN) { openPrice = HistoryDealGetDouble(d, DEAL_PRICE); openTime = (datetime)HistoryDealGetInteger(d, DEAL_TIME); lots = HistoryDealGetDouble(d, DEAL_VOLUME); isSell = (HistoryDealGetInteger(d, DEAL_TYPE) == DEAL_TYPE_SELL) ? 1 : 0; // entry deal type } } int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS); string json = "{"; json += "\"ticket\":" + (string)posId + ","; json += "\"symbol\":\"" + symbol + "\","; json += "\"type\":\"" + (isSell ? "sell" : "buy") + "\","; json += "\"lots\":" + DoubleToString(lots, 2) + ","; json += "\"openPrice\":" + DoubleToString(openPrice, digits) + ","; json += "\"closePrice\":" + DoubleToString(closePrice, digits) + ","; json += "\"openTime\":\"" + TimeToString(openTime, TIME_DATE|TIME_MINUTES) + "\","; json += "\"closeTime\":\""+ TimeToString(closeTime, TIME_DATE|TIME_MINUTES) + "\","; json += "\"profit\":" + DoubleToString(netProfit, 2) + ","; json += "\"platform\":\"mt5\"}"; char post[]; char result[]; string rheaders; StringToCharArray(json, post, 0, StringLen(json)); string headers = "Content-Type: application/json\r\nX-MT-Token: " + Token + "\r\n"; ResetLastError(); int code = WebRequest("POST", ServerURL, headers, 8000, post, result, rheaders); if(code == -1) { int err = GetLastError(); if(err == 4014 || err == 4060) Alert("JTVertex Journal: add ", ServerURL, " under Tools > Options > Expert Advisors > Allow WebRequest for listed URL."); Print("JTVertex Journal WebRequest failed, err=", err); return(false); } if(code == 200) { Print("JTVertex Journal: synced position ", posId); return(true); } Print("JTVertex Journal: server returned ", code, " ", CharArrayToString(result)); return(false); } //+------------------------------------------------------------------+