Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
ASO_v1
ÿþ//+------------------------------------------------------------------+

//|                                                          ASO.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property description "Absolute Strength Oscillator"

#property indicator_separate_window

#property indicator_buffers 9

#property indicator_plots   2

//--- plot Bulls

#property indicator_label1  "Bulls strength"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Bears

#property indicator_label2  "Bears strength"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- enums

enum ENUM_MODE

  {

   MODE_RSI,   // RSI

   MODE_STO    // Stochastic

  };

//--- input parameters

input uint                 InpPeriod         =  9;             // Period

input uint                 InpPeriodSm       =  2;             // Smoothing

input ENUM_MODE            InpMode           =  MODE_RSI;      // Mode

input ENUM_MA_METHOD       InpMethod         =  MODE_SMA;      // Method

input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // Applied price

//--- indicator buffers

double         BufferBulls[];

double         BufferBears[];

double         BufferBL[];

double         BufferBR[];

double         BufferAvgBL[];

double         BufferAvgBR[];

double         BufferAvgSmBL[];

double         BufferAvgSmBR[];

double         BufferMA[];

//--- global variables

int            period_ind;

int            period_sm;

int            period_max;

int            handle_ma;

int            weight_sum_bl;

int            weight_sum_br;

int            weight_sum_sbl;

int            weight_sum_sbr;

//--- includes

#include <MovingAverages.mqh>

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_ind=int(InpPeriod<1 ? 1 : InpPeriod);

   period_sm=int(InpPeriodSm<2 ? 2 : InpPeriodSm);

   period_max=fmax(period_ind,period_sm);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferBulls,INDICATOR_DATA);

   SetIndexBuffer(1,BufferBears,INDICATOR_DATA);

   SetIndexBuffer(2,BufferBL,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,BufferBR,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,BufferAvgBL,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferAvgBR,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,BufferAvgSmBL,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,BufferAvgSmBR,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,BufferMA,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   string method=StringSubstr(EnumToString(InpMode),5);

   IndicatorSetString(INDICATOR_SHORTNAME,"ASO ("+(string)period_ind+","+(string)period_sm+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferBulls,true);

   ArraySetAsSeries(BufferBears,true);

   ArraySetAsSeries(BufferBL,true);

   ArraySetAsSeries(BufferBR,true);

   ArraySetAsSeries(BufferAvgBL,true);

   ArraySetAsSeries(BufferAvgBR,true);

   ArraySetAsSeries(BufferAvgSmBL,true);

   ArraySetAsSeries(BufferAvgSmBR,true);

   ArraySetAsSeries(BufferMA,true);

//--- create MA's handles

   ResetLastError();

   handle_ma=iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,InpAppliedPrice);

   if(handle_ma==INVALID_HANDLE)

     {

      Print("The iMA(1) object was not created: Error ",GetLastError());

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

  {

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   if(rates_total<fmax(period_max,4) || Point()==0) return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferBulls,EMPTY_VALUE);

      ArrayInitialize(BufferBears,EMPTY_VALUE);

      ArrayInitialize(BufferBL,0);

      ArrayInitialize(BufferBR,0);

      ArrayInitialize(BufferAvgBL,0);

      ArrayInitialize(BufferAvgBR,0);

      ArrayInitialize(BufferAvgSmBL,0);

      ArrayInitialize(BufferAvgSmBR,0);

      ArrayInitialize(BufferMA,0);

     }

//--- >43>B>2:0 40==KE

   int bars=(limit>1 ? rates_total : 1),copied=0;

   copied=CopyBuffer(handle_ma,0,0,bars,BufferMA);

   if(copied!=bars) return 0;

   

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      double Pr0=BufferMA[i];

      double Pr1=BufferMA[i+1];



      if(InpMode==MODE_RSI)

        {

         BufferBL[i]=0.5*fabs(Pr0-Pr1)+Pr0-Pr1;

         BufferBR[i]=0.5*fabs(Pr0-Pr1)-Pr0+Pr1;

        }

      else

        {

         int bh=Highest(period_ind,i);

         int bl=Lowest(period_ind,i);

         if(bh==WRONG_VALUE || bl==WRONG_VALUE)

            continue;

         double max=high[bh];

         double min=low[bl];

         BufferBL[i]=Pr0-min;

         BufferBR[i]=max-Pr0;

        }

     }

   switch(InpMethod)

     {

      case MODE_EMA  :

        if(ExponentialMAOnBuffer(rates_total,prev_calculated,0,period_ind,BufferBL,BufferAvgBL)==0) return 0;

        if(ExponentialMAOnBuffer(rates_total,prev_calculated,0,period_ind,BufferBR,BufferAvgBR)==0) return 0;

        break;

      case MODE_SMMA :

        if(SmoothedMAOnBuffer(rates_total,prev_calculated,0,period_ind,BufferBL,BufferAvgBL)==0) return 0;

        if(SmoothedMAOnBuffer(rates_total,prev_calculated,0,period_ind,BufferBR,BufferAvgBR)==0) return 0;

        break;

      case MODE_LWMA :

        if(LinearWeightedMAOnBuffer(rates_total,prev_calculated,0,period_ind,BufferBL,BufferAvgBL,weight_sum_bl)==0) return 0;

        if(LinearWeightedMAOnBuffer(rates_total,prev_calculated,0,period_ind,BufferBR,BufferAvgBR,weight_sum_br)==0) return 0;

        break;

      //---MODE_SMA

      default        :

        if(SimpleMAOnBuffer(rates_total,prev_calculated,0,period_ind,BufferBL,BufferAvgBL)==0) return 0;

        if(SimpleMAOnBuffer(rates_total,prev_calculated,0,period_ind,BufferBR,BufferAvgBR)==0) return 0;

        break;

     }

   switch(InpMethod)

     {

      case MODE_EMA  :

        if(ExponentialMAOnBuffer(rates_total,prev_calculated,period_ind,period_sm,BufferAvgBL,BufferAvgSmBL)==0) return 0;

        if(ExponentialMAOnBuffer(rates_total,prev_calculated,period_ind,period_sm,BufferAvgBR,BufferAvgSmBR)==0) return 0;

        break;

      case MODE_SMMA :

        if(SmoothedMAOnBuffer(rates_total,prev_calculated,period_ind,period_sm,BufferAvgBL,BufferAvgSmBL)==0) return 0;

        if(SmoothedMAOnBuffer(rates_total,prev_calculated,period_ind,period_sm,BufferAvgBR,BufferAvgSmBR)==0) return 0;

        break;

      case MODE_LWMA :

        if(LinearWeightedMAOnBuffer(rates_total,prev_calculated,period_ind,period_sm,BufferAvgBL,BufferAvgSmBL,weight_sum_sbl)==0) return 0;

        if(LinearWeightedMAOnBuffer(rates_total,prev_calculated,period_ind,period_sm,BufferAvgBR,BufferAvgSmBR,weight_sum_sbr)==0) return 0;

        break;

      //---MODE_SMA

      default        :

        if(SimpleMAOnBuffer(rates_total,prev_calculated,period_ind,period_sm,BufferAvgBL,BufferAvgSmBL)==0) return 0;

        if(SimpleMAOnBuffer(rates_total,prev_calculated,period_ind,period_sm,BufferAvgBR,BufferAvgSmBR)==0) return 0;

        break;

     }



//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      BufferBulls[i]=BufferAvgSmBL[i]/Point();

      BufferBears[i]=BufferAvgSmBR[i]/Point();

     }



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

   return(rates_total);

  }

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

//| >72@0I05B 8=45:A <0:A8<0;L=>3> 7=0G5=8O B09<A5@88 High          |

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

int Highest(const int count,const int start)

  {

   double array[];

   ArraySetAsSeries(array,true);

   return(CopyHigh(Symbol(),PERIOD_CURRENT,start,count,array)==count ? ArrayMaximum(array)+start : WRONG_VALUE);

  }

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

//| >72@0I05B 8=45:A <8=8<0;L=>3> 7=0G5=8O B09<A5@88 Low            |

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

int Lowest(const int count,const int start)

  {

   double array[];

   ArraySetAsSeries(array,true);

   return(CopyLow(Symbol(),PERIOD_CURRENT,start,count,array)==count ? ArrayMinimum(array)+start : WRONG_VALUE);

   return WRONG_VALUE;

  }

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

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