Scripts: Balance Graph HTML

To add comments, please log in or register
Automated-Trading
152595
Automated-Trading  

Balance Graph HTML:

Display interactive HTML graph of the account balance inside the web browser.

Balance Graph HTML

Author: amrali

fxsaber
15877
fxsaber  
Super! More examples with HighCharts please.
fxsaber
15877
fxsaber  
For MT4/5.
// MQL4&5-code

#property strict

// Copy "Graph.txt" to "Files\Graph.txt".
// https://www.mql5.com/en/code/25199
// #resource "\\Scripts\\Graph_HTML\\Resource\\Graph.txt" as string htm_file

#import "shell32.dll"
  int ShellExecuteW( int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd );
#import

#ifdef __MQL5__
  #include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

  #define BASEPATH (TerminalInfoString(TERMINAL_PATH) + "\\MQL5\\Files\\")
#else // __MQL5__
  #define BASEPATH (TerminalInfoString(TERMINAL_PATH) + "\\MQL4\\Files\\")
#endif // __MQL5__

bool CreateBalanceData( const string FileName = "exdat.txt" )
{
  const int handle = FileOpen(FileName, FILE_WRITE | FILE_TXT | FILE_ANSI);
  const bool Res = (handle != INVALID_HANDLE);

  if (Res)
  {
  // https://www.mql5.com/ru/forum/170953/page14#comment_10550116
  #define CLOSETIME_INDEX 0
  #define TICKET_INDEX 1
    long Tickets[][2];
    int Total = OrdersHistoryTotal();    

  #ifdef __MQL5__
    static const bool Sort = false;
  #else // __MQL5__
    #ifdef __VIRTUAL__
      static const bool IsTester = ::MQLInfoInteger(MQL_TESTER);

      const bool Sort = !IsTester && !VIRTUAL::GetHandle();
    #else // __VIRTUAL__
      static const bool Sort = !::MQLInfoInteger(MQL_TESTER);
    #endif // __VIRTUAL__

    if (Sort)
    {
      int Amount = 0;

      for (int i = (::ArrayResize(Tickets, Total) >> 1) - 1; i >= 0; i--) // https://www.mql5.com/ru/forum/170953/page16#comment_10604064
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
          Tickets[Amount][CLOSETIME_INDEX] = OrderCloseTime();
          Tickets[Amount++][TICKET_INDEX] = OrderTicket();
        }

      if ((bool)(Total = ::ArrayResize(Tickets, Amount) >> 1)) // https://www.mql5.com/ru/forum/170953/page16#comment_10604064
        ::ArraySort(Tickets);
    }
  #endif // __MQL5__
    
    double Balance = 0;

    FileWriteString(handle, "var dat1=[\n");

    for (int i = 0; i < Total; i++)
      if ((Sort ? OrderSelect((int)Tickets[i][TICKET_INDEX], SELECT_BY_TICKET) : (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))) && // int - (Sort == true) only for MQL4.
          ((OrderType() <= OP_SELL) || (OrderType() > OP_SELLSTOP)))
    #undef TICKET_INDEX
    #undef CLOSETIME_INDEX
      {
        Balance += OrderProfit() + OrderCommission() + OrderSwap();

        FileWriteString(handle, "[" + (string)((long)OrderCloseTime() * 1000) + "," + DoubleToString(Balance, 2) + "],\n");
      }

    FileWriteString(handle, "];\n");
    FileWriteString(handle, "var T1=dat1[0][0];\n");
    FileWriteString(handle, "var T2=dat1[dat1.length-1][0];\n");
    FileWriteString(handle, "var nTrades=dat1.length;\n");
    FileWriteString(handle, "var Balance=" + DoubleToString(Balance, 2) + ";\n");
    FileWriteString(handle, "var Currency=\"" + AccountInfoString(ACCOUNT_CURRENCY) + "\";\n");

    FileClose(handle);
  }

  return(Res);
}

string GetHTM( const string FileName = "Graph.txt" )
{
  uchar Data[];

  const int handle = FileOpen(FileName, FILE_READ | FILE_BIN);
  const bool Res = (handle != INVALID_HANDLE);

  if (Res)
  {
    FileReadArray(handle, Data);

    FileClose(handle);
  }

  return(Res ? CharArrayToString(Data) : NULL);
}

//+------------------------------------------------------------------+
//| Creates 'HighCharts' balance report on trades in .html format    |
//+------------------------------------------------------------------+
void CreateHighChartsBalanceReport( const string FileName = "Graph.htm" )
{
  const int handle = FileOpen(FileName, FILE_WRITE | FILE_TXT | FILE_ANSI);

  if (handle != INVALID_HANDLE)
  {
//    FileWriteString(handle, htm_file);
    FileWriteString(handle, GetHTM());
    FileClose(handle);

    if (CreateBalanceData())
      ShellExecuteW(0, "Open", BASEPATH + FileName, NULL, NULL, 3);
  }
}

void OnStart()
{
  CreateHighChartsBalanceReport();
}


amrali
1851
amrali  
Hi fxsaber. Just copy Graph.txt to Files\Graph.htm 
fxsaber
15877
fxsaber  
amrali:
Hi fxsaber. Just copy Graph.txt to Files\Graph.htm 

txt.

amrali
1851
amrali  

For extra bonus

void OnTester()
{
  CreateHighChartsBalanceReport();
}
amrali
1851
amrali  

Function GetHtm() is not necessary if you copy as Graph.htm directly.

But I include it as a resource to automate the process  

fxsaber
15877
fxsaber  
amrali:

Function GetHtm() is not necessary if you copy as Graph.htm directly.

But I include it as a resource to automate the process  

Only for MT5.


CreateHighChartsBalanceReport("Graph_NewReport.htm");
amrali
1851
amrali  

Another option to include the html code as a string defined in an auxiliary header. open Graph.txt in a text editor and  repace \ with \\

Then replace " with \" 

//htm.mqh
string htm_code ="<html>...</html>";
fxsaber
15877
fxsaber  
amrali:

Another option to include the html code as a string defined in an auxiliary header. open Graph.txt in.text editor and replace " with \" 

amrali
1851
amrali  
fxsaber:
For MT4/5.


This might be shorter and faster

#property strict
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CreateBalanceData(const string FileName="exdat.txt")
  {
   const int handle=FileOpen(FileName,FILE_WRITE|FILE_TXT|FILE_ANSI);
   const bool Res=(handle!=INVALID_HANDLE);

   if(Res)
     {
      //--- Sort history profits by close time
      double Profits[][2];
      int cnt=0;
      for(int i=0; i<OrdersHistoryTotal(); i++)
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
            if(OrderType()<=OP_SELL || OrderType()>=6)
              {
               ArrayResize(Profits,++cnt,1000);
               Profits[cnt-1][0] = OrderCloseTime();
               Profits[cnt-1][1] = OrderProfit()+OrderCommission()+OrderSwap();
              }
      ArraySort(Profits);
      //---
      double Balance=0;
      FileWriteString(handle,"var dat1=[\n");
      for(int i=0; i<cnt; i++)
        {
         Balance+=Profits[i][1];
         FileWriteString(handle,"["+(string)(ulong)(Profits[i][0]*1000)+","+DoubleToString(Balance,2)+"],\n");
        }
      FileWriteString(handle,"];\n");
      FileWriteString(handle,"var T1=dat1[0][0];\n");
      FileWriteString(handle,"var T2=dat1[dat1.length-1][0];\n");
      FileWriteString(handle,"var nTrades=dat1.length;\n");
      FileWriteString(handle,"var Balance="+DoubleToString(Balance,2)+";\n");
      FileWriteString(handle,"var Currency=\""+AccountInfoString(ACCOUNT_CURRENCY)+"\";\n");

      FileClose(handle);
     }

   return(Res);
  }
12
To add comments, please log in or register