Four MA on OBV

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
On Balance Volume indicatorMoving average indicator
0 Views
0 Downloads
0 Favorites
Four MA on OBV
ÿþ//+------------------------------------------------------------------+

//|                                               Four MA on OBV.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property version   "1.000"

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   4

//--- plot MA 1

#property indicator_label1  "MA 1 on OBV"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrSeaGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot MA 2

#property indicator_label2  "MA 2 on OBV"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrDarkViolet

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- plot MA 3

#property indicator_label3  "MA 3 on OBV"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrLawnGreen

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

//--- plot MA 4

#property indicator_label4  "MA 4 on OBV"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrTomato

#property indicator_style4  STYLE_SOLID

#property indicator_width4  2

//--- input parameters

input group             "MA 1 on OBV"

input int                  Inp_MA_1_ma_period      = 3;           // MA 1 on OBV: averaging period

input int                  Inp_MA_1_ma_shift       = 0;           // MA 1 on OBV: horizontal shift

input ENUM_MA_METHOD       Inp_MA_1_ma_method      = MODE_SMA;    // MA 1 on OBV: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_1_applied_price  = PRICE_CLOSE; // MA 1 on OBV: type of price

input group             "MA 2 on OBV"

input int                  Inp_MA_2_ma_period      = 6;           // MA 2 on OBV: averaging period

input int                  Inp_MA_2_ma_shift       = 0;           // MA 2 on OBV: horizontal shift

input ENUM_MA_METHOD       Inp_MA_2_ma_method      = MODE_SMA;    // MA 2 on OBV: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_2_applied_price  = PRICE_CLOSE; // MA 2 on OBV: type of price

input group             "MA 3 on OBV"

input int                  Inp_MA_3_ma_period      = 12;          // MA 3 on OBV: averaging period

input int                  Inp_MA_3_ma_shift       = 0;           // MA 3 on OBV: horizontal shift

input ENUM_MA_METHOD       Inp_MA_3_ma_method      = MODE_SMA;    // MA 3 on OBV: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_3_applied_price  = PRICE_CLOSE; // MA 3 on OBV: type of price

input group             "MA 4 on OBV"

input int                  Inp_MA_4_ma_period      = 24;          // MA 4 on OBV: averaging period

input int                  Inp_MA_4_ma_shift       = 0;           // MA 4 on OBV: horizontal shift

input ENUM_MA_METHOD       Inp_MA_4_ma_method      = MODE_SMA;    // MA 4 on OBV: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_4_applied_price  = PRICE_CLOSE; // MA 4 on OBV: type of price

input group             "iOBV"

input ENUM_APPLIED_VOLUME  Inp_OBV_applied_volume  = VOLUME_TICK; // OBV: volume type for calculation

//--- indicator buffers

double   MA_1_On_OBV_Buffer[];

double   MA_2_On_OBV_Buffer[];

double   MA_3_On_OBV_Buffer[];

double   MA_4_On_OBV_Buffer[];

//---

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

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

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

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

int      handle_iOBV;               // variable for storing the handle of the iOBV indicator

int      bars_calculated=0;         // we will keep the number of values in the Moving Average indicator

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,MA_1_On_OBV_Buffer,INDICATOR_DATA);

   SetIndexBuffer(1,MA_2_On_OBV_Buffer,INDICATOR_DATA);

   SetIndexBuffer(2,MA_3_On_OBV_Buffer,INDICATOR_DATA);

   SetIndexBuffer(3,MA_4_On_OBV_Buffer,INDICATOR_DATA);

   //--- set labels for the line 

   PlotIndexSetString(0,PLOT_LABEL,"MA("+IntegerToString(Inp_MA_1_ma_period)+") on OBV"); 

   PlotIndexSetString(1,PLOT_LABEL,"MA("+IntegerToString(Inp_MA_2_ma_period)+") on OBV"); 

   PlotIndexSetString(2,PLOT_LABEL,"MA("+IntegerToString(Inp_MA_3_ma_period)+") on OBV"); 

   PlotIndexSetString(3,PLOT_LABEL,"MA("+IntegerToString(Inp_MA_4_ma_period)+") on OBV"); 

//--- create handle of the iOBV indicator

   handle_iOBV=iOBV(Symbol(),Period(),Inp_OBV_applied_volume);

//--- if the handle is not created

   if(handle_iOBV==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iOBV 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 iMA

   handle_iMA_1_On_OBV=iMA(Symbol(),Period(),Inp_MA_1_ma_period,Inp_MA_1_ma_shift,

                           Inp_MA_1_ma_method,handle_iOBV);

//--- if the handle is not created

   if(handle_iMA_1_On_OBV==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the MA 1 on OBV 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 iMA

   handle_iMA_2_On_OBV=iMA(Symbol(),Period(),Inp_MA_2_ma_period,Inp_MA_2_ma_shift,

                           Inp_MA_2_ma_method,handle_iOBV);

//--- if the handle is not created

   if(handle_iMA_2_On_OBV==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the MA 2 on OBV 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 iMA

   handle_iMA_3_On_OBV=iMA(Symbol(),Period(),Inp_MA_3_ma_period,Inp_MA_3_ma_shift,

                           Inp_MA_3_ma_method,handle_iOBV);

//--- if the handle is not created

   if(handle_iMA_3_On_OBV==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the MA 3 on OBV 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 iMA

   handle_iMA_4_On_OBV=iMA(Symbol(),Period(),Inp_MA_4_ma_period,Inp_MA_4_ma_shift,

                           Inp_MA_4_ma_method,handle_iOBV);

//--- if the handle is not created

   if(handle_iMA_4_On_OBV==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the MA 4 on OBV 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 iMA indicator

   int values_to_copy;

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

   int calculated_1=BarsCalculated(handle_iMA_1_On_OBV);

   if(calculated_1<=0)

     {

      PrintFormat("BarsCalculated(handle_iMA_1_On_OBV) returned %d, error code %d",calculated_1,GetLastError());

      return(0);

     }

   int calculated_2=BarsCalculated(handle_iMA_2_On_OBV);

   if(calculated_2<=0)

     {

      PrintFormat("BarsCalculated(handle_iMA_2_On_OBV) returned %d, error code %d",calculated_2,GetLastError());

      return(0);

     }

   if(calculated_2!=calculated_1)

      return(0);

//---

   int calculated_3=BarsCalculated(handle_iMA_3_On_OBV);

   if(calculated_3<=0)

     {

      PrintFormat("BarsCalculated(handle_iMA_3_On_OBV) returned %d, error code %d",calculated_3,GetLastError());

      return(0);

     }

   if(calculated_3!=calculated_2)

      return(0);

//---

   int calculated_4=BarsCalculated(handle_iMA_4_On_OBV);

   if(calculated_4<=0)

     {

      PrintFormat("BarsCalculated(handle_iMA_4_On_OBV) returned %d, error code %d",calculated_4,GetLastError());

      return(0);

     }

   if(calculated_4!=calculated_3)

      return(0);

//---

   int calculated=calculated_1;

//--- 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 the iMABuffer array is greater than the number of values in the iMA 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 iMABuffer array with values of the Moving Average indicator

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

   if(!FillArrayFromBuffer(MA_1_On_OBV_Buffer,Inp_MA_1_ma_shift,handle_iMA_1_On_OBV,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(MA_2_On_OBV_Buffer,Inp_MA_2_ma_shift,handle_iMA_2_On_OBV,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(MA_3_On_OBV_Buffer,Inp_MA_3_ma_shift,handle_iMA_3_On_OBV,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(MA_4_On_OBV_Buffer,Inp_MA_4_ma_shift,handle_iMA_4_On_OBV,values_to_copy))

      return(0);

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

   bars_calculated=calculated;

//--- return the prev_calculated value for the 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);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iMA_4_On_OBV!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_4_On_OBV);

   if(handle_iMA_3_On_OBV!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_3_On_OBV);

   if(handle_iMA_2_On_OBV!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_2_On_OBV);

   if(handle_iMA_1_On_OBV!=INVALID_HANDLE)

      IndicatorRelease(handle_iMA_1_On_OBV);

   if(handle_iOBV!=INVALID_HANDLE)

      IndicatorRelease(handle_iOBV);

  }

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

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