Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1284

 
Youri Lazurenko:

Thanks again, now the line is like a line, nice to see. OnlyObjectDelete(0, name);; is needed, otherwise once drawn the line is not redrawn. The same in mql4, I just forgot.

I would have done so

bool CreateLine(string name, datetime time1, double price1, datetime time2, double price2, color clr)
 {
  if(ObjectFind(ChartID(), name) < 0)
    if(!ObjectCreate(ChartID(), name, OBJ_TREND, 0, 0, 0, 0, 0))
     {
      Print("Ошибка в создании линии");
      return (false);
     }
  ObjectSetInteger(ChartID(), name, OBJPROP_COLOR, clr);
  ObjectSetInteger(ChartID(), name, OBJPROP_STYLE, TrendStyle);
  ObjectSetInteger(ChartID(), name, OBJPROP_WIDTH, TrendWidth);
  ObjectSetInteger(ChartID(), name, OBJPROP_TIME, 0, time1);
  ObjectSetInteger(ChartID(), name, OBJPROP_TIME, 1, time2);
  ObjectSetDouble(ChartID(), name, OBJPROP_TIME, 0, price1);
  ObjectSetDouble(ChartID(), name, OBJPROP_TIME, 1, price2);

  ChartRedraw();
  return(true);
 }
 
Vladimir Karputov:

What a nonsense. The drawn graphical object moves smoothly. Example is given in help for each object (for horizontal lineOBJ_HLINE).

You can also change any property to an already drawn object: colour, style , smell ...

It is done manually, I don't argue. But the Expert Advisor draws trend lines by DeMark points. With the function ObjectDelete(0, name);, when the points change, the old trend line is removed and a new one is drawn. Without this function, the first drawn lines are no longer redrawn. This is not nonsense, it's a fact.

Документация по MQL5: Графические объекты / ObjectDelete
Документация по MQL5: Графические объекты / ObjectDelete
  • www.mql5.com
ObjectDelete - Графические объекты - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Youri Lazurenko:

Manually, I don't argue. The Expert Advisor, on the other hand, draws trend lines by DeMark points. With function ObjectDelete(0, name);, when the points change, the old trend line is removed and a new one is drawn. Without this function, the first drawn lines are no longer redrawn. This is not nonsense, it is a fact.

Why don't you read the help? And run the example.

 
Alexey Viktorov:

I'd do that.

Thanks, but I'll leave my variant, it works. Although I'll try it instead of ObjectDelete(0, name);.

 ChartRedraw();
Документация по MQL5: Графические объекты / ObjectDelete
Документация по MQL5: Графические объекты / ObjectDelete
  • www.mql5.com
ObjectDelete - Графические объекты - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Vladimir Karputov:

Read the help, for God's sake! And run the example.

I've read the reference. Only you are indignant and I'm writing from practice. Another thing is to use ChartRedraw() instead of ObjectDelet(), I don't argue and will check it. This is the only thing you need to point out by referring to the example.

 
Youri Lazurenko:

I have read the brief. Only you are indignant with words and I am writing from practice. Another thing is to use ChartRedraw() instead of ObjectDelet(), I don't argue and check it. This is the only thing you need to point out by referring to the example.

People usually ask for advice in this thread and when they get it, they listen. At the very least read the help and apply examples. Please, if you don't want to listen to others, don't ask anything at all.

 
Youri Lazurenko:


An example of how you can change any property of the"Horizontal line" graphical object on the fly - you don't need to delete the object to do this, you just need to change the property.

C:\Users\barab\OneDrive\Изображения\Screenpresso\2021-02-01_16h09_06.gif

Code:

//+------------------------------------------------------------------+
//|                       OBJ_HLINE change properties on the fly.mq5 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//--- input parameters
input string          InpName="HLine";     // Line name
//--- an array for storing colors contains 14 elements
color colors[]=
  {
   clrRed,clrBlue,clrGreen,clrChocolate,clrMagenta,clrDodgerBlue,clrGoldenrod,
   clrIndigo,clrLightBlue,clrAliceBlue,clrMoccasin,clrWhiteSmoke,clrCyan,clrMediumPurple
  };
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(1);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//--- the number of colors
   int size=ArraySize(colors);
//--- get a random value
   int number=MathRand();
//--- get an index in the col[] array as a remainder of the integer devision
   int i=number%size;
//--- set line color
   ObjectSetInteger(ChartID(),InpName,OBJPROP_COLOR,colors[i]);
   ChartRedraw();
  }
//+------------------------------------------------------------------+
Документация по MQL5: Константы, перечисления и структуры / Константы объектов / Типы объектов
Документация по MQL5: Константы, перечисления и структуры / Константы объектов / Типы объектов
  • www.mql5.com
Типы объектов - Константы объектов - Константы, перечисления и структуры - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Youri Lazurenko:

Thanks, but I'll leave my variant, it works. Although I will try it instead of ObjectDelete(0, name);.

Completely wrong decision. Deleting a graphical object and refreshing a chart in no way replace each other.

My example first checks if there is an object, in particular, a trend with the name tfyu, which is name in the Russian keyboard layout)). Then, if it does not exist, the trend is drawn. If the drawing attempt is unsuccessful, the message is displayed and the function is exited with return false.

Everything after that, irrespective of whether the trend is already present or has just been drawn, it is assigned the specified parameters, time and coordinate prices, type, thickness and anything else you can add. After that the chart is updated and the function returns true.

In terms of speed of execution, it will be less expensive to check for the presence of the trend than to delete it and draw a new one.

But... the final decision is up to you and if you like scratching your left ear with your right little finger, I have no right to stop you.

 
Youri Lazurenko:

And back to drawing the trend line. If you need code.

Very bad code.

 
Alexey Viktorov:

I would do this.

I would have done this:

void CreateLine(string name, datetime time1, double price1, datetime time2, double price2, color clr)
 {
  if(ObjectFind(ChartID(), name) < 0)
   {
    ObjectCreate(ChartID(), name, OBJ_TREND, 0, 0, 0, 0, 0);
    ObjectSetInteger(ChartID(), name, OBJPROP_COLOR, clr);
    ObjectSetInteger(ChartID(), name, OBJPROP_STYLE, TrendStyle);
    ObjectSetInteger(ChartID(), name, OBJPROP_WIDTH, TrendWidth);
   }
   ObjectSetInteger(ChartID(), name, OBJPROP_TIME, 0, time1);
   ObjectSetInteger(ChartID(), name, OBJPROP_TIME, 1, time2);
   ObjectSetDouble(ChartID(), name, OBJPROP_PRICE, 0, price1);
   ObjectSetDouble(ChartID(), name, OBJPROP_PRICE, 1, price2);

  ChartRedraw();
 }
Reason: