//+------------------------------------------------------------------+ //| JTVertexJournal.mq4 | //| Auto-syncs your closed MT4 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" #property strict //--- 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 = "|"; // running list of tickets already sent: |123|456| bool ready = false; //+------------------------------------------------------------------+ int OnInit() { if(StringLen(Token) < 5) { Alert("JTVertex Journal: paste your Token (from the Connect MT4 panel) into the EA inputs."); Comment("JTVertex Journal — NO TOKEN. Open EA inputs and paste your token."); return(INIT_SUCCEEDED); // stay loaded so user can fix inputs } 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(); } // fires when an order closes //+------------------------------------------------------------------+ //| Scan history, send any closed trade we haven't sent yet | //+------------------------------------------------------------------+ void SyncClosed() { int total = OrdersHistoryTotal(); for(int i = 0; i < total; i++) { if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue; int type = OrderType(); if(type != OP_BUY && type != OP_SELL) continue; // skip deposits/credits int ticket = OrderTicket(); string tag = "|" + IntegerToString(ticket) + "|"; if(StringFind(sent, tag) >= 0) continue; // already sent if(SendTrade()) sent += IntegerToString(ticket) + "|"; } } //+------------------------------------------------------------------+ //| POST the currently-selected order as JSON | //+------------------------------------------------------------------+ bool SendTrade() { double net = OrderProfit() + OrderSwap() + OrderCommission(); string json = "{"; json += "\"ticket\":" + IntegerToString(OrderTicket()) + ","; json += "\"symbol\":\"" + OrderSymbol() + "\","; json += "\"type\":\"" + (OrderType()==OP_SELL ? "sell" : "buy") + "\","; json += "\"lots\":" + DoubleToString(OrderLots(),2) + ","; json += "\"openPrice\":" + DoubleToString(OrderOpenPrice(), (int)MarketInfo(OrderSymbol(),MODE_DIGITS)) + ","; json += "\"closePrice\":" + DoubleToString(OrderClosePrice(), (int)MarketInfo(OrderSymbol(),MODE_DIGITS)) + ","; json += "\"sl\":" + DoubleToString(OrderStopLoss(), (int)MarketInfo(OrderSymbol(),MODE_DIGITS)) + ","; json += "\"tp\":" + DoubleToString(OrderTakeProfit(), (int)MarketInfo(OrderSymbol(),MODE_DIGITS)) + ","; json += "\"openTime\":\"" + TimeToString(OrderOpenTime(), TIME_DATE|TIME_MINUTES) + "\","; json += "\"closeTime\":\""+ TimeToString(OrderCloseTime(),TIME_DATE|TIME_MINUTES) + "\","; json += "\"profit\":" + DoubleToString(net,2) + ","; json += "\"comment\":\"" + EscapeJson(OrderComment()) + "\","; json += "\"platform\":\"mt4\"}"; char post[]; char result[]; string rheaders; StringToCharArray(json, post, 0, StringLen(json)); // no trailing null 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 == 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 ticket ", OrderTicket()); return(true); } Print("JTVertex Journal: server returned ", code, " ", CharArrayToString(result)); return(false); } //+------------------------------------------------------------------+ string EscapeJson(string s) { string out = ""; for(int i = 0; i < StringLen(s); i++) { ushort c = StringGetCharacter(s, i); if(c == '"' || c == '\\') out += "\\"; if(c == '\n' || c == '\r') continue; out += ShortToString(c); } return(out); } //+------------------------------------------------------------------+