Search code examples
mql5metatrader5

MQL5 calculate the Stochastic value for previous 15 candle or 15 minutes


I am trying to access the previous values or the stochastic of the chart in MQL5. But I only know how to calculate for the current.

What I am trying to do is:

int stochastic_output = iStochastic(_Symbol,PERIOD_M1,5,3,3,MODE_SMA,STO_LOWHIGH);  

But I don't know how I can get the value of the previous 15 candles or the previous 3 minutes. Kindly, help me how to get it.


Solution

  • //--- inputs
    input int Candles=15;
    input int NeededCandle=3;
    // --- global variables
    int stoch_handle;
    
    int OnInit(){
        stoch_handle=iStochastic(_Symbol,PERIOD_M1,5,3,3,MODE_SMA,STO_LOWHIGH);
        if(stoch_handle==INVALID_HANDLE)
            return(INIT_FAILED);
    }
    
    void OnTick(){
        double main[],signal[];
        ArrayResize(main,Candles);
        ArraySetAsSeries(main,true);
        ArrayResize(signal,Candles);
        ArraySetAsSeries(signal,true);
        if(CopyBuffer(stoch_handle,MAIN_LINE,0,Candles,main)!=Candles)
            return;
        if(CopyBuffer(stoch_handle,SIGNAL_LINE,0,Candles,signal)!=Candles)
            return;
        printf("%i - main=%.2f, signal=%.2f",__LINE__,main[NeededCandle-1],signal[NeededCandle-1]);
    }