Download MetaTrader 5

Can't Move CChartObjectTrend

To add comments, please log in or register
Ask your broker to open the official channel. Thousands of traders will read the news!
Daniel Cherry
951
Daniel Cherry 2015.02.21 10:30 

Hi,

I am trying move a  CChartObjectTrend object on the CHARTEVENT_OBJECT_DRAG event, but I am get the error 4202 object does not exist.

Interestingly, if you click the created trend line object after the error message, the code executes fine with no error message.

Please can anyone tell me what I am doing wrong? 

Kind regards

Dan.

#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

#include <ChartObjects\ChartObjectsLines.mqh>
#include <stdlib.mqh>

// global variable for the trend object
CChartObjectTrend trendLine;

int OnInit()
{

   // set co-ordinates for the trendline
   datetime x1 = TimeCurrent() - (PeriodSeconds() * 50);
   datetime x2 = TimeCurrent();
   double y = iOpen(NULL, 0, 0);
   
   // create the trendline and set some properties.
   trendLine.Create(0, "testLine", 0, x1, y, x2, y);
   trendLine.Selectable(true);
   trendLine.RayRight(false);
   

   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   if (sparam == trendLine.Name())
   {
      // on Drag, want to re-set co-ordinates of trendline.
      if (id == CHARTEVENT_OBJECT_DRAG)
      {
   
         // get the new co-ordinates
         datetime newX = TimeCurrent() - (PeriodSeconds() * 50);
         double newY = iOpen(NULL, 0, 50);
   
         // lets just check the object exists
         if (ObjectFind(ChartID(),trendLine.Name()) > -1)
         {
            // now move the line.
            if (!ObjectMove(ChartID(),trendLine.Name(),0,newX,newY))
            {
               int error = GetLastError();
               Print("ERROR: ", error, " ", ErrorDescription(error));                        
            }
            else
            {
               Print("Object Moved");
            }
         }
         else
            Print("Object ", trendLine.Name(), " does NOT exists!");         

      }
   }         
}
//+------------------------------------------------------------------+

Output after dragging the the object:

-> ERROR: 4202 object does not exist

Output on a subsequent click of the object:

-> Object Moved 

Vladimir Karputov
Moderator
46127
Vladimir Karputov 2015.02.21 13:22  
mytradertools :

...

Incorrect handling of events OnChartEvent () in MQL4. Similar code in MQL5 works well.
Daniel Cherry
951
Daniel Cherry 2015.02.21 18:58  
barabashkakvn:
Incorrect handling of events OnChartEvent () in MQL4. Similar code in MQL5 works well.

Is that I am handling the OnChartEvent() in MQL4 incorrectly, or it's a problem with MQL4?

Thanks. 

Vladimir Karputov
Moderator
46127
Vladimir Karputov 2015.02.21 19:02  
mytradertools:

Is that I am handling the OnChartEvent() in MQL4 incorrectly, or it's a problem with MQL4?

Thanks. 

Problem with MQL4.
Trinh Dat
37213
Trinh Dat 2015.02.22 11:24  
mytradertools:

Is that I am handling the OnChartEvent() in MQL4 incorrectly, or it's a problem with MQL4?

Thanks. 

Hi

Try it.

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   if (sparam == trendLine.Name())
   {
      // on Drag, want to re-set co-ordinates of trendline.
      if (id == CHARTEVENT_OBJECT_DRAG)
      {
         // get the new co-ordinates
         datetime newX = TimeCurrent() - (PeriodSeconds() * 50);
         double newY = iOpen(NULL,0,50);
         ObjectSetDouble(0,trendLine.Name(),OBJPROP_PRICE1,newY);
         ObjectSetInteger(0,trendLine.Name(),OBJPROP_TIME1,newX);
      }
   }        
}


Daniel Cherry
951
Daniel Cherry 2015.02.23 10:17  
tuoitrecuoi:

Hi

Try it.

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   if (sparam == trendLine.Name())
   {
      // on Drag, want to re-set co-ordinates of trendline.
      if (id == CHARTEVENT_OBJECT_DRAG)
      {
         // get the new co-ordinates
         datetime newX = TimeCurrent() - (PeriodSeconds() * 50);
         double newY = iOpen(NULL,0,50);
         ObjectSetDouble(0,trendLine.Name(),OBJPROP_PRICE1,newY);
         ObjectSetInteger(0,trendLine.Name(),OBJPROP_TIME1,newX);
      }
   }        
}


Brill - thanks tuoitrecuoi - that worked a treat.

So what's the lesson here? Don't use the the ObjectMove() method?

To add comments, please log in or register