Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1484

 
Maxim Kuznetsov #:

you and I have different "lefts" :-)

1110<<1 := 11100

I read it on the internet. This is for 32 bits I understand, for 16 it's the same, nothing jumped by a bit. But I'm just learning, probably you are right.

It's not the point, it's just a side note. I was surprised by such a construction in the terminal. This indicator is not even from the codebase, it is from the terminal from the Examples folder.

Files:
8888.jpg  49 kb
 

How do you change an old design?

ObjectSetText("Maximum",DoubleToString(ChartGetDouble(0,CHART_PRICE_MAX,0),5),8,"Arial",Red);

Here it's all clear. Such and such object, so and so describe, such and such font size, font type, colour.

But that's from the old one.

I read what's being replaced

ObjectSetText

To

ObjectGetInteger,ObjectSetString,ObjectSetIntegerStringLen.

But in all these functions there are no such parameters as in ObjectSetText.

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

I had this code on µl4

int OnInit()
  {
   SetIndexBuffer(0,H);
   SetIndexStyle(0,2);
   SetIndexBuffer(1,L);
   SetIndexStyle(1,2);

   if(ObjectCreate("Maximum",OBJ_LABEL,0,0,0))
     {
      ObjectSet("Maximum",OBJPROP_XDISTANCE,1);
      ObjectSet("Maximum",OBJPROP_YDISTANCE,1);
      ObjectSet("Maximum",OBJPROP_CORNER,1);
     }
   ObjectSetText("Maximum",DoubleToStr(WindowPriceMax(),4),8,"Arial",Red);

   if(ObjectCreate("Minimum",OBJ_LABEL,0,0,0))
     {
      ObjectSet("Minimum",OBJPROP_XDISTANCE,1);
      ObjectSet("Minimum",OBJPROP_YDISTANCE,1);
      ObjectSet("Minimum",OBJPROP_CORNER,3);
     }
   ObjectSetText("Minimum",DoubleToStr(WindowPriceMin(),4),8,"Arial",Blue);

   return(0);
  }



I changed it to µl5, only ObjectSetText I didn't understand how to change it.


int OnInit()
  {
   SetIndexBuffer(0, H, INDICATOR_DATA);
   PlotIndexSetInteger(1,5,0);
   SetIndexBuffer(1, L, INDICATOR_DATA);
   PlotIndexSetInteger(2,5,0);

   if(ObjectCreate(0,"Maximum",OBJ_LABEL,0,0,0))
     {
      ObjectSetInteger(0,"Maximum",OBJPROP_XDISTANCE,1);
      ObjectSetInteger(0,"Maximum",OBJPROP_YDISTANCE,1);
      ObjectSetInteger(0,"Maximum",OBJPROP_CORNER,1);
     }
   ObjectSetText("Maximum",DoubleToString(ChartGetDouble(0,CHART_PRICE_MAX,0),5),8,"Arial",Red);

   if(ObjectCreate(0,"Minimum",OBJ_LABEL,0,0,0))
     {
      ObjectSetInteger(0,"Minimum",OBJPROP_XDISTANCE,1);
      ObjectSetInteger(0,"Minimum",OBJPROP_YDISTANCE,1);
      ObjectSetInteger(0,"Minimum",OBJPROP_CORNER,3);
     }
   ObjectSetText("Minimum",DoubleToString(ChartGetDouble(0,CHART_PRICE_MIN,0),5),8,"Arial",Blue);

   return(0);
  }
 
Novichokkk #:

How do you change an old design?

It's all clear here. We describe such and such an object in such and such a font size, font type, colour.

But that's from the old one.

I read what's being replaced

To

ObjectGetInteger,ObjectSetString,ObjectSetIntegerStringLen

But in all these functions there are no such parameters as in ObjectSetText.

Font, size, colour, background colour, etc. are set by separate calls of https://www.mql5.com/ru/docs/constants/objectconstants/enum_object_property.

 
JRandomTrader #:

Font, size, colour, background colour, etc. are set by separate calls to https://www.mql5.com/ru/docs/constants/objectconstants/enum_object_property.

Instead of

ObjectSetText("Maximum",DoubleToStr(WindowPriceMax(),4),8,"Arial",Red);

Should I do it like this?

    ObjectSetString(0,"Maximum",OBJPROP_TEXT,DoubleToString(ChartGetDouble(0,CHART_PRICE_MAX,0),5)); // описание-что вставляем в объект "Maximum"
    ObjectSetString(0,"Maximum",OBJPROP_FONTSIZE,8);    // размер шрифта описания                                      
    ObjectSetString(0,"Maximum",OBJPROP_FONT,"Arial");  // какой шрифт
    ObjectSetString(0,"Maximum",OBJPROP_COLOR,Red);     // цвет шрифта
 
Novichokkk #:

So instead of

You have to do this?

OBJPROP_FONTSIZE and OBJPROP_COLOR - via ObjectSetInteger()

Instead of DoubleToString() it is sometimes more convenient to use StringFormat(), you can control the format more flexibly.

 

Why does this script output 196

//+------------------------------------------------------------------+
//|                                                         0000.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()

  {
//---
   Alert((int)ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR));
   
  }
//+------------------------------------------------------------------+

and this one 104.

//+------------------------------------------------------------------+
//|                                                         0000.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()

  {
//---
   Alert(CHART_FIRST_VISIBLE_BAR);
   
  }
//+------------------------------------------------------------------+

The result should be the same.

 
Novichokkk #:

Why does such a script output 196

And this one 104.

The result should be the same.

Why should it be the same?

The first case is to get the value of the property number CHART_FIRST_VISIBLE_BAR of the current chart.

The second case is to get the number of this property itself.

Roughly, the difference is the same as between the value of an array cell and the index of this cell.

 


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 OrangeRed

extern int Ratio=2;
int limit;
int limitBars=0;

double H[];
double L[];
//+------------------------------------------------------------------+ 
int init()
{
   SetIndexBuffer(0,H); SetIndexStyle(0,2);
   SetIndexBuffer(1,L); SetIndexStyle(1,2);

   if(ObjectCreate("Maximum",OBJ_LABEL,0,0,0))  {
      ObjectSet("Maximum",OBJPROP_XDISTANCE,1);
      ObjectSet("Maximum",OBJPROP_YDISTANCE,1);
      ObjectSet("Maximum",OBJPROP_CORNER,1);  }
   ObjectSetText("Maximum",DoubleToStr(WindowPriceMax(),4),8,"Arial",Red);

   if(ObjectCreate("Minimum",OBJ_LABEL,0,0,0))  {
      ObjectSet("Minimum",OBJPROP_XDISTANCE,1);
      ObjectSet("Minimum",OBJPROP_YDISTANCE,1);
      ObjectSet("Minimum",OBJPROP_CORNER,3);  }
   ObjectSetText("Minimum",DoubleToStr(WindowPriceMin(),4),8,"Arial",Blue);

   return(0);
}
//+------------------------------------------------------------------+ 
int start()
{
   double max[256];
   double min[256];

   if(limitBars<Bars) limit=WindowFirstVisibleBar();

   for(int i=0; i<limit; i++)
   {
      for(int shift=0; shift<Ratio; shift++) {
         max[shift]=High[i*Ratio+shift];
         min[shift]=Low[i*Ratio+shift];   }

      H[i]=max[ArrayMaximum(max,Ratio,0)];
      L[i]=min[ArrayMinimum(min,Ratio,0)];
   }

   if(limit>1)
   {
      max[0]=H[ArrayMaximum(H,limit,0)];
      min[0]=L[ArrayMinimum(L,limit,0)];
      ObjectSetText("Maximum",DoubleToStr(max[0],4));
      ObjectSetText("Minimum",DoubleToStr(min[0],4));
   }

   limitBars=Bars;
   limit=1;
   return(0);
}

There was such an indicator on the old MQL4.
 

I rewrote it to MQL5. I removed the work with objects from it.

When setting Ratio=1, it appears on the chart as it should be, i.e. it is just superimposed on the chart as a Hi-Low bar. But in debugging mode it is not so for some reason,

it is displayed as if Ratio is set to 2, while it is set to 1.

Is everything correct in the code?

//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot Histogram_2
#property indicator_label1  "Histogram_2"
#property indicator_type1   DRAW_HISTOGRAM2
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

input int Ratio=1;
int limit;
int limitBars=0;

double H[];
double L[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,H,INDICATOR_DATA);
   SetIndexBuffer(1,L,INDICATOR_DATA);
   ArraySetAsSeries(H,true);
   ArraySetAsSeries(L,true);
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

   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[])
  {
   double max[256];
   double min[256];

   if(limitBars<Bars(_Symbol,_Period))
      limit=CHART_FIRST_VISIBLE_BAR;

   for(int i=0; i<limit; i++)
     {
      for(int shift=0; shift<Ratio; shift++)
        {
         max[shift]=iHigh(Symbol(),Period(),i*Ratio+shift);
         min[shift]=iLow(Symbol(),Period(),i*Ratio+shift);
        }

      H[i]=max[ArrayMaximum(max,0,Ratio)];
      L[i]=min[ArrayMinimum(min,0,Ratio)];
     }

   if(limit>1)
     {
      max[0]=H[ArrayMaximum(H,0,limit)];
      min[0]=L[ArrayMinimum(L,0,limit)];
     

     }

   limitBars=Bars(_Symbol,_Period);
   limit=1;
   return(rates_total);
  }
//+------------------------------------------------------------------+