iMACD iStochastic in the Same Window

Author: Copyright © 2021, Vladimir Karputov
Indicators Used
MACD HistogramStochastic oscillator
0 Views
0 Downloads
0 Favorites
iMACD iStochastic in the Same Window
ÿþ//+------------------------------------------------------------------+

//|                         iMACD iStochastic in the Same Window.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input group             "MACD"

input int                  Inp_MACD_fast_ema_period      = 12;             // MACD: period for Fast average calculation

input int                  Inp_MACD_slow_ema_period      = 26;             // MACD: period for Slow average calculation

input int                  Inp_MACD_signal_period        = 9;              // MACD: period for their difference averaging

input ENUM_APPLIED_PRICE   Inp_MACD_applied_price        = PRICE_CLOSE;    // MACD: type of price

input group             "Stochastic"

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

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

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

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

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

//---

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

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- create handle of the indicator iMACD

   handle_iMACD=iMACD(Symbol(),Period(),Inp_MACD_fast_ema_period,Inp_MACD_slow_ema_period,

                      Inp_MACD_signal_period,Inp_MACD_applied_price);

//--- if the handle is not created

   if(handle_iMACD==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iMACD indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      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

      return(INIT_SUCCEEDED);

     }

//---

   int debug         = MQLInfoInteger(MQL_DEBUG);

   int profiler      = MQLInfoInteger(MQL_PROFILER);

   int tester        = MQLInfoInteger(MQL_TESTER);

   int forward       = MQLInfoInteger(MQL_FORWARD);

   int optimization  = MQLInfoInteger(MQL_OPTIMIZATION);

   int visual_mode   = MQLInfoInteger(MQL_VISUAL_MODE);

   /*

      Print("MQL_DEBUG: ",debug,", ",

            "MQL_PROFILER: ",profiler,", ",

            "MQL_TESTER: ",tester,", ",

            "MQL_FORWARD: ",forward,", ",

            "MQL_OPTIMIZATION: ",optimization,", ",

            "MQL_VISUAL_MODE: ",visual_mode);

   */

   /*

      F5          -> MQL_DEBUG: 1, MQL_PROFILER: 0, MQL_TESTER: 0, MQL_FORWARD: 0, MQL_OPTIMIZATION: 0, MQL_VISUAL_MODE: 0

      Ctrl + F5   -> MQL_DEBUG: 1, MQL_PROFILER: 0, MQL_TESTER: 1, MQL_FORWARD: 0, MQL_OPTIMIZATION: 0, MQL_VISUAL_MODE: 1

      Online      -> MQL_DEBUG: 0, MQL_PROFILER: 0, MQL_TESTER: 0, MQL_FORWARD: 0, MQL_OPTIMIZATION: 0, MQL_VISUAL_MODE: 0

   */

//---

   if((debug==1 && tester==0) || (debug==0 && tester==0)) // F5 OR Online

     {

      int windows_total=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);

      for(int i=windows_total-1; i>=0; i--)

        {

         for(int j=ChartIndicatorsTotal(0,i)-1; j>=0; j--)

           {

            string name=ChartIndicatorName(0,i,j);

            if(name=="Stoch("+IntegerToString(Inp_STO_Kperiod)+","+IntegerToString(Inp_STO_Dperiod)+","+IntegerToString(Inp_STO_slowing)+")")

               ChartIndicatorDelete(0,i,name);

            if(name=="MACD("+IntegerToString(Inp_MACD_fast_ema_period)+","+IntegerToString(Inp_MACD_slow_ema_period)+","+IntegerToString(Inp_MACD_signal_period)+")")

               ChartIndicatorDelete(0,i,name);

           }

        }

      //---

      windows_total=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);

      bool add_iMACD=ChartIndicatorAdd(ChartID(),windows_total,handle_iMACD);

      Print("Result Chart Indicator Add 'MACD': ",((add_iMACD)?"true":"false"));

      bool add_iStochastic=ChartIndicatorAdd(ChartID(),windows_total,handle_iStochastic);

      Print("Result Chart Indicator Add 'Stochastic': ",((add_iStochastic)?"true":"false"));

     }

//---

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

  {

//---

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

   return(rates_total);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iMACD!=INVALID_HANDLE)

      IndicatorRelease(handle_iMACD);

   if(handle_iStochastic!=INVALID_HANDLE)

      IndicatorRelease(handle_iStochastic);

//---

   int debug         = MQLInfoInteger(MQL_DEBUG);

   int profiler      = MQLInfoInteger(MQL_PROFILER);

   int tester        = MQLInfoInteger(MQL_TESTER);

   int forward       = MQLInfoInteger(MQL_FORWARD);

   int optimization  = MQLInfoInteger(MQL_OPTIMIZATION);

   int visual_mode   = MQLInfoInteger(MQL_VISUAL_MODE);

   /*

      Print("MQL_DEBUG: ",debug,", ",

            "MQL_PROFILER: ",profiler,", ",

            "MQL_TESTER: ",tester,", ",

            "MQL_FORWARD: ",forward,", ",

            "MQL_OPTIMIZATION: ",optimization,", ",

            "MQL_VISUAL_MODE: ",visual_mode);

   */

   /*

      F5          -> MQL_DEBUG: 1, MQL_PROFILER: 0, MQL_TESTER: 0, MQL_FORWARD: 0, MQL_OPTIMIZATION: 0, MQL_VISUAL_MODE: 0

      Ctrl + F5   -> MQL_DEBUG: 1, MQL_PROFILER: 0, MQL_TESTER: 1, MQL_FORWARD: 0, MQL_OPTIMIZATION: 0, MQL_VISUAL_MODE: 1

      Online      -> MQL_DEBUG: 0, MQL_PROFILER: 0, MQL_TESTER: 0, MQL_FORWARD: 0, MQL_OPTIMIZATION: 0, MQL_VISUAL_MODE: 0

   */

//---

   if((debug==1 && tester==0) || (debug==0 && tester==0)) // F5 OR Online

     {

      int windows_total=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);

      for(int i=windows_total-1; i>=0; i--)

        {

         for(int j=ChartIndicatorsTotal(0,i)-1; j>=0; j--)

           {

            string name=ChartIndicatorName(0,i,j);

            if(name=="Stoch("+IntegerToString(Inp_STO_Kperiod)+","+IntegerToString(Inp_STO_Dperiod)+","+IntegerToString(Inp_STO_slowing)+")")

               ChartIndicatorDelete(0,i,name);

            if(name=="MACD("+IntegerToString(Inp_MACD_fast_ema_period)+","+IntegerToString(Inp_MACD_slow_ema_period)+","+IntegerToString(Inp_MACD_signal_period)+")")

               ChartIndicatorDelete(0,i,name);

           }

        }

     }

  }

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

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 ---