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

//|                                                   Timed_MACD.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 "Timed MACD oscillator"

#property indicator_separate_window

#property indicator_buffers 6

#property indicator_plots   2

//--- plot TmMACD

#property indicator_label1  "Timed MACD"

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrGreen,clrRed,clrDarkGray

#property indicator_style1  STYLE_SOLID

#property indicator_width1  8

//--- plot Signal

#property indicator_label2  "Signal"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input uint                 InpFastDuration   =  60;            // Fast duration (seconds)

input uint                 InpSlowDuration   =  2500;          // Slow duration (seconds)

input uint                 InpPeriodSignal   =  9;             // Signal period

input ENUM_MA_METHOD       InpMethod         =  MODE_SMA;      // Method

input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // Applied price

input double               InpThresholdUP    = 30;             // Upper threshold (points)

input double               InpThresholdDN    =-30;             // Lower threshold (points)

//--- indicator buffers

double         BufferTmMACD[];

double         BufferColors[];

double         BufferSignal[];

double         BufferRawFast[];

double         BufferRawSlow[];

double         BufferPrice[];

//--- global variables

double         threshold_up;

double         threshold_dn;

int            duration_fast;

int            duration_slow;

int            period_sig;

int            handle_ma;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   duration_fast=int(InpFastDuration<1 ? 1 : InpFastDuration);

   duration_slow=int(InpSlowDuration<1 ? 1 : InpSlowDuration);

   period_sig=int(InpPeriodSignal<1 ? 1 : InpPeriodSignal);

   if(duration_fast*60<(int)Period()) duration_fast=(int)Period();

   threshold_up=(InpThresholdUP<0 ? 0 : InpThresholdUP*Point());

   threshold_dn=(fabs(InpThresholdDN)<0 ? 0 : -fabs(InpThresholdDN)*Point());

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferTmMACD,INDICATOR_DATA);

   SetIndexBuffer(1,BufferColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,BufferSignal,INDICATOR_DATA);

   SetIndexBuffer(3,BufferRawFast,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,BufferRawSlow,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferPrice,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Timed MACD (fast "+(string)duration_fast+", slow "+(string)duration_slow+" sec duration, "+(string)period_sig+" signal period)");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,threshold_up);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,threshold_dn);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferTmMACD,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(BufferSignal,true);

   ArraySetAsSeries(BufferRawFast,true);

   ArraySetAsSeries(BufferRawSlow,true);

   ArraySetAsSeries(BufferPrice,true);

//--- create MA handle

   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(time,true);

//--- @>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(BufferTmMACD,EMPTY_VALUE);

      ArrayInitialize(BufferColors,2);

      ArrayInitialize(BufferSignal,EMPTY_VALUE);

      ArrayInitialize(BufferRawFast,0);

      ArrayInitialize(BufferRawSlow,0);

      ArrayInitialize(BufferPrice,0);

     }



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

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

   copied=CopyBuffer(handle_ma,0,0,count,BufferPrice);

   if(copied!=count) return 0;

   

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

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

     {

      datetime time_slow=time[i]-duration_slow;

      datetime time_fast=time[i]-duration_fast;

      if(time_slow==0 || time_fast==0)

         continue;

      int bs=BarShift(NULL,PERIOD_CURRENT,time_slow);

      int bf=BarShift(NULL,PERIOD_CURRENT,time_fast);

      if(bs==WRONG_VALUE || bf==WRONG_VALUE)

         continue;

      int period_fast=bf-i+1;

      int period_slow=bs-i+1;

      BufferRawFast[i]=GetMA(rates_total,i,InpMethod,period_fast,BufferPrice,BufferRawFast);

      BufferRawSlow[i]=GetMA(rates_total,i,InpMethod,period_slow,BufferPrice,BufferRawSlow);

      BufferTmMACD[i]=BufferRawFast[i]-BufferRawSlow[i];

     }

   

//--- &25B 8 A83=0;L=0O ;8=8O

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

     {

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

      BufferSignal[i]=GetMA(rates_total,i,MODE_EMA,period_sig,BufferTmMACD,BufferSignal);

     }



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

   return(rates_total);

  }

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

//| >72@0I05B A<5I5=85 10@0 ?> 2@5<5=8                              |

//| https://www.mql5.com/ru/forum/743/page11#comment_7010041         |

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

int BarShift(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const datetime time,bool exact=false)

  {

   int res=Bars(symbol_name,timeframe,time+1,UINT_MAX);

   if(exact) if((timeframe!=PERIOD_MN1 || time>TimeCurrent()) && res==Bars(symbol_name,timeframe,time-PeriodSeconds(timeframe)+1,UINT_MAX)) return(WRONG_VALUE);

   return res;

  }

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

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