Author: Copyright © 2018, Лукашук Виктор Геннадьевич
0 Views
0 Downloads
0 Favorites
MACDfMFI
ÿþ//+------------------------------------------------------------------+ 

//|                                                     MACD^MFI.mq5 | 

//|                     Copyright © 2018, C:0HC: 8:B>@ 5==04L528G | 

//|                                                                  | 

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

#property copyright "Copyright © 2018, C:0HC: 8:B>@ 5==04L528G"

#property link "" 

//---- =><5@ 25@A88 8=48:0B>@0

#property version   "1.00"

//---- >B@8A>2:0 8=48:0B>@0 2 >B45;L=>< >:=5

#property indicator_separate_window 

//---- :>;8G5AB2> 8=48:0B>@=KE 1CD5@>2 4

#property indicator_buffers 4 

//---- 8A?>;L7>20=> 2A53> B@8 3@0D8G5A:8E ?>AB@>5=8O

#property indicator_plots   3

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

//|  0@0<5B@K >B@8A>2:8 MACD                  |

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

//---- >B@8A>2:0 8=48:0B>@0 2 2845 38AB>3@0<<K

#property indicator_type1 DRAW_HISTOGRAM

//---- 2 :0G5AB25 F25B0 4803@0<<K MACD 8=48:0B>@0 8A?>;L7>20= A5@K9 F25B

#property indicator_color1 clrGray

//---- ;8=8O 8=48:0B>@0 - =5?@5@K2=0O :@820O

#property indicator_style1 STYLE_SOLID

//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 2

#property indicator_width1 2

//---- >B>1@065=85 <5B:8 8=48:0B>@0

#property indicator_label1 "MACD"

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

//|  0@0<5B@K >B@8A>2:8 A83=0;L=>9 ;8=88 MACD |

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

//---- >B@8A>2:0 8=48:0B>@0 2 2845 ;8=88

#property indicator_type2 DRAW_LINE

//---- 2 :0G5AB25 F25B0 A83=0;L=>9 ;8=88 8A?>;L7>20= :@0A=K9 F25B

#property indicator_color2 clrRed

//---- ;8=8O 8=48:0B>@0 - HB@8E?C=:B8@=0O :@820O

#property indicator_style2 STYLE_DASHDOTDOT

//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 1

#property indicator_width2 1

//---- >B>1@065=85 <5B:8 A83=0;L=>9 ;8=88

#property indicator_label2  "Signal Line"

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

//|  0@0<5B@K >B@8A>2:8 MFI                   |

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

//---- >B@8A>2:0 8=48:0B>@0 2 2845 ;8=88

#property indicator_type3   DRAW_COLOR_LINE

//---- 2 :0G5AB25 F25B0 ;8=88 MFI 8A?>;L7>20=K

#property indicator_color3 clrGray,clrDodgerBlue,clrMagenta

//---- ;8=8O 8=48:0B>@0 - A?;>H=0O :@820O

#property indicator_style3 STYLE_SOLID

//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 3

#property indicator_width3 3

//---- >B>1@065=85 <5B:8 A83=0;L=>9 ;8=88

#property indicator_label3  "MFI"

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

//|  >1JO2;5=85 ?5@5G8A;5=89          |

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

enum ENUM_APPLIED_PRICE_ //"8? :>=AB0=BK

  {

   PRICE_CLOSE_ = 1,     //PRICE_CLOSE

   PRICE_OPEN_,          //PRICE_OPEN

   PRICE_HIGH_,          //PRICE_HIGH

   PRICE_LOW_,           //PRICE_LOW

   PRICE_MEDIAN_,        //PRICE_MEDIAN

   PRICE_TYPICAL_,       //PRICE_TYPICAL

   PRICE_WEIGHTED_,      //PRICE_WEIGHTED

   PRICE_SIMPL_,         //PRICE_SIMPL_

   PRICE_QUARTER_,       //PRICE_QUARTER_

   PRICE_TRENDFOLLOW0_,  //PRICE_TRENDFOLLOW0_

   PRICE_TRENDFOLLOW1_,  // TrendFollow_2 Price 

   PRICE_DEMARK_         // Demark Price 

  };

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

//|  %+  " + "      |

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

input int Fast_MA = 12; //?5@8>4 1KAB@>3> <C28=30 MACD

input int Slow_MA = 26; //3;C18=0  <54;5==>3> <C28=30 MACD

input ENUM_MA_METHOD MA_Method_=MODE_EMA; //<5B>4 CA@54=5=8O 8=48:0B>@0

input int Signal_SMA=9; //?5@8>4 A83=0;L=>9 ;8=88 

input ENUM_APPLIED_PRICE_ AppliedPrice_=PRICE_CLOSE_;//F5=>20O :>=AB0=B0

input uint MFIPeriod=12;  // ?5@8>4 MFI

input ENUM_APPLIED_VOLUME MFIVolumeType=VOLUME_TICK;  // >1JQ< MFI  

input double caliber=4.0; //<0AHB018@>20=85

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

//---- 8=48:0B>@=Ke 1CD5@K

double MACDBuffer[],SignBuffer[],MFIBuffer[],ColorMFIBuffer[];

double positive_,negative_;

int macd_start,sig_start,mfi_start;

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

// ?8A0=85 DC=:F88 iPriceSeries                                     |

// ?8A0=85 :;0AA0 Moving_Average                                    | 

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

#include <SmoothAlgorithms.mqh> 

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

//| MACD indicator initialization function                           | 

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

void OnInit()

  {

//---- =8F80;870F8O ?5@5<5==KE =0G0;0 >BAGQB0 40==KE  

   if(MA_Method_!=MODE_EMA)

      macd_start=MathMax(Fast_MA,Slow_MA);

   else macd_start=0;

   sig_start=macd_start+Signal_SMA;

   mfi_start=sig_start+int(MFIPeriod)+2;

//---- ?@52@0I5=85 48=0<8G5A:>3> <0AA820 MACDBuffer 2 8=48:0B>@=K9 1CD5@

   SetIndexBuffer(0,MACDBuffer,INDICATOR_DATA);

//---- >ACI5AB2;5=85 A42830 =0G0;0 >BAGQB0 >B@8A>2:8 8=48:0B>@0

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,macd_start);

//---- CAB0=>2:0 7=0G5=89 8=48:0B>@0, :>B>@K5 =5 1C4CB 2848<K =0 3@0D8:5

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);

//---- ?@52@0I5=85 48=0<8G5A:>3> <0AA820 SignBuffer 2 8=48:0B>@=K9 1CD5@

   SetIndexBuffer(1,SignBuffer,INDICATOR_DATA);

//---- >ACI5AB2;5=85 A42830 =0G0;0 >BAGQB0 >B@8A>2:8 8=48:0B>@0

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,sig_start);

//---- CAB0=>2:0 7=0G5=89 8=48:0B>@0, :>B>@K5 =5 1C4CB 2848<K =0 3@0D8:5

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(1,PLOT_SHOW_DATA,false);

//---- ?@52@0I5=85 48=0<8G5A:>3> <0AA820 MFIBuffer 2 8=48:0B>@=K9 1CD5@

   SetIndexBuffer(2,MFIBuffer,INDICATOR_DATA);

//---- >ACI5AB2;5=85 A42830 =0G0;0 >BAGQB0 >B@8A>2:8 8=48:0B>@0

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,mfi_start+1);

//---- CAB0=>2:0 7=0G5=89 8=48:0B>@0, :>B>@K5 =5 1C4CB 2848<K =0 3@0D8:5

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(2,PLOT_SHOW_DATA,false);

//---- ?@52@0I5=85 48=0<8G5A:>3> <0AA820 2 F25B>2>9, 8=45:A=K9 1CD5@   

   SetIndexBuffer(3,ColorMFIBuffer,INDICATOR_COLOR_INDEX);

//---- 8=8F80;870F88 ?5@5<5==>9 4;O :>@>B:>3> 8<5=8 8=48:0B>@0

   string shortname;

   StringConcatenate(shortname,"MACD_MFI(",Fast_MA,",",Slow_MA,",",Signal_SMA,")");

//--- A>740=85 8<5=8 4;O >B>1@065=8O 2 >B45;L=>< ?>4>:=5 8 2> 2A?;K20NI59 ?>4A:07:5

   IndicatorSetString(INDICATOR_SHORTNAME,shortname);

//--- >?@545;5=85 B>G=>AB8 >B>1@065=8O 7=0G5=89 8=48:0B>@0

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);

//---- 7025@H5=85 8=8F80;870F88

  }

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

//| MACD iteration function                                          | 

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

int OnCalculate(

                const int rates_total,    // :>;8G5AB2> 8AB>@88 2 10@0E =0 B5:CI5< B8:5

                const int prev_calculated,// :>;8G5AB2> 8AB>@88 2 10@0E =0 ?@54K4CI5< B8:5

                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 10@>2 =0 4>AB0B>G=>ABL 4;O @0AGQB0

   if(rates_total<mfi_start) return(0);



//---- 1JO2;5=85 F5;KE ?5@5<5==KE

   int first,bar;

//---- 1JO2;5=85 ?5@5<5==KE A ?;020NI59 B>G:>9  

   double price_,fast_ma,slow_ma;



//---- @0AGQB AB0@B>2>3> =><5@0 first 4;O F8:;0 ?5@5AGQB0 10@>2

   if(prev_calculated>rates_total || prev_calculated<=0)// ?@>25@:0 =0 ?5@2K9 AB0@B @0AGQB0 8=48:0B>@0

     {

      first=0; // AB0@B>2K9 =><5@ 4;O @0AGQB0 2A5E 10@>2

      positive_=negative_=NULL;

     }

   else first=prev_calculated-1; // AB0@B>2K9 =><5@ 4;O @0AGQB0 =>2KE 10@>2



//---- >1JO2;5=85 ?5@5<5==KE :;0AA0 CMoving_Average 87 D09;0 MASeries_Cls.mqh

   static CMoving_Average MA1,MA2,MA3;



//---- A=>2=>9 F8:; @0AGQB0 8=48:0B>@0 MACD

   for(bar=first; bar<rates_total; bar++)

     {

      price_=PriceSeries(AppliedPrice_,bar,open,low,high,close);

      fast_ma = MA1.MASeries(0, prev_calculated, rates_total, Fast_MA, MA_Method_, price_, bar, false);

      slow_ma = MA2.MASeries(0, prev_calculated, rates_total, Slow_MA, MA_Method_, price_, bar, false);

      MACDBuffer[bar] = fast_ma - slow_ma;

      SignBuffer[bar] = MA3.SMASeries(macd_start, prev_calculated, rates_total, Signal_SMA, MACDBuffer[bar], bar, false);

     }



//---- @0AGQB AB0@B>2>3> =><5@0 first 4;O F8:;0 ?5@5AGQB0 10@>2 8 AB0@B>20O 8=8F80;870F8O ?5@5<5==KE

   if(prev_calculated>rates_total || prev_calculated<=0) first=mfi_start+1;

//---- A=>2=>9 F8:; @0AGQB0 8=48:0B>@0 MFI

   for(bar=first; bar<rates_total && !IsStopped(); bar++)

     {

      if(MFIVolumeType==VOLUME_TICK) CalculateMFI(bar,MACDBuffer,tick_volume,MFIBuffer);

      else CalculateMFI(bar,MACDBuffer,volume,MFIBuffer);

      ColorMFIBuffer[bar]=0;

      if(MFIBuffer[bar]>SignBuffer[bar]) ColorMFIBuffer[bar]=1;

      if(MFIBuffer[bar]<SignBuffer[bar]) ColorMFIBuffer[bar]=2;

     }

//----     

   return(rates_total);

  }

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

//| Calculate MFI by volume from argument                            |

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

void CalculateMFI(const int index,

                  const double &InBuffer[],

                  const long &VolBuffer[],

                  double &OutBuffer[])

  {

//----

   double dPositiveMF=0.0;

   double dNegativeMF=0.0;

   double dCurrentTP=double(VolBuffer[index]);

   for(int kkk=1; kkk<=int(MFIPeriod); kkk++)

     {

      int iii=index-kkk;

      double dPreviousTP=double(VolBuffer[iii]);

      if(dCurrentTP>dPreviousTP) dPositiveMF+=VolBuffer[iii-1]*dCurrentTP;

      if(dCurrentTP<dPreviousTP) dNegativeMF+=VolBuffer[iii-1]*dCurrentTP;

      dCurrentTP=dPreviousTP;

     }

   if(dNegativeMF) OutBuffer[index]=caliber*_Point*(50.0-100.0/(1+dPositiveMF/dNegativeMF));

   else OutBuffer[index]=caliber*_Point*100.0;

//----

  }

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

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