# 4-Hour Stochastic values at 15min marks

25

Hi all,

I have done a search but I have not gotten an answer to my queries and therefore am writing this to seek help.

This is what I am trying to do:

I want to obtain the stochastic value from a 4-Hour chart in an EA that runs on a 15min chart. I understand the following code executes what I want, however, it only returns the current minute stochastic value.

`iStochastic(NULL,PERIOD_H4,fast,3,3,MODE_SMA,0,MODE_MAIN,0);`

What if say I want to obtain the stochastic value from a period of 4-Hour that was 15 minutes ago. Below is an example:

1) Time now is 3.20pm and iStochastic(NULL,PERIOD_H4,fast,3,3,MODE_SMA,0,MODE_MAIN,0) gives me a value of 82.222
2) I want the stochastic value of the period 4-Hour at time 3.05, which has a value say 83.333
3) Doing iStochastic(NULL,PERIOD_H4,fast,3,3,MODE_SMA,0,MODE_MAIN,1) however gives me the value that was from the last 4-Hour candle, which has a value say 84.444

Can anyone advise what can I do? I would be so grateful if any kind souls could help. Many many thanks in advance, cheers!

Moderator
18019

Hi all,

I have done a search but I have not gotten an answer to my queries and therefore am writing this to seek help.

This is what I am trying to do:

I want to obtain the stochastic value from a 4-Hour chart in an EA that runs on a 15min chart. I understand the following code executes what I want, however, it only returns the current minute stochastic value.

What if say I want to obtain the stochastic value from a period of 4-Hour that was 15 minutes ago. Below is an example:

You can't unless you add up all the M15 Bars that make your pseudo H4 bars and then do the Stochastic calculation on that data . . . standard H4 bars have set boundaries, they start at the start of the hour and last 4 hours, they never start at 15 minute boundaries such as 30 minutes past the hour.
25

Thanks RaptorUK for the prompt response.

The method that you mentioned about adding all the M15 bars, is that the only way to do it or are there alternatives? If so, am I to get the high/low values of a specific time range, add them up and divide by the number of bars to get the value? And how do I add 2 values, both high/low to get a single stochastic value?

As for the code iStochastic(NULL,PERIOD_H4,fast,3,3,MODE_SMA,0,MODE_MAIN,0), it does give me per minute value, is it not possible for other time?

So let's say I want to get the H4 stochastic cross over of 20/80 on a M15 chart for the current H4 line.
Because the H4 line is not completed as the hour is not at say 0400hrs, however the earlier part of H4 was above 80 line and the current H4 value is below, and I would like to go into a trade with that, how can I go about doing this?

Else if I store the value of the H4 stochastic that was 15 minutes ago in an array, will the array be deleted later as my script runs on a M15 chart?

If you have other suggestion, please let me know too. Thanks so much once again!

Moderator
18019

Thanks RaptorUK for the prompt response.

The method that you mentioned about adding all the M15 bars, is that the only way to do it or are there alternatives? If so, am I to get the high/low values of a specific time range, add them up and divide by the number of bars to get the value? And how do I add 2 values, both high/low to get a single stochastic value?

You have to figure out how to solve this issue and code it.

As for the code iStochastic(NULL,PERIOD_H4,fast,3,3,MODE_SMA,0,MODE_MAIN,0), it does give me per minute value, is it not possible for other time?

No it doesn't, it gives you the Stoch for the currently forming H4 bar, that bar is forming so is changing so it's fair to assume that the Stoch will change as the bar forms.
15335

1. If you want to open when the H4 stochastic crosses the 20/80, do it. Why are you waiting 15 minutes later? Why do you need the old value - it didn't cross then or you would have opened then.
2. "What if say I want to obtain the stochastic value from a period of 4-Hour that was 15 minutes ago. at time 3.05" Can't, mt4 doesn't have tick history, remember it per bar, 3.00, 3.15...
3. If you must wait, remember the previous value
```static datetime Time0; bool isNewBar = Time0 != Time[0];
if(isNewBar){ Time0 != Time[0];
static double stochH4Prev, stochH4Curr;
stochH4Prev = stochH4Curr; stochH4Curr = iStoch(NULL,PERIOD_H4,..., 0);```
4. the array will deleted later if you delete it.
5. If you need multiple values, you'll have to remember them (ResizeBuffer from my code)
Not compiled, notTested
```datetime last_h4;    void  OnInitH4(){ last_h4=0;  }
double   stoch_h4[]; void  OnTickH4(){
ResizeBuffer(stoch_h4, Bars);
for(int iBar = iBarShift(NULL,0, last_h4);   iBar >= 0; iBar--){
int iH4  = iBarShift(NULL,PERIOD_H4, Time[iBar]);
stoch_h4[iBar] = iStochastic(NULL, PERIOD_H4, ..., iH4);
}
last_H4 = Time[0];
}
////////////////////////////////////////////////////////////////////////////////
int init(){    OnInitH4(); ... }
int start(){   OnTickH4(); Comment("h4 stoch last bar was ", stoch_h4[1]); ...
////////////////////////////////////////////////////////////////////////////////
bool     ResizeBuffer(double& buffer[], int size){ // buffer can be 1D or 2D
if(ArrayRange(buffer,0) != size){      // ArrayRange allows 1D or 2D arrays
ArraySetAsSeries(buffer, false);    // Shift values B[2]=B[1]; B[1]=B[0]
if( !MyArrayResize(buffer, size, "ResizeBuffer") ) return(false);
ArraySetAsSeries(buffer, true);
}
return(true);
}```
Not compiled, notTested
25

Thanks both RaptorUK and WHRoeder for prompt response once again.

RaptorUK: No it doesn't, it gives you the Stoch for the currently forming H4 bar, that bar is forming so is changing so it's fair to assume that the Stoch will change as the bar forms.

Yes that's what I need exactly, the current minute value of the H4 bar that is forming.

WHRoeder:

1. I run my EA over on a M15 chart, checked for 1 trade per bar. I actually also have other indicators running on the M15 which is why I require, best if, the H4 values on M15 chart.
Also even if I didn't need the M15 chart, there was still the problem of getting values of a H4 stochastic from the previous 15minutes, or whatever minutes before since the iStochastic method only returns the current value and the previous closed value.
But correct me if I am wrong because I have always run my EA on M1 chart until recently.

2. I have the impression how MQL4 works is that the EA is run every 15 minutes given the code static datetime Time0; bool isNewBar = Time0 != Time[0];, given that my chart is a M15 chart. This means all variables created previously will be destroyed and recreated each time the EA is run again at the 15th minute mark. But am I mistaken to believe that? Or is it that although the EA executes every 15 minutes, variables and values are stored locally and can be retrieved later.

4. I will try out this piece of code you have given me, many thanks! You have my heartfelt gratitude!

Thanks once again! And will post back the result later.

Edit: Thanks WHRoeder! I think your code worked brilliantly! Looking at the journal from backtesting, it is now printing the right H4 stoch values. Will continue to monitor to see if there are any other problems but thanks so much!! Not sure how do I +rep for you in here.

15335

1) There is NO H4 stochastic from the previous 15 minutes or the previous M15 bar. It is gone. YOU must remember it.

2) The EA is run EVERY TICK, not every 15 minutes. The if(isNewBar){ runs at the start of a new bar.} Non-global/static "variables are destroyed and recreated each time"

25

Thanks WHRoeder.

Now I fully understand how it works. Been making mistake for the longest time, thanks so much.

In the end I had some issues with the code. I have written my own for those who want to achieve the same as me:

```void  OnTickH4(){
for (int xx = 14; xx >= 0; xx--)
{
if (xx == 0)
{
main_4Hlast[xx] = iStochastic(NULL,PERIOD_H4,fast,3,3,MODE_SMA,0,MODE_MAIN,0);
}
else
main_4Hlast[xx] = main_4Hlast[xx-1];
}
}```

My array size always remain the same so I do not have to always resize because I only require the last 15 results.