Stochastic cross form over bought or oversold regions

 

I have been learning the MQL4 and so far I have made some progress however, I have this challenge I encountered while trying to code the below:

  • "Generally, the area above 80 indicates an overbought region, while the area below 20 is considered an oversold region. A sell signal is given when the oscillator is above the 80 level and then crosses back below 80. Conversely, a buy signal is given when the oscillator is below 20 and then crossed back above 20. 80 and 20 are the most common levels used but can be adjusted as needed".

I tried this code (below), but it does not take all the signals:


   

  bool buy_condition_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 1) < 20 ;

    bool buy_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) > 20 ;


bool sell_condition_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 1) > 80 ;

bool sell_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) < 80 ;





This does not work exactly as the above quote.

Please help me with an alternative code.

Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
When a graphical object is created using the ObjectCreate() function, it's necessary to specify the type of object being created, which can be one of the values of the ENUM_OBJECT enumeration. Further specifications of object properties are possible using functions for working with graphical objects.
 
Chinedu Onuoha :

    bool buy_condition_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 1) < 20 ;

    bool buy_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) > 20 ;


This does not work exactly as the above quote.

Please help me with an alternative code.

Have you misunderstood something? I think that there is no problem in particular.

Please try the following program.

//+------------------------------------------------------------------+
//|                                                TestIndicator.mq4 |
//|                                                    Naguisa Unada |
//|                                                             None |
//+------------------------------------------------------------------+
#property copyright "Naguisa Unada"
#property link      "https://www.mql5.com/en/users/unadajapon/news"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_level1 20
#property indicator_level2 80
//--- plot Line
#property indicator_label1  "Line"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Arrow
#property indicator_label2  "Arrow"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrAqua
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters

//--- indicator buffers
double         Line_Buffer[];
double         Arrow_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
        //--- indicator buffers mapping
        SetIndexBuffer(0, Line_Buffer);
        SetIndexBuffer(1, Arrow_Buffer);
        SetIndexArrow(1, 159);
        //---
        return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
        //---
        int i, limit;
        
        if (prev_calculated == 0)
                limit = rates_total - 11;
        else
                limit = rates_total - prev_calculated;
        
        for (i = limit; i >= 0; i--)
        {
                Line_Buffer[i] = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, i);
                
                bool buy_condition_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, i + 1) < 20 ;
             bool buy_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, i) > 20 ;
        
             if (buy_condition_1 && buy_condition_2)
                   Arrow_Buffer[i] = 20;
        }
        //--- return value of prev_calculated for next call
        return(rates_total);
}
//+------------------------------------------------------------------+
 
Naguisa Unada:

Have you misunderstood something? I think that there is no problem in particular.

Please try the following program.


Thanks I will try this now

 
// Init function

int init(){

    open = 0;

    RealPoint = RealPipPoint(Symbol());

}



// Start function

int start(){

  if (open  == Open[0]) return 0;

  open = Open[0];

  

  //long

   OrderSelect(LongTicket,SELECT_BY_TICKET);

   if(OrderCloseTime() != 0 || LongTicket == 0) {



      bool buy_condition_1 = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1)  >  0  ;

      bool buy_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 1) < 20 ;

      bool buy_condition_3 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) > 20 ;

      

       

      if( buy_condition_1  &&  buy_condition_2  &&  buy_condition_3   ){



          OrderSelect(ShortTicket,SELECT_BY_TICKET);

Naguisa Unada
:

Have you misunderstood something? I think that there is no problem in particular.

Please try the following program.


Sorry if I sound stupid but please I am still learning, this got me more confused especially the i+1.... below is where I got stuck initially 




/

 

Hello,

I am a beginner with programming completely, but myself I would use more than two indicators to place orders. I plan on having 5 to 7 indicators to tell me about the securities I place positions on.

 
Chinedu Onuoha :

Sorry if I sound stupid but please I am still learning, this got me more confused especially the i+1.... below is where I got stuck initially 


I changed 1 to i + 1 and 0 to i to output consecutive data, but it's the same for output one point of data. You have no mistake.

 
Naguisa Unada:

I changed 1 to i + 1 and 0 to i to output consecutive data, but it's the same for output one point of data. You have no mistake.


Ok, but this does not work exactly like this quote:


  • "Generally, the area above 80 indicates an overbought region, while the area below 20 is considered an oversold region. A sell signal is given when the oscillator is above the 80 level and then crosses back below 80. Conversely, a buy signal is given when the oscillator is below 20 and then crossed back above 20. 80 and 20 are the most common levels used but can be adjusted as needed".
It picks some but not all
 
Chinedu Onuoha :

Ok, but this does not work exactly like this quote:


  • "Generally, the area above 80 indicates an overbought region, while the area below 20 is considered an oversold region. A  sell signal  is given when the oscillator is above the 80 level and then crosses back below 80. Conversely, a buy signal is given when the oscillator is below 20 and then crossed back above 20. 80 and 20 are the most common levels used but can be adjusted as needed".
It picks some but not all
It works exactly like that as I proved in my program. If it does not work, there will be other problems. But I do not know.
 
Chinedu Onuoha: below is where I got stuck initially
  1. Don't double post!

              General rules and best pratices of the Forum. - General - MQL5 programming forum
  2. When you post code please use the SRC button! Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  3. // Init function
    
    int init(){...}
    
    
    // Start function
    
    int start(){...
    Start using the new Event Handling Functions.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference

  4. OrderSelect(LongTicket,SELECT_BY_TICKET);
    Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  5. if(OrderCloseTime() != 0 || LongTicket == 0) {
    
    
          bool buy_condition_1 = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1)  >  0  ;
    
          bool buy_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 1) < 20 ;
    
          bool buy_condition_3 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) > 20 ;
    Again, all you are doing is setting variables. Unless you write code that opens orders, n o t h i n g  w i l l   e v e r   h a p p e n.

 

============

Stochastic


The beginning

  1. Stochastic Oscillator, the beginning

After

  1. Stochastic thread.
  2. Stochastic Trader thread 
  3. Multi pair indicators thread
    (Multi pair MACD, Multi pair CoeffOfLine, Multi pair RSi, Multi pair Stochastic, Multi pair WPR (Williams % range), Multi pair Laguerre RSI, Multi pair Wilders DMI, Multi pair dss, and more)
  4. Stochastic RSI for MT5 - the post 
  5. Stochastic rsi (oma) colored or MT5 - the post:
    - RSI types: Cuttler's RSI; Ehlers' smoothed RSI; Harris' RSI; Rapid RSI; RSI; RSX; Slow RSI
    - levels: floating; quantile; fixed
  6. Round price DOC indicator for MT5 - the postAdded 2 parameters : price (so we can chose the price we would like to use - in the original it is Close) and T3Original (false for Fulks/Matulich calculation which is faster then the original Tim Tillson calculation).

============

Reason: