MAGNUS_Cycles

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
0 Views
0 Downloads
0 Favorites
MAGNUS_Cycles
ÿþ//+------------------------------------------------------------------+

//|                                                MAGNUS_Cycles.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 "MAGNUS Cycles indicator"

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots   4

//--- plot Zone

#property indicator_label1  "Zone"

#property indicator_type1   DRAW_FILLING

#property indicator_color1  clrGainsboro,clrGainsboro

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot L1

#property indicator_label2  "Magnus Cicles"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrDodgerBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- plot L2

#property indicator_label3  "Smooth 1"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

//--- plot L3

#property indicator_label4  "Smooth 2"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrLimeGreen

#property indicator_style4  STYLE_SOLID

#property indicator_width4  2

//--- input parameters

input uint     InpPeriod1     =  92;      // Period

input uint     InpPeriod2     =  21;      // Primary smoothing

input uint     InpPeriod3     =  96;      // Secondary smoothing

input double   InpOverbought  = -20.0;    // Overbought

input double   InpOversold    =  80.0;    // Oversold

//--- indicator buffers

double         BufferZone1[];

double         BufferZone2[];

double         BufferL1[];

double         BufferL2[];

double         BufferL3[];

//--- global variables

double         overbought;

double         oversold;

int            period1;

int            period2;

int            period3;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period1=int(InpPeriod1<1 ? 1 : InpPeriod1);

   period2=int(InpPeriod2<1 ? 1 : InpPeriod2);

   period3=int(InpPeriod3<1 ? 1 : InpPeriod3);

   double ob=-fabs(InpOverbought);

   double os=-fabs(InpOversold);

   overbought=(ob>0 ? 0 : ob<-99.0 ? -99.0 : ob);

   oversold=(os<-100.0 ? -100.0 : os>=overbought ? overbought-1.0 : os);

   if(overbought<=oversold) overbought=oversold+1.0;

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferZone1,INDICATOR_DATA);

   SetIndexBuffer(1,BufferZone2,INDICATOR_DATA);

   SetIndexBuffer(2,BufferL1,INDICATOR_DATA);

   SetIndexBuffer(3,BufferL2,INDICATOR_DATA);

   SetIndexBuffer(4,BufferL3,INDICATOR_DATA);

//--- setting plot buffer parameters

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferZone1,true);

   ArraySetAsSeries(BufferZone2,true);

   ArraySetAsSeries(BufferL1,true);

   ArraySetAsSeries(BufferL2,true);

   ArraySetAsSeries(BufferL3,true);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"MAGNUS Cycles("+(string)period1+","+(string)period2+","+(string)period3+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,overbought);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,oversold);

   IndicatorSetString(INDICATOR_LEVELTEXT,0,"Overbought");

   IndicatorSetString(INDICATOR_LEVELTEXT,1,"Oversold");

   IndicatorSetDouble(INDICATOR_MAXIMUM,15);

   IndicatorSetDouble(INDICATOR_MINIMUM,-110);

//---

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

   ArraySetAsSeries(close,true);

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

   if(rates_total<fmax(period1,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(BufferZone1,0);

      ArrayInitialize(BufferZone2,0);

      ArrayInitialize(BufferL1,0);

      ArrayInitialize(BufferL2,0);

      ArrayInitialize(BufferL3,0);

     }



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

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

     {

      int bh=ArrayMaximum(high,i,period1);

      int bl=ArrayMinimum(low,i,period1);

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

         continue;

      BufferL1[i]=100.0*(close[i]-high[bh])/(high[bh]-low[bl]);

      BufferL2[i]=GetMA(rates_total,i,MODE_SMA,period2,BufferL1,BufferL2);

      BufferL3[i]=GetMA(rates_total,i,MODE_EMA,period3,BufferL1,BufferL3);

     }

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

     {

      BufferZone1[i]=BufferZone2[i]=0;

      if(BufferL3[i]>BufferL2[i])

        {

         BufferZone1[i]=0;

         BufferZone2[i]=-100;

        }

     }



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

   return(rates_total);

  }

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

//| >72@0I05B MA ?> B8?C                                            |

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

double GetMA(const int rates_total,const int shift,const ENUM_MA_METHOD method,const int period_ma,const double &buffer_price[],const double &buffer_ma[])

  {

   switch(method)

     {

      case MODE_EMA  : return EMA(rates_total,buffer_price[shift],buffer_ma[shift+1],period_ma,shift);

      case MODE_SMMA : return SMMA(rates_total,buffer_price,buffer_ma[shift+1],period_ma,shift);

      case MODE_LWMA : return LWMA(rates_total,buffer_price,period_ma,shift);

      //---MODE_SMA

      default        : return SMA(rates_total,buffer_price,period_ma,shift);

     }

  }

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

//| Simple Moving Average                                            |

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

double SMA(const int rates_total,const double &array_src[],const int period,const int shift)

  {

   if(period<1 || shift>rates_total-period-1)

      return array_src[shift];

   double sum=0;

   for(int i=0; i<period; i++)

      sum+=array_src[shift+i];

   return(sum/period);

  }

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

//| Exponential Moving Average                                       |

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

double EMA(const int rates_total,const double price,const double prev,const int period,const int shift)

  {

   return(shift>=rates_total-2 || period<1 ? price : prev+2.0/(1+period)*(price-prev));

  }

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

//| Linear Weighted Moving Average                                   |

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

double LWMA(const int rates_total,const double &array_src[],const int period,const int shift)

  {

   if(period<1 || shift>rates_total-period-1)

      return 0;

   double sum=0;

   double weight=0;

   for(int i=0; i<period; i++)

     {

      weight+=(period-i);

      sum+=array_src[shift+i]*(period-i);

     }

   return(weight>0 ? sum/weight : 0);

  }

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

//| Smoothed Moving Average                                          |

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

double SMMA(const int rates_total,const double &array_src[],const double prev,const int period,const int shift)

  {

   if(period<1 || shift>rates_total-period-1)

      return 0;

   double smma=0;

   if(shift==rates_total-period-1)

      smma=SMA(rates_total,array_src,period,shift);

   else if(shift<rates_total-period-1)

     {

      double sum=0;

      for(int i = 0; i<period; i++)

         sum+=array_src[shift+i+1];

      smma=(sum-prev+array_src[shift])/period;

     }

   return smma;

  }

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

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