Differential_Average_By_Sultonov_Signal

Author: Copyright © 2018, Nikolay Kositsin
Price Data Components
2 Views
0 Downloads
0 Favorites
Differential_Average_By_Sultonov_Signal
ÿþ//+---------------------------------------------------------------------+

//|                         Differential_Average_By_Sultonov_Signal.mq5 | 

//|                                  Copyright © 2018, Nikolay Kositsin | 

//|                                 Khabarovsk,   farria@mail.redcom.ru | 

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

//| ;O @01>BK 8=48:0B>@0 D09; SmoothAlgorithms.mqh A;54C5B ?>;>68BL    |

//| 2 ?0?:C (48@5:B>@8N): :0B0;>3_40==KE_B5@<8=0;0\\MQL5\Include        |

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

#property copyright "Copyright © 2018, Nikolay Kositsin"

#property link "farria@mail.redcom.ru"

#property description ""

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

#property version   "1.60"

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

#property indicator_separate_window

//--- D8:A8@>20==0O 2KA>B0 ?>4>:=0 8=48:0B>@0 2 ?8:A5;OE 

#property indicator_height 20

//--- =86=55 8 25@E=55 >3@0=8G5=8O H:0;K >B45;L=>3> >:=0 8=48:0B>@0

#property indicator_maximum +1.9

#property indicator_minimum +0.3

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

//|  >1JO2;5=85 :>=AB0=B                         |

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

#define RESET 0                           // >=AB0=B0 4;O 2>72@0B0 B5@<8=0;C :><0=4K =0 ?5@5AG5B 8=48:0B>@0

#define INDTOTAL 1                        // >=AB0=B0 4;O :>;8G5AB20 >B>1@0605<KE 8=48:0B>@>2

#define INDICATOR_NAME "Differential_Average_By_Sultonov" // >=AB0=B0 4;O 8<5=8 8=48:0B>@0

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

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

#property indicator_buffers 4 // INDTOTAL*4

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

#property indicator_plots   2 // INDTOTAL*2

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

//|  0@0<5B@K >B@8A>2:8 8=48:0B>@0 1            |

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

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

#property indicator_type1   DRAW_COLOR_LINE

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

#property indicator_color1 clrBlue,clrRed

//--- ;8=8O 8=48:0B>@0 - HB@8E

#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  "Signal line"

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

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

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

//--- >B@8A>2:0 8=48:0B>@0 2 2845 G5BK@5EF25B=KE 7=0G:>2

#property indicator_type2 DRAW_COLOR_ARROW

//--- 2 :0G5AB25 F25B>2 ?OB8F25B=>9 38AB>3@0<<K 8A?>;L7>20=K

#property indicator_color2 clrBlue,clrRed

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

#property indicator_style2 STYLE_SOLID

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

#property indicator_width2 5

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

#property indicator_label2  "Signal Arrow"

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

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

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

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

  {

   PRICE_CLOSE_ = 1,     // Close

   PRICE_OPEN_,          // Open

   PRICE_HIGH_,          // High

   PRICE_LOW_,           // Low

   PRICE_MEDIAN_,        // Median Price (HL/2)

   PRICE_TYPICAL_,       // Typical Price (HLC/3)

   PRICE_WEIGHTED_,      // Weighted Close (HLCC/4)

   PRICE_SIMPL_,         // Simple Price (OC/2)

   PRICE_QUARTER_,       // Quarted Price (HLOC/4) 

   PRICE_TRENDFOLLOW0_,  // TrendFollow_1 Price 

   PRICE_TRENDFOLLOW1_,  // TrendFollow_2 Price 

   PRICE_DEMARK_         // Demark Price 

  };

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

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

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

enum Smooth_Method

  {

   MODE_SMA_,  // SMA

   MODE_EMA_,  // EMA

   MODE_SMMA_, // SMMA

   MODE_LWMA_, // LWMA

   MODE_JJMA,  // JJMA

   MODE_JurX,  // JurX

   MODE_ParMA, // ParMA

   MODE_T3,    // T3

   MODE_VIDYA, // VIDYA

   MODE_AMA,   // AMA

  };

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

//| E>4=K5 ?0@0<5B@K 8=48:0B>@0                 |

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

input ENUM_TIMEFRAMES TimeFrame=PERIOD_H4;   // 5@8>4 3@0D8:0

input uint i_nPeriod=1000;  // Calculate period / 5@8>4 @0AG5B0

input Smooth_Method XMethod=MODE_SMA_; //<5B>4 CA@54=5=8O A3;06820=8O 

input uint XLength=12; //3;C18=0 A3;06820=8O                    

input int XPhase=15; //?0@0<5B@ A3;06820=8O,

//---- 4;O JJMA 87<5=ONI89AO 2 ?@545;0E -100 ... +100, 2;8O5B =0 :0G5AB2> ?5@5E>4=>3> ?@>F5AA0;

//---- ;O VIDIA MB> ?5@8>4 CMO, 4;O AMA MB> ?5@8>4 <54;5==>9 A:>;L7OI59

input Applied_price_ IPC=PRICE_CLOSE_;//F5=>20O :>=AB0=B0

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

//--- >1JO2;5=85 F5;>G8A;5==KE ?5@5<5==KE =0G0;0 >BAG5B0 40==KE

int min_rates_total;

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

//|  >;CG5=85 B09<D@59<0 2 2845 AB@>:8                              |

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

string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)

  {return(StringSubstr(EnumToString(timeframe),7,-1));}

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

//|  ;0AA 8=48:0B>@=KE 1CD5@>2                                      |

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

class CIndBuffers

  {

   //---

public:

   double            m_LineBuffer[];

   double            m_ColorLineBuffer[];

   double            m_ArrowBuffer[];

   double            m_ColorArrowBuffer[];

   int               m_Handle;

   ENUM_TIMEFRAMES   m_TimeFrame;

   //--- 

  };

//--- >1JO2;5=85 48=0<8G5A:8E <0AA82>2, :>B>@K5 2 40;L=59H5<

//--- 1C4CB 8A?>;L7>20=K 2 :0G5AB25 8=48:0B>@=KE 1CD5@>2

CIndBuffers Ind[INDTOTAL];

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

//|Differential_Average_By_Sultonov indicator initialization function| 

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

bool IndInit(uint Number)

  {

//--- ?@>25@:0 ?5@8>4>2 3@0D8:>2 =0 :>@@5:B=>ABL

   if(Ind[Number].m_TimeFrame<Period() && Ind[Number].m_TimeFrame!=PERIOD_CURRENT)

     {

      Print("IndInit(",Number,"): 5@8>4 3@0D8:0 4;O 8=48:0B>@0 Differential_Average_By_Sultonov =5 <>65B 1KBL <5=LH5 ?5@8>40 B5:CI53> 3@0D8:0");

      return(false);

     }

//--- ?>;CG5=85 E5=4;>2 8=48:0B>@>2  

   Ind[Number].m_Handle=iCustom(Symbol(),Ind[Number].m_TimeFrame,"Differential_Average_By_Sultonov",i_nPeriod,XMethod,XLength,XPhase,IPC,0);

   if(Ind[Number].m_Handle==INVALID_HANDLE)

     {

      Print("IndInit(",Number,"): 5 C40;>AL ?>;CG8BL E5=4; 8=48:0B>@0 Differential_Average_By_Sultonov");

      return(false);

     }

//---

   uint BIndex=Number*4+0;

   uint PIndex=Number*2+0;

   InitTsIndBuffer(BIndex,PIndex,Ind[Number].m_LineBuffer,EMPTY_VALUE,min_rates_total);

   InitTsIndColorBuffer(BIndex+1,Ind[Number].m_ColorLineBuffer);

   InitTsIndArrBuffer(BIndex+2,PIndex+1,Ind[Number].m_ArrowBuffer,EMPTY_VALUE,min_rates_total);

   InitTsIndColorBuffer(BIndex+3,Ind[Number].m_ColorArrowBuffer);

//---   

   string tmf=GetStringTimeframe(Ind[Number].m_TimeFrame);

   PlotIndexSetString(PIndex+0,PLOT_LABEL,INDICATOR_NAME+"Line("+tmf+")");

   PlotIndexSetString(PIndex+1,PLOT_LABEL,INDICATOR_NAME+"Arrow("+tmf+")");

//--- 7025@H5=85 8=8F80;870F88 >4=>3> 8=48:0B>@0

   return(true);

  }

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

//|  =8F80;870F8O 8=48:0B>@=>3> 1CD5@0 B09<A5@88                    |

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

void InitTsIndBuffer(uint Number,uint Plot,double &IndBuffer[],double Empty_Value,uint Draw_Begin)

  {

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

   SetIndexBuffer(Number,IndBuffer,INDICATOR_DATA);

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

   PlotIndexSetInteger(Plot,PLOT_DRAW_BEGIN,Draw_Begin);

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

   PlotIndexSetDouble(Plot,PLOT_EMPTY_VALUE,Empty_Value);

//--- 8=45:A0F8O M;5<5=B>2 2 1CD5@5 :0: 2 B09<A5@88

   ArraySetAsSeries(IndBuffer,true);

//---

  }

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

//|  =8F80;870F8O 8=48:0B>@=>3> 1CD5@0 B09<A5@88                    |

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

void InitTsIndArrBuffer(uint Number,uint Plot,double &IndBuffer[],double Empty_Value,uint Draw_Begin)

  {

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

   SetIndexBuffer(Number,IndBuffer,INDICATOR_DATA);

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

   PlotIndexSetInteger(Plot,PLOT_DRAW_BEGIN,Draw_Begin);

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

   PlotIndexSetDouble(Plot,PLOT_EMPTY_VALUE,Empty_Value);

//--- 2K1>@ A8<2>;0 4;O >B@8A>2:8

   PlotIndexSetInteger(Plot,PLOT_ARROW,159);

//--- 8=45:A0F8O M;5<5=B>2 2 1CD5@5 :0: 2 B09<A5@88

   ArraySetAsSeries(IndBuffer,true);

//---

  }

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

//|  =8F80;870F8O 8=48:0B>@=>3> 1CD5@0 F25B0 B09<A5@88              |

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

void InitTsIndColorBuffer(uint Number,double &IndColorBuffer[])

  {

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

   SetIndexBuffer(Number,IndColorBuffer,INDICATOR_COLOR_INDEX);

//--- 8=45:A0F8O M;5<5=B>2 2 1CD5@5 :0: 2 B09<A5@88

   ArraySetAsSeries(IndColorBuffer,true);

//---

  }

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

//| IndOnCalculate                                                   | 

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

bool IndOnCalculate(int Number,int Limit,const datetime &Time[],uint Rates_Total,uint Prev_Calculated)

  {

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

   int limit_;

//--- >1JO2;5=85 ;>:0;L=KE ?5@5<5==KE

   datetime Time_[1],Time0;

   static int LastCountBar[INDTOTAL];

//--- @0AG5BK =5>1E>48<>3> :>;8G5AB20 :>?8@C5<KE 40==KE

//--- 8 AB0@B>2>3> =><5@0 limit 4;O F8:;0 ?5@5AG5B0 10@>2

   if(Prev_Calculated>Rates_Total || Prev_Calculated<=0)// ?@>25@:0 =0 ?5@2K9 AB0@B @0AG5B0 8=48:0B>@0

     {

      limit_=Limit;

      LastCountBar[Number]=limit_;

     }

   else limit_=int(MathMin(LastCountBar[Number]+Limit,Rates_Total-2)); // AB0@B>2K9 =><5@ 4;O @0AG5B0 =>2KE 10@>2

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

   for(int bar=int(limit_); bar>=0 && !IsStopped(); bar--)

     {

      //--- >1=C;8< A>45@68<>5 8=48:0B>@=KE 1CD5@>2 4> @0AG5B0

      Ind[Number].m_LineBuffer[bar]=Number+1.0;

      Ind[Number].m_ArrowBuffer[bar]=EMPTY_VALUE;

      Ind[Number].m_ColorLineBuffer[bar]=EMPTY_VALUE;



      Time0=Time[bar];

      //--- :>?8@C5< 2=>2L ?>O282H85AO 40==K5 2 <0AA82

      if(CopyTime(Symbol(),Ind[Number].m_TimeFrame,Time0,1,Time_)<=0) return(RESET);



      if(Time0>=Time_[0] && Time[bar+1]<Time_[0])

        {

         LastCountBar[Number]=bar;

         Ind[Number].m_ArrowBuffer[bar]=Number+1.0;

         Ind[Number].m_ColorLineBuffer[bar]=2;

         double Up[2],Dn[2];

         //--- :>?8@C5< 2=>2L ?>O282H85AO 40==K5 2 <0AA82K

         if(CopyBuffer(Ind[Number].m_Handle,0,Time0,1,Up)<=0) return(RESET);

         if(CopyBuffer(Ind[Number].m_Handle,1,Time0,1,Dn)<=0) return(RESET);



         double diff1=Up[0]-Dn[0];



         if(diff1>0)

           {

            Ind[Number].m_ColorLineBuffer[bar]=0;

            Ind[Number].m_ColorArrowBuffer[bar]=0;

           }



         if(diff1<0)

           {

            Ind[Number].m_ColorLineBuffer[bar]=1;

            Ind[Number].m_ColorArrowBuffer[bar]=1;

           }

        }

      if(Ind[Number].m_ColorLineBuffer[bar+1]!=EMPTY_VALUE && Ind[Number].m_ColorLineBuffer[bar]==EMPTY_VALUE)

        {

         Ind[Number].m_ColorLineBuffer[bar]=Ind[Number].m_ColorLineBuffer[bar+1];

        }

     }

//--- 7025@H5=85 @0AG5B0 >4=>3> 8=48:0B>@0    

   return(true);

  }

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

//|Differential_Average_By_Sultonov indicator initialization function| 

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

int OnInit()

  {

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

   min_rates_total=3;

//--- 8=8F80;870F8O ?5@5<5==KE 

   Ind[0].m_TimeFrame=TimeFrame;

//--- 8=8F80;870F8O 1CD5@>2 8=48:0B>@>2

   for(int count=0; count<INDTOTAL; count++) if(!IndInit(count)) return(INIT_FAILED);

   string word=GetStringTimeframe(Ind[0].m_TimeFrame);

//--- 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,"Differential_Average_By_Sultonov("+word+")");

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

   IndicatorSetInteger(INDICATOR_DIGITS,0);

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

   return(INIT_SUCCEEDED);

  }

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

//| Differential_Average_By_Sultonov 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 @0AG5B0

   if(rates_total<min_rates_total) return(RESET);

   for(int count=0; count<INDTOTAL; count++)

      if(BarsCalculated(Ind[count].m_Handle)<Bars(Symbol(),Ind[count].m_TimeFrame))

         return(prev_calculated);

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

   int limit;

//--- @0AG5B AB0@B>2>3> =><5@0 limit 4;O F8:;0 ?5@5AG5B0 10@>2

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

      limit=rates_total-min_rates_total-1; // AB0@B>2K9 =><5@ 4;O @0AG5B0 2A5E 10@>2

   else limit=rates_total-prev_calculated; // AB0@B>2K9 =><5@ 4;O @0AG5B0 =>2KE 10@>2 

//--- 8=45:A0F8O M;5<5=B>2 2 <0AA820E :0: 2 B09<A5@8OE  

   ArraySetAsSeries(time,true);

   for(int count=0; count<INDTOTAL; count++) if(!IndOnCalculate(count,limit,time,rates_total,prev_calculated)) return(RESET);

//---   

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