Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1494

 
ANDREY:
Could you please tell me why the program (on mql 4, on minutes, all ticks) reads the number explicitly and the same number calculated in the function differently.
Here is the code

The program does not read Pr as 1.60854 althoughPrint( ) shows the value of Pr as 1.60854 . As a result, the program opens the second order, but it shouldn't do that
.


And if we set the number 1.60854 instead of the Pr variable , the program reads it and does not open the second order.

QUESTION What changes should be made to the code so that the program reads Pr and doesn't open the second order.
Thanks
for your help

It is not recommended to compare real numbers directly. It is more correct to compare their normalized difference with zero.

NormalizeDouble(Lt1-Pr, _Digits) != 0.0;
 
Alexey Viktorov:

It is not recommended to compare real numbers directly. It is correct to compare their normalised difference to zero.

Thanks for the valuable information. I never would have guessed that myself.

 
ANDREY:

Thank you for the valuable information. I never would have guessed that myself.

Not recommended does not mean forbidden. If, for example, we compare the current price with the price of the expected stop, then we can compare directly whether it is more or less... The difference will be quite significant. But it is quite another matter to compare it on equal terms...

 
Alexey Viktorov:

Not recommended does not mean that it is forbidden. If, for example, we compare the current price with the price of the expected stop, then we can compare directly whether it is more or less... The difference will be quite significant. And it's quite another matter when you compare it to equality...

Got it. Thank you.

 
I thought of creating a separate mqh file with a list of macro substitutions to include the specified code files.
// файл со списком макроподстановок

#define  ARRAY_C #include <JvL/Arrays/C_Array.mqh>
#define  MTF_C #include <JvL/C_MTF.mqh>
// файл с кодом в котором нужно использовать включения других файлов с кодом

#include <JvL/Includes.mqh>
ARRAY_С
But it doesn't work that easily.

macro argument expected instead of 'include' Includes.mqh
'include' - expressions are not allowed on a global scope C_Meter.mqh


How can I outsmart the compiler?

 

Hello. Please help me to understand the Zigzag indicator. It turns out that the entire array ZigZagBuffer[] has taken the value 0.0. Why does the expression if(res != 0.0) sometimes become true? After all, the entire array has the value 0.0?????

 if(prev_calculated==0)
     {
      ArrayInitialize(ZigZagBuffer,0.0);
      ArrayInitialize(HighMapBuffer,0.0);
      ArrayInitialize(LowMapBuffer,0.0);
      start=InpDepth;//Print(start);
     }
   if(prev_calculated>0) 
     {
      i=rates_total-1;//Print(i);
      //--- searching for the third extremum from the last uncompleted bar
      while(extreme_counter<ExtRecalc && i>rates_total-100)
        {
         res=ZigZagBuffer[i]; 
         if(res!=0.0)                       //!!!
            extreme_counter++; x++;
         i--;
        }
      i++;
      start=i;
 

Hi all. Dear programmers, I need your help.

I am trying to make a multicurrency indicator, it should show SMA of two symbols in the basement.

If it's not difficult, please advise what is the error.


//+------------------------------------------------------------------+
//|                                          Normalzd_I_I_Oscill.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                                 https://mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://mql5.com"
#property version   "1.00"
#property description "Normlzd_I_I_Oscill"
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_plots   2
//--- plot Int0
#property indicator_label1  "Int0"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDeepSkyBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Int1
#property indicator_label2  "Int1"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- input parameters - buffer 0 ---------
input string          symbol0="EURUSD";      //----- Instrument 1 ------
input ENUM_TIMEFRAMES tf0=PERIOD_CURRENT; //Timeframe 1
input uint            InpPeriod0=10;   // Period1
//--- input parameters - buffer 1 ---------
input string          symbol1="USDCAD";      //----- Instrument 2 ------
input ENUM_TIMEFRAMES tf1=PERIOD_CURRENT; //Timeframe 2
input uint            InpPeriod1=10;   // Period2
//--- input other parameters --------------
input int             max_bars=100;       //Maximum Bars Calculate

//--- indicator buffers
double         Buffer1NII[];
double         Buffer1Temp[];
double         Buffer1Raw[];
double         Buffer2NII[];
double         Buffer2Temp[];
double         Buffer2Raw[];
//--- global variables
int            period_sm0;
int            period_sm1;
string         _symbol0,_symbol1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(symbol0==symbol1)
     {
      Alert("Wrong setting Symbol NAME");
      return(INIT_FAILED);
     }
   _symbol0=symbol0;
   _symbol1=symbol1;
   if(symbol0=="")
      _symbol0=Symbol();
   if(symbol1=="")
      _symbol1=Symbol();
//--- set global variables
   period_sm0=int(InpPeriod0<1 ? 1 : InpPeriod0);
   period_sm1=int(InpPeriod1<1 ? 1 : InpPeriod1);
   
//--- indicator buffers mapping
   SetIndexBuffer(0,Buffer1NII,INDICATOR_DATA);
   SetIndexBuffer(1,Buffer1Raw,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,Buffer1Temp,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,Buffer2NII,INDICATOR_DATA);
   SetIndexBuffer(4,Buffer2Raw,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,Buffer2Temp,INDICATOR_CALCULATIONS);

   PlotIndexSetString(0,PLOT_LABEL,symbol0+" Int0-" +EnumToString(tf0));   
   PlotIndexSetString(1,PLOT_LABEL,symbol1+" Int1-" +EnumToString(tf0));   
//--- setting indicator parameters
   IndicatorSetString(INDICATOR_SHORTNAME,"Normlzd_I_I_O"); 
   IndicatorSetInteger(INDICATOR_DIGITS,Digits());
//--- setting buffer arrays as timeseries
   ArraySetAsSeries(Buffer1NII,true);
   ArraySetAsSeries(Buffer1Temp,true);
   ArraySetAsSeries(Buffer1Raw,true);
   ArraySetAsSeries(Buffer2NII,true);
   ArraySetAsSeries(Buffer2Temp,true);
   ArraySetAsSeries(Buffer2Raw,true);
   
   if(!SymbolInfoInteger(symbol0,SYMBOL_SELECT)) 
      SymbolSelect(symbol0,true);                
   if(!SymbolInfoInteger(symbol1,SYMBOL_SELECT))
      SymbolSelect(symbol1,true);
//---
   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[])
  {
   bool error=false;
   string symerror="";
   ResetLastError();
//--- Установка массивов буферов как таймсерий
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(close,true);
   ArraySetAsSeries(tick_volume,true);
//--- Проверка и расчёт количества просчитываемых баров
   if(rates_total<fmax(period_sm0,4)) return 0;
   if(rates_total<fmax(period_sm1,4)) return 1;
//--- Проверка и расчёт количества просчитываемых баров
   int limit=rates_total-prev_calculated;
   if(limit>1)
     {
      limit=rates_total-0;
      ArrayInitialize(Buffer1NII,EMPTY_VALUE);
      ArrayInitialize(Buffer1Temp,0);
      ArrayInitialize(Buffer1Raw,0);
          
      limit=rates_total-1;
      ArrayInitialize(Buffer2NII,EMPTY_VALUE);
      ArrayInitialize(Buffer2Temp,1);
      ArrayInitialize(Buffer2Raw,1);
     }

//--- Расчёт индикатора
   for(int i=limit; i>=0 && !IsStopped(); i--)
     {
      Buffer1Temp[i]=(high[i]-low[i]!=0 ? (2.0*close[i]-high[i]-low[i])/(high[i]-low[i])*tick_volume[i] : 0);
      Buffer1Raw[i]=(double)tick_volume[i];
      double raw_avg1=GetSMA(rates_total,i,period_sm0,Buffer1Raw);
      Buffer1NII[i]=(raw_avg1!=0 ? GetSMA(rates_total,i,period_sm0,Buffer1Temp)/raw_avg1 : 0);
           {
            error=true;
            symerror=symbol0; 
           }
     
      Buffer2Temp[i]=(high[i]-low[i]!=0 ? (2.0*close[i]-high[i]-low[i])/(high[i]-low[i])*tick_volume[i] : 0);
      Buffer2Raw[i]=(double)tick_volume[i];
      double raw_avg2=GetSMA(rates_total,i,period_sm1,Buffer2Raw);
      Buffer2NII[i]=(raw_avg2!=0 ? GetSMA(rates_total,i,period_sm1,Buffer2Temp)/raw_avg2 : 0);
           {
            error=true;
            symerror=symbol0; 
           } 
     }
//--- return value of prev_calculated for next call
   if(error)
     
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Simple Moving Average                                            |
//+------------------------------------------------------------------+
double GetSMA(const int rates_total,const int index,const int period,const double &price[],const bool as_series=true)
  {
//---
   double result=0.0; 
//--- check position
   bool check_index=(as_series ? index<=rates_total-period-1 : index>=period-1);
   if(period<1 || !check_index)
      return 0;
//--- calculate value
   for(int i=0; i<period; i++)
      result=result+(as_series ? price[index+i]: price[index-i]);
//---
   return(result/period);
  }
//+------------------------------------------------------------------+
 
ANDREY:

Good day to all.
Can you please tell me how to code the following condition correctly using I don't remember which mathematical function.

if (Bid - Low[1]>=0.0030 && Bid - Low[1]<0.0035) {action;}
I know that there is a math function that can be used in the above condition without && sign . But I don't remember what this mathematical function is called and how to apply it.
Thank you for your help.

if(MathAbs(Bid - Low[1]) >= 0.0004) {action;}

 
Taras Slobodyanik:

if(MathAbs(Bid - Low[1]) >= 0.0004) {action;}

Oh Taras, Taras... Didn't you learn to think at school? Is 0.0004 ever less than 0.0035 ...............

 
Alexey Viktorov:

Oh Taras, Taras... Didn't they teach you to think at school? Is 0.0004 less than 0.0035 ...............

Learn to read, you liar.

and the program will check for price consistency in the 4-point range.