Geting last Account equity from Account History

 

Hi Guys,

Can any body guid me to get the highest account equity value from account history?

As you know, by Closing each trade it will recorded an Account balance and a Account eqiuty. so now I want to get the highest amount of equity from closed trades in order to use it in my ea.

If you know any refrences for this topic or you can code this problem please share it for me.

Many Thanks

 

As far as I am aware, if all of your trades are closed, then your account balance IS your account equity. Your equity will only differ from account balance while trades are open. (Equity is account balance + unrealized profit/loss).

When you say that you "want to get the highest amount of equity from closed trades", I presume you are opening only one trade at a time and you want to find the highest account equity reached DURING that trade?

Alternatively, perhaps you are opening multiple trades at a time and you just want to know the highest equity reached over a time period (during a backtest for example)?

Either way, the answer is the same. Since the account history is only concerned with the open and close of a trade, and the highest or lowest equity might occur DURING a trade, then the account history is useless for you purposes.

You can solve the problem by having a double variable which only updates itself to the current account equity if the current account equity is higher than it. Then you update it every tick, and it acts as a high water mark.

The problem is not creating the record of the highest equity - the problem is retrieving it!

If you want to save the highest equity reached from a backtest, then you just get the EA to print the value of your equity variable it during deinit.

If you want to save the highest equity reached from each pass of an optimization then it gets more tricky, because Print() won't do anything in an optimization. During the deinit you need to save the result by either creating a global variable (which does work), or writing to .csv.

If you just want to know the highest equity reached by a trade, for use in subsequent trades, then you will need to query your new equity variable at the close of that trade (or the open of the next trade).

When using the close. since you won't get a notification of when the trade closes from your broker if you are closing it with a server executed sl or tp, then you will obviously need to run a check every tick to see if the trade is still open or has been closed (or use hidden sl and tp).

If you are running multiple trades at the same time, then you will probably want to separate them by ticket number or similar to know which trade was responsible for contributing which equity (if that is important for your system)?

 
clerin6:

As far as I am aware, if all of your trades are closed, then your account balance IS your account equity. Your equity will only differ from account balance while trades are open. (Equity is account balance + unrealized profit/loss).

When you say that you "want to get the highest amount of equity from closed trades", I presume you are opening only one trade at a time and you want to find the highest account equity reached DURING that trade?

Alternatively, perhaps you are opening multiple trades at a time and you just want to know the highest equity reached over a time period (during a backtest for example)?

Either way, the answer is the same. Since the account history is only concerned with the open and close of a trade, and the highest or lowest equity might occur DURING a trade, then the account history is useless for you purposes.

You can solve the problem by having a double variable which only updates itself to the current account equity if the current account equity is higher than it. Then you update it every tick, and it acts as a high water mark.

The problem is not creating the record of the highest equity - the problem is retrieving it!

If you want to save the highest equity reached from a backtest, then you just get the EA to print the value of your equity variable it during deinit.

If you want to save the highest equity reached from each pass of an optimization then it gets more tricky, because Print() won't do anything in an optimization. During the deinit you need to save the result by either creating a global variable (which does work), or writing to .csv.

If you just want to know the highest equity reached by a trade, for use in subsequent trades, then you will need to query your new equity variable at the close of that trade (or the open of the next trade).

When using the close. since you won't get a notification of when the trade closes from your broker if you are closing it with a server executed sl or tp, then you will obviously need to run a check every tick to see if the trade is still open or has been closed (or use hidden sl and tp).

If you are running multiple trades at the same time, then you will probably want to separate them by ticket number or similar to know which trade was responsible for contributing which equity (if that is important for your system)?



Many Thanks for your Answer. That is a insightful and Complete Answer. You are very clever and you predict all options.

As you say i am opening multiple trades at a time and I just want to know the highest equity reached over last closed trades during a backtest. So in such situation if there are some trades in the opposite direction and only profitable trades are allowed to close, so the Account balance is more than Account equity. Therefore it is for me very important to record the highest account equity that is more than every other account equity of the past.

For this problem you got the answer .

I can solve the problem by having a double variable which only updates itself to the current account equity if the current account equity is higher than it. Then it updates every tick, and it acts as a high water mark.

The problem is not creating the record of the highest equity - the problem is retrieving it!

So i do not now how to use deinit in order to solve this problem.

I have already wrote the below code but it does not work.

double accountequity[];
double last;

for(i=0;i<OrdersHistoryTotal();i++)

{
   OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol() )
    { 
       if(OrderType()==OP_BUY || OrderType()==OP_SELL) accountequity[i]=AccountEquity();
       if (accountequity[i]>accountequity[i+1]) last=accountequity[i];
       break; 
    }
}
 

OK, thanks. I think you could probably just enumerate a variable with the list of all the other variables you are using in the EA, at the start:

double highestequity;

And then place this next piece of code where it can run every tick.

if ( AccountEquity() > highestequity )

{ highestequity = AccountEquity(); }

Then place this next piece of code in your int deinit() function:

Print("Highest equity in backtest = "+highestequity");

The deinit() function runs whenever an EA is deactivated. Therefore, any code you place in this function is run whenever the EA is deactivated. When a backtest is complete, it automatically runs the deinit() function - so the above code will print the value for your variable called "highestequity" to the strategy tester journal.

If you are running optimizations (as opposed to single backtests), then simply using Print() (or even Alert(), Comment() or SendMail()) will not leave any trace in the strategy tester journal. Therefore, you should either transfer the value for "highest equity" to a global variable or write it to .csv, during deinit(). Remember that denint() is run at the end of each pass in the optimization, not just at the end of all the optimizations.

Learning how to write to .csv can take a while, so just use the global variable if you want to be quick - but remember to call each global variable for each pass of the optimization something different, otherwise you will just overwrite the answer from the previous pass! If you are just running a handful of backtests, then it is probably easier to stick with just using Print() then run each pass manually as a different backtest and record the answer from the journal manually using pen and paper, because it will be quicker than learning how to write to file or handle to global variables, unless you are doing more than about 100 passes ...

 

I've asked the same question as you in a slightly different manner not too long ago. Within that post I kinda brain-stormed how someone could -possibly- code the highest-account-equity using history.....link here. Currently I use the method written by clerin6 above.

if(AccountEquity()>t_BankHigh){t_BankHigh=AccountEquity();}
You can write to file or print depending on your need. Because I'm interested in getting the same answer as you, I'll attempt to code this solution and provide code here.
 

Getting a unique identifier for each global variable (so that new optimizations do not overwrite previous ones) is tricky, because you cannot save data BETWEEN passes of an optimization simply using INTERNAL variables. Therefore, subsequent passes will not know how many passes have gone before, and will not know what pass number to assign to the global variable created for that pass. Using a randomly generated number is useless, because you do not know which pass goes with which number. Using TimeLocal() to create a sequential unique identifier won't work, because that is modeled during an optimization and seems to remain the same throughout the optimization. The only way I figured was to create a separate global variable which simply counts the number of passes in an optimization. You must make sure that that global variable is deleted manually before starting the optimization (if it is left over from a previous optimization). You cannot simply delete that variable in the init() of the EA, because it would get deleted at the start of every pass and so would not be able to persist and pass information BETWEEN passes of the optimization. Then the global variable which carries the highest equity information from each pass is given a unique identifier from the global variable which counts the number of passes, and THAT unique identifier correlates which the pass number so that you can marry the two sets of data (the highest equity and the optimization results) later. There are two problems with this approach however: firstly it creates a large number of global variables, and secondly you need a script to convert that info into .csv and export it anyway (if there are a large number of passes which would otherwise make it tedious to copy the info down manually using pen and paper). The number of global variables on a terminal is not a major problem (I have managed to create several hundred thousand at once before in crashed), but the script to format and export the data is the most difficult thing. Remember that you cannot manually copy and paste multiple global variable data in the same way that you can for optimization pass data. Simply using Print() or Alert() to transfer the data to the journal is pointless because you cannot select multiple lines of the journal (or experts tab) in order to copy them. Even if you merge the equity values from multiple pass results into a string, and print multiple values per LINE of the journal, you are still limited to a small number of characters per line (64 characters I think) and you then have to detangle the data later. The only way round this, other than writing to file, is to SendMail().

But, if you are going to go to the trouble of writing a script to format the global variable equity data from multiple passes into an easily useable .csv format (which is basically needed for retrieving the global variable data ANYWAY), then you might as well just write to file in the deinit in the first place and skip the whole global variable bit. Then, you only need the one global variable which counts the number of passes so that you can write the pass number next to the highest equity value (although even this is not strictly needed if you separate the equity value for each subsequent pass with a separate line when writing to .csv). Writing to file is the most tricky bit of the whole exercise, but the guides in the "book" and "documentation" are great, so its not a major problem. Remember that you can use "\n" to add a new line in a string.

If you have excel then you can use the import data option to get the .csv data into an easily useable format. If you select copy the whole optimization data (by right clicking on the results at the end of the optimization and clicking "copy all") you can paste that into a TEXT document such as notepad. Then go to excel and use the option Data/Import External Data/Import Data and select the text file you just saved the optimization results to. This allows you to DELIMIT the data. You can get rid of the annoying "=" sign which connects external variable values with their name in the optimization results and otherwise prevents you from easily using them in excel formulas. You can remove the "=" by ticking the "Other" box in the excel import data menu, and then typing "=" into the box next to it. Once you have that optimization data, you can find the file you created by using write file to save the equity data, then import that using the same method and line the two sets of data up side my side. The most tricky bit is making sure the way you write to .csv is easily delimted by the import option in excel - but that is relatively straightforward after a few trial and errors.

The most exciting thing about this method is that you can use it to extract *ANY* information from any number of optimizations. For example, I use it to write the profit/loss of EVERY closed trade in EVERY pass of the optimization to file (hence hundreds of thousands of global variables using the global variable method) - then import that data to excel and draw graphs or do any of the hundreds of statistical analyses in excel to compare optimization results to each other. (That way you get not just the end profit/loss value of the entire pass, but also the coefficient, sharpe ratio - or whatever else you want to calculate).

 
clerin6:

OK, thanks. I think you could probably just enumerate a variable with the list of all the other variables you are using in the EA, at the start:

double highestequity;

And then place this next piece of code where it can run every tick.

if ( AccountEquity() > highestequity )

{ highestequity = AccountEquity(); }

Then place this next piece of code in your int deinit() function:

Print("Highest equity in backtest = "+highestequity");

The deinit() function runs whenever an EA is deactivated. Therefore, any code you place in this function is run whenever the EA is deactivated. When a backtest is complete, it automatically runs the deinit() function - so the above code will print the value for your variable called "highestequity" to the strategy tester journal.

If you are running optimizations (as opposed to single backtests), then simply using Print() (or even Alert(), Comment() or SendMail()) will not leave any trace in the strategy tester journal. Therefore, you should either transfer the value for "highest equity" to a global variable or write it to .csv, during deinit(). Remember that denint() is run at the end of each pass in the optimization, not just at the end of all the optimizations.

Learning how to write to .csv can take a while, so just use the global variable if you want to be quick - but remember to call each global variable for each pass of the optimization something different, otherwise you will just overwrite the answer from the previous pass! If you are just running a handful of backtests, then it is probably easier to stick with just using Print() then run each pass manually as a different backtest and record the answer from the journal manually using pen and paper, because it will be quicker than learning how to write to file or handle to global variables, unless you are doing more than about 100 passes ...

Many thanks for your Advise, i need this code again and i search for this topic. I see today that i forgotten to thank you last time.

Many many thanks.

respect!

 
ubzen:

I've asked the same question as you in a slightly different manner not too long ago. Within that post I kinda brain-stormed how someone could -possibly- code the highest-account-equity using history.....link here. Currently I use the method written by clerin6 above.

You can write to file or print depending on your need. Because I'm interested in getting the same answer as you, I'll attempt to code this solution and provide code here.


Many thanks for your Advise, i need this code again and i search for this topic. I see today that i forgotten to thank you last time.

Many many thanks.

respect!

Reason: