Code for short moving averages

 

Please how can I fix the following errors in the below code?

1.'if' expressions are not allowed on a global scope.

2.'}'expressions are not allowed on a global scope.

3.'if'open parentheses expected.

4.'first'-undeclared identifier

5.'}'-not all control paths return a value.


//+------------------------------------------------------------------+

//|                                              First Indicator.mq5 |
//|                                                    Kamau Muchiri |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Kamau Muchiri"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Ind_value
#property indicator_label1  "Ind_value"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      MAPeriod=15;//Average Period
input int  MAShift=0;//Horizontal shift in bars
//--- indicator buffers
double         Ind_valuesBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Ind_valuesBuffer,INDICATOR_DATA);
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,MAPeriod);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---

//--- return value of prev_calculated for next call
   if(rates_total<begin+MAPeriod-1)
      return (0);



   if previous_calculated=0;
  {
   first=begin+MAPeriod-1;
  }
  }
if else
  {
     {
      first=prev_calculated-1;
     }
   for(bar=first; bar<rates_total; bar++)
        {
         Sum=0.0;
         for(iii=0; iii<MAPeriod; iii++)
           {
            Sum=Sum+price[bar-iii];
           }
         SMA=Sum/MAPeriod;
         ind_valuesBuffer[bar]=SMA;
        }
      Alert(...)
     }
  }
//+------------------------------------------------------------------+
Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
  • 2022.11.25
  • www.mql5.com
MQL5: linguagem de estratégias de negociação inseridas no Terminal do Cliente MetaTrader 5. A linguagem permite escrever seus próprios sistemas automáticos de negócios, indicadores técnicos, scripts e bibliotecas de funções
 
  1. 0796214726: Please how can I fix the following errors in the below code?

    Learn to code.

  2.    first=begin+MAPeriod-1;
      }
      }
    if else
      {
    
    What does that bracket close?
  3. Does an “if else” exist? Do you mean else?
 
William Roeder #:
  1. Learn to code.

  2. What does that bracket close?
  3. Does an “if else” exist? Do you mean else?

Thanks!!!!

 
1//+------------------------------------------------------------------+

2//|                                              First Indicator.mq5 |
3//|                                                    Kamau Muchiri |
4//|                                             https://www.mql5.com |
5//+------------------------------------------------------------------+
6#property copyright "Kamau Muchiri"
7#property link      "https://www.mql5.com"
8#property version   "1.00"
9#property indicator_chart_window
10#property indicator_buffers 1
11#property indicator_plots   1
12//--- plot Ind_value
13#property indicator_label1  "Ind_value"
14#property indicator_type1   DRAW_LINE
15#property indicator_color1  clrRed
16#property indicator_style1  STYLE_SOLID
17#property indicator_width1  1
18//--- input parameters
19input int      MAPeriod=15;//Average Period
20input int  MAShift=0;//Horizontal shift in bars
21//--- indicator buffers
22double         Ind_valuesBuffer[];
23//+------------------------------------------------------------------+
24//| Custom indicator initialization function                         |
25//+------------------------------------------------------------------+
26int OnInit()
27  {
28//--- indicator buffers mapping
29   SetIndexBuffer(0,Ind_valuesBuffer,INDICATOR_DATA);
 30  PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,MAPeriod);
31
32//---
33  return(INIT_SUCCEEDED);
34  }
35//+------------------------------------------------------------------+
36//| Custom indicator iteration function                              |
37//+------------------------------------------------------------------+
38
39/+------------------------------------------------------------------+
40//|                                                                  |
41/+------------------------------------------------------------------+
42int OnCalculate(const int rates_total,
 43               const int prev_calculated,
44               const int begin,
45              const double &price[])
 46 {
47//---
48
49//--- return value of prev_calculated for next call
50  if(rates_total<begin+MAPeriod-1)
51      return (0);
52 }
53
54
55  if previous_calculated=0;
 56 {
57   first=begin+MAPeriod-1;
58  }
59  }
60if else
 61 {
 62   {
 63     first=prev_calculated-1;
 64    }
 65  for(bar=first; bar<rates_total; bar++)
 66      {
  67       Sum=0.0;
 68      for(iii=0; iii<MAPeriod; iii++)
 69         {
 70          Sum=Sum+price[bar-iii];
 71          }
 72       SMA=Sum/MAPeriod;
 73        ind_valuesBuffer[bar]=SMA;
  74      }
 75   Alert(...)
 76    }
78//+------------------------------------------------------------------+
 
0796214726 #:

Thanks!!!!

Hi there ?I am thankful! Only 2 errors remaining. Please how should I fix them?

   Line             Error

  Line 52: '}'- not all control paths return a value

   Line 55:  'if' -expressions are not allowed on a global scope

Reason: