Efficiënt use of the HistoryDealsTotal() function

 

Hi everyone,

I'm fairly new to MT5 programming, so if someone has a more efficient way to do this i'd be happy with the advice 

In my EA need to go through the History of deals,
and based on the history my EA can make furhter decisions.

First thing i do to do this is:

for(i = 0; i < HistoryDealsTotal(); i++)
   {

ticket= HistoryDealGetTicket(i);
magic= HistoryDealGetInteger(ticket,DEAL_MAGIC);

then some calculations.

-----

This all works great at first in the tester. but as my deals go byond 500-800, the stratigy tester system starts slowing down because it has to interpret all the deals again and again every minute. 

How can i optimise this?

use some array functions to store details of the deals i make? then delete details when they served their use?
To select only part of the history, opening error to making wrong calculations?

Advice appreciated. 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties - Documentation on MQL5
 
Financialarts:

Hi everyone,

I'm fairly new to MT5 programming, so if someone has a more efficient way to do this i'd be happy with the advice 

In my EA need to go through the History of deals,
and based on the history my EA can make furhter decisions.

First thing i do to do this is:

for(i = 0; i < HistoryDealsTotal(); i++)
   {

ticket= HistoryDealGetTicket(i);
magic= HistoryDealGetInteger(ticket,DEAL_MAGIC);s

then some calculations.

-----

This all works great at first in the tester. but as my deals go byond 500-800, the stratigy tester system starts slowing down because it has to interpret all the deals again and again every minute. 

How can i optimise this?

use some array functions to store details of the deals i make? then delete details when they served their use?
To select only part of the history, opening error to making wrong calculations?

Advice appreciated. 

Hi financialarts,

You're not the only one who doing this - I don't know why some people have to iterate history deals, it will even bigger in scalping EA, and slow down the EA itself. IMHO, its history, so forget about it.

But then again I can only see partial of your code, so ... before iterate

1. Check if value of HistoryDealsTotal() has change, something like - if (last_total_deal  != HistoryDealsTotal())

2. Before iterate use HistorySelect() . You can also use HistorySelectByPosition(), and HistoryDealsSelect() may also help - click those.

The idea is to iterate the most recent deals only, not entire deals and not doing it in every minute. Just some ideas, without seeing the whole codes. 

:D 

 
onewithzachy:

Hi financialarts,

You're not the only one who doing this - I don't know why some people have to iterate history deals, it will even bigger in scalping EA, and slow down the EA itself. IMHO, its history, so forget about it.

But then again I can only see partial of your code, so ... before iterate

1. Check if value of HistoryDealsTotal() has change, something like - if (last_total_deal  != HistoryDealsTotal())

2. Before iterate use HistorySelect() . You can also use HistorySelectByPosition(), and HistoryDealsSelect() may also help - click those.

The idea is to iterate the most recent deals only, not entire deals and not doing it in every minute. Just some ideas, without seeing the whole codes. 

:D 


I agree with onewithzachy, but it also sounds like you want to be using the OnTrade() built-in, and have your history work inside that.


IF not, populate variables with the deal history (win/loss) and work with that.


Calling HistoryDealsTotal() still has a cost, so if you're trying to do something when a deal occurs, do it inside the OnTrade built-in

 

Thank you for replying :),

 I don't think i've found the answer yet to the problem. I'll give some more detail on my EA,

I work with sessions, meaning, every session I start i want to end at a certain profit level or break even, etc,
each session i start, has its own new magic number, so I can trace through the history of deals with the same magic number to open new positions, or to close the session.
I do this so i can trade muktiple sessions on 1 pair:
example: when 1 session says go short 1 lot, other session says go long 1 lot . result = Not trading . but internally i still keep track of the outcome of the deals,

so at certain points in the future ( lets keep it simple) , market moves up 100 pips, at 50 pips I close session 1(read opening 1 lot long), resulting in 50 pips loss, at 100 pips i close session 2( open 1 lot short) result is 50 net worth pips.

 EA itself is more complicated but you get the idea.  

In order to know where to open positions i go through the History data, first

HistorySelect(0,TimeCurrent()); 

then: for(i = 0; i < HistoryDealsTotal(); i++)
{ + calculations + maybe new deal

So this rules out the OnTrade function, cause i need to calculate before a transaction occurs.
* I do not use SL and TP

I do like this:  Check if value of HistoryDealsTotal() has change, something like - if (last_total_deal != HistoryDealsTotal()) 
In the situation I would write all my Deal details in arrays, clearing out the details when they arent needed. Maybe even split pairs into sepperate arrays.  

Documentation on MQL5: Trade Functions / HistorySelect
Documentation on MQL5: Trade Functions / HistorySelect
  • www.mql5.com
Trade Functions / HistorySelect - Documentation on MQL5
 
Financialarts:

...

In the situation I would write all my Deal details in arrays, clearing out the details when they arent needed. Maybe even split pairs into sepperate arrays.  

Hi financialarts,

Your last sentence there caught me, is your EA: a single EA trade multiple pairs or just ordinary single EA trade single pair, coz in this case I definitely choose single EA trade single pair.

:D 

 


@ onewithzachy

You can't use multiple ea's in the ATC 2012 :) 

 
Financialarts:


@ onewithzachy

You can't use multiple ea's in the ATC 2012 :) 

Hi financialarts,

Ah yes, Championship Rules.

Well then, if it is that multi currency EA can only run on one chart, then that's gonna burden the EA a lot - and I have no more ideas then.

[Edit : What is it that you collect from history deal anyway ?, Is it Open Prices, Lot Sizes ?, Can you just sump those up and use the final result in the end ?] 

:D 

The Rules of Automated Trading Championship 2012
  • championship.mql5.com
The Rules of Automated Trading Championship 2012
 

First I get ticket number,

Then I ask of each ticket
- Magic number, volume, comment

- do some calculations, store findings

I do this for each session i have.

I've got some solutions in my mind and am going to try this today or tomorrow 

 
Financialarts:

First I get ticket number,

Then I ask of each ticket
- Magic number, volume, comment

- do some calculations, store findings

I do this for each session i have.

I've got some solutions in my mind and am going to try this today or tomorrow 

Hi financialarts,

Well, like I said before I have no idea what your coding and ... history is history, it's value will never change. So, continue calculation from the last calculation, hence the use of HistorySelect() and if value of HistoryDealsTotal() has change.

You get the idea.

 

Documentation on MQL5: Trade Functions / HistorySelect
Documentation on MQL5: Trade Functions / HistorySelect
  • www.mql5.com
Trade Functions / HistorySelect - Documentation on MQL5