Heiken Ashi Exit indicator

TBS
193
TBS  
Hi Friends, any one help me to convert MQL4 code to MQL5 code


This is MQL4 Code

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_color4 Lime

extern int MaMetod = 1;
extern int MaPeriod = 40;
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
int count = 0;

int init() {
   SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID, 1, Red);
   SetIndexBuffer(0, Buffer1);
   SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID, 1, Lime);
   SetIndexBuffer(1, Buffer2);
   SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID, 3, Red);
   SetIndexBuffer(2, Buffer3);
   SetIndexStyle(3, DRAW_HISTOGRAM, STYLE_SOLID, 3, Lime);
   SetIndexBuffer(3, Buffer4);
   SetIndexDrawBegin(0, 5);
   SetIndexBuffer(0, Buffer1);
   SetIndexBuffer(1, Buffer2);
   SetIndexBuffer(2, Buffer3);
   SetIndexBuffer(3, Buffer4);
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   double closeMA;
   double lowMA;
   double openMA;
   double highMA;
   double haOpen;
   double haHigh;
   double haLow;
   double haClose;
   if (Bars <= 10) return (0);
   count = IndicatorCounted();
   if (count < 0) return (-1);
   if (count > 0) count--;
   for (int i = Bars - count - 1; i >= 0; i--) {
      closeMA  = iMA(NULL, 0, MaPeriod, 0, MaMetod, PRICE_CLOSE, i);
      lowMA    = iMA(NULL, 0, MaPeriod, 0, MaMetod, PRICE_LOW, i);
      openMA   = iMA(NULL, 0, MaPeriod, 0, MaMetod, PRICE_OPEN, i);
      highMA   = iMA(NULL, 0, MaPeriod, 0, MaMetod, PRICE_HIGH, i);
      haOpen    = (Buffer3[i + 1] + (Buffer4[i + 1])) / 2.0;
      haClose    = (closeMA + highMA + openMA + lowMA) / 4.0;
      haHigh    = MathMax(highMA, MathMax(haOpen, haClose));
      haLow    = MathMin(openMA, MathMin(haOpen, haClose));
      if (haOpen < haClose) {
         Buffer1[i] = haLow;
         Buffer2[i] = haHigh;
      } else {
         Buffer1[i] = haHigh;
         Buffer2[i] = haLow;
      }
      Buffer3[i] = haOpen;
      Buffer4[i] = haClose;
   }
   return (0);
}

and I write MQL5 code but not working

//+------------------------------------------------------------------+
//|                                             Heiken Ashi Exit.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers   5
#property indicator_plots     1
#property indicator_color1    clrDodgerBlue,clrRed
#property indicator_type1     DRAW_COLOR_HISTOGRAM

//--- input parameters
input int                     MaPeriod =  40;         // MA : Period


double   Buffer1[];
double   Buffer2[];
double   Buffer3[];
double   Buffer4[];
double   Buffer5[];

double   maOpen,maClose,maHigh,maLow;
double   haOpen,haClose,haHigh,haLow;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Buffer1,INDICATOR_DATA);
   SetIndexBuffer(1,Buffer2,INDICATOR_DATA);
   SetIndexBuffer(2,Buffer3,INDICATOR_DATA);
   SetIndexBuffer(3,Buffer4,INDICATOR_DATA);
   SetIndexBuffer(4,Buffer5,INDICATOR_COLOR_INDEX);

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

   IndicatorSetString(INDICATOR_SHORTNAME,"Heiken Ashi Exit");

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.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[])
  {
//---
//--- if the size of price[] is too small
   if(rates_total<MaPeriod)
      return(0); // do not calculate or draw anything

   int i,limit;
//--- if it's the first call
   if(prev_calculated==0)
     {
      //--- set zero values for zero indexes
      Buffer1[0]=0.0;
      Buffer2[0]=0.0;
      Buffer3[0]=0.0;
      Buffer4[0]=0.0;
      limit=1;
     }
   else
      limit = prev_calculated-1;

// the main loop of calsulations
   for(i=limit; i<rates_total; i++)
     {
      maOpen   =  iMA(_Symbol,PERIOD_CURRENT,MaPeriod,i,MODE_EMA,PRICE_OPEN);
      maClose  =  iMA(_Symbol,PERIOD_CURRENT,MaPeriod,i,MODE_EMA,PRICE_CLOSE);
      maHigh   =  iMA(_Symbol,PERIOD_CURRENT,MaPeriod,i,MODE_EMA,PRICE_HIGH);
      maLow    =  iMA(_Symbol,PERIOD_CURRENT,MaPeriod,i,MODE_EMA,PRICE_LOW);

      haOpen   = (Buffer3[i+1] + Buffer4[i+1])/2.0;
      haClose  = (maClose+maHigh+maLow+maOpen)/4.0;
      haHigh   =  MathMax(maHigh,MathMax(haOpen,haClose));
      haLow    =  MathMin(maOpen,MathMin(haOpen,haClose));

      if(haOpen < haClose)
        {
         Buffer1[i]=haLow;
         Buffer2[i]=haHigh;
        }
      else
        {
         Buffer1[i]=haHigh;
         Buffer2[i]=haLow;
        }
      Buffer3[i]=haOpen;
      Buffer4[i]=haClose;

      if(haOpen<haClose)
         Buffer5[i]=0.0;
      else
         Buffer5[i]=1.0;

     }



//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
William Roeder
24772
William Roeder  
      maOpen   =  iMA(_Symbol,PERIOD_CURRENT,MaPeriod,i,MODE_EMA,PRICE_OPEN);
      maClose  =  iMA(_Symbol,PERIOD_CURRENT,MaPeriod,i,MODE_EMA,PRICE_CLOSE);
      maHigh   =  iMA(_Symbol,PERIOD_CURRENT,MaPeriod,i,MODE_EMA,PRICE_HIGH);
      maLow    =  iMA(_Symbol,PERIOD_CURRENT,MaPeriod,i,MODE_EMA,PRICE_LOW);

Perhaps you should read the manual, especially the examples.
   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 (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.03.08
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
          How to call indicators in MQL5 - MQL5 Articles 12 March 2010