Download MetaTrader 5
To add comments, please log in or register
Do you know that MQL5 is an object-oriented language?
PaulTheTrader
41
PaulTheTrader 2014.03.28 22:58 

Hello

There are 2 variants of the ObjectCreate () function. One of them takes as the first parameter the Chart_ID - defining the target open chart that the graphic will be drawn on.

However, I've not been able to successfully use this function from an EA either in the strategy tester, or applied to any given chart to draw a graphic onto another open chart. Has anyone experience in this that can comment, please?

Thanks!

Keith Watford
Moderator
8007
Keith Watford 2014.03.29 01:32  

Hi, Try this, works as a script

   long id=ChartID();
   long idnext = ChartNext(id);
   string idname= ChartSymbol(id);
   string idnamenext= ChartSymbol(idnext);
   Print("Current chart is ",idname,". Next chart is ",idnamenext);
   
   double chigh = iHigh(idnamenext,PERIOD_H1,1);
   datetime ctime = iTime(idnamenext,PERIOD_H1,1);
   ObjectCreate(idnext,"Obj",OBJ_ARROW_THUMB_UP,0,ctime,chigh);
   ObjectSetInteger(idnext,"Obj",OBJPROP_WIDTH,5);
   ObjectSetInteger(idnext,"Obj",OBJPROP_ANCHOR,ANCHOR_BOTTOM);
   
PaulTheTrader
41
PaulTheTrader 2014.03.29 05:56  

Thanks GumRai.

It works as an EA if I drag it onto one of the charts. The object is then drawn on one of the other charts. Great!

However, it doesn't work if I run it from the Tester, even if I hard-code the chart IDs.

Would you know how to get it to work from the tester?

Thanks

PaulTheTrader
41
PaulTheTrader 2014.03.29 06:12  
…actually, after further testing, I have intermittent results. Attaching the EA to some charts will cause the object to be drawn on another (with hard coded chart ID), and attaching to other charts - not.
Keith Watford
Moderator
8007
Keith Watford 2014.03.29 06:18  
2ndTime:

Thanks GumRai.

It works as an EA if I drag it onto one of the charts. The object is then drawn on one of the other charts. Great!

However, it doesn't work if I run it from the Tester, even if I hard-code the chart IDs.

Would you know how to get it to work from the tester?

Thanks


Sorry, I've no idea.

I can't envisage any time that I would want the tester to draw an object on another chart, so I haven't looked into it.

Keith Watford
Moderator
8007
Keith Watford 2014.03.29 06:19  
2ndTime:
…actually, after further testing, I have intermittent results. Attaching the EA to some charts will cause the object to be drawn on another (with hard coded chart ID), and attaching to other charts - not.


I don't understand that, can you show your code?

Have you checked the objects list to see if the object is being drawn somewhere unexpected?

PaulTheTrader
41
PaulTheTrader 2014.03.29 08:47  

Code attached.

I have checked the object list to be sure that it is not being drawn somewhere unexpected.

From the code you will see that I have identified one of the chart numbers, and hardcoded it in. The EA works when applied to one of my open charts, but not another. Strange.

And of course, it doesn't work from the strategy tester - which is what I'm ultimately trying to do.

//+------------------------------------------------------------------+
//|                                                    FindChart.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+


long id =ChartID();
long idnext=ChartNext(id);//==130404533134121823
string idname=ChartSymbol(id);
string idnamenext = ChartSymbol(idnext);

int OnInit()
  {
//---
Alert("idnamenext: "+idnamenext);
double chigh = iHigh(130404533134121823,PERIOD_H1,1);
datetime ctime = iTime(130404533134121823,PERIOD_H1,1);
ObjectCreate(idnext,"Obj",OBJ_ARROW_THUMB_UP,0,ctime,chigh);
ObjectSetInteger(idnext,"Obj",OBJPROP_WIDTH,5);
ObjectSetInteger(idnext,"Obj",OBJPROP_ANCHOR,ANCHOR_BOTTOM);
  

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   


  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
28672
Alain Verleyen 2014.03.29 09:29  
2ndTime:

Code attached.

I have checked the object list to be sure that it is not being drawn somewhere unexpected.

From the code you will see that I have identified one of the chart numbers, and hardcoded it in. The EA works when applied to one of my open charts, but not another. Strange.

And of course, it doesn't work from the strategy tester - which is what I'm ultimately trying to do.

  • It's a bad idea to hardcode the ID in your code.
  • As I explain you in your other topic, you have to check for an error when appropriate. You started by checking error everywhere, and now you don't check it at all.
  • You also have to check for error for ObjectCreate().
  • If it doesn't work from the Strategy Tester, it's either a limitation or a bug, I suggest you to write to Service Desk of Mteaquotes to ask them.
Keith Watford
Moderator
8007
Keith Watford 2014.03.29 10:20  
2ndTime:

Code attached.

I have checked the object list to be sure that it is not being drawn somewhere unexpected.

From the code you will see that I have identified one of the chart numbers, and hardcoded it in. The EA works when applied to one of my open charts, but not another. Strange.

And of course, it doesn't work from the strategy tester - which is what I'm ultimately trying to do.


It's not strange at all.

If you are going to hardcode the chart ID, you have to hardcode everything

long id =ChartID();
long idnext=ChartNext(id);//==130404533134121823
string idname=ChartSymbol(id);
string idnamenext = ChartSymbol(idnext);

int OnInit()
  {
//---
Alert("idnamenext: "+idnamenext);
double chigh = iHigh(130404533134121823,PERIOD_H1,1);
datetime ctime = iTime(130404533134121823,PERIOD_H1,1);
ObjectCreate(idnext,"Obj",OBJ_ARROW_THUMB_UP,0,ctime,chigh);
ObjectSetInteger(idnext,"Obj",OBJPROP_WIDTH,5);
ObjectSetInteger(idnext,"Obj",OBJPROP_ANCHOR,ANCHOR_BOTTOM);

idnext will be different for every chart that you apply it to. So if you want to hardcode it

long id =ChartID();
long idnext=ChartNext(id);//==130404533134121823
string idname=ChartSymbol(id);
string idnamenext = ChartSymbol(idnext);

int OnInit()
  {
//---
Alert("idnamenext: "+idnamenext);
double chigh = iHigh(130404533134121823,PERIOD_H1,1);
datetime ctime = iTime(130404533134121823,PERIOD_H1,1);
ObjectCreate(130404533134121823,"Obj",OBJ_ARROW_THUMB_UP,0,ctime,chigh);
ObjectSetInteger(130404533134121823,"Obj",OBJPROP_WIDTH,5);
ObjectSetInteger(130404533134121823,"Obj",OBJPROP_ANCHOR,ANCHOR_BOTTOM);

Otherwise, you are placing an object on a different chart than you expect and the parameters are likely to place it Off the chart

whroeder1
13644
whroeder1 2014.03.29 12:54  
These shouldn't even compile.
long id =ChartID();
long idnext=ChartNext(id);//==130404533134121823
string idname=ChartSymbol(id);
string idnamenext = ChartSymbol(idnext);
Initialization of Variables - MQL4 Documentation: "Global and static variables can be initialized only by a constant of the corresponding type or a constant expression."
long id;// =ChartID();                             
long idnext;//=ChartNext(id);//==130404533134121823
string idname;//=ChartSymbol(id);                  
string idnamenext;// = ChartSymbol(idnext);        
Oninit(){
   id =ChartID();
   idnext=ChartNext(id);//==130404533134121823
   idname=ChartSymbol(id);
   idnamenext = ChartSymbol(idnext);
Try this.
PaulTheTrader
41
PaulTheTrader 2014.03.29 17:21  
Thanks GumRai.
/
To add comments, please log in or register