Search code examples
mql5metatrader5

Getting garbage values from iStochastic function MQL5


Here is the code and the output of the Strategy Tester:

void OnInit()
{
    double K[],D[];

    int s  = iStochastic(_Symbol,PERIOD_M1,5,3,3,MODE_SMA,STO_LOWHIGH);
    ArraySetAsSeries(K,true);
    CopyBuffer(s,0,0,15,K);
    for (int i=0; i< ArraySize(K); i++) {
        PrintFormat("K%d:  %f",i,K[i]);
    }
}

The output is here:

2018.03.23 17:52:23.771 EURUSD,M1: testing of Experts\testing lines and trdae.ex5 from 2018.02.01 00:00 to 2018.02.20 00:00 started
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K0:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K1:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K2:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K3:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K4:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K5:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K6:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K7:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K8:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K9:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K10:  -199836181689466826799237394770318879446132938967474237529901448300177121542331695157382317059237724271211152856484244886720180840440527383821209628107006660663428982847878694191497216.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K11:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K12:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K13:  0.000000
2018.03.23 17:52:23.821 2018.02.01 00:00:00   K14:  0.000000

Previously I was getting correct values but now the values are all garbage.


Solution

  • I guess there is nothing coming into your K[] and D[]. Hence you are facing the garbage value. And you are not willing to go with OnTick() instead want to implement with OnInit(), then I guess I can try something and you may debug it.

    void OnInit()
    {
        double K[],D[];
    
        int s  = iStochastic(_Symbol,PERIOD_M1,5,3,3,MODE_SMA,STO_LOWHIGH);
        ArraySetAsSeries(K,true);
        int KBuffer = CopyBuffer(s,0,0,15,K);
        if(KBuffer == -1)
       {
       Sleep(50);
       s  = iStochastic(_Symbol,PERIOD_M1,5,3,3,MODE_SMA,STO_LOWHIGH);
       CopyBuffer(s,0,0,15,K);
       }
        for (int i=0; i< ArraySize(K); i++) {
            PrintFormat("K%d:  %f",i,K[i]);
        }
    }
    

    I guess this is it. If you get the garbage again try increasing the Sleep() milliseconds.