Download MetaTrader 5

Watch how to download trading robots for free

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

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


Object Emulator - library for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

votes: 35
2013.03.25 10:45
2016.09.26 09:20
objectemul.mqh (34.25 KB)view


MetaTrader 4 allows using the objects in the Expert Advisor when testing. The chart with the objects will be opened at the end of testing. Arrows makes observation where the Expert Advisor can perform the operation convenient, but this condition didn't allow it. There are a lot of examples of usage.

As compared to MetaTrader 4 version, we cannot use the objects in testing in MetaTrader 5. This situation doesn't suit me! Well, let's work it out.


Now the Expert Advisors are creating without using the objects in MetaTrader 5. Object Emulator allows getting data from the object, but I do not recommend to do that, as the testing process will be slowed at times and there are some differences from the official functions.

Use Object Emulator only to display the objects in the chart after testing.


Add into the tester.tpl tester template the ObjectEmul_indicator indicator.

For those who don't know how to do it:

  • Open chart on any currency pair.
  • Throw to the ObjectEmul_indicator indicator.
  • Right clicking on the chart.
  • Templates => Save a template by specifying the "tester" file name.


The "Open chart" button on tab "Setting" was removed in MetaTrader 5 Strategy Tester. If the Expert Advisor in the test didn't perform trading operation, the chart won't appear automatically. You will have to switch to "Back-testing", right click and select "Open chart".

Before using throw the ObjectEmul_UnitTest_script on the empty chart and read the log in the tab "Experts" to know the differences from the official functions to work with objects.

Add into the main file of the Expert Advisor:

#include <ObjectEmul.mqh>
CObjectEmul _objs;

And change Object to _objs.Objct for standard functions.

To run Help by the object functions in the editor you should just add the letter "e" in "Object" and press F1.

The example

EURUSD, H1, MetaQuotes Software Corp., MetaTrader 5, Demo

//|    The example of use of Object Emulator                         |
#include <ObjectEmul.mqh>
CObjectEmul _objs;

int ZigZag;
datetime lastbar=0;

double trianglePrice[3];
datetime triangleTime[3];
int triangleIndex=0;

int OnInit()

void OnTick()
   datetime time[];
   if(lastbar==time[0]) return;

   double zgbuffer[];
   if(CopyBuffer(ZigZag,0,0,15,zgbuffer)==0) {Print("CopyBuffer(ZigZag) Error"); return;}

   bool first=true;
   for(int x=0,count=ArraySize(zgbuffer);x<count && triangleIndex<3;x++)
      // Here is something wrong, but I like the result :)
      if(zgbuffer[x]>0 && first) {first=false; x+=3;}
      else if(zgbuffer[x]>0 && (triangleIndex==0 || trianglePrice[triangleIndex-1]!=zgbuffer[x]))
      else if(zgbuffer[x]>0) break;

   if(triangleIndex<3) return;

   string objname="ObjectEmul_Demo_"+(string)(int)lastbar;

// Object Emulator

// Standard Object*() analogues

      // The example of use of indexes
      int index=_objs.ObjctFindIndex(0,objname);
      if(index>-1) _objs.ObjctSetIntegerIndex(index,OBJPROP_FILL,0,true);
   else Print("ObjctCreate Error");

// Object Emulation



  • string FileName - File name. It is default, but you can change it.
  • bool SaveAuto = false - Should we save the objects into a file when deleting the class object? Default "true" value is only for the simple test.


All graphical Object*() objects are realized, but have the Objct*() names. But there are no analogues of TextSetFont(), TextOut() and TextGetSize().

  • int Count() - The number of objects.
  • void ReleaseAll() - Deletes all objects (faster than ObjctDeleteAll).
  • bool LoadFile(string fname = "", bool print_error = false) - Download the objects from the file.
  • bool SaveFile(string fname = "", bool print_error = false) - Save the objects into a file.
  • void DrawAll() - Draws all objects in the chart.

There are also Objct*Index() functions to speed up the work:

  • int ObjctFindIndex(long chart_id, string name) - in comparison with ObjctFind() returns the serial number of the object.

To avoid ambiguity of overloading, I had to select separate functions for indexes. These functions do not call ObjctFindIndex() for search and the objects must be controlled for them to be used!

ObjctSetInteger(0,"objname",OBJPROP_STYLE,STYLE_DASH) == ObjctSetIntegerIndex(ObjctFindIndex(0,"objname"),OBJPROP_STYLE,STYLE_DASH);

The LoadFile() function uses these functions, but the Count() function shows the number of all objects.

Certainly there are things that should be worked out in the implementation, but I need to find any decision. Of course, I will try to update this library and hoping for the Community help.

Translated from Russian by MetaQuotes Software Corp.
Original code:

Easy Trend Visualizer Easy Trend Visualizer

Simple trends visualizer

Donchian Channel Donchian Channel

Donchian channel is a technical indicator developed by Richard Davoud Donchian.

Average Volumes (Volumes Forecasting) Average Volumes (Volumes Forecasting)

The indicator displays in a separate window not only volumes, but their average value on history. The average value of volumes is used for drawing the forecast of volumes value for the current and the following days

CCIArrows CCIArrows

The semaphore signal indicator fixing moments of breakthrough of the zero level by the iCCI technical indicator and displaying these moments by color points