Awesome_Oscillator_Custom_Averaging_Period

Author: Copyright © 2019, Vladimir Karputov
Indicators Used
Moving average indicator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Awesome_Oscillator_Custom_Averaging_Period
ÿþ//+------------------------------------------------------------------+

//|                   Awesome_Oscillator_Custom_Averaging_Period.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

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

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

#property copyright "Copyright © 2019, Vladimir Karputov"

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

#property version   "1.000"

//--- indicator settings

#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

#property indicator_label1  "AO Custom"

//--- input parameters

input int   Inp_Fast_MA_ma_period   = 8;  // MA Fast: averaging period

input int   Inp_Slow_MA_ma_period   = 13; // MA Slow: averaging period

//--- indicator buffers

double ExtAOBuffer[];

double ExtColorBuffer[];

double ExtFastBuffer[];

double ExtSlowBuffer[];

//--- handles for MAs

int    ExtFastSMAHandle;

int    ExtSlowSMAHandle;

//--- bars minimum for calculation

int DATA_LIMIT;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(Inp_Fast_MA_ma_period<=0)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "\"MA Fast: averaging period\" =5 <>65B 1KBL <5=LH5 8;8 @025= =C;N!":

                      "\"MA Fast: averaging period\" cannot be less than or equal to zero!";

      Alert(__FUNCTION__,", ERROR: ",err_text);

      return(INIT_PARAMETERS_INCORRECT);

     }

   if(Inp_Slow_MA_ma_period<=0)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "\"MA Slow: averaging period\" =5 <>65B 1KBL <5=LH5 8;8 @025= =C;N!":

                      "\"MA Slow: averaging period\" cannot be less than or equal to zero!";

      Alert(__FUNCTION__,", ERROR: ",err_text);

      return(INIT_PARAMETERS_INCORRECT);

     }

   if(Inp_Fast_MA_ma_period>=Inp_Slow_MA_ma_period)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "\"MA Fast: averaging period\" =5 <>65B 1KBL 1>;LH5 8;8 @025= \"MA Slow: averaging period\"!":

                      "\"MA Fast: averaging period\" can not be greater than or equal to \"MA Slow: averaging period\"!";

      Alert(__FUNCTION__,", ERROR: ",err_text);

      return(INIT_PARAMETERS_INCORRECT);

     }

//---

   DATA_LIMIT=Inp_Slow_MA_ma_period-1;

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtAOBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtColorBuffer,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,ExtFastBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,ExtSlowBuffer,INDICATOR_CALCULATIONS);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,DATA_LIMIT);

//--- name for DataWindow 

   IndicatorSetString(INDICATOR_SHORTNAME,"AO Custom("+IntegerToString(Inp_Fast_MA_ma_period)+";"+IntegerToString(Inp_Slow_MA_ma_period)+")");

//--- get handles

//--- create handle of the indicator iMA 

   ExtFastSMAHandle=iMA(Symbol(),Period(),Inp_Fast_MA_ma_period,0,

                        MODE_SMA,PRICE_MEDIAN);

//--- if the handle is not created 

   if(ExtFastSMAHandle==INVALID_HANDLE)

     {

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

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

   ExtSlowSMAHandle=iMA(Symbol(),Period(),Inp_Slow_MA_ma_period,0,

                        MODE_SMA,PRICE_MEDIAN);

//--- if the handle is not created 

   if(ExtSlowSMAHandle==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iMA (\"Slow\") indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//---- initialization done

   return(INIT_SUCCEEDED);

  }

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

//|  Awesome Oscillator                                              |

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

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

  {

//--- check for rates total

   if(rates_total<=DATA_LIMIT)

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

//--- not all data may be calculated

   int calculated=BarsCalculated(ExtFastSMAHandle);

   if(calculated<rates_total)

     {

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

      return(0);

     }

   calculated=BarsCalculated(ExtSlowSMAHandle);

   if(calculated<rates_total)

     {

      Print("Not all data of ExtSlowSMAHandle 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 FastSMA buffer

   if(IsStopped()) return(0); //Checking for stop flag

   if(CopyBuffer(ExtFastSMAHandle,0,0,to_copy,ExtFastBuffer)<=0)

     {

      Print("Getting fast SMA is failed! Error",GetLastError());

      return(0);

     }

//--- get SlowSMA buffer

   if(IsStopped()) return(0); //Checking for stop flag

   if(CopyBuffer(ExtSlowSMAHandle,0,0,to_copy,ExtSlowBuffer)<=0)

     {

      Print("Getting slow SMA is failed! Error",GetLastError());

      return(0);

     }

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

   int i,limit;

   if(prev_calculated<=DATA_LIMIT)

     {

      for(i=0;i<DATA_LIMIT;i++)

         ExtAOBuffer[i]=0.0;

      limit=DATA_LIMIT;

     }

   else

      limit=prev_calculated-1;

//--- main loop of calculations

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

     {

      ExtAOBuffer[i]=ExtFastBuffer[i]-ExtSlowBuffer[i];

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

         ExtColorBuffer[i]=0.0; // set color Green

      else

         ExtColorBuffer[i]=1.0; // set color Red

     }

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