Preguntas de los principiantes MQL5 MT5 MetaTrader 5 - página 1484

 

Tenía este código en µ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);
  }



Lo cambié a µl5, sólo ObjectSetText no entendí cómo cambiarlo.


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 #:

¿Cómo se cambia un diseño antiguo?

Aquí está todo claro. Describimos tal o cual objeto en tal o cual tamaño de letra, tipo de fuente, color.

Pero eso es del antiguo.

Leo lo que está siendo reemplazado

A

ObjectGetInteger,ObjectSetString,ObjectSetIntegerStringLen

Pero en todas estas funciones no hay parámetros como en ObjectSetText.

La fuente, el tamaño, el color, el color de fondo, etc. se establecen mediante llamadas separadas de https://www.mql5.com/ru/docs/constants/objectconstants/enum_object_property.

 
JRandomTrader #:

La fuente, el tamaño, el color, el color de fondo, etc. se establecen mediante llamadas independientes a https://www.mql5.com/ru/docs/constants/objectconstants/enum_object_property.

En lugar de

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

¿Debería hacerlo así?

    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 #:

Así que en lugar de

¿Tienes que hacer esto?

OBJPROP_FONTSIZE y OBJPROP_COLOR - mediante ObjectSetInteger()

En lugar de DoubleToString() a veces es más conveniente usar StringFormat(), puedes controlar el formato de forma más flexible.

 

¿Por qué este script produce 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));
   
  }
//+------------------------------------------------------------------+

y éste 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);
   
  }
//+------------------------------------------------------------------+

El resultado debería ser el mismo.

 
Novichokkk #:

¿Por qué sale un script de este tipo 196

Y este 104.

El resultado debería ser el mismo.

¿Por qué debería ser el mismo?

El primer caso es para obtener el valor de la propiedad número CHART_FIRST_VISIBLE_BAR del gráfico actual.

En el segundo caso, se trata de obtener el número de esta propiedad.

A grandes rasgos, la diferencia es la misma que entre el valor de una celda del array y el índice de esta celda.

 


#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);
}

Había un indicador de este tipo en el antiguo MQL4.
 

Lo reescribí a MQL5. He eliminado el trabajo con los objetos de la misma.

Al establecer Ratio=1, aparece en el gráfico como debe ser, es decir, sólo se superpone en el gráfico como una barra Hi-Low. Pero en modo depuración no es así por alguna razón,

se muestra como si la relación se establece en 2, mientras que se establece en 1.

¿Está todo correcto en el código?

//+------------------------------------------------------------------+
#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);
  }
//+------------------------------------------------------------------+
 
Cómo obtener el valor del parámetro
rates_total
por script

Está claro en el indicador. ¿Pero en el script?


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

  {
      Alert(rates_total());
    }
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.12.04
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Novichokkk #:
Cómo obtener el valor del parámetro por script

Está claro en el indicador. ¿Pero en el script?


https://www.mql5.com/ru/docs/series/bars

Документация по MQL5: Доступ к таймсериям и индикаторам / Bars
Документация по MQL5: Доступ к таймсериям и индикаторам / Bars
  • www.mql5.com
Bars - Доступ к таймсериям и индикаторам - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
Razón de la queja: