MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal

Source code library - Expert Advisors, Indicators and Scripts

Buy algorithmic trading applications in MetaTrader Market. Its fast!
To post a new code, please log in or register

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2009.11.22 21:19
Equity Recorder

Equity Recorder - library for MetaTrader 4

| English Spanish Portuguese

votes: 5

I often see people post "equity" curves from the backtester, showing only smooth lines and ignoring any drawdown between opening and closing a trade. Since MetaTrader only plots a new datapoint in the equity plot when a trade is closed, you simply can not get realistic plots. Also the time is completely missing, the x axis shows the number of trades and does not draw a clear picture of the time you would have waited. In general the equity plots produced by the backtester always tend to look much nicer than they really are.

This is about to change now!

Attached is a library that will allow you to produce realistic plots of the total equity (the only important number) versus time. The plots are saved as offline charts, you can view and analyze them just like ordiary charts.

Put the library into the include folder, include it in your EA and on every tick you call the function recordEquity() with name, period and magic number. Thats all!

The function can also be used to create plots of manual or automated trading in real time filtered by magic number and/or comment string. Use it to monitor the individual performances of your concurrently running EAs in separate charts. When in backtesting mode the name of the chart file will always be prepended with an underscore, so it doesn't interfere with the live chart the same EA may have already produced over time.

If you want to make a plot of closed source EAs or of your manual trading then simply create an indicator with recordEquity() calls for every magic number or comment string you want to record in the start() function and attach it to any chart: 

Example code for using this library in such an indicator would look like this:

//| equity_recorder.mq4 |
//| Bernd Kreuss |
//| |
#property copyright "Bernd Kreuss"
#property link ""

#property indicator_chart_window

#include <offline_charts.mqh>

* the following will record 4 equity charts.
* attached to *any* chart (no matter which) this indi will on every tick
* calculated the profits, filter them by magic and comment and write
* them each into their own chart.
int start(){
// all trading, whole account
// produce a dayly chart the with name "all" for it
recordEquity("all", PERIOD_D1, -1);

// all manual trading (magic = 0)
// produce a dayly chart with the name "all_manual" for it
recordEquity("all_manual", PERIOD_D1, 0);

// only manual trading with a comment of "foo" in the orders.
// Note that in this example we use an additional parameter
// to filter all manual trades by their comment string.
// It will match all trades beginning with, ending with
// or containing
the word "foo".
// Produce a dayly chart with the name "fooStrat" for it
recordEquity("fooStrat", PERIOD_D1, 0, "foo");

// record the equity of the expert advisor with the magic number of 1234
// produce an H1 chart with the name "scalperEA" for it
recordEquity("scalperEA", PERIOD_H1, 1234);

Attached is an image of an example plot. It is a simple grid system and the above library was used in the EA to create this equity chart:

Example Plot

Last comments | Go to discussion (11)
MQL4 Comments
mql4_comments | 16 Feb 2010 at 15:37

Hello, I have some spike bars on the different charts. I've attached M1 sample. Could you comment this problem? It's not caused by EA (ca 3000% margin, and 0.1 Lots). Renko Idea is probably good, thank you.

equity chart problem

7bit | 22 Feb 2010 at 20:55

Hello, I have some spike bars on the different charts. I've attached M1 sample. Could you comment this problem?

Try the current version i just uploaded (version > 2010.2.x.x). This seems to happen when metatader is restarted completely and the history is not yet loaded from the server. I can not fully guarantee that it is completely fixed now but I didn't see this problem appearing here anymore.

Also the new version has a dramatic increase in performance (faster backtests, much less CPU usage) since the realized profit is not calculated on every tick anymore, only when the order history has changed.

7bit | 20 Mar 2010 at 21:29

I have moved the hosting of all my files to one central place since it is becoming more and more complex maintaining projects (and providing people with current versions of source code) with needed dependencies spread all over across the whole www.

The current version of this library can now always be found here:

jeff007 | 1 Apr 2010 at 19:33

I installed your library and it works fine with a dialy period.
It is really much better than the native Equity curve provided by metatrader and very useful.

Nevertheless I have tried to use weekly or monthly period and the chart is truncated or empty.

Maybe code does not support these period ?

Or Iam doing something wrong ?

Thanks for you help.

MQL4 Comments
mql4_comments | 13 Feb 2014 at 15:47
Great work! Is there any chance that you will alter the file to cope with the new HST file format?