Bears Power Custom Smoothing Color

Author: Copyright © 2021, Vladimir Karputov
Price Data Components
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Bears Power Custom Smoothing Color
ÿþ//+------------------------------------------------------------------+

//|                           Bears Power Custom Smoothing Color.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.001"

#property description "Bears Power Custom Smoothing"

//--- indicator settings

#include <MovingAverages.mqh>

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   1

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrLimeGreen,clrOrangeRed

#property indicator_width1  2

//--- input parameters

input int               InpBearsPeriod    = 13;          // Bears: Period

input int               InpWidth          = 2;           // Bears: Width

input ENUM_MA_METHOD    Inp_ma_method     = MODE_SMA;    // Smoothing type

input int               Inp_ma_period     = 6;           // Smoothing averaging period

//--- indicator buffers

double   BearsBufferSmoothing[];

double   BearsColorSmoothing[];

double   BearsBuffer[];

double   TempBuffer[];

//--- handle of EMA

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

//---

bool     m_init_error   = false;    // error on InInit

int      m_weightsum;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   ArrayFree(BearsBufferSmoothing);

   ArrayFree(BearsBuffer);

   ArrayFree(TempBuffer);

//--- indicator buffers mapping

   SetIndexBuffer(0,BearsBufferSmoothing,INDICATOR_DATA);

   SetIndexBuffer(1,BearsColorSmoothing,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,BearsBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,TempBuffer,INDICATOR_CALCULATIONS);

//---

   IndicatorSetInteger(INDICATOR_DIGITS,Digits()+1);

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpBearsPeriod-1);

//--- width line

   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,InpWidth);

//--- name for DataWindow and indicator subwindow label

   string text="";

   switch(Inp_ma_method)

     {

      case  MODE_SMA:

         text="SMA";

         break;

      case  MODE_EMA:

         text="EMA";

         break;

      case  MODE_SMMA:

         text="SMMA";

         break;

      default: // MODE_LWMA

         text="LWMA";

         break;

     }

   IndicatorSetString(INDICATOR_SHORTNAME,"Bears "+

                      text+"("+IntegerToString(Inp_ma_period)+")"+

                      " Smoothing("+IntegerToString(InpBearsPeriod)+")");

//--- create handle of the indicator iMA

   handle_iMA=iMA(Symbol(),Period(),InpBearsPeriod,0,

                  MODE_EMA,PRICE_CLOSE);

//--- if the handle is not created

   if(handle_iMA==INVALID_HANDLE)

     {

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

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

     }

//---

   m_weightsum=0;

//--- initialization done

   return(INIT_SUCCEEDED);

  }

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

//| Average True Range                                               |

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

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

   int i,limit;

//--- check for bars count

   if(rates_total<InpBearsPeriod)

      return(0);// not enough bars for calculation

//--- not all data may be calculated

   int calculated=BarsCalculated(handle_iMA);

   if(calculated<rates_total)

     {

      Print("Not all data of handle_iMA is calculated (",calculated,"bars ). Error",GetLastError());

      return(0);

     }

//--- we can copy not all data

   int to_copy;

   if(prev_calculated>rates_total || prev_calculated<0)

      to_copy=rates_total;

   else

     {

      to_copy=rates_total-prev_calculated;

      if(prev_calculated>0)

         to_copy++;

     }

//---- get ma buffers

   if(IsStopped())

      return(0); //Checking for stop flag

   if(CopyBuffer(handle_iMA,0,0,to_copy,TempBuffer)<=0)

     {

      Print("getting handle_iMA_EMA is failed! Error",GetLastError());

      return(0);

     }

//--- first calculation or number of bars was changed

   if(prev_calculated<InpBearsPeriod)

      limit=InpBearsPeriod;

   else

      limit=prev_calculated-1;

//--- the main loop of calculations

   for(i=limit; i<rates_total && !IsStopped(); i++)

     {

      BearsBuffer[i]=low[i]-TempBuffer[i];

     }

   switch(Inp_ma_method)

     {

      case  MODE_SMA:

         SimpleMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,BearsBuffer,BearsBufferSmoothing);

         break;

      case  MODE_EMA:

         ExponentialMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,BearsBuffer,BearsBufferSmoothing);

         break;

      case  MODE_SMMA:

         SmoothedMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,BearsBuffer,BearsBufferSmoothing);

         break;

      default: // MODE_LWMA

         LinearWeightedMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,BearsBuffer,BearsBufferSmoothing,m_weightsum);

         break;

     }

//--- main loop

   for(i=limit; i<rates_total && !IsStopped(); i++)

     {

      if(i==0)

         BearsColorSmoothing[i]=0.0;

      else

        {

         if(BearsBufferSmoothing[i-1]>BearsBufferSmoothing[i])

            BearsColorSmoothing[i]=1.0;

         else

            BearsColorSmoothing[i]=0.0;

        }

     }

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

   return(rates_total);

  }

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

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