Best way to get Account and Trade History Data from MQL4 / MQL5 to website?

 
Hello friends!

I am building a NextJS project to improve my full stack skills and am wanting to know how to get trade/account history to display on a dashboard?

Much like MyFXBook and FXBlue do it! How do they do it? haha

I know MYFXBOOK must use some kind of api as you can enter a username and investor password and it grabs data from the broker somehow, this would be ideal but I think there might be some decent costs around it?

FXBlue use the EA method.

So far I have created an EA that sends JSON data via a POST request to an API I built, I can get current account data and trade history but no account history. I'll paste the code I've put together so far below.

Account history would be great so I can create some graphs of the balance over time, and will enable me to put together portfolios.

So basically, below in the ea, it has 2 functions, getHistory and sendHistory.

SendHistory is just the post function which I've omitted because that works fine, the timer can be set to post every x seconds passed into EventSetTimer

getHistory does the work to get the data, create arrays of objects and serializes the data


So in the below, what would be the best way to get account history?

Do I need to loop over history, and somehow get the initial balance and then balances every week or something?

I know I'm close, I hope this code can help anyone else on this mission and we can all learn together!

Peace, Rohin.
#include <requests/requests.mqh>
#include <JAson.mqh> //--- include the JSON library

extern string AccountUUID = "152b6daf-f9d6-4b4a-a6f6-b2e185e1c37d";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  

//--- create timer
   EventSetTimer(10);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   Comment("Publisher Running.");
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

   GetHistory();
   SendHistory();

  }
//+------------------------------------------------------------------+

string GetHistory()
{
   int digits = _Digits;
   CJAVal data;
   CJAVal accountData;
   CJAVal tradeData;
   
   accountData["account_uuid"] =     AccountUUID;
   accountData["balance"] =          AccountInfoDouble(ACCOUNT_BALANCE);
   accountData["credit"] =           AccountInfoDouble(ACCOUNT_CREDIT);
   accountData["profit"] =           AccountInfoDouble(ACCOUNT_PROFIT);
   accountData["equity"] =           AccountInfoDouble(ACCOUNT_EQUITY);
   accountData["margin"] =           AccountInfoDouble(ACCOUNT_MARGIN);
   accountData["margin_free"] =      AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   accountData["margin_level"] =     AccountInfoDouble(ACCOUNT_MARGIN_LEVEL);
   accountData["margin_so_call"] =   AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL);
   accountData["margin_so_so"] =     AccountInfoDouble(ACCOUNT_MARGIN_SO_SO);
//--- integers
   accountData["login"] =            AccountInfoInteger(ACCOUNT_LOGIN);
   accountData["leverage"] =         AccountInfoInteger(ACCOUNT_LEVERAGE);
   accountData["trade_allowed"] =    AccountInfoInteger(ACCOUNT_TRADE_ALLOWED);
   accountData["ea_allowed"] =       AccountInfoInteger(ACCOUNT_TRADE_EXPERT);
   accountData["trade_mode"] =       AccountInfoInteger(ACCOUNT_TRADE_MODE);
   accountData["margin_so_mode"] =   AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE);
//-- strings
   accountData["company"] =          AccountInfoString(ACCOUNT_COMPANY);
   accountData["currency"] =         AccountInfoString(ACCOUNT_CURRENCY);
   accountData["name"] =             AccountInfoString(ACCOUNT_NAME);
   accountData["server"] =           AccountInfoString(ACCOUNT_SERVER);
   
   
   data["accountData"] = accountData;
   data["trades"] = tradeData;

   int i,hstTotal=HistoryTotal();
   for(i=0;i<hstTotal;i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
      {
         if(OrderSymbol()!="") // Skip balance statements for now
         {
            tradeData["Order"] = OrderTicket();
            tradeData["Time"] = GetTimeString(OrderOpenTime());
            tradeData["Type"] = OrderType();
            tradeData["Lots"] = DoubleToString(OrderLots(), 2);
            tradeData["Symbol"] = OrderSymbol();
            tradeData["Price"] = DoubleToString(OrderOpenPrice(), digits);
            tradeData["Stoploss"] = DoubleToString(OrderStopLoss(), digits);
            tradeData["TakeProfit"] = DoubleToString(OrderTakeProfit(), digits);
            tradeData["CloseTime"] = GetTimeString(OrderCloseTime());
            tradeData["Swap"] = DoubleToString(OrderSwap(), 2);
            tradeData["Profit"] = DoubleToString(OrderProfit(), 2);
            tradeData["Comment"] = OrderComment();
            tradeData["Commission"] = DoubleToString(OrderCommission(),2);
            tradeData["Expiration"] = GetTimeString(OrderExpiration());
            tradeData["MagicNumber"] = OrderMagicNumber();
            tradeData["AccountNumber"] = AccountNumber();
         }
         if (tradeData["Order"].type != jtUNDEF) {
         data["tradeData"].Add(tradeData);
         }
         
      }
      
   }
   
   string jsonString = data.Serialize();
   return(jsonString);
}
 
Rohin Stirling Dufty:
Hello friends!

I am building a NextJS project to improve my full stack skills and am wanting to know how to get trade/account history to display on a dashboard?

Much like MyFXBook and FXBlue do it! How do they do it? haha

I know MYFXBOOK must use some kind of api as you can enter a username and investor password and it grabs data from the broker somehow, this would be ideal but I think there might be some decent costs around it?

FXBlue use the EA method.

So far I have created an EA that sends JSON data via a POST request to an API I built, I can get current account data and trade history but no account history. I'll paste the code I've put together so far below.

Account history would be great so I can create some graphs of the balance over time, and will enable me to put together portfolios.

So basically, below in the ea, it has 2 functions, getHistory and sendHistory.

SendHistory is just the post function which I've omitted because that works fine, the timer can be set to post every x seconds passed into EventSetTimer

getHistory does the work to get the data, create arrays of objects and serializes the data


So in the below, what would be the best way to get account history?

Do I need to loop over history, and somehow get the initial balance and then balances every week or something?

I know I'm close, I hope this code can help anyone else on this mission and we can all learn together!

Peace, Rohin.

To understand you better, maybe we need your json script...

 
J. Oriol #:

To understand you better, maybe we need your json script...

This creates a json object with account data and trade data, not sure what script you are referring to

You can then write the object to a file or send via post req to your api endpoint.


This works but I need account equity history with each trade so I can plot it on a chart