How can I compare the value of the main line and the signal line in MQL5..?

 

thank you guys


I want to compare the value of the main line and the signal line in MQL5.

i was doing like this in MQL4:-

     if((iStochastic(NULL,0,5,3,3,MODE_SMA,1,MODE_MAIN,1)<iStochastic(NULL,0,5,3,3,MODE_SMA,1,MODE_SIGNAL,1))&&(iStochastic(NULL,0,5,3,3,MODE_SMA,1,MODE_MAIN,0)>iStochastic(NULL,0,5,3,3,MODE_SMA,1,MODE_SIGNAL,0))&&(iStochastic(NULL,0,5,3,3,MODE_SMA,1,MODE_MAIN,0)>20)) // Here is your open buy rule

    how can i do that command in MQL5....?


Thank you agine..

 

What did you try ?

 
Alain Verleyen:

What did you try ?

Indicator is working, there is no problem.

But I want to extract the value of the main line and the value of the signal line for use them in comparison operations, and on the outcome of that comparison the expert will perform the trades.

Thank you for your reply...

//---------- Stochastic Property Eare ------------
#property indicator_separate_window 
#property indicator_buffers 2 
#property indicator_plots   2 
//--- the Main plot 
#property indicator_label1  "Stochastic_Main" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- the Signal plot 
#property indicator_label2  "Stochastic_Signal" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrRed 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//--- set limit of the indicator values 
#property indicator_minimum 0 
#property indicator_maximum 100 
//--- horizontal levels in the indicator window 
#property indicator_level1  -100.0 
#property indicator_level2  20.0 
#property indicator_level3  50.0 
#property indicator_level4  80.0 
#property indicator_level5  100.0 

//--------------------------------------------------------------------
enum Creation_iStochastic
  { 
   Call_iStochastic,       // use iStochastic 
   Call_IndicatorCreate    // use IndicatorCreate 
  }; 
//--- input iStochastic parameters Eare  ------------
input Creation_iStochastic isto_type         =   Call_iStochastic;     // iStochastic type of the function  
input int                  isto_Kperiod      =   5;                 // iStochastic the K period (the number of bars for calculation) 
input int                  isto_Dperiod      =   3;                 // iStochastic the D period (the period of primary smoothing) 
input int                  isto_slowing      =   3;                 // iStochastic period of final smoothing       
input ENUM_MA_METHOD       isto_ma_method    =   MODE_SMA;        // iStochastic type of smoothing    
input ENUM_STO_PRICE       isto_price_field  =   STO_LOWHIGH;   // iStochastic method of calculation of the Stochastic 
//input string               isto_symbol       =   " ";                // iStochastic symbol  
input ENUM_TIMEFRAMES      isto_period       =   PERIOD_CURRENT;     // iStochastic timeframe 
//--- iStochastic Variables Eare  ------------
double  isto_main_line_buffer[];                   // Main Line buffers
double  isto_signal_line_buffer[];                // Signal Line buffers
int     isto_handle;                          // Variable for storing the handle of the iStochastic indicator  
int     isto_main_line;
int     isto_signal_line;
int     isto_bars_calculated = 0;        // We will keep the number of values in the Stochastic Oscillator indicator
string  isto_name            =   Symbol(); 
string  isto_short_name;                      // name of the indicator on a chart 
//--------------------------------------------------------------------
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
    Run_istochastic();
    printf("istochastic is running ");
   return(INIT_SUCCEEDED);
  }

//--------------------------------------------------------------------


int Run_istochastic()
 {
    
    //--- assignment of arrays to indicator buffers 
   SetIndexBuffer(0,isto_main_line_buffer,INDICATOR_DATA); 
   SetIndexBuffer(1,isto_signal_line_buffer,INDICATOR_DATA); 
   
   if(isto_type == Call_iStochastic)
    
    isto_handle = iStochastic(Symbol(),isto_period,isto_Kperiod,isto_Dperiod,isto_slowing,isto_ma_method,isto_price_field);
     else 
     { 
      //--- fill the structure with parameters of the indicator      
      MqlParam pars[5]; 
      //--- the K period for calculations 
      pars[0].type = TYPE_INT; 
      pars[0].integer_value = isto_Kperiod; 
      //--- the D period for primary smoothing 
      pars[1].type = TYPE_INT; 
      pars[1].integer_value = isto_Dperiod; 
      //--- the K period for final smoothing 
      pars[2].type = TYPE_INT; 
      pars[2].integer_value = isto_slowing; 
      //--- type of smoothing 
      pars[3].type = TYPE_INT; 
      pars[3].integer_value = isto_ma_method; 
      //--- method of calculation of the Stochastic 
      pars[4].type = TYPE_INT; 
      pars[4].integer_value = isto_price_field;
       
      isto_handle = IndicatorCreate(isto_name,isto_period,IND_STOCHASTIC,5,pars); 
     } 
//--- if the handle is not created 
   if(isto_handle == INVALID_HANDLE) 
     { 
      //--- tell about the failure and output the error code 
      PrintFormat("Failed to create handle of the iStochastic indicator for the symbol %s/%s, error code %d", 
                  isto_name, 
                  EnumToString(isto_period), 
                  GetLastError()); 
      //--- the indicator is stopped early 
      return(INIT_FAILED); 
     } 
//--- show the symbol/timeframe the Stochastic Oscillator indicator is calculated for 
   isto_short_name = StringFormat("iStochastic(%s/%s, %d, %d, %d, %s, %s)",isto_name,EnumToString(isto_period), 
                           isto_Kperiod,isto_Dperiod,isto_slowing,EnumToString(isto_ma_method),EnumToString(isto_price_field)); 
   IndicatorSetString(INDICATOR_SHORTNAME,isto_short_name); 

    
    
   return(INIT_SUCCEEDED); 
  }

//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 



//+------------------------------------------------------------------+ 
//| Calculate iStochastic 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[]) 
  { 
//--- number of values copied from the iStochastic indicator 
   int values_to_copy; 
//--- determine the number of values calculated in the indicator 
   int calculated = BarsCalculated(isto_handle); 
   if(calculated <= 0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- if it is the first start of calculation of the indicator or if the number of values in the iStochastic indicator changed 
//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history) 
   if(prev_calculated == 0 || calculated != isto_bars_calculated || rates_total > prev_calculated + 1) 
     { 
      //--- if the StochasticBuffer array is greater than the number of values in the iStochastic indicator for symbol/period, then we don't copy everything  
      //--- otherwise, we copy less than the size of indicator buffers 
      if(calculated > rates_total) values_to_copy = rates_total; 
      else                         values_to_copy = calculated; 
     } 
   else 
     { 
      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate() 
      //--- for calculation not more than one bar is added 
      values_to_copy = (rates_total - prev_calculated) + 1; 
     } 
    //--- fill the arrays with values of the iStochastic indicator 
    //--- if FillArraysFromBuffer returns false, it means the information is nor ready yet, quit operation 
   if(!FillArraysFrom_iSto_Buffers(isto_main_line_buffer,isto_signal_line_buffer,isto_handle,values_to_copy)) return(0); 
    //--- form the message 
   string comm = StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            isto_short_name, 
                            values_to_copy); 
    //--- display the service message on the chart 
   Comment(comm); 
    //--- memorize the number of values in the Stochastic Oscillator indicator 
   isto_bars_calculated = calculated; 
    //--- return the prev_calculated value for the next call
    Print(rates_total,"   ",prev_calculated,"     ",calculated); 
   return(rates_total); 
  } 
//}
//+------------------------------------------------------------------+ 
//| Filling indicator buffers from the iStochastic indicator         | 
//+------------------------------------------------------------------+ 
bool FillArraysFrom_iSto_Buffers(double &main_buffer[],       // indicator buffer of Stochastic Oscillator values 
                                 double &signal_buffer[],     // indicator buffer of the signal line 
                                 int    ind_handle,           // handle of the iStochastic indicator 
                                 int    amount                // number of copied values 
                           ) 
  { 
//--- reset error code 
   ResetLastError(); 
//--- fill a part of the StochasticBuffer array with values from the indicator buffer that has 0 index 
   isto_main_line = CopyBuffer(ind_handle,MAIN_LINE,0,amount,main_buffer);
   if(isto_main_line < 0) 
     { 
      //--- if the copying fails, tell the error code 
      PrintFormat("Failed to copy data from the iStochastic indicator, error code %d",GetLastError()); 
      //--- quit with zero result - it means that the indicator is considered as not calculated 
      return(false); 
     } 
//--- fill a part of the SignalBuffer array with values from the indicator buffer that has index 1 
   isto_signal_line = CopyBuffer(ind_handle,SIGNAL_LINE,0,amount,signal_buffer);
   if(isto_signal_line < 0) 
     { 
      //--- if the copying fails, tell the error code 
      PrintFormat("Failed to copy data from the iStochastic indicator, error code %d",GetLastError()); 
      //--- quit with zero result - it means that the indicator is considered as not calculated 
      return(false); 
     } 
//--- everything is fine
   return(true); 
  } 
 
Waleed_2018:

Indicator is working, there is no problem.

But I want to extract the value of the main line and the value of the signal line for use them in comparison operations, and on the outcome of that comparison the expert will perform the trades.

Thank you for your reply...

No need to reinvent the wheel my fren.

https://www.mql5.com/en/docs/standardlibrary/technicalindicators/oscillatorindicators/cistochastic
 
SanjayBalraj:
No need to reinvent the wheel my fren.

https://www.mql5.com/en/docs/standardlibrary/technicalindicators/oscillatorindicators/cistochastic
Thank you so much... i did it by your way... goooooood luck guys
 
SanjayBalraj:
No need to reinvent the wheel my fren.

https://www.mql5.com/en/docs/standardlibrary/technicalindicators/oscillatorindicators/cistochastic
I'm a beginner at mql5 programming. Can you explain how to use this ? Thanks
Reason: