Two MA one Stochastic

Author: Copyright © 2021, Vladimir Karputov
Indicators Used
Moving average indicatorStochastic oscillator
0 Views
0 Downloads
0 Favorites
Two MA one Stochastic
ÿþ//+------------------------------------------------------------------+

//|                                        Two MA one Stochastic.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43161 |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43161"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_plots   2

//--- plot BUY

#property indicator_label1  "BUY"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrDeepSkyBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot SELL

#property indicator_label2  "SELL"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrSalmon

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input group             "MA Fast"

input int                  Inp_MA_Fast_ma_period      = 4;           // MA Fast: averaging period

input int                  Inp_MA_Fast_ma_shift       = 0;           // MA Fast: horizontal shift

input ENUM_MA_METHOD       Inp_MA_Fast_ma_method      = MODE_EMA;    // MA Fast: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Fast_applied_price  = PRICE_CLOSE; // MA Fast: type of price

input group             "MA Slow"

input int                  Inp_MA_Slow_ma_period      = 20;          // MA Slow: averaging period

input int                  Inp_MA_Slow_ma_shift       = 0;           // MA Slow: horizontal shift

input ENUM_MA_METHOD       Inp_MA_Slow_ma_method      = MODE_EMA;    // MA Slow: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Slow_applied_price  = PRICE_CLOSE; // MA Slow: type of price

input group             "Stochastic"

input int                  Inp_STO_Kperiod            = 13;          // Stochastic: K-period (number of bars for calculations)

input int                  Inp_STO_Dperiod            = 5;           // Stochastic: D-period (period of first smoothing)

input int                  Inp_STO_slowing            = 5;           // Stochastic: final smoothing

input ENUM_MA_METHOD       Inp_STO_ma_method          = MODE_EMA;    // Stochastic: type of smoothing

input ENUM_STO_PRICE       Inp_STO_price_field        = STO_LOWHIGH; // Stochastic: stochastic calculation method

input group             "Arrow"

input uchar                Inp_Arrow_BUY_Code      = 236;         // BUY: symbol code from the Wingdings font

input int                  Inp_Arrow_BUY_Shift     = 10;          // BUY: vertical shift of arrows in pixels

input uchar                Inp_Arrow_SELL_Code     = 238;         // SELL: symbol code from the Wingdings font

input int                  Inp_Arrow_SELL_Shift    = 10;          // SELL: vertical shift of arrows in pixels

//--- indicator buffers

double   BUYBuffer[];

double   SELLBuffer[];

double   MA_Fast_Buffer[];

double   MA_Slow_Buffer[];

double   STO_Main_Buffer[];

double   STO_Signal_Buffer[];

//---

int      handle_iMA_Fast;                       // variable for storing the handle of the iMA indicator

int      handle_iMA_Slow;                       // variable for storing the handle of the iMACD indicator

int      handle_iStochastic;                    // variable for storing the handle of the iStochastic indicator

int      bars_calculated      = 0;              // we will keep the number of values in  indicators

bool     m_init_error         = false;          // error on InInit

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,BUYBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,SELLBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,MA_Fast_Buffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,MA_Slow_Buffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,STO_Main_Buffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,STO_Signal_Buffer,INDICATOR_CALCULATIONS);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,159);

   PlotIndexSetInteger(1,PLOT_ARROW,159);

//--- define the symbol code for drawing in PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,Inp_Arrow_BUY_Code);

   PlotIndexSetInteger(1,PLOT_ARROW,Inp_Arrow_SELL_Code);

//--- set the vertical shift of arrows in pixels

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,Inp_Arrow_BUY_Shift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-Inp_Arrow_SELL_Shift);

//--- set as an empty value 0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//--- create handle of the indicator iMA

   handle_iMA_Fast=iMA(Symbol(),Period(),Inp_MA_Fast_ma_period,Inp_MA_Fast_ma_shift,

                       Inp_MA_Fast_ma_method,Inp_MA_Fast_applied_price);

//--- if the handle is not created

   if(handle_iMA_Fast==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iMA indicator ('Fast') for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iMA

   handle_iMA_Slow=iMA(Symbol(),Period(),Inp_MA_Slow_ma_period,Inp_MA_Slow_ma_shift,

                       Inp_MA_Slow_ma_method,Inp_MA_Slow_applied_price);

//--- if the handle is not created

   if(handle_iMA_Slow==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iMA indicator ('Slow') for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator iStochastic

   handle_iStochastic=iStochastic(Symbol(),Period(),

                                  Inp_STO_Kperiod,Inp_STO_Dperiod,Inp_STO_slowing,

                                  Inp_STO_ma_method,Inp_STO_price_field);

//--- if the handle is not created

   if(handle_iStochastic==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",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   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[])

  {

   if(m_init_error)

      return(0);

//--- number of values copied from the iMA indicator

   int values_to_copy;

//--- determine the number of values calculated in the indicator

   int calculated_fast=BarsCalculated(handle_iMA_Fast);

   if(calculated_fast<=0)

     {

      PrintFormat("BarsCalculated(handle_iMA_Fast) returned %d, error code %d",calculated_fast,GetLastError());

      return(0);

     }

//--- determine the number of values calculated in the indicator

   int calculated_slow=BarsCalculated(handle_iMA_Slow);

   if(calculated_slow<=0)

     {

      PrintFormat("BarsCalculated(handle_iMA_Slow) returned %d, error code %d",calculated_slow,GetLastError());

      return(0);

     }

//--- determine the number of values calculated in the indicator

   int calculated_sto=BarsCalculated(handle_iStochastic);

   if(calculated_sto<=0)

     {

      PrintFormat("BarsCalculated(handle_iStochastic) returned %d, error code %d",calculated_sto,GetLastError());

      return(0);

     }

   if(calculated_fast!=calculated_slow || calculated_fast!=calculated_sto)

     {

      PrintFormat("BarsCalculated(handle_iMA_Fast) returned %d, BarsCalculated(handle_iMA_Slow) returned %d,BarsCalculated(handle_iStochastic) returned %d",

                  calculated_fast,calculated_slow,calculated_sto);

      return(0);

     }

   int calculated=calculated_fast;

//--- if it is the first start of calculation of the indicator or if the number of values in the iMA 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!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if Buffer array is greater than the number of values in indicator for symbol, 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 Buffer array with values of indicator

//--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation

   if(!FillArrayFromBuffer(MA_Fast_Buffer,Inp_MA_Fast_ma_shift,handle_iMA_Fast,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(MA_Slow_Buffer,Inp_MA_Slow_ma_shift,handle_iMA_Slow,values_to_copy))

      return(0);

   if(!FillArraysFromBuffers(STO_Main_Buffer,STO_Signal_Buffer,handle_iStochastic,values_to_copy))

      return(0);

//--- memorize the number of values in the Moving Average indicator

   bars_calculated=calculated;

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

   for(int i=limit; i<rates_total; i++)

     {

      BUYBuffer[i]=0.0;

      SELLBuffer[i]=0.0;

      if(STO_Main_Buffer[i]>STO_Signal_Buffer[i] && STO_Main_Buffer[i]<30.0 && close[i]>MA_Fast_Buffer[i] &&

         open[i]<MA_Fast_Buffer[i] && close[i]<MA_Slow_Buffer[i])

        {

         BUYBuffer[i]=low[i];

        }

      if(STO_Main_Buffer[i]<STO_Signal_Buffer[i] && STO_Main_Buffer[i]>70.0 && close[i]<MA_Fast_Buffer[i] &&

         open[i]>MA_Fast_Buffer[i] && close[i]>MA_Slow_Buffer[i])

        {

         SELLBuffer[i]=high[i];

        }

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| Filling indicator buffers from the MA indicator                  |

//+------------------------------------------------------------------+

bool FillArrayFromBuffer(double &values[],   // indicator buffer of Moving Average values

                         int shift,          // shift

                         int ind_handle,     // handle of the iMA indicator

                         int amount          // number of copied values

                        )

  {

//--- reset error code

   ResetLastError();

//--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(ind_handle,0,-shift,amount,values)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iMA 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);

  }

//+------------------------------------------------------------------+

//| Filling indicator buffers from the iStochastic indicator         |

//+------------------------------------------------------------------+

bool FillArraysFromBuffers(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

   if(CopyBuffer(ind_handle,MAIN_LINE,0,amount,main_buffer)<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

   if(CopyBuffer(ind_handle,SIGNAL_LINE,0,amount,signal_buffer)<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);

  }

//+------------------------------------------------------------------+

//| Indicator deinitialization function                              |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   if(handle_iMA_Fast!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Fast);

   if(handle_iMA_Slow!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_Slow);

   if(handle_iStochastic!=INVALID_HANDLE)

      IndicatorRelease(handle_iStochastic);

  }

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---