Strength_Osc

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

//|                                                 Strength_Osc.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 "MA, MACD or Stochastic strength indicator"

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   1

//--- plot Strength

#property indicator_label1  "Strength Osc"

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrGreen,clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  8

//--- enums

enum ENUM_MODE_STRENGTH

  {

   MODE_OSC_MA,         // MA strength

   MODE_OSC_MACD,       // MACD strength

   MODE_OSC_STO         // Stochactic strength

  };

//--- input parameters

input ENUM_MODE_STRENGTH   InpModeOsc        =  MODE_OSC_MA;   // Strength mode

input uint                 InpPeriodFastMA   =  13;            // Fast MA period

input uint                 InpPeriodSlowMA   =  21;            // Slow MA period

input uint                 InpPeriodFastMACD =  12;            // MACD fast EMA period

input uint                 InpPeriodSlowMACD =  26;            // MACD slow EMA period

input uint                 InpPeriodSigMACD  =  9;             // MACD signal period

input uint                 InpPeriodK        =  14;            // Stochastic period %K

input uint                 InpPeriodD        =  3;             // Stochastic period %D

input uint                 InpSlowing        =  3;             // Stochastic slowing

input ENUM_MA_METHOD       InpMethodOSC      =  MODE_EMA;      // Method MA,Stochastic

input ENUM_APPLIED_PRICE   InpPriceMA        =  PRICE_CLOSE;   // MA,MACD applied price

input ENUM_STO_PRICE       InpPriceField     =  STO_LOWHIGH;   // Stochastic price field

input uint                 InpBarsAvg        =  50;            // Number of bars for calculating levels

//--- indicator buffers

double         BufferOSC[];

double         BufferColors[];

double         BufferMain[];

double         BufferSig[];

//--- global variables

int            period_fast_ma;

int            period_slow_ma;

int            period_fast_macd;

int            period_slow_macd;

int            period_sig_macd;

int            period_k;

int            period_d;

int            slowing;

int            bars_levels;

int            handle_ind1;

int            handle_ind2;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_fast_ma=int(InpPeriodFastMA<1 ? 1 : InpPeriodFastMA);

   period_slow_ma=int(InpPeriodSlowMA==period_fast_ma ? period_fast_ma+1 : InpPeriodSlowMA<1 ? 1 : InpPeriodSlowMA);

   period_fast_macd=int(InpPeriodFastMACD<1 ? 1 : InpPeriodFastMACD);

   period_slow_macd=int(InpPeriodSlowMACD==period_fast_macd ? period_fast_macd+1 : InpPeriodSlowMACD<1 ? 1 : InpPeriodSlowMACD);

   period_sig_macd=int(InpPeriodSigMACD<1 ? 1 : InpPeriodSigMACD);

   period_k=int(InpPeriodK<1 ? 1 : InpPeriodK);

   period_d=int(InpPeriodD<1 ? 1 : InpPeriodD);

   slowing=int(InpSlowing<1 ? 1 : InpSlowing);

   bars_levels=int(InpBarsAvg<1 ? 1 : InpBarsAvg);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferOSC,INDICATOR_DATA);

   SetIndexBuffer(1,BufferColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,BufferMain,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,BufferSig,INDICATOR_CALCULATIONS);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferOSC,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(BufferMain,true);

   ArraySetAsSeries(BufferSig,true);

//--- setting indicator parameters

   string datas="";

   string name="";

//--- create handle and set parameters

   ResetLastError();

   if(InpModeOsc==MODE_OSC_MA)

     {

      datas=(string)period_fast_ma+","+(string)period_slow_ma;

      name="MA";

      handle_ind1=iMA(NULL,PERIOD_CURRENT,period_fast_ma,0,InpMethodOSC,InpPriceMA);

      if(handle_ind1==INVALID_HANDLE)

        {

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

         return INIT_FAILED;

        }

      handle_ind2=iMA(NULL,PERIOD_CURRENT,period_slow_ma,0,InpMethodOSC,InpPriceMA);

      if(handle_ind2==INVALID_HANDLE)

        {

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

         return INIT_FAILED;

        }

     }

   else if(InpModeOsc==MODE_OSC_MACD)

     {

      datas=(string)period_fast_macd+","+(string)period_slow_macd+","+(string)period_sig_macd;

      name="MACD";

      handle_ind1=iMACD(NULL,PERIOD_CURRENT,period_fast_macd,period_slow_macd,period_sig_macd,InpPriceMA);

      if(handle_ind1==INVALID_HANDLE)

        {

         Print("The iMACD(",datas,") object was not created: Error ",GetLastError());

         return INIT_FAILED;

        }

     }

   else

     {

      datas=(string)period_k+","+(string)period_d+","+(string)slowing;

      name="Stochastic";

      handle_ind1=iStochastic(NULL,PERIOD_CURRENT,period_k,period_d,slowing,InpMethodOSC,InpPriceField);

      if(handle_ind1==INVALID_HANDLE)

        {

         Print("The iStochastic(",datas,") object was not created: Error ",GetLastError());

         return INIT_FAILED;

        }

     }

   IndicatorSetString(INDICATOR_SHORTNAME,name+" Strength Oscillator ("+datas+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

   IndicatorSetInteger(INDICATOR_LEVELS,2);

//---

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

  {

//--- @>25@:0 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<4) 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(BufferOSC,EMPTY_VALUE);

      ArrayInitialize(BufferColors,EMPTY_VALUE);

      ArrayInitialize(BufferMain,0);

      ArrayInitialize(BufferSig,0);

     }

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

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

   if(InpModeOsc!=MODE_OSC_MA)

     {

      copied=CopyBuffer(handle_ind1,0,0,count,BufferMain);

      if(copied!=count) return 0;

      copied=CopyBuffer(handle_ind1,1,0,count,BufferSig);

      if(copied!=count) return 0;

     }

   else

     {

      copied=CopyBuffer(handle_ind1,0,0,count,BufferMain);

      if(copied!=count) return 0;

      copied=CopyBuffer(handle_ind2,0,0,count,BufferSig);

      if(copied!=count) return 0;

     }

   

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

   int count_pos=0;

   int count_neg=0;

   double total_pos=0;

   double avg_pos=0; 

   double total_neg=0;

   double avg_neg=0;

   for(int i=limit; i>=0; i--)

     {

      BufferOSC[i]=BufferMain[i]-BufferSig[i];

      BufferColors[i]=(BufferOSC[i]>BufferOSC[i+1] ? 0 : BufferOSC[i]<BufferOSC[i+1] ? 1 : 2);

      if(i==0)

        {

         for(int j=0;j<bars_levels;j++)

           {

            if(BufferOSC[j]>0)

              {

               count_pos+=1;

               total_pos+=BufferOSC[j];

              }

            else if(BufferOSC[j]<0)

              {

               count_neg+=1;

               total_neg+=BufferOSC[j];

              }

           }

         avg_pos=(count_pos!=0 ? total_pos/count_pos : 0);

         avg_neg=(count_neg!=0 ? total_neg/count_neg : 0);

         IndicatorSetDouble(INDICATOR_LEVELVALUE,0,avg_pos);

         IndicatorSetDouble(INDICATOR_LEVELVALUE,1,avg_neg);

        }

     }

   

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