Automatically log all account history to .csv file - page 2

 
Strix. #: 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: And this is what I've come up with for a separate CSV file to log all the live data of open positions:

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.)

I'm somewhat confused. I was under the original impression you were looking for a solution for MetaTrader 5, and even provided a reference link for the MetaTrader 5 documentation about exporting the trade history data, about which you did not comment.

However, your code is clearly centred around MetaTrade 4 and the MQL4 trade functions, and not MQL5 trade functionality.

Can you please confirm, which of these two platforms you are truely targeting?

 
Keith Watford #:

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.

👍
 
Fernando Carreiro #:

I'm somewhat confused. I was under the original impression you were looking for a solution for MetaTrader 5, and even provided a reference link for the MetaTrader 5 documentation about exporting the trade history data, about which you did not comment.

However, your code is clearly centred around MetaTrade 4 and the MQL4 trade functions, and not MQL5 trade functionality.

Can you please confirm, which of these two platforms you are truely targeting?

MQL4, thats probably why your referance link was of no use to me since it was for a completely different terminal...

 
Strix. #: MQL4, thats probably why your referance link was of no use to me since it was for a completely different terminal...

Then don't you think you should have said something back then to clarify the situation, instead of dragging the issue for more than a week?

Our willingness to help you, also depends on our level of frustration with your lack of feedback on our input, and if we feel we have been wasting our time, we move on by ignoring you. That may sound harsh, but it is the reality of things.

 
Fernando Carreiro #:

Then don't you think you should have said something back then to clarify the situation, instead of dragging the issue for more than a week?

Our willingness to help you, also depends on our level of frustration with your lack of feedback on our input, and if we feel we have been wasting our time, we move on by ignoring you. That may sound harsh, but it is the reality of things.

Sorry but I am new here, never used a forum before and just wanted some help, sent in all the code and what I think my solution is but I cant figure it out. 


If you don't want to help me, that's fine, but please stop commenting on this thread as its just making it harder for the people that do want to actually help me to track the progress.

I'll be sure to reference all the info next time I'm stuck, but I'm still stuck on this issue that I know is simple to fix I just need some HELP from someone with more knowledge in MQL4 than me...

 
Strix. #: Sorry but I am new here, never used a forum before and just wanted some help, sent in all the code and what I think my solution is but I cant figure it out. If you don't want to help me, that's fine, but please stop commenting on this thread as its just making it harder for the people that do want to actually help me to track the progress. I'll be sure to reference all the info next time I'm stuck, but I'm still stuck on this issue that I know is simple to fix I just need some HELP from someone with more knowledge in MQL4 than me...

I will help then, but not the way you think. The problem is that you are trying to code things that are at the moment much too complex for your current level of knowledge about MQL. So lets start at the begging and totally ignore all the CSV stuff.

Lets start with event handlers — OnTick() vs start().

Both these event handlers are the same thing. "OnTick()" is the modern style called MQL4+ because it is compatible with MQL5 and it is only used in Expert Advisors (EAs), while "start()" is the old style of MQL4 that code used indiscriminately in Scripts, EAs and Indicators.

You need to pick one and not both. My advice is to use the modern style OnTick() of MQL4+ and not the old style.

Now what does this OnTick event handler do?

The terminal will call this event handler on every new tick that arrives. Can you imagine what will happen to your CSV code if it is repeated on every tick? You will be writing the file over and over again several times a second.

So, the first thing to decide is when do your want your CSV file to be generated?

  • Is it only once, when you need it?
    Then maybe it should be a Script that runs only once and the exits. Such a Script will use the "OnStart" event handler and not "OnTick".
  • Is it perhaps on every new bar?
    Then you can use an Expert Advisor's OnTick, but you will have to first detect when a new bar is formed.
  • Is it perhaps only when a new trade is placed or closed?
    Then besides using OnTick, you first have to detect when the trade conditions change.
  • etc.

So, in order to help you build your code, lets do it from the beginning and step by step.

Tell us, WHEN do you want your CSV file to be output or updated? Explain in as much detail as possible.


 
Fernando Carreiro #:

I will help then, but not the way you think. The problem is that you are trying to code things that are at the moment much too complex for your current level of knowledge about MQL. So lets start at the begging and totally ignore all the CSV stuff.

Lets start with event handlers — OnTick() vs start().

Both these event handlers are the same thing. "OnTick()" is the modern style called MQL4+ because it is compatible with MQL5 and it is only used in Expert Advisors (EAs), while "start()" is the old style of MQL4 that code used indiscriminately in Scripts, EAs and Indicators.

You need to pick one and not both. My advice is to use the modern style OnTick() of MQL4+ and not the old style.

Now what does this OnTick event handler do?

The terminal will call this event handler on every new tick that arrives. Can you imagine what will happen to your CSV code if it is repeated on every tick? You will be writing the file over and over again several times a second.

So, the first thing to decide is when do your want your CSV file to be generated?

  • Is it only once, when you need it?
    Then maybe it should be a Script that runs only once and the exits. Such a Script will use the "OnStart" event handler and not "OnTick".
  • Is it perhaps on every new bar?
    Then you can use an Expert Advisor's OnTick, but you will have to first detect when a new bar is formed.
  • Is it perhaps only when a new trade is placed or closed?
    Then besides using OnTick, you first have to detect when the trade conditions change.
  • etc.

So, in order to help you build your code, lets do it from the beginning and step by step.

Tell us, WHEN do you want your CSV file to be output or updated? Explain in as much detail as possible.


Hi Fernando, sorry for the late reply.

I have been using OnTick on the actual EA but I have been using start for my test file to try get it working. The whole EA will be ran on the same handler of course. I need it to be updated by the tick as I need the CSV to be updated live every tick with the relevant info. I am trying to build a live dashboard, which is all working fine right now. It shows live data for my account and displays it on my website in realtime.

Its just the open trades I need to get working now. I need it to look at my open trades and output the pair, size, time and current PNL into a CSV file that updates every tick or second (whichever really since the api refresh rate is 1 second anyway)

Here is my dashboard so far, as you can see the 5 circles at the top are working just fine and update in real time from the REST API that I have made to send the data, but I can't seem to log the data for my open positions which is really just what I'm trying to figure out now.
https://www.mofxdashboard.com/

Thanks for the help, let me know if you want any more info or more of my code... :)

 
Im trying to do the same thing but for MT5 that would help a lot.

Did you found solution for your problem?
 
NathanYsp #:
Im trying to do the same thing but for MT5 that would help a lot.

Did you found solution for your problem?

This is the MT4 section.

Fernando provided some links early in the thread concerning MQL5. Have you checked them?

Reason: