can any one fix problems in below code. its actually a multi time frame mql5 z-score scanner for multi pairs and time frames

 
here are problems that compiler shows '{' - function definition unexpected z-score-script.mq5 @Line no. 151 'for' - expressions are not allowed on a global scope z-score-script.mq5 @Line no. 156 '}' - expressions are not allowed on a global scope z-score-script.mq5 @Line no. 182 OnStart function defined in the non-script program z-score-script.mq5 @Line no. 140 'IndicatorInitialize' - undeclared identifier z-score-script.mq5 @Line no. 148 ',' - unexpected token z-score-script.mq5 148 'NULL' - some operator expected z-score-script.mq5 @Line no. 148 '(' - unbalanced left parenthesis z-score-script.mq5 @Line no. 148 ')' - unexpected token z-score-script.mq5 @Line no. 148 expression has no effect z-score-script.mq5 @Line no. 148 ')' - unexpected token z-score-script.mq5 @Line no. 148 Below is complete code....
here are problems that compiler shows

'{' - function definition unexpected    z-score-script.mq5      @Line no. 151   
'for' - expressions are not allowed on a global scope   z-score-script.mq5    @Line no. 156     
'}' - expressions are not allowed on a global scope     z-score-script.mq5      @Line no. 182   
OnStart function defined in the non-script program      z-score-script.mq5      @Line no. 140   
'IndicatorInitialize' - undeclared identifier   z-score-script.mq5      @Line no. 148   
',' - unexpected token  z-score-script.mq5      148     
'NULL' - some operator expected z-score-script.mq5      @Line no. 148   
'(' - unbalanced left parenthesis       z-score-script.mq5      @Line no. 148   
')' - unexpected token  z-score-script.mq5      @Line no. 148   
expression has no effect        z-score-script.mq5      @Line no. 148   
')' - unexpected token  z-score-script.mq5      @Line no. 148   
        

Below is complete code....

```
//+------------------------------------------------------------------+
//|                                                ZScoreScanner.mq5 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                                 https://mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   1

//--- plot ZScore
#property indicator_label1  "ZScore"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- input parameters
input uint                 InpPeriod         =  20;            // Period of calculation
input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // Applied price
input string               InpCurrencyPairs  = "EURUSD,GBPUSD,USDJPY,AUDUSD,NZDUSD,USDCAD,USDCHF"; // List of currency pairs to scan
input string               InpTimeframes     = "M15,H1,H4,D1"; // List of timeframes to scan

//--- indicator buffers
double         BufferZScore[];
double         BufferDev[];
double         BufferMA1[];
double         BufferMA2[];

//--- global variables
int            period;
int            handle_dev;
int            handle_ma1;
int            handle_ma2;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    //--- setting global variables
    period = int(InpPeriod < 1 ? 1 : InpPeriod);

    //--- indicator buffers mapping
    SetIndexBuffer(0, BufferZScore, INDICATOR_DATA);
    SetIndexBuffer(1, BufferDev, INDICATOR_CALCULATIONS);
    SetIndexBuffer(2, BufferMA1, INDICATOR_CALCULATIONS);
    SetIndexBuffer(3, BufferMA2, INDICATOR_CALCULATIONS);

    //--- settings indicators parameters
    IndicatorSetInteger(INDICATOR_DIGITS, Digits());
    IndicatorSetString(INDICATOR_SHORTNAME, "ZScore("+(string)period+")");

    //--- setting buffer arrays as timeseries
    ArraySetAsSeries(BufferZScore, true);
    ArraySetAsSeries(BufferDev, true);
    ArraySetAsSeries(BufferMA1, true);
    ArraySetAsSeries(BufferMA2, true);

    //--- creating handles StdDev, MA1, MA2
    handle_dev = iStdDev(Symbol(), PERIOD_CURRENT, period, 0, MODE_SMA, InpAppliedPrice);
    if(handle_dev == INVALID_HANDLE)
    {
        Print("Error creating StdDev's handle: ", GetLastError());
        return INIT_FAILED;
    }

    handle_ma1 = iMA(Symbol(), PERIOD_CURRENT, 1, 0, MODE_SMA, InpAppliedPrice);
    if(handle_ma1 == INVALID_HANDLE)
    {
        Print("Error creating MA(1)'s handle: ", GetLastError());
        return INIT_FAILED;
    }

    handle_ma2 = iMA(Symbol(), PERIOD_CURRENT, period, 0, MODE_SMA, InpAppliedPrice);
    if(handle_ma2 == INVALID_HANDLE)
    {
        Print("Error creating MA(2)'s handle: ", GetLastError());
        return INIT_FAILED;
    }

    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<period) return 0;
//--- Проверка и расчёт количества просчитываемых баров
   int limit=rates_total-prev_calculated;
   if(limit>1)
     {
      limit=rates_total-1;
      ArrayInitialize(BufferZScore,EMPTY_VALUE);
      ArrayInitialize(BufferDev,EMPTY_VALUE);
      ArrayInitialize(BufferMA1,EMPTY_VALUE);
      ArrayInitialize(BufferMA2,EMPTY_VALUE);
     }
//--- Подготовка данных
   int copied=(limit>1 ? rates_total : 1);
   int copied_dev=CopyBuffer(handle_dev,0,0,copied,BufferDev);
   if(copied_dev!=copied)
      return 0;
   int copied_ma1=CopyBuffer(handle_ma1,0,0,copied,BufferMA1);
   if(copied_ma1!=copied)
      return 0;
   int copied_ma2=CopyBuffer(handle_ma2,0,0,copied,BufferMA2);
   if(copied_ma2!=copied)
      return 0;
//--- Расчёт индикатора
   for(int i=limit; i>=0; i--)
     {
      double dev=BufferDev[i];
      BufferZScore[i]=(BufferMA1[i]-BufferMA2[i])/(dev>0 ? dev : DBL_MIN);
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| Script initialization function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
    //--- parse input parameters
    string pairs[];
    StringSplit(InpCurrencyPairs, ',', pairs);
    string timeframes[];
    StringSplit(InpTimeframes, ',', timeframes);
    //--- initialize indicator
    if (!IndicatorInitialize (NULL, 0))
    
}
    {
        Print("Error initializing indicator: ", GetLastError());
        return;
    }
    //--- loop through currency pairs and timeframes
    for(int i = 0; i < ArraySize(pairs); i++)
    {
        for(int j = 0; j < ArraySize(timeframes); j++)
        {
            //--- switch chart to currency pair and timeframe
            if(!SymbolSelect(pairs[i], true))
            {
                Print("Error selecting symbol: ", GetLastError());
                continue;
            }
            if(!PeriodSet(STRTOPERIOD(timeframes[j])))
            {
                Print("Error setting period: ", GetLastError());
                continue;
            }
            //--- calculate ZScore value
            double zscore = iCustom(NULL, 0, "ZScoreScanner", InpPeriod, InpAppliedPrice);
            if(zscore == 0)
            {
                Print("Error calculating ZScore: ", GetLastError());
                continue;
            }
            //--- output result
            Print(pairs[i], ", ", timeframes[j], ": ", zscore);
        }
    }
}

```
 
zillehusnain:
here are problems that compiler shows '{' - function definition unexpected z-score-script.mq5 @Line no. 151 'for' - expressions are not allowed on a global scope z-score-script.mq5 @Line no. 156 '}' - expressions are not allowed on a global scope z-score-script.mq5 @Line no. 182 OnStart function defined in the non-script program z-score-script.mq5 @Line no. 140 'IndicatorInitialize' - undeclared identifier z-score-script.mq5 @Line no. 148 ',' - unexpected token z-score-script.mq5 148 'NULL' - some operator expected z-score-script.mq5 @Line no. 148 '(' - unbalanced left parenthesis z-score-script.mq5 @Line no. 148 ')' - unexpected token z-score-script.mq5 @Line no. 148 expression has no effect z-score-script.mq5 @Line no. 148 ')' - unexpected token z-score-script.mq5 @Line no. 148 Below is complete code....

An indicator may not have OnStart function and a script may not have  OnCalculate function

Your code needs to be seperated by either indicator or script or should be coded in two seperate files each

 
Arpit T #:

An indicator may not have OnStart function and a script may not have  OnCalculate function

Your code needs to be seperated by either indicator or script or should be coded in two seperate files each

@Arpit T Thanks for your reply. can you do for me yourself the suggested changes? as i am a layman in coding.

can any one fix problems in below code. its actually a multi time frame mql5 z-score scanner for multi pairs and time frames
can any one fix problems in below code. its actually a multi time frame mql5 z-score scanner for multi pairs and time frames
  • 2023.03.15
  • www.mql5.com
here are problems that compiler shows '{' - function definition unexpected z-score-script.mq5 @Line no...
 
zillehusnain #:

@Arpit T Thanks for your reply. can you do for me yourself the suggested changes? as i am a layman in coding.

ofcourse here is indicator code, i just removed few lines

//+------------------------------------------------------------------+
//|                                                ZScoreScanner.mq5 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                                 https://mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   1

//--- plot ZScore
#property indicator_label1  "ZScore"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- input parameters
input uint                 InpPeriod         =  20;            // Period of calculation
input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // Applied price
input string               InpCurrencyPairs  = "EURUSD,GBPUSD,USDJPY,AUDUSD,NZDUSD,USDCAD,USDCHF"; // List of currency pairs to scan
input string               InpTimeframes     = "M15,H1,H4,D1"; // List of timeframes to scan

//--- indicator buffers
double         BufferZScore[];
double         BufferDev[];
double         BufferMA1[];
double         BufferMA2[];

//--- global variables
int            period;
int            handle_dev;
int            handle_ma1;
int            handle_ma2;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    //--- setting global variables
    period = int(InpPeriod < 1 ? 1 : InpPeriod);

    //--- indicator buffers mapping
    SetIndexBuffer(0, BufferZScore, INDICATOR_DATA);
    SetIndexBuffer(1, BufferDev, INDICATOR_CALCULATIONS);
    SetIndexBuffer(2, BufferMA1, INDICATOR_CALCULATIONS);
    SetIndexBuffer(3, BufferMA2, INDICATOR_CALCULATIONS);

    //--- settings indicators parameters
    IndicatorSetInteger(INDICATOR_DIGITS, Digits());
    IndicatorSetString(INDICATOR_SHORTNAME, "ZScore("+(string)period+")");

    //--- setting buffer arrays as timeseries
    ArraySetAsSeries(BufferZScore, true);
    ArraySetAsSeries(BufferDev, true);
    ArraySetAsSeries(BufferMA1, true);
    ArraySetAsSeries(BufferMA2, true);

    //--- creating handles StdDev, MA1, MA2
    handle_dev = iStdDev(Symbol(), PERIOD_CURRENT, period, 0, MODE_SMA, InpAppliedPrice);
    if(handle_dev == INVALID_HANDLE)
    {
        Print("Error creating StdDev's handle: ", GetLastError());
        return INIT_FAILED;
    }

    handle_ma1 = iMA(Symbol(), PERIOD_CURRENT, 1, 0, MODE_SMA, InpAppliedPrice);
    if(handle_ma1 == INVALID_HANDLE)
    {
        Print("Error creating MA(1)'s handle: ", GetLastError());
        return INIT_FAILED;
    }

    handle_ma2 = iMA(Symbol(), PERIOD_CURRENT, period, 0, MODE_SMA, InpAppliedPrice);
    if(handle_ma2 == INVALID_HANDLE)
    {
        Print("Error creating MA(2)'s handle: ", GetLastError());
        return INIT_FAILED;
    }

    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<period) return 0;
//--- Проверка и расчёт количества просчитываемых баров
   int limit=rates_total-prev_calculated;
   if(limit>1)
     {
      limit=rates_total-1;
      ArrayInitialize(BufferZScore,EMPTY_VALUE);
      ArrayInitialize(BufferDev,EMPTY_VALUE);
      ArrayInitialize(BufferMA1,EMPTY_VALUE);
      ArrayInitialize(BufferMA2,EMPTY_VALUE);
     }
//--- Подготовка данных
   int copied=(limit>1 ? rates_total : 1);
   int copied_dev=CopyBuffer(handle_dev,0,0,copied,BufferDev);
   if(copied_dev!=copied)
      return 0;
   int copied_ma1=CopyBuffer(handle_ma1,0,0,copied,BufferMA1);
   if(copied_ma1!=copied)
      return 0;
   int copied_ma2=CopyBuffer(handle_ma2,0,0,copied,BufferMA2);
   if(copied_ma2!=copied)
      return 0;
//--- Расчёт индикатора
   for(int i=limit; i>=0; i--)
     {
      double dev=BufferDev[i];
      BufferZScore[i]=(BufferMA1[i]-BufferMA2[i])/(dev>0 ? dev : DBL_MIN);
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Its not possible to fix script because a function IndicatorInitialize is missing
 
Arpit T #:

ofcourse here is indicator code, i just removed few lines

@ Arpit T thanks for quick reply for for your help. God bless you. Amen
 
zillehusnain #:
@ Arpit T thanks for quick reply for for your help. God bless you. Amen
Arpit T #:

ofcourse here is indicator code, i just removed few lines

Its not possible to fix script because a function IndicatorInitialize is missing

I have written script code with the help of chat gpt but it has below errors. can you fix it please?

'}' - unexpected end of program z-score-script.mq5 @ line 43

'{' - unbalanced parentheses z-score-script.mq5 @ line 14


Below is the Script code with  IndicatorInitialize ....

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

//| Custom indicator initialization function |

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

int IndicatorInitialize()

{

//--- do initialization here

return 0; // return 0 on success, -1 on failure

}


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

//| Script start function |

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

void OnStart()

{

   {

//--- check if initialization was successful

if (OnInit() != INIT_SUCCEEDED) {

return;

         }


//--- loop through currency pairs and timeframes

for(int i = 0; i < ArraySize(pairs); i++) {

    for(int j = 0; j < ArraySize(timeframes); j++) {

        //--- switch chart to currency pair and timeframe

        if(!SymbolSelect(pairs[i], true)) {

            Print("Error selecting symbol: ", GetLastError());

            continue;

        }

        if(!PeriodSet(STRTOPERIOD(timeframes[j]))) {

            Print("Error setting period: ", GetLastError());

            continue;

        }

        //--- calculate ZScore value

        double zscore = iCustom(NULL, 0, "ZScoreScanner", InpPeriod, InpAppliedPrice);

        if(zscore == 0) {

            Print("Error calculating ZScore: ", GetLastError());

            continue;

        }

        //--- output result

        Print(pairs[i], ", ", timeframes[j], ": ", zscore);

      }

   }

}

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int IndicatorInitialize()
{
//--- do initialization here
return 0; // return 0 on success, -1 on failure
}

//+------------------------------------------------------------------+
//| Script start function |
//+------------------------------------------------------------------+
void OnStart()
{
   {
//--- check if initialization was successful
if (OnInit() != INIT_SUCCEEDED) {
return;
         }

//--- loop through currency pairs and timeframes
for(int i = 0; i < ArraySize(pairs); i++) {
    for(int j = 0; j < ArraySize(timeframes); j++) {
        //--- switch chart to currency pair and timeframe
        if(!SymbolSelect(pairs[i], true)) {
            Print("Error selecting symbol: ", GetLastError());
            continue;
        }
        if(!PeriodSet(STRTOPERIOD(timeframes[j]))) {
            Print("Error setting period: ", GetLastError());
            continue;
        }
        //--- calculate ZScore value
        double zscore = iCustom(NULL, 0, "ZScoreScanner", InpPeriod, InpAppliedPrice);
        if(zscore == 0) {
            Print("Error calculating ZScore: ", GetLastError());
            continue;
        }
        //--- output result
        Print(pairs[i], ", ", timeframes[j], ": ", zscore);
      }
   }
}




 
zillehusnain: here are problems that compiler shows '{' - function definition unexpected
void OnStart()
{                                                 <<<< Start of OnStart
    //--- parse input parameters
    string pairs[];
    StringSplit(InpCurrencyPairs, ',', pairs);
    string timeframes[];
    StringSplit(InpTimeframes, ',', timeframes);
    //--- initialize indicator
    if (!IndicatorInitialize (NULL, 0))
                                                  <<<< Where is the rest of 'if' code?
}                                                 <<<< End of OnStart
    {                                             <<<< This code outside any function?
        Print("Error initializing indicator: ", GetLastError());
        return;
    }
    //--- loop through currency pairs and timeframes
    for(int i = 0; i < ArraySize(pairs); i++)
Reason: