Jaffer Wilson
Jaffer Wilson

Reputation: 7273

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.

Upvotes: 1

Views: 2130

Answers (2)

There is a simple way to do that.

What you need is to copy the data per minute of previous 15 candles using CopyBuffer.
See the example:

double K[],D[];
ArraySetAsSeries(K,true);
ArraySetAsSeries(D,true);
int stochastic_output = iStochastic(_Symbol,PERIOD_M1,5,3,3,MODE_SMA,STO_LOWHIGH);  
CopyBuffer(stochastic_output,0,0,15,K);
CopyBuffer(stochastic_output,1,0,15,D);
Print("K size:  ",ArraySize(K));
Print("D size:  ",ArraySize(D));

Output of the above:

K Size:  15
D Size:  15

Hope this will help you.

Upvotes: 2

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

//--- 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]);
}

Upvotes: 1

Related Questions