passing indicator to mt5

 

Hello everyone! I am transferring this squeezer indicator from mt4 (which I have always worked with) to mt5 and the truth is I am quite lost... I have mostly passed it, but I don't quite understand what is the correct way I have to do to obtain iMA data.

Going from the simplicity of mt4 to mt5 is a bit crazy XD

If anyone can lend a hand I would appreciate it, because I have tried everything but I can't find the right way and my neurons can't go any further.

Automated translation applied by moderator

//------------------------------------------------------------------
#property copyright ""
#property link  ""

//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 1
#property indicator_label1 "Histogram"
#property indicator_type1 DRAW_COLOR_HISTOGRAM
#property indicator_color1 clrDodgerBlue,clrCrimson
#property indicator_width1 2
#property indicator_maximum 1
#property indicator_minimum -1
//
input int    inpPeriod = 20;  
//
//--- indicator buffers
int emah[];
double ema[];
double histo[],histoc[];

int OnInit()
  {
   SetIndexBuffer(0,histo,INDICATOR_DATA);
   SetIndexBuffer(1,histoc,INDICATOR_COLOR_INDEX);
   IndicatorSetInteger(INDICATOR_DIGITS,0);
   IndicatorSetString(INDICATOR_SHORTNAME,"Squeezer");
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason) { return; }

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[])
  {
   int counted_bars=prev_calculated;
   int limit;
   if(counted_bars<0)
      return(-1);
   if(counted_bars>0)
      counted_bars--;
   limit=Bars(Symbol(),Period())-31;
   if(counted_bars>=31)
      limit=Bars(Symbol(),Period())-counted_bars-1;

   if(Bars(_Symbol,_Period)<rates_total)
      return(-1);

   for(int i = 0; i < rates_total; i++)
     {
      emah[i] = iMA(NULL, 0, inpPeriod, i, MODE_EMA, PRICE_CLOSE);
      if(emah[i] == INVALID_HANDLE)
        {
         Print("EMA error");
         return(INIT_FAILED);
        }
     }

   for(int i=(int)MathMax(prev_calculated-1,0); i<rates_total && !_StopFlag; i++)
     {
      histo[i] = LinearRegressionValue(inpPeriod,i);
      if(histo[i] > 0)
         histoc[i] =1;
      else
         histoc[i] =-1;
     }
   return(rates_total);
  }

//+------------------------------------------------------------------+
//|                                 |
//+------------------------------------------------------------------+
double LinearRegressionValue(int Len,int shift)
  {
   double SumBars = 0;
   double SumSqrBars = 0;
   double SumY = 0;
   double Sum1 = 0;
   double Sum2 = 0;
   double Slope = 0;

   SumBars = Len * (Len-1) * 0.5;
   SumSqrBars = (Len - 1) * Len * (2 * Len - 1)/6;

   for(int x=0; x<=Len-1; x++)
     {
      double HH = iLow(_Symbol,_Period,x+shift);
      double LL = iHigh(_Symbol,_Period,x+shift);
      for(int y=x; y<=(x+Len)-1; y++)
        {
         HH = MathMax(HH, iHigh(_Symbol,_Period,y+shift));
         LL = MathMin(LL, iLow(_Symbol,_Period,y+shift));
        }

      if(CopyBuffer(emah[shift], 0,x+shift, 1, ema) <= 0)
                 return(0);
        

      Sum1 += x* (iClose(_Symbol,_Period,x+shift)-((HH+LL)/2 + ema[0])/2);
      SumY += (iClose(_Symbol,_Period,x+shift)-((HH+LL)/2 + ema[0])/2);
     }
   Sum2 = SumBars * SumY;
   double Num1 = Len * Sum1 - Sum2;
   double Num2 = SumBars * SumBars-Len * SumSqrBars;

   if(Num2 != 0.0)
     {
      Slope = Num1/Num2;
     }
   else
     {
      Slope = 0;
     }

   double Intercept = (SumY - Slope*SumBars) /Len;
   double LinearRegValue = Intercept+Slope * (Len - 1);

   return (LinearRegValue);
  }
//+------------------------------------------------------------------+
 

the original is this (there are things that I have eliminated in mt5 but I verified that it does not affect the main histogram, which is what I am trying to replicate)

Automated translation applied by moderator

Files:
 
On the English forum, please write in English. Either use the automatic translation tool, or post in one of the other language forums.
 
Oliver Ojeda Ojeda: I am quite lost... I have mostly passed it, but I don't quite understand what is the correct way I have to do to obtain iMA data.

Perhaps you should read the manual, especially the examples. iMA - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 
Fernando Carreiro #:
En el foro en inglés, por favor escribe en inglés. Utilice la herramienta de traducción automática o publique en uno de los foros de otros idiomas.

sorry, with automatic translator i thougt i was in spanish forum ...