Automatically log all account history to .csv file

 

Hello! I am pretty new to MQL and I was wondering how I could make an EA that sends all closed trades to a csv file and keeps updating once new orders are closed.


Just trying to make an excel dashboard to show all my stats and help visualize everything. Managed to get live PNL working and balance but can't for the life of me get it to export trade history automatically every time a new order is closed.


Any suggestions would be great, I have seen a few places online such as https://forexbook.com/ that are able to do this but I want to have the data myself.


Thanks - Jack

 
Strix.: Hello! I am pretty new to MQL and I was wondering how I could make an EA that sends all closed trades to a csv file and keeps updating once new orders are closed. Just trying to make an excel dashboard to show all my stats and help visualize everything. Managed to get live PNL working and balance but can't for the life of me get it to export trade history automatically every time a new order is closed. Any suggestions would be great, I have seen a few places online such as ... that are able to do this but I want to have the data myself.

Before posting, remember to use this sites search function, and also read up on the terminal's documentation.

In the Codebase you will find several examples of such functionality: https://www.mql5.com/en/search#!keyword=export%20trades&module=mql5_module_codebase

You can also export your trade history report directly to an Excel sheet: MetaTrader 5 HelpTrading OperationsFor Advanced UsersTrading Report

 
Fernando Carreiro #:

Before posting, remember to use this sites search function, and also read up on the terminal's documentation.

In the Codebase you will find several examples of such functionality: https://www.mql5.com/en/search#!keyword=export%20trades&module=mql5_module_codebase

You can also export your trade history report directly to an Excel sheet: MetaTrader 5 HelpTrading OperationsFor Advanced UsersTrading Report

Hi Fernando,


I have tried searching the forum for a while but had no hope. And I am going over the documentation again to try wrap my head around it now

 
Strix. #: I have tried searching the forum for a while but had no hope. And I am going over the documentation again to try wrap my head around it now

My post directs you to what I found in a matter of seconds in a quick search with the keywords "export trades". So, follow up on the links I provided.

 
Strix. #:

Do not double post!

I have deleted your new topic concerning this.

Continue your discussion here.

 
Keith Watford #:

Do not double post!

I have deleted your new topic concerning this.

Continue your discussion here.

I have not? That was a separate issue regarding live trades and looping for each open position. Not logging all data to a csv file...
 
Strix. #:
I have not? That was a separate issue regarding live trades and looping for each open position. Not logging all data to a csv file...

Your new topic concerned logging open trades to a csv file so basically the same as this topic. Just open trades instead of closed trades.

Strix.:

Hello, I am trying to develop a bot that outputs all my open trade information into a CSV file that updates every tick.................

 
Keith Watford #:

Your new topic concerned logging open trades to a csv file so basically the same as this topic. Just open trades instead of closed trades.

No one on this thread seems to know how to log live or closed trades though and I'm just wondering how to get it working, I just need to somehow loop through the code for each open position. Maybe add a counter and log all the position ID's to ignore?
 
Strix. #: No one on this thread seems to know how to log live or closed trades though and I'm just wondering how to get it working, I just need to somehow loop through the code for each open position. Maybe add a counter and log all the position ID's to ignore?

Most of the medium to experienced MQL coders know how to log the live trades or trade history to external files, and I have already directed you to the necessary research material, but it seems you are not very inclined to putting in the effort.

There is very little difference between accessing and logging the trade history or the current live trades and there are examples in the CodeBase, there are Articles about it and there are many forum topics about it, and finally there is also the documentation to reference it all of it. And it is all accessible via the Search functionality on this website.

So far, you have not shown any code of yours, attempting to do it. In fact, you could have started with an existing code from the CodeBase and started changing it little by little to accomplish your goal.

Yet, we have not seen that much from you! So what else do you want use to do?

 
Fernando Carreiro #:

Most of the medium to experienced MQL coders know how to log the live trades or trade history to external files, and I have already directed you to the necessary research material, but it seems you are not very inclined to putting in the effort.

There is very little difference between accessing and logging the trade history or the current live trades and there are examples in the CodeBase, there are Articles about it and there are many forum topics about it, and finally there is also the documentation to reference it all of it. And it is all accessible via the Search functionality on this website.

So far, you have not shown any code of yours, attempting to do it. In fact, you could have started with an existing code from the CodeBase and started changing it little by little to accomplish your goal.

Yet, we have not seen that much from you! So what else do you want use to do?

I attached code into the new thread but Keith decided to delete it, I'll send my current code and the code I'm working on to get the current details of live trades:

This is the current code I am using to send all my live account data to a single string in CSV:


void OnTick()
   {
      string AccountServer=AccountInfoString(ACCOUNT_SERVER);
      string AccountCurrency=AccountInfoString(ACCOUNT_CURRENCY);
      string AccountName=AccountInfoString(ACCOUNT_NAME);
      double AccountBalance=AccountInfoDouble(ACCOUNT_BALANCE);
      double AccountEquity=AccountInfoDouble(ACCOUNT_EQUITY);
      double AccountProfit=AccountInfoDouble(ACCOUNT_PROFIT);
      int AccountTradeMode=AccountInfoInteger(ACCOUNT_TRADE_MODE);
      float AccountOrders=OrdersTotal();
      
      
      
      // Create a readable version for the trade mode
      ENUM_ACCOUNT_TRADE_MODE account_type=(ENUM_ACCOUNT_TRADE_MODE)AccountInfoInteger(ACCOUNT_TRADE_MODE);
//--- Now transform the value of  the enumeration into an understandable form
      string trade_mode;
      switch(account_type)
         {
         case  ACCOUNT_TRADE_MODE_DEMO:
            trade_mode="Demo";
            break;
         case  ACCOUNT_TRADE_MODE_CONTEST:
            trade_mode="Contest";
            break;
         default:
            trade_mode="Real";
            break;
         }
         
         
         double profit = 0;
         int i,hstTotal=OrdersHistoryTotal();
           for(i=0;i<hstTotal;i++)
          {
           if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==TRUE)
             {
               if(OrderType()>1) continue;
               if(TimeToStr(TimeCurrent(),TIME_DATE) == TimeToStr(OrderCloseTime(),TIME_DATE))
               {
                  profit += OrderProfit() + OrderSwap() + OrderCommission();
               }
             }
          }
         
      
      Comment (
               "STRIX ACCOUNT ANALYZER WITH CSV 3\n",
               "Account Server: ", AccountServer,"\n",
               "Account Currency: ", AccountCurrency,"\n",
               "Account Name: ", AccountName,"\n",
               "Account Type: ", trade_mode,"\n",
               "Account Balance: ", AccountBalance,"\n",
               "Account Equity: ", AccountEquity,"\n",
               "Account Profit: ", AccountProfit,"\n",
               "Today Profit: ", profit,"\n",
               "Account Orders: ", AccountOrders,"\n"
              );
      
      // Create A file Name        
      string mySpreadsheet="JacksData.csv";
      
      // Open the file for reading and writing, as CSV format, ASNI Mode
      int mySpreadsheetHandle=FileOpen (mySpreadsheet,FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI);
      
      // Append data to csv
      FileWrite(mySpreadsheetHandle,AccountProfit,AccountBalance,AccountEquity,trade_mode,AccountServer,profit,AccountOrders);
      
      // Close the CSV file
      
      FileClose(mySpreadsheetHandle);
      
   }
   
OrderSelect(i,SELECT_BY_POS,MODE_OPEN);


And this is what I've come up with for a separate CSV file to log all the live data of open positions:


string FileName="LiveData.csv";
int start()
  { 
       for(int i=0;i<OrdersTotal();i++)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==TRUE)   
            {  
             int handle=FileOpen(FileName,FILE_CSV|FILE_READ|FILE_WRITE,",");                 
             FileWrite(handle,OrderType(),OrderOpenPrice(),OrderOpenTime(),OrderSymbol(),OrderLots(),OrderProfit());
             FileClose(handle);
            }   
        }     
  return(0);
  } 


So as I said in the new thread that got deleted, I think I know the issue I just don't know how to fix it. What I believe the issue is, is that each time the code runs its only pulling the last trade that is opened and returning 1 line with all the info on it, so I think that I need to do is to loop it for each open position and keep a counter of all the position ID's that have already been logged before it goes over and does it all again, then for each open position it just places it on a new line. 


So thats the bit I'm stuck on as mentioned in the new thread. Just looping to get all open positions and have them on separate lines. I'm sure its just a logic thing that needs changing to allow for multiple orders to be logged but I cant for the life of me think how to loop while scanning for open orders... Maybe a nested loop?

Hopefully now you have some code examples and a description you might be able to give us a pointer in the right logic I need to get it to work

(And yes sorry for not being an experienced MQL coder I'm just getting started in MQL and learning it all step by step.)

 
Strix. #:

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.