Error 4806

 

Dear Fellows

I know this is very common error and lot of posts on the forum, which I have tried out.

My problem is somehow different in sense that it occurs only when running getRZS() method. In all other calls it does not return any error.

Guide me what could be potential cause.

//+-----------------------------------------------------------------------------------------------------------------------------+
//| CLASS:                              CiVROC.mqh
//+-----------------------------------------------------------------------------------------------------------------------------+
class CiVROC {

public:
          CiVROC() { CiVROC(_Symbol,_Period,gParamVROC); }
          CiVROC(string pSymbol,ENUM_TIMEFRAMES pTimeFrame,SParamVROC &pParamVROC);
         ~CiVROC() {  }

private:
                string                                                          mSymbol;
                ENUM_TIMEFRAMES                         mTimeFrame;
                int                                                                             mDigits;
                int                                                                             mHandleVROC;

public:
                double                                                          getVROC(int pIdx);
                double                                                          getRZS(int pIdx);
                double                                                          logTransformed(int pIdx);

}; // END Of Class Definition
//+-----------------------------------------------------------------------------------------------------------------------------+
//| METHOD:                             CiVROC()
//+-----------------------------------------------------------------------------------------------------------------------------+
CiVROC::CiVROC(string pSymbol,ENUM_TIMEFRAMES pTimeFrame,SParamVROC &pParamVROC) {

                mSymbol          = pSymbol;
                mTimeFrame = pTimeFrame;
          mDigits        = getDigits(mSymbol);

                TesterHideIndicators(true);
                ResetLastError();
                mHandleVROC = iCustom(mSymbol,mTimeFrame,"::" + iVROCPath,pParamVROC.maPeriod,pParamVROC.appliedVolume);
          if(mHandleVROC == INVALID_HANDLE) {
            PrintFormat("%s Error[#%i] creating handle iVROC ",__FUNCTION__,GetLastError());
          }
                TesterHideIndicators(false);

} // END Of method CiVROC()
//+-----------------------------------------------------------------------------------------------------------------------------+
//| METHOD:                     getVROC()
//+-----------------------------------------------------------------------------------------------------------------------------+
double CiVROC::getVROC(int pIdx) {

          double arrayData[1];                                                                                                                                                  int bufferVROC = 0;
                ResetLastError();
          if(CopyBuffer(mHandleVROC,bufferVROC,pIdx,1,arrayData) < 1) {
            PrintFormat("[%s] LogError[%i] getting iVROC at Index[%i]",__FUNCTION__,GetLastError(),pIdx);
            return(0.00);
          }

          return(arrayData[0]);

} // END Of method getVROC()
//+-----------------------------------------------------------------------------------------------------------------------------+
//| METHOD:                     getRZS() | We calculate Robust ZSore of VROC
//+-----------------------------------------------------------------------------------------------------------------------------+
double CiVROC::getRZS(int pIdx) {

                // STEP [1]: Get VROC values for 34 bars from pIdx. Taken a Fib number between 21 and 55.
                int lookBack = 34;
                int start                = lookBack + pIdx;
                int count                = start-pIdx;

                vector vctVROC(1+lookBack);

                // Reverse loop to get VROC values upto pIdx = 1 (CurrBar or Element 0 excluded)
                for(int i = start; i >= pIdx; i--) {
                        vctVROC[count] = getVROC(i);
                        count = count-1;
                }

                // STEP [2]: Calculate median:
    double median = vctVROC.Median();
    
    // STEP [3]: Calculate Median Absolute Deviation (MAD)
    vector vctABSDeviation = MathAbs(vctVROC - median);
    double MAD = vctABSDeviation.Median();

    // STEP [4]: Calculate Robust Z-Score only for the pIdx bar
    const double c = 1.4826; // Scaling constant for normal distribution
    double robustZS = (vctVROC[0] - median) / (c * (MAD == 0 ? 1 : MAD)); // Prevent division by zero

                return(NormalizeDouble(robustZS,6));


} // END Of method getRZS()
//+-----------------------------------------------------------------------------------------------------------------------------+
//| METHOD:                     logTransformed()
//+-----------------------------------------------------------------------------------------------------------------------------+
double CiVROC::logTransformed(int pIdx) {

                double eps = 1e-8; // Use a small positive value for stability

                double VROC    = getVROC(pIdx);
                double absVal  = MathMax(MathAbs(VROC), eps);
                double signVal = (VROC >= 0) ? 1.0 : -1.0;

                return(NormalizeDouble(signVal * MathLog1p(absVal),6));

} // End of logTransformed()
//+-----------------------------------------------------------------------------------------------------------------------------+

Regards

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Anil Varma:

Dear Fellows

I know this is very common error and lot of posts on the forum, which I have tried out.

My problem is somehow different in sense that it occurs only when running getRZS() method. In all other calls it does not return any error.

Guide me what could be potential cause.

Regards


Most probable cause is the instantiation of the indicator either hasn't been done or has failed.

I suggest to initialize the handle variable with INVALID_HANDLE and include a check to see if it was initialized before usage.
 
You can also check if the indicator returns some bars with BarsCalculated.
if the return is -1 no chance of escaping 4806
 
Dominik Egert #:

Most probable cause is the instantiation of the indicator either hasn't been done or has failed.

I suggest to initialize the handle variable with INVALID_HANDLE and include a check to see if it was initialized before usage.

Thanks Dominik

I will try your suggestion.

 
Gerard William G J B M Dinh Sy #:
You can also check if the indicator returns some bars with BarsCalculated.
if the return is -1 no chance of escaping 4806

Thanks GWilliam

I will try your suggestion.

 
Anil Varma # :

Thanks GWilliam

I will try your suggestion.

It was nothing
beware of copybuffer()
I didn't want to pass any codes, so these are "my words" but this function is very capricious