z score mtf script errors

zillehusnain  

can someone plz help fix problems in below 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                              |

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





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

//| 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(StringToPeriod(timeframes[j])))

            {

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

                continue;

            }

            //--- calculate ZScore value

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

            if(zscore == 0.0)

            {

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

                continue;

            }

            //--- output result

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

        }

    }

}



//--- function to convert string timeframe to a period enumeration value

int StringToPeriod(string timeframe)

{

    if(timeframe == "M1") return PERIOD_M1;

    if(timeframe == "M5") return PERIOD_M5;

    if(timeframe == "M15") return PERIOD_M15;

    if(timeframe == "M30") return PERIOD_M30;

    if(timeframe == "H1") return PERIOD_H1;

    if(timeframe == "H4") return PERIOD_H4;

    if(timeframe == "D1") return PERIOD_D1;

    if(timeframe == "W1") return PERIOD_W1;

    if(timeframe == "MN1") return PERIOD_MN1;

    return 0;

}


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.03.14
  • 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
Sergey Golubev  

Please edit your post to use the CODE button (Alt-S)!

Use the CODE button

Reason: