Bulls Power Custom Smoothing

Author: Copyright © 2019, Vladimir Karputov
Indicators Used
Moving average indicator
2 Views
0 Downloads
0 Favorites
Bulls Power Custom Smoothing
ÿþ//+------------------------------------------------------------------+

//|                                 Bulls Power Custom Smoothing.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.002"

#property description "Bulls Power Custom Smoothing"

//--- indicator settings

#include <MovingAverages.mqh>

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_plots   1

#property indicator_type1   DRAW_HISTOGRAM

#property indicator_color1  clrSilver

#property indicator_width1  2

//--- input parameters

input int               InpBullsPeriod    = 13;          // Bulls: Period

input color             InpColor          = clrBlue;     // Bulls: Color

input int               InpWidth          = 2;           // Bulls: Width

input ENUM_MA_METHOD    Inp_ma_method     = MODE_SMA;    // Smoothing type

input int               Inp_ma_period     = 6;           // Smoothing averaging period

//--- indicator buffers

double    ExtBullsBufferSmoothing[];

double    ExtBullsBuffer[];

double    ExtTempBuffer[];

//--- handle of EMA 

int       ExtEmaHandle;

//---

int       m_weightsum;

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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

//---

   ArrayFree(ExtBullsBufferSmoothing);

   ArrayFree(ExtBullsBuffer);

   ArrayFree(ExtTempBuffer);

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtBullsBufferSmoothing,INDICATOR_DATA);

   SetIndexBuffer(1,ExtBullsBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(2,ExtTempBuffer,INDICATOR_CALCULATIONS);

//---

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpBullsPeriod-1);

//--- color line

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,InpColor);

//--- 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,"Bulls "+

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

                      " Smoothing("+IntegerToString(InpBullsPeriod)+")");

//--- get MA handle

   ExtEmaHandle=iMA(NULL,0,InpBullsPeriod,0,MODE_EMA,PRICE_CLOSE);

//---

   m_weightsum=0;

//--- initialization done

  }

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

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

  {

   int i,limit;

//--- check for bars count

   if(rates_total<InpBullsPeriod)

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

//--- not all data may be calculated

   int calculated=BarsCalculated(ExtEmaHandle);

   if(calculated<rates_total)

     {

      Print("Not all data of ExtEmaHandle 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(ExtEmaHandle,0,0,to_copy,ExtTempBuffer)<=0)

     {

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

      return(0);

     }

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

   if(prev_calculated<InpBullsPeriod)

      limit=InpBullsPeriod;

   else limit=prev_calculated-1;

//--- the main loop of calculations

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

     {

      ExtBullsBuffer[i]=high[i]-ExtTempBuffer[i];

     }

   switch(Inp_ma_method)

     {

      case  MODE_SMA:

         SimpleMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,ExtBullsBuffer,ExtBullsBufferSmoothing);

         break;

      case  MODE_EMA:

         ExponentialMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,ExtBullsBuffer,ExtBullsBufferSmoothing);

         break;

      case  MODE_SMMA:

         SmoothedMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,ExtBullsBuffer,ExtBullsBufferSmoothing);

         break;

      default: // MODE_LWMA

         LinearWeightedMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,ExtBullsBuffer,ExtBullsBufferSmoothing,m_weightsum);

         break;

     }

//SimpleMAOnBuffer(rates_total,prev_calculated,0,6,ExtBullsBuffer,ExtBullsBufferSmoothing);

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