iCCI iStochastic iRSI weight

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Commodity channel indexStochastic oscillatorRelative strength index
0 Views
0 Downloads
0 Favorites
iCCI iStochastic iRSI weight
ÿþ//+------------------------------------------------------------------+

//|                                 iCCI iStochastic iRSI weight.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property version   "1.002"

//---

#include <MovingAverages.mqh>

//---

#property indicator_separate_window

#property indicator_buffers 6

#property indicator_plots   3

//--- plot Sum

#property indicator_label1  "Sum"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Fast

#property indicator_label2  "Fast"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrYellowGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Slow

#property indicator_label3  "Slow"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrDodgerBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- input parameters

input    ushort InpEasyStart  = 5000;  // Easy Start ('0' -> OFF)

input    double InpWeightCCI  = 0.1;   // Weight CCI (from 0 to 1)

input    double InpWeightRSI  = 0.1;   // Weight RSI (from 0 to 1)

//--- iCCI

input int                  Inp_CCI_ma_period    = 5;              // CCI: averaging period

input ENUM_APPLIED_PRICE   Inp_CCI_applied_price= PRICE_TYPICAL;  // CCI: type of price or handle

//--- Stochastic

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

input int                  Inp_STO_Dperiod      = 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

//--- RSI

input int                  Inp_RSI_ma_period    = 5;              // RSI: averaging period

input ENUM_APPLIED_PRICE   Inp_RSI_applied_price= PRICE_CLOSE;    // RSI: type of price

//--- MA

input int                  Inp_Fast_ma_period   = 3;              // Fast averaging period

input int                  Inp_Slow_ma_period   = 5;              // Slow averaging period

//---

int      handle_iCCI;                  // variable for storing the handle of the iCCI indicator

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

int      handle_iRSI;                  // variable for storing the handle of the iRSI indicator

//--- indicator buffers

double         SumBuffer[];

double         FastBuffer[];

double         SlowBuffer[];

double         iCCIBuffer[];

double         iStochasticBuffer[];

double         iRSIBuffer[];

//--- we will keep the number of values indicator

int      bars_calculated=0;

int      m_weightsum=0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,SumBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,FastBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,SlowBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,iCCIBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,iStochasticBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,iRSIBuffer,INDICATOR_CALCULATIONS);

   //--- the 0.0 (empty) value will mot participate in drawing 

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

//--- create handle of the indicator iCCI

   handle_iCCI=iCCI(Symbol(),Period(),Inp_CCI_ma_period,Inp_CCI_applied_price);

//--- if the handle is not created

   if(handle_iCCI==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//--- 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_FAILED);

     }

//--- create handle of the indicator iRSI

   handle_iRSI=iRSI(Symbol(),Period(),Inp_RSI_ma_period,Inp_RSI_applied_price);

//--- if the handle is not created

   if(handle_iRSI==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//---

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

  {

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

   int values_to_copy;

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

   int calculated=-1;



   int calculated_cci=BarsCalculated(handle_iCCI);

   if(calculated_cci<=0)

     {

      PrintFormat("BarsCalculated(handle_iCCI) returned %d, error code %d",calculated,GetLastError());

      return(0);

     }



   int calculated_sto=BarsCalculated(handle_iStochastic);

   if(calculated_sto<=0)

     {

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

      return(0);

     }



   int calculated_rsi=BarsCalculated(handle_iRSI);

   if(calculated_rsi<=0)

     {

      PrintFormat("BarsCalculated(handle_iRSI) returned %d, error code %d",calculated,GetLastError());

      return(0);

     }



   if(calculated_cci!=calculated_sto)

     {

      PrintFormat("BarsCalculated(handle_iCCI) %d != BarsCalculated(handle_iStochastic) %d",calculated_cci,calculated_sto);

      return(0);

     }

   if(calculated_sto!=calculated_rsi)

     {

      PrintFormat("BarsCalculated(handle_iStochastic) %d != BarsCalculated(handle_iRSI) %d",calculated_sto,calculated_rsi);

      return(0);

     }

   calculated=calculated_cci;

//--- if it is the first start of calculation of the indicator or if the number of values in the iCCI 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 the iCCIBuffer array is greater than the number of values in the iCCI 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 iCCIBuffer array with values of the Commodity Channel Index indicator

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

   if(!FillArrayFromBufferCCI(iCCIBuffer,handle_iCCI,values_to_copy))

      return(0);

//--- fill the arrays with values of the iStochastic indicator

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

   if(!FillArraysFromBufferStochastic(iStochasticBuffer,handle_iStochastic,values_to_copy))

      return(0);

//--- fill the array with values of the iRSI indicator

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

   if(!FillArrayFromBufferRSI(iRSIBuffer,handle_iRSI,values_to_copy))

      return(0);

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      if(InpEasyStart>0)

         limit=(rates_total>InpEasyStart)?rates_total-InpEasyStart:0;

      else

         limit=0;

      if(limit>0)

        {

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

           {

            SumBuffer[i]=0.0;

            FastBuffer[i]=0.0;

            SlowBuffer[i]=0.0;

           }

        }

     }

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

     {

      SumBuffer[i]=iCCIBuffer[i]*InpWeightCCI + iRSIBuffer[i]*InpWeightRSI + (iStochasticBuffer[i]-50.0)*(1.0-InpWeightCCI-InpWeightRSI);

      LinearWeightedMAOnBuffer(rates_total,prev_calculated,0,Inp_Fast_ma_period,SumBuffer,FastBuffer,m_weightsum);

      LinearWeightedMAOnBuffer(rates_total,prev_calculated,0,Inp_Slow_ma_period,SumBuffer,SlowBuffer,m_weightsum);

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iCCI indicator                |

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

bool FillArrayFromBufferCCI(double &values[],  // indicator buffer of Commodity Channel Index values

                            int ind_handle,    // handle of the iCCI indicator

                            int amount         // number of copied values

                           )

  {

//--- reset error code

   ResetLastError();

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

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

     {

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

      PrintFormat("Failed to copy data from the iCCI 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 buffer from the iStochastic indicator          |

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

bool FillArraysFromBufferStochastic(double &main_buffer[],    // indicator buffer of Stochastic Oscillator values

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

     }

//--- everything is fine

   return(true);

  }

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

//| Filling indicator buffers from the iRSI indicator                |

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

bool FillArrayFromBufferRSI(double &rsi_buffer[],  // indicator buffer of Relative Strength Index values

                            int ind_handle,        // handle of the iRSI indicator

                            int amount             // number of copied values

                           )

  {

//--- reset error code

   ResetLastError();

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

   if(CopyBuffer(ind_handle,0,0,amount,rsi_buffer)<0)

     {

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

      PrintFormat("Failed to copy data from the iRSI 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_iCCI!=INVALID_HANDLE)

      IndicatorRelease(handle_iCCI);



  }

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

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