UltraAbsolutelyNoLagLwma

Author: Copyright © 2018, Nikolay Kositsin
0 Views
0 Downloads
0 Favorites
UltraAbsolutelyNoLagLwma
ÿþ//+------------------------------------------------------------------+

//|                                     UltraAbsolutelyNoLagLwma.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: :0B0;>3_40==KE_B5@<8=0;0\\MQL5\Include |

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

//---- 02B>@AB2> 8=48:0B>@0

#property copyright "Copyright © 2018, Nikolay Kositsin"

//---- AAK;:0 =0 A09B 02B>@0

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

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

//---- 4;O @0AG5B0 8 >B@8A>2:8 8=48:0B>@0 8A?>;L7>20=> B@8 1CD5@0

#property indicator_buffers 3

//---- 8A?>;L7>20=> >4=> 3@0D8G5A:>5 ?>AB@>5=85

#property indicator_plots   1

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

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

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

//|  0@0<5B@K >B@8A>2:8 70;82:8      |

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

//---- >B@8A>2:0 8=48:0B>@0 2 2845 70;82:8 <564C 42C<O ;8=8O<8

#property indicator_type1   DRAW_COLOR_HISTOGRAM2

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

#property indicator_color1  clrGray,clrMagenta,clrHotPink,clrRed,clrBrown,clrLimeGreen,clrTeal,clrLime,clrPaleGreen

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

#property indicator_width1 4

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

#property indicator_label1 "Ultra AbsolutelyNoLagLwma"

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

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

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

#include <SmoothAlgorithms.mqh> 

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



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

CXMA XMA[];

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

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

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

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_,  //TrendFollow_1 Price 

   PRICE_TRENDFOLLOW1_,  //TrendFollow_2 Price 

   PRICE_DEMARK_         //Demark Price

  };

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

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

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

enum WIDTH

  {

   Width_1=1, // 1

   Width_2,   // 2

   Width_3,   // 3

   Width_4,   // 4

   Width_5    // 5

  };

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

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

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

enum STYLE

  {

   SOLID_,       // !?;>H=0O ;8=8O

   DASH_,        // (B@8E>20O ;8=8O

   DOT_,         // C=:B8@=0O ;8=8O

   DASHDOT_,     // (B@8E-?C=:B8@=0O ;8=8O

   DASHDOTDOT_   // (B@8E-?C=:B8@=0O ;8=8O A 42>9=K<8 B>G:0<8

  };

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

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

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

input uint FLength=7;                                // 3;C18=0 A3;06820=8O                   

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

//----

input Smooth_Method W_Method=MODE_JJMA;             // 5B>4 CA@54=5=8O

input int StartLength=3;                            // !B0@B>2K9 ?5@8>4 CA@54=5=8O                    

input int WPhase=100;                               // 0@0<5B@ CA@54=5=8O

//----  

input uint Step=5;                                  // (03 87<5=5=8O ?5@8>40

input uint StepsTotal=10;                           // >;8G5AB2> 87<5=5=89 ?5@8>40

//----

input Smooth_Method SmoothMethod=MODE_JJMA;         // 5B>4 A3;06820=8O

input int SmoothLength=3;                           // ;C18=0 A3;06820=8O

input int SmoothPhase=100;                          // 0@0<5B@ A3;06820=8O

//----                          

input uint UpLevel=80;                              // #@>25=L ?5@5:C?;5==>AB8 2 %%

input uint DnLevel=20;                              // #@>25=L ?5@5?@>40==>AB8 2 %%

input color UpLevelsColor=Blue;                     // &25B C@>2=O ?5@5:C?;5==>AB8

input color DnLevelsColor=Blue;                     // &25B C@>2=O ?5@5?@>40==>AB8

input STYLE Levelstyle=DASH_;                       // !B8;L C@>2=59

input WIDTH  LevelsWidth=Width_1;                   // ">;I8=0 C@>2=59                       

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

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

double BullsBuffer[];

double BearsBuffer[];

double ColorBuffer[];

//---- >1JO2;5=85 <0AA820 ?5@5<5==KE 4;O E@0=5=8O ?5@8>4>2 A83=0;L=KE ;8=89 8=48:0B>@0 WPR

int period[];

//---- >1JO2;5=85 <0AA82>2 ?5@5<5==KE 4;O E@0=5=8O CA@54=5==KE 7=0G5=89 8=48:0B>@0 RSI

double invelue0[],invelue1[];

//----

double dUpLevel,dDnLevel;

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

uint StTot1,StTot2;

int min_rates_total,min_rates_lwma2,min_rates_xma;

//---- >1JO2;5=85 3;>10;L=KE ?5@5<5==KE

int Count[];

double Price[],Lwma[];

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

//|  5@5AG5B ?>78F88 A0<>3> =>2>3> M;5<5=B0 2 <0AA825               |

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

void Recount_ArrayZeroPos(int &CoArr[],// >72@0B ?> AAK;:5 =><5@0 B5:CI53> 7=0G5=8O F5=>2>3> @O40

                          int Size)

  {

//----

   int numb,Max1,Max2;

   static int count=1;



   Max2=Size;

   Max1=Max2-1;



   count--;

   if(count<0) count=Max1;



   for(int iii=0; iii<Max2; iii++)

     {

      numb=iii+count;

      if(numb>Max1) numb-=Max2;

      CoArr[iii]=numb;

     }

//----

  }

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//---- @0A?@545;5=85 ?0<OB8 ?>4 <0AA82K ?5@5<5==KE

   int size=int(StepsTotal+3);

   if(ArrayResize(XMA,size)<size) Print("5 C40;>AL @0A?@545;8BL ?0<OBL ?>4 <0AA82 XMA[]");

   size-=2;

   if(ArrayResize(invelue0,size)<size) Print("5 C40;>AL @0A?@545;8BL ?0<OBL ?>4 <0AA82 invelue0[]");

   if(ArrayResize(invelue1,size)<size) Print("5 C40;>AL @0A?@545;8BL ?0<OBL ?>4 <0AA82 invelue1[]");

   if(ArrayResize(period,size)<size) Print("5 C40;>AL @0A?@545;8BL ?0<OBL ?>4 <0AA82 period[]");



   ZeroMemory(invelue0);

   ZeroMemory(invelue1);

   ZeroMemory(period);



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

   for(int sm=int(StepsTotal); sm>=0 && !IsStopped(); sm--) period[sm]=int(StartLength+sm*Step);

   dUpLevel=StepsTotal*UpLevel/100.0;

   dDnLevel=StepsTotal*DnLevel/100.0;



//---- CAB0=>2:0 0;5@B>2 =0 =54>?CAB8<K5 7=0G5=8O 2=5H=8E ?5@5<5==KE

   XMA[0].XMALengthCheck("StartLength", StartLength);

   XMA[0].XMAPhaseCheck("WPhase", WPhase, W_Method);

   XMA[0].XMALengthCheck("SmoothLength", SmoothLength);

   XMA[0].XMAPhaseCheck("SmoothPhase", SmoothPhase, SmoothMethod);



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

   min_rates_total=int(FLength*2.0);

   min_rates_xma=min_rates_lwma2+XMA[0].GetStartBars(W_Method,StartLength+Step*StepsTotal,WPhase)+1;

   min_rates_total=min_rates_xma+XMA[0].GetStartBars(SmoothMethod,SmoothLength,SmoothPhase);

   StTot1=StepsTotal+1;

   StTot2=StepsTotal+2;



//---- @0A?@545;5=85 ?0<OB8 ?>4 <0AA82K ?5@5<5==KE  

   ArrayResize(Count,FLength);

   ArrayResize(Price,FLength);

   ArrayResize(Lwma,FLength);



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

   SetIndexBuffer(0,BullsBuffer,INDICATOR_DATA);

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

   ArraySetAsSeries(BullsBuffer,true);



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

   SetIndexBuffer(1,BearsBuffer,INDICATOR_DATA);

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

   ArraySetAsSeries(BearsBuffer,true);



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

   SetIndexBuffer(2,ColorBuffer,INDICATOR_COLOR_INDEX);

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

   ArraySetAsSeries(ColorBuffer,true);



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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);



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

   string shortname="Ultra AbsolutelyNoLagLwma";

//--- 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,2);



//---- ?0@0<5B@K >B@8A>2:8 ;8=89  

   IndicatorSetInteger(INDICATOR_LEVELS,2);



   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,dUpLevel);

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,UpLevelsColor);

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,Levelstyle);

   IndicatorSetInteger(INDICATOR_LEVELWIDTH,0,LevelsWidth);



   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,dDnLevel);

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,DnLevelsColor);

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,Levelstyle);

   IndicatorSetInteger(INDICATOR_LEVELWIDTH,1,LevelsWidth);

//----

   return(0);

  }

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

//| Custom indicator 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[],     // F5=>2>9 <0AA82 <0:A8<C<>2 F5=K 4;O @0AG5B0 8=48:0B>@0

                const double& low[],      // F5=>2>9 <0AA82 <8=8<C<>2 F5=K  4;O @0AG5B0 8=48:0B>@0

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



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

   int limit,bar,maxbar0,maxbar1,maxbar2,weight;

   double upsch,dnsch;

   double sum,sumw,lwma2;



//---- @0AG5B AB0@B>2>3> =><5@0 maxbar 4;O DC=:F88 XMASeries()

   maxbar0=rates_total-1;

   maxbar1=maxbar0-min_rates_lwma2;

   maxbar2=maxbar0-min_rates_xma;



//---- @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=maxbar0;                       // AB0@B>2K9 =><5@ 4;O @0AG5B0 2A5E 10@>2

      ArrayInitialize(Count,0);

      ArrayInitialize(Price,0.0);

      ArrayInitialize(Lwma,0.0);

     }

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

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(close,true);



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

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

     {

      Price[Count[0]]=PriceSeries(IPC,bar,open,low,high,close);

      //----

      sum=0;

      sumw=0;

      for(int kkk=0; kkk<int(FLength); kkk++) { weight=int(FLength)-kkk; sumw+=weight; sum+=weight*Price[Count[kkk]]; }

      if(sumw) Lwma[Count[0]]=sum/sumw;

      else  Lwma[Count[0]]=NULL;

      //----

      sum=0;

      sumw=0;

      for(int kkk=0; kkk<int(FLength); kkk++) { weight=int(FLength)-kkk; sumw+=weight; sum+=weight*Lwma[Count[kkk]]; }

      if(sumw) lwma2=sum/sumw;

      else  lwma2=NULL;

      if(bar<rates_total-1) Recount_ArrayZeroPos(Count,FLength);



      for(int sm=int(StepsTotal); sm>=0 && !IsStopped(); sm--)

         invelue0[sm]=XMA[sm].XMASeries(maxbar1,prev_calculated,rates_total,W_Method,WPhase,period[sm],lwma2,bar,true);



      if(bar>maxbar2)

        {

         if(bar) ArrayCopy(invelue1,invelue0,0,0,WHOLE_ARRAY);

         continue;

        }



      upsch=0;

      dnsch=0;

      for(int sm=int(StepsTotal); sm>=0 && !IsStopped(); sm--)

         if(invelue0[sm]>invelue1[sm]) upsch++;

      else                          dnsch++;



      BullsBuffer[bar]=XMA[StTot1].XMASeries(maxbar2,prev_calculated,rates_total,SmoothMethod,SmoothPhase,SmoothLength,upsch,bar,true);

      BearsBuffer[bar]=XMA[StTot2].XMASeries(maxbar2,prev_calculated,rates_total,SmoothMethod,SmoothPhase,SmoothLength,dnsch,bar,true);



      if(bar) ArrayCopy(invelue1,invelue0,0,0,WHOLE_ARRAY);

     }



//---- :>@@5:F8O 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--;



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

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

     {

      ColorBuffer[bar]=0;



      if(BullsBuffer[bar]>BearsBuffer[bar])

        {

         if(BullsBuffer[bar]>dUpLevel || BearsBuffer[bar]<dDnLevel)

           {

            if(BullsBuffer[bar+1]<=BullsBuffer[bar]) ColorBuffer[bar]=7;

            else ColorBuffer[bar]=8;

           }

         else

           {

            if(BullsBuffer[bar+1]<=BullsBuffer[bar]) ColorBuffer[bar]=5;

            else ColorBuffer[bar]=6;

           }

        }



      if(BullsBuffer[bar]<BearsBuffer[bar])

        {

         if(BullsBuffer[bar]<dDnLevel || BearsBuffer[bar]>dUpLevel)

           {

            if(BearsBuffer[bar+1]<=BearsBuffer[bar]) ColorBuffer[bar]=1;

            else ColorBuffer[bar]=2;

           }

         else

           {

            if(BearsBuffer[bar+1]<=BearsBuffer[bar]) ColorBuffer[bar]=3;

            else ColorBuffer[bar]=4;

           }

        }

     }

//----     

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