Watch how to download trading robots for free
Find us on Telegram!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

Object Emulator - library for MetaTrader 5

Views:
6248
Rating:
(38)
Published:
2013.03.25 10:45
Updated:
2016.09.26 09:20
\MQL5\Indicators\ \MQL5\Scripts\ \MQL5\Include\
objectemul.mqh (34.25 KB) view
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

Description

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.

Note:

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.

Setting

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.

Use

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()
  {
   ZigZag=iCustom(_Symbol,_Period,"Examples\\ZigZag",5,5,3);
   MathSrand(GetTickCount());
   return(0);
  }

void OnTick()
  {
   datetime time[];
   CopyTime(_Symbol,_Period,0,15,time);
   if(lastbar==time[0]) return;
   lastbar=time[0];

   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]))
        {
         trianglePrice[triangleIndex]=zgbuffer[x];
         triangleTime[triangleIndex]=time[x];
         triangleIndex++;
         break;
        }
      else if(zgbuffer[x]>0) break;
     }

   if(triangleIndex<3) return;

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

//////////////////////////
// Object Emulator

// Standard Object*() analogues
   if(_objs.ObjctCreate(0,objname,OBJ_TRIANGLE,0,
      triangleTime[0],trianglePrice[0],triangleTime[1],trianglePrice[1]))
     {
      _objs.ObjctSetInteger(0,objname,OBJPROP_TIME,2,triangleTime[2]);
      _objs.ObjctSetDouble(0,objname,OBJPROP_PRICE,2,trianglePrice[2]);
      _objs.ObjctSetInteger(0,objname,OBJPROP_COLOR,(MathRand()%255)*(MathRand()%255)*(MathRand()%255));

      // 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
//////////////////////////

   trianglePrice[0]=trianglePrice[1];
   triangleTime[0]=triangleTime[1];
   trianglePrice[1]=trianglePrice[2];
   triangleTime[1]=triangleTime[2];
   triangleIndex=2;
  }

Variables

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

Functions

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 Ltd.
Original code: https://www.mql5.com/ru/code/1583

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