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

//|                                                       Influx.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 "Influx oscillator"

#property indicator_separate_window

#property indicator_buffers 6

#property indicator_plots   1

//--- plot Influx

#property indicator_label1  "Influx"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

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

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

input ENUM_MA_METHOD       InpMethod         =  MODE_SMA;      // Method

input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // Applied price

//--- indicator buffers

double         BufferInflux[];

double         BufferRawFast[];

double         BufferRawSlow[];

double         BufferAvgFast[];

double         BufferAvgSlow[];

double         BufferPrice[];

//--- global variables

int            duration_fast;

int            duration_slow;

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

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

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferInflux,INDICATOR_DATA);

   SetIndexBuffer(1,BufferRawFast,INDICATOR_CALCULATIONS);

   SetIndexBuffer(2,BufferRawSlow,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,BufferAvgFast,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,BufferAvgSlow,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferPrice,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Influx (fast "+(string)duration_fast+", slow "+(string)duration_slow+" sec duration)");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferInflux,true);

   ArraySetAsSeries(BufferRawFast,true);

   ArraySetAsSeries(BufferRawSlow,true);

   ArraySetAsSeries(BufferAvgFast,true);

   ArraySetAsSeries(BufferAvgSlow,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 || 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-1;

      ArrayInitialize(BufferInflux,EMPTY_VALUE);

      ArrayInitialize(BufferRawFast,0);

      ArrayInitialize(BufferRawSlow,0);

      ArrayInitialize(BufferAvgFast,0);

      ArrayInitialize(BufferAvgSlow,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;

   

   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>1 ? period_fast : 1),BufferPrice,BufferRawFast);

      BufferRawSlow[i]=GetMA(rates_total,i,InpMethod,(period_slow>1 ? period_slow : 1),BufferPrice,BufferRawSlow);

     }



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

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

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

      double fast=2.0*BufferRawFast[i]-BufferAvgFast[i];

      double slow=2.0*BufferRawSlow[i]-BufferAvgSlow[i];

      BufferInflux[i]=(fast-slow)/Point();

     }



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