G-channel indicator MT5 conversion

 

Hi all,

I am a beginner and I'm trying to port in MQL5 an indicator I know in Pinescript and MQL4, the G-Channel made by god coder Alex Grover.

I think I've done all I had to, but the indicators doesn't work as it should...

Here is the code:

//+------------------------------------------------------------------+
//|                                                      G Channels |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3
//--- plot Upper
#property indicator_label1  "Upper"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- plot Lower
#property indicator_label3  "Lower"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrLightGreen
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2
//--- plot Average
#property indicator_label2  "Average"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrYellow
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2


//--- input parameters
input int                     Length = 100;
input ENUM_APPLIED_PRICE      PriceType = PRICE_CLOSE;

//--- indicator buffers
double UpperBuffer[];
double LowerBuffer[];
double AverageBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping
  SetIndexBuffer(0, UpperBuffer, INDICATOR_DATA);
  SetIndexBuffer(2, LowerBuffer, INDICATOR_DATA);
  SetIndexBuffer(1, AverageBuffer, INDICATOR_DATA);
  //--- sets the AS_SERIES flag to a selected buffer array
  ArraySetAsSeries(UpperBuffer, true);
  ArraySetAsSeries(LowerBuffer, true);
  ArraySetAsSeries(AverageBuffer, true);

   // Imposta il nome dell'indicatore
   IndicatorSetString(INDICATOR_SHORTNAME, "G Channels");
   //--- set accuracy
  IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
  //--- set first bar from what index will be drawn
  PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Length);
  //--- set drawing line empty value
  PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   //---
   
  // Print( ArrayGetAsSeries(UpperBuffer),"  ",ArrayGetAsSeries(LowerBuffer),"  ",ArrayGetAsSeries(AverageBuffer) );

   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( rates_total <= Length)
    return(0);
 
  //--- how many bars to calculate
  int count = rates_total - prev_calculated - 1;
  if(prev_calculated > 0) count++;
  

   // Calcola i valori di Upper, Average e Lower
   double a = 0;
   double b = 0;
   for (int i = rates_total - 1; i >= 0; i--)
   {
      a = MathMax(PriceType, a) - (a - b) / Length;
      b = MathMin(PriceType, b) + (a - b) / Length;

      UpperBuffer[i] = a;
      LowerBuffer[i] = b;
      AverageBuffer[i] = (a + b) / 2;
      
   }

   //---
   return(rates_total);
}

 //+------------------------------------------------------------------+
double NonZero(double _a, double _b = 0)
  {
   if(_a == 0 || _a == EMPTY_VALUE)
      return _b;
   else
      return _a;
  }
//+------------------------------------------------------------------+

Any help or suggestions?

Thanks

 
This is the originale mql4 code 
Files:
 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
MQL5 forum: Technical Indicators
MQL5 forum: Technical Indicators
  • www.mql5.com
Questions about the development of technical indicators in the MQL5 language
 
Any help?
 
Focus on trading, no need to spend time pretending to be developer
it takes lots of time and its not easy use freelance. Automatic conversion
will do the job in 2 seconds error free with your g channel indicator. 
 

Converting original mql4 files.

  SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);
  SetIndexBuffer(1,LowerBuffer,INDICATOR_DATA);
  SetIndexBuffer(2,MiddleBuffer,INDICATOR_DATA);
  SetIndexBuffer(3,aBuffer,INDICATOR_CALCULATIONS);
  SetIndexBuffer(4,bBuffer,INDICATOR_CALCULATIONS);

  ArraySetAsSeries(UpperBuffer, true);
  ArraySetAsSeries(LowerBuffer, true);
  ArraySetAsSeries(MiddleBuffer, true);
  ArraySetAsSeries(aBuffer, true);
  ArraySetAsSeries(bBuffer, true);

   for(int i = 0; i <= 5; i++)
      PlotIndexSetDouble(i,PLOT_EMPTY_VALUE, 0.0);

   for(int i = 3; i <= 5; i++)
      PlotIndexSetString(i,PLOT_LABEL, "");
double getPrice(ENUM_APPLIED_PRICE tprice, const double &open[], const double &close[], const double &high[], const double &low[], int i)
  {
   ArraySetAsSeries(open, true);
   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);
   ArraySetAsSeries(close, true);
        
   if(i >= 0)
      switch(tprice)
 
Algo Alex:

Hi all,

I am a beginner and I'm trying to port in MQL5 an indicator I know in Pinescript and MQL4, the G-Channel made by god coder Alex Grover.

I think I've done all I had to, but the indicators doesn't work as it should...

Here is the code:

Any help or suggestions?

Thanks

Hi

Try this :

#property copyright "5 minute warm up inc."
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3
//--- plot a
#property indicator_label1  "a"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrLightCoral
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot b
#property indicator_label2  "b"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrMediumSeaGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot baseline
#property indicator_label3  "baseline"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrWhite
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
input int Length=100;//length
input ENUM_APPLIED_PRICE Price=PRICE_CLOSE;//Price

//--- indicator buffers
double         a[];
double         b[];
double         baseline[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,a,INDICATOR_DATA);
   SetIndexBuffer(1,b,INDICATOR_DATA);
   SetIndexBuffer(2,baseline,INDICATOR_DATA);
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);
   ArrayFill(a,0,ArraySize(a),0.0);
   ArrayFill(b,0,ArraySize(b),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[])
  {
//---
  //the open high low close etc are indexed from [0] right most 
  //the index buffers are indexed from [0] left most
  for(int i=prev_calculated;i<rates_total;i++){
  //the previous index in buffers : i-1
    int prev_buffer_index=i-1;
  //is the calculation viable ?
    if(prev_buffer_index>=0){
    double le_price=get_price(Price,open,high,low,close,i);
    a[i]=MathMax(le_price,a[prev_buffer_index])-((a[prev_buffer_index]-b[prev_buffer_index])/((double)Length));
    b[i]=MathMin(le_price,b[prev_buffer_index])+((a[prev_buffer_index]-b[prev_buffer_index])/((double)Length));
    baseline[i]=(a[i]+b[i])/((double)2.00);
    }
  } 
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

double get_price(ENUM_APPLIED_PRICE price,
                 const double &open[],
                 const double &high[],
                 const double &low[],
                 const double &close[],
                 int i){
if(price==PRICE_CLOSE){
  return(close[i]);
  }
else if(price==PRICE_HIGH){
  return(high[i]);
  }
else if(price==PRICE_LOW){
  return(low[i]);
  }
else if(price==PRICE_OPEN){
  return(open[i]);
  }
else if(price==PRICE_MEDIAN){
  return((high[i]+low[i])/((double)2.00));
  }
else if(price==PRICE_TYPICAL){
  return((high[i]+low[i]+close[i])/((double)3.00));
  }
else if(price==PRICE_WEIGHTED){
  return((high[i]+low[i]+close[i]+close[i])/((double)4.00));
  }
return(0.0);
}
 

Thanks @Lorentzos Roussos , it works! 

Now I'm gonna study why it works :-D

 
Algo Alex #:

Thanks @Lorentzos Roussos , it works! 

Now I'm gonna study why it works :-D

Awesome , feel free to axe anything here if you need to

Reason: