Stocastic Oscillator in mql. How to set and use "levels" in code

 

Hello,

 I had been looking through the documentation on the iStochastic and I haven't found what I am looking for, with the info in the documentation it wasn't clear to me how to implement what I want. 

 

Im setting up conditions for which a trade can be opened. One of those conditions is if the main and signal lines cross. I want to add a condition that the stochastic oscillator signal has to be in a certain range as well. I have been experamenting with stochastic levels of 20 and 80. If the main and signal cross AND stochastic level is above 80, then can open trade, for example. 

 

How do I define "levels" for a stochastic oscillator in mql4 code? More specifically, how do I extract or return the actual value of the main or signal line in the oscillator? I tried like this: 

iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0) < 20 //Stochastic Oscillator < fixed value

 But it will still open buy positions even if the line value is above 20.

 

Full Example code:

if(Cross(0, iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0) > iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0)) //signal crosses above main
   && iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0) < 20 //Stochastic Oscillator < fixed value
   )
     {
      RefreshRates();
      price = Ask;
      SL = 20 * myPoint; //Stop Loss = value in points (relative to price)
      TP = 40 * myPoint; //Take Profit = value in points (relative to price)
      if(TimeCurrent() - LastTradeTime < NextOpenTradeAfterBars * PeriodSeconds()) return; //next open trade after time   
      if(IsTradeAllowed())
        {
         ticket = myOrderSend(OP_BUY, price, MM_Size(), "");
         if(ticket <= 0) return;
        }
      else //not autotrading => only send alert
         myAlert("order", "");
      LastTradeTime = TimeCurrent();
      myOrderModifyRel(ticket, 0, TP);
      myOrderModifyRel(ticket, SL, 0);
     }
 
if(Cross(0, iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0) > iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0)) //signal crosses above main
   && iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0) < 20 //Stochastic Oscillator < fixed value
What is it??????
 

Croos will looks like

iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0)>20 && iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1) < =20
 
Vasyl Nosal:
What is it??????

Its conditional code that says if the main "line" of the stochastic crosses the signal "line" AND the value of the signal line is below 20, pretty simple. The if(Cross . . .  means that it is instant, it does not keep evaluating true while the main line is above the signal line. While the signal line value is below 20, if the main line crosses signal line, it will evaluate true, that is what I am going after.

 The code you wrote, "if the signal line is above 20 AND if the signal line is less than or equal to 20". On the second part of your code you put the shift as 1. So the code says if current period stochastic signal line is above 20 AND previous period (one candle ago for example) stochastic signal line is below or equal to 20. That is a cross in regards to a "level", as in the value of the stochastic signal line, that is NOT a cross that would be of particular use in my case, it would be true when the signal line value is above 20 and previous candle was below or equal 20, and I believe this would remain true until the next candle. My code checks when the main line and signal line cross, my code works in that regards. Though I am trying to have it only evaluate true if the main and signal cross(works) AND if the signal line value is above or below a certain level.

I am using

iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0) < 20 

 I wouldnt need to see if it crosses with your code because if it is below 20, it has obviously cross 20, and I want that part of the conditional to remain true while it is above 20, think I just figured it out. It is the "Cross" in my code, ill just turn it into two nested if statements, the first with the if(Cross.. and the second without it so it will evaluate true continuously while signal line is below 20, see if that works.

 Main line crossing the signal line of a stochastic oscillator is pretty much the basis of all stochastic indicator strategy. 

Reason: