Working Code but is not working the way it is intended. Please help!

 

Learning for MQL5 coding for the past two weeks. This is how far I have gotten trying to translate a pine script code. It is working but not giving me the signals as in the TradingView version. What did I do wrong? 

Trying to convert this Pine Script:

//@version=4
//Full credit to AlexGrover:
study("G-Channel Trend Detection", shorttitle="G-Trend", overlay=true)

length = input(100)
src = input(close)

//----
var a = 0.0
var b = 0.0

a := max(src, nz(a[1])) - nz(a[1] - b[1]) / length
b := min(src, nz(b[1])) + nz(a[1] - b[1]) / length

avg = avg(a, b)

//----
crossup = b[1] < close[1] and b > close
crossdn = a[1] < close[1] and a > close
bullish = barssince(crossdn) <= barssince(crossup)

c = bullish ? color.lime : color.red

//plot(a, "Upper", color=color.blue, linewidth=2, transp=50)
//plot(b, "Lower", color=color.blue, linewidth=2, transp=50)

p1 = plot(avg, "Average", color=c, linewidth=1, transp=90)
p2 = plot(close, "Close price", color=c, linewidth=1, transp=100)
fill(p1, p2, color=c, transp=90)

showcross = input(true)

buy_signal = showcross and bullish and not bullish[1] ? avg : na
sell_signal = showcross and not bullish and bullish[1] ? avg : na

plotshape(buy_signal, location=location.absolute, style=shape.labelup, color=color.lime, size=size.tiny, text="Buy", textcolor=#ffffff, transp=0, offset=-1)

plotshape(sell_signal, location=location.absolute, style=shape.labeldown, color=color.red, size=size.tiny, text="Sell", textcolor=#ffffff, transp=0, offset=-1)


This is what I got, and all I care about is the buy/sell signals although the upper, lower, avg, and close are graphed:

#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#property indicator_buffers 5
#property indicator_plots 4

#property indicator_style1 STYLE_SOLID
#property indicator_color1 clrRed
#property indicator_label1 "UpperA"

#property indicator_style2 STYLE_SOLID
#property indicator_color2 clrBlue
#property indicator_label2 "LowerB" // Corrected label name

#property indicator_style3 STYLE_SOLID
#property indicator_color3 clrGreen
#property indicator_label3 "Avg"

#property indicator_style4 STYLE_SOLID
#property indicator_color4 clrGreen
#property indicator_label4 "Close"

#property indicator_chart_window
input int InpLength = 100; // Length

double BufferA[];
double BufferB[];
double BufferAvg[];
double BufferClose[];
double Bufferbullish[];

bool crossup, crossdn, buy_signal, sell_signal;
int countdn, countup, countTrueBarscrossup, countTrueBarscrossdn;

int OnInit()
{

   SetIndexBuffer(0, BufferA);
   PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetString(0, PLOT_LABEL, "Upper");
   ArraySetAsSeries(BufferA, true);

   SetIndexBuffer(1, BufferB);
    PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetString(1, PLOT_LABEL, "Lower");
   ArraySetAsSeries(BufferB, true);

   SetIndexBuffer(2, BufferAvg);
    PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetString(2, PLOT_LABEL, "Average");
   ArraySetAsSeries(BufferAvg, true);
   
      SetIndexBuffer(3, BufferClose);
    PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetString(3, PLOT_LABEL, "Close Price");
   ArraySetAsSeries(BufferClose, true);

      SetIndexBuffer(4, Bufferbullish);
   ArraySetAsSeries(Bufferbullish, true);
  
   return(INIT_SUCCEEDED);
}

/*-------------------------------------------*/
int countTrueBarscrossup() 
        {
         countup = 0;
         int totalBarsCU = Bars(_Symbol, _Period);
         
         for (int i = 0; i < totalBarsCU; i++) {
            if(crossup)  {
               countup++;
                           } 
                                                }
         return countup;
         }

int countTrueBarscrossdn() 
        {
         countdn = 0;
         int totalBarsCD = Bars(_Symbol, _Period);
         
         for (int i = 0; i < totalBarsCD; i++) {
            if(crossdn)  {
               countdn++;
                           } 
                                                }
         return countdn;
         }
/*-------------------------------------------*/
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 <= InpLength)
        return 0;

    if (IsStopped())
        return 0;

    int count = (prev_calculated == 0) ? (rates_total - InpLength) : (rates_total - prev_calculated + 1);

    ArraySetAsSeries(close, true);

    for (int i = count - 1; i >= 0; i--)
    {
        BufferA[i] = MathMax(close[i], BufferA[i + 1]) - (BufferA[i + 1] - BufferB[i + 1]) / InpLength; // Corrected parentheses
        BufferB[i] = MathMin(close[i], BufferB[i + 1]) + (BufferA[i + 1] - BufferB[i + 1]) / InpLength; // Corrected parentheses
        BufferAvg[i] = (BufferA[i] + BufferB[i]) / 2;
        BufferClose[i] = close[i];
        
         crossup = BufferB[i + 1] < BufferClose[i + 1] && BufferB[i] > BufferClose[i];
         crossdn = BufferA[i + 1] < BufferClose[i + 1] && BufferA[i] > BufferClose[i];
         Bufferbullish[i] = countTrueBarscrossdn() <= countTrueBarscrossup();
         
                 if (Bufferbullish[i] && !Bufferbullish[i + 1])
            Print("Buy signal at bar ", i, " with avg = ", BufferAvg[i]);
        else if (!Bufferbullish[i] && Bufferbullish[i + 1])
            Print("Sell signal at bar ", i, " with avg = ", BufferAvg[i]);
     }
    return (rates_total);
}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.04.03
  • 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
 
toddy333: What did I do wrong?

Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
      General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
          Messages Editor
      Forum rules and recommendations - General - MQL5 programming forum (2023)

 
I can't seem to update this post with your suggested changes because I do not have the edit option available. I think this id due to me having a current issue with my mql5 account that I am trying to resolve. 
 
William Roeder #:

Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
      General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
          Messages Editor
      Forum rules and recommendations - General - MQL5 programming forum (2023)

My account is fixed now but still I cannot edit the post. I have looked and looked and I do not see where the edit button is. I have pointed the pointer everywhere and it also doesn't show up. Please help.

Reason: