Questions from a "dummy" - page 262

 
agvozdezkiy:

Just started to learn MQL, and here I wrote an EA that draws an iMA and draws a tangent (very close) to the chart in the iMA at the point where the mouse pointer is on the time axis.

Problem one. The Expert Advisor is VERY slow. It means that the tangent is redrawn for a long time and it does not follow the movement of the mouse...

Look at my version:

//+------------------------------------------------------------------+
//|                                                Antonio_Mouse.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
int Handle_iMA;
double Buffer_iMA[];
string Name_iMA;
datetime TimeArray[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//---Устанавливаем свойство графика посылать события при движении мышы     
   ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true);
//---Рисуем индикатор iMA
   Handle_iMA=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE);
   if(Handle_iMA!=INVALID_HANDLE)
     {
      ChartIndicatorAdd(0,0,Handle_iMA);
      Print("iMA скопировано в буффер.");
     }
//---
   string name="Line";
   ObjectCreate(0,name,OBJ_TREND,0,0,0);
   ObjectSetInteger(0,name,OBJPROP_COLOR,clrRed);
   ObjectSetInteger(0,name,OBJPROP_WIDTH,10);
   ObjectSetInteger(0,name,OBJPROP_BACK,true);
   ObjectSetInteger(0,name,OBJPROP_RAY_LEFT,false);
   ObjectSetInteger(0,name,OBJPROP_RAY_RIGHT,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(Handle_iMA);
  }
datetime CarrentBarTime[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
  }
double PriceCoord,Angle;
datetime TimeCoord;
int SubWindow,BarPos,X1,Y1,X2,Y2;
int prevX=-1;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &XCoord,
                  const double &YCoord,
                  const string &SParam)
  {
   if(id==CHARTEVENT_MOUSE_MOVE)
     {
      int x=(int)XCoord;
      int y=(int)YCoord;
      if(x==prevX) return;
      CopyTime(NULL,0,0,1,CarrentBarTime);
      //---
      ChartXYToTimePrice(NULL,x,y,SubWindow,TimeCoord,PriceCoord);
      if(TimeCoord>CarrentBarTime[0]) return;
      CopyBuffer(Handle_iMA,0,TimeCoord+PeriodSeconds(),2,Buffer_iMA);
      //---
      string name="Line";
      ObjectSetDouble(0,name,OBJPROP_PRICE,0,Buffer_iMA[0]);
      ObjectSetInteger(0,name,OBJPROP_TIME,0,TimeCoord);
      ObjectSetDouble(0,name,OBJPROP_PRICE,1,Buffer_iMA[1]);
      ObjectSetInteger(0,name,OBJPROP_TIME,1,TimeCoord+PeriodSeconds());
      //---
      ChartRedraw();
      prevX=x;
     }
  }

On my laptop - everything flies!

 

I get error 5040 (Corrupted parameter of type string) the first time I call the object creation function

void  CreateTL(string name,datetime time1,double price1,datetime time2,double price2,bool ray_left,bool ray_right,color color_TL)
  {
//------------ nTL,nTLh,nTLl,nTLe
   nTL="nTL_"+IntegerToString(MathRand());
   nTLh="nTLh_"+IntegerToString(MathRand());
   nTLl="nTLl_"+IntegerToString(MathRand());
   nTLe="nTLe_"+IntegerToString(MathRand());
//------------
   ObjectCreate(0,name,OBJ_TREND,0,time1,price1,time2,price2);
   ObjectSetInteger(0,name,OBJPROP_COLOR,color_TL);
   ObjectSetInteger(0,name,OBJPROP_STYLE,STYLE_DOT);
   ObjectSetInteger(0,name,OBJPROP_RAY_RIGHT,ray_left);
   ObjectSetInteger(0,name,OBJPROP_RAY_RIGHT,ray_right);
   ObjectSetInteger(0,name,OBJPROP_BACK,true);
  };

If before a call set

nTL="";

error 4002 (Invalid parameter when calling the client terminal function internally)

If set before the call

nTL="a";

the first object will be named a.

I call it this way

if(hlp_var==high_low)
     {
      //CreateTL(nTL,dt[0],pr[0],dt[0],pr[1],false,false,nTL_color);
      CreateTL(nTL,dt[0],pr[0],dt[0],pr[1],false,false,nTL_color);
      CreateTL(nTLe,dt[1],pr[0],dt[1],pr[1],false,false,nTL_color);
      CreateTL(nTLh,dt[0],pr[0],dt[1],pr[0],false,true,nTL_color);
      CreateTL(nTLl,dt[0],pr[1],dt[1],pr[1],false,true,nTL_color);
      CreateTL(nTL,dt[0],pr[0],dt[1],pr[1],false,true,nTL_color);
      CreateTL(nTL,dt[0],pr[1],dt[1],pr[0],false,true,nTL_color);
      Print("CreateTL GetLastError "+IntegerToString(GetLastError()));
     };

Could you tell me where is an error?

Документация по MQL5: Основы языка / Операторы / Оператор создания объекта new
Документация по MQL5: Основы языка / Операторы / Оператор создания объекта new
  • www.mql5.com
Основы языка / Операторы / Оператор создания объекта new - Документация по MQL5
 

In the old version.

ObjectCreate(0,nTL="nTL_"+IntegerToString(MathRand()),OBJ_TREND,0,time1,price1,time2,price2);

never had any failures.

Is it a bug in the code or in the terminal?

 
Silent:

Can you tell me where the error is?

I don't know where the error is, but the handling of object names is very questionable to say the least.
Документация по MQL5: Стандартные константы, перечисления и структуры / Константы объектов / Свойства объектов
Документация по MQL5: Стандартные константы, перечисления и структуры / Константы объектов / Свойства объектов
  • www.mql5.com
Стандартные константы, перечисления и структуры / Константы объектов / Свойства объектов - Документация по MQL5
 
It is unclear why you would want a random number in the name of the object, this can also lead to an error.
 
Silent:

I get error 5040 (Corrupted parameter of type string) the first time I call the object creation function

If before a call set

error 4002 (Invalid parameter when calling the client terminal function internally)

If set before the call

the first object will be named a.

I call like this

Can you tell me where the error is?

Are you worried because Get LastError prints something other than zero? If so, you should only call for an error if the function reports a failure. And you shouldn't ask for an error at the end of the plot, you might get a rubbish value. This is how it is in MT4, maybe it is different here.

Pseudocode:

if( ! ObjectCreate(...) )
    Print( GetLastError() );
 
Silent:

I get error 5040 (Corrupted parameter of type string) the first time I call the object creation function

If before a call set

error 4002 (Invalid parameter when calling the client terminal function internally)

If set before the call

the first object will be named a.

I call it this way

Could you tell me where the error is?

Try "gluing" strings through StringConcatenate()

 
TheXpert:
I don't know where the error is, but the handling of object names is very questionable to put it mildly.
I'll be racking my brains for another week. Can you describe it in more detail? (you can be crude :))
zfs:
It's not clear why you need a random number in the object name, it could lead to an error.

Is there another option for a couple of hundred objects with unique names?

(A group of objects should have the same part of the string in the name, to remove the group.)

 
220Volt:

Are you worried because Get Last Errors prints something other than zero? If so, you should only ask for an error if the function reports a failure. And you shouldn't ask for an error at the end of the plot, you might get a rubbish value. That's how it is in MT4, maybe it's different here.

No, because the first call doesn't create an object due to an error. I duplicate (for now) the line of the first call, then from the second run it creates.
 
220Volt:

Are you worried because Get Last Errors prints something other than zero?

Well, logically, if the function sets last error at all, then on successful completion of the function there should be a zero error or the value described in the documentation. There is no other way.

That is, if the call was successful and the error is non-zero, this case must be described in the documentation.

Silent:

I will be racking my brains this way for another week. May I give you some more details? (You may be crude :))

Well usually names of objects of the same type are formed from common part, for easy removal ("nTL_" in your case) and differences. I also use module identifier, if there can be more than one of these very modules at once on the chart.

The difference can be anything at all, even a counter, which you increase every time you create an object. But it's better to make distinctions by logic -- object time, object coordinate, the goal is to achieve guaranteed uniqueness. The side effect is that by constructing a name again, you can change the properties of an already created object.

Random does not provide this "guaranteed uniqueness".

Документация по MQL5: Основы языка / Операторы / Оператор создания объекта new
Документация по MQL5: Основы языка / Операторы / Оператор создания объекта new
  • www.mql5.com
Основы языка / Операторы / Оператор создания объекта new - Документация по MQL5