SnakeInBorders

Author: Copyright © 2016, Bookkeeper
2 Views
0 Downloads
0 Favorites
SnakeInBorders
ÿþ//+------------+--------------------------------------------------------+

//| v.11.09.06 |                                     SnakeInBorders.mq5 |

//+------------+                                      "$," #  !" |

//|  ><?;5:B  |                           Copyright © 2006, Bookkeeper |

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

//|  Snake @0AAG8BK205B :>@84>@ >BD8;LB@>20==>3> @K=:0, >3@0=8G5==K9    |

//|  42C<O 1>@4N@0<8 BorderTop[] 8 BorderBot[], 8 A83=0;L=CN Mart[].    |

//|                                                                     |

//|  =5H=85 ?5@5<5==K5:                                                |

//|  SnakeRange - ?>;C?5@8>4 @0AG5B0 >A8 Snake'0 Axis[] - >?B8<0;L=>    |

//|  = 4;                                                               |

//|  FilterPeriod - ?5@8>4 D8;LB@0F88. ( >?B8<0;L=> 1>;LH5 13);         |

//|  MartFiltr - :>MDD8F85=B D8;LB@0F88 @K=:0. '5< MartFiltr 1>;LH5,    |

//|  B5< C65 :>@84>@ >BD8;LB@>20==>3> @K=:0. >MDD8F85=B =04>           |

//|  ?>418@0BL, ?> C<>;G0=8N = 2;                                       |

//|  # Snake'0 420 @568<0: HardCalc = true - =5 ?5@5AG8BK20BL, 8        |

//|  = false - ?5@5AG8BK20BL 7=0G5=8O Axis[Pos]...[Pos+SnakeRange+1].   |

//|  >2545=85 A83=0;L=>9 Mart[] 2=CB@8 :>@84>@0:                       |

//|  @8 42865=88 @K=:0 225@E - A83=0;L=0O >BE>48B >B =86=53>           |

//|  1>@4N@0, ?5@5A5:05B :>@84>@ 8 A;8205BAO A 25@E=8< 1>@4N@><.        |

//|  =0;>38G=>, A B>G=>ABLN 4> =0>1>@>B, - ?@8 42865=88 @K=:0 2=87.    |

//|  > B5E ?>@, ?>:0 @K=>: 1C45B 42830BLAO =0?@02;5==>, A83=0;L=0O     |

//|  1C45B "45@60BLAO" A>>B25BAB2CNI59 3@0=8FK :>@84>@0. @8 MB><       |

//|  C25;8G5=85 H8@8=K :>@84>@0 >7=0G05B =0?@02;5==>5 42865=85. @8     |

//|  :>;510=8OE @K=:0 :>@84>@ =0G8=05B AC60BLAO. !C65=85< :>@84>@0      |

//|  A>?@>2>6405BAO 42865=85< A83=0;L=>9 >B >4=>9 3@0=8FK : 4@C3>9      |

//|  2=CB@8 :>@84>@0, ?>A;5 4>AB865=8O ?@>B82>?>;>6=>9 3@0=8FK -        |

//|  :>@84>@ =0G=5B @0AH8@OBLAO.                                        |

//|  Snake <>6=> 8A?>;L7>20BL :0: A0<>AB>OB5;L=>, A@02=820O 42865=85    |

//|  A83=0;L=>9 =0 @07=KE "$ >4=>2@5<5==>, B0: 8 4;O ?>AB@>5=8O         |

//|  4@C38E 8=48:0B>@>2, 8A?>;L7CO Mart[] 2<5AB> F5=K 10@0. ;O         |

//|  8=48:0B>@>2 B8?0 MA, OA, AC,.. 8A?>;L7>20BL HardCalc = true, 0     |

//|  4;O 8=48:0B>@>2 B8?0 ZigZag, Channel,.. - HardCalc = false, 0      |

//|  7=0G5=85 MartFiltr ?>418@0BL >B 3...5. @8 MB>< <>6=> >BA5820BL    | 

//|  8AB8==K5 8 ;>6=K5 25@H8=K: 5A;8 25@H8=0 ?@><56CB>G=0O,             | 

//|  A83=0;L=0O Mart[] =5 1C45B A>2?040BL A 1>@4N@><.                   |

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

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

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

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

#property copyright "Copyright © 2016, Bookkeeper"

#property link "yuzefovich@gmail.com"

#property description ">@84>@ >BD8;LB@>20==>3> @K=:0, >3@0=8G5==K9 42C<O 1>@4N@0<8"

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

#property version   "1.00"

//---- >B@8A>2:0 8=48:0B>@0 2 3;02=>< >:=5

#property indicator_chart_window 

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

#property indicator_buffers 4 

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

#property indicator_plots   2

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

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

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

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

#property indicator_type1   DRAW_LINE

//---- 2 :0G5AB25 F25B0 ;8=88 8=48:0B>@0 8A?>;L7>20= A8=5-D8>;5B>2K9 F25B

#property indicator_color1 clrBlueViolet

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

#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  "Mart"

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

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

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

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

#property indicator_type2   DRAW_FILLING

//---- 2K1>@ F25B0 >1;0:0

#property indicator_color2  clrLavender

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

#property indicator_label2  "Channels"

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

//|  ?8A0=85 :;0AA>2 CA@54=5=89               |

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

#include <SmoothAlgorithms.mqh> 

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

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

CXMA XMA1;

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

//|  >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_,         //Simpl 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 - ?5@5G8A;5=85 >1JO2;5=> 2 D09;5 SmoothAlgorithms.mqh

  {

   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

  }; */

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

//|  %+  " + "               |

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

input Smooth_Method XMA_Method=MODE_LWMA_; //<5B>4 CA@54=5=8O

input uint SnakeRange=2; //?>;C?5@8>4 @0AG5B0 >A8 Snake'0 Axis                   

input int XPhase=15; //?0@0<5B@ ?5@2>3> CA@54=5=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 uint   FilterPeriod = 24;  // ?5@8>4 D8;LB@0F88

input double MartFiltr    = 2;  // :>MDD8F85=B D8;LB@0F88 @K=:0. '5< MartFiltr 1>;LH5, B5< C65 :>@84>@ >BD8;LB@>20==>3> @K=:0. 0==K9 :>MDD8F85=B A;54C5B ?>418@0BL. > C<>;G0=8N 2;

input bool   HardCalc     = true;

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

input int Shift=0; // A4283 8=48:0B>@0 ?> 3>@87>=B0;8 2 10@0E

input int PriceShift=0; // c4283 8=48:0B>@0 ?> 25@B8:0;8 2 ?C=:B0E

input color  Upper_color=clrMediumSeaGreen;

input color  Lower_color=clrRed;

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

//---- >1JO2;5=85 48=0<8G5A:>3> <0AA820, :>B>@K9 1C45B 2 

// 40;L=59H5< 8A?>;L7>20= 2 :0G5AB25 8=48:0B>@=>3> 1CD5@0

double MartBuffer[];

//---- >1JO2;5=85 48=0<8G5A:8E <0AA82>2, :>B>@K5 1C4CB 2 40;L=59H5< 8A?>;L7>20=K 2 :0G5AB25 8=48:0B>@=KE 1CD5@>2

double BorderTopBuffer[],BorderBotBuffer[];

//---- >1JO2;5=85 48=0<8G5A:8E <0AA82>2, :>B>@K5 1C4CB 2 40;L=59H5< 8A?>;L7>20=K 2 :0G5AB25 1CD5@>2 E@0=5=8O 40==KE

double AxisBuffer[];

//---- 1JO2;5=85 ?5@5<5==>9 7=0G5=8O 25@B8:0;L=>3> A42830 <C28=30

double dPriceShift;

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

int min_rates_total,min_rates_1,SnakeRange1;

//---- 1JO2;5=85 AB@8=3>2 4;O B5:AB>2KE <5B>:

string upper_name,lower_name;

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

//| XMA BBx3 indicator initialization function                       | 

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

void OnInit()

  {

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

   SnakeRange1=int(SnakeRange)+1;

   min_rates_total=GetStartBars(XMA_Method,SnakeRange1,XPhase)+1;

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

   XMA1.XMALengthCheck("XLength",SnakeRange1);

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

   XMA1.XMAPhaseCheck("XPhase",XPhase,XMA_Method);

//---- =8F80;870F8O A42830 ?> 25@B8:0;8

   dPriceShift=_Point*PriceShift;



//---- =8F80;870F8O AB@8=3>2

   upper_name="SnakeInBorders upper text lable";

   lower_name="SnakeInBorders lower text lable";



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

   SetIndexBuffer(0,MartBuffer,INDICATOR_DATA);

//---- >ACI5AB2;5=85 A42830 8=48:0B>@0 1 ?> 3>@87>=B0;8

   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);

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



//---- ?@52@0I5=85 48=0<8G5A:8E <0AA82>2 2 8=48:0B>@=K5 1CD5@K

   SetIndexBuffer(1,BorderTopBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,BorderBotBuffer,INDICATOR_DATA);

//---- CAB0=>2:0 ?>78F88, A :>B>@>9 =0G8=05BAO >B@8A>2:0 :0=0;0

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);

//---- 70?@5B =0 >B@8A>2:C 8=48:0B>@>< ?CABKE 7=0G5=89

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- >ACI5AB2;5=85 A42830 8=48:0B>@>2 ?> 3>@87>=B0;8 =0 Shift

   PlotIndexSetInteger(1,PLOT_SHIFT,Shift);



//---- ?@52@0I5=85 48=0<8G5A:>3> <0AA820 2 1CD5@ 4;O @0AGQB>2

   SetIndexBuffer(3,AxisBuffer,INDICATOR_CALCULATIONS);



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

   ArraySetAsSeries(MartBuffer,true);

   ArraySetAsSeries(BorderTopBuffer,true);

   ArraySetAsSeries(BorderBotBuffer,true);

   ArraySetAsSeries(AxisBuffer,true);



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



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

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

//----

   ObjectDelete(0,upper_name);

   ObjectDelete(0,lower_name);

//----

  }

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

//| XMA BBx3 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<min_rates_total) return(0);



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

   int limit,maxbar=rates_total-1;



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

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

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



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

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);



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

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

     {

      double price=PriceSeries(IPC,bar,open,high,low,close);

      AxisBuffer[bar]=XMA1.XMASeries(maxbar,prev_calculated,rates_total,XMA_Method,XPhase,SnakeRange1,price,bar,true);

      MainCalculation(bar,open,high,low,close,rates_total);

      BorderTopBuffer[bar]+=dPriceShift;

      BorderBotBuffer[bar]+=dPriceShift;

      MartBuffer[bar]+=dPriceShift;

     }

   int bar0=0;

   SetRightPrice(0,upper_name,0,time[bar0],BorderTopBuffer[bar0],Upper_color,"Georgia");

   SetRightPrice(0,lower_name,0,time[bar0],BorderBotBuffer[bar0],Lower_color,"Georgia");

//----     

   return(rates_total);

  }

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

//| MainCalculation()                                                |

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

void MainCalculation(int Pos,

              const double &Open[],

              const double &High[],

              const double &Low[],

              const double &Close[],

              const int Rates_Total)

  {

//----

   MartAxis(Pos,Open,Low,High,Close,Rates_Total);

   if(HardCalc==true) SmoothOverMart2(Pos,Rates_Total);

   else SmoothOverMart(Pos,Rates_Total);

//----

   return;

  }

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

//|                                                                  |

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

void MartAxis(int Pos,

              const double &Open[],

              const double &High[],

              const double &Low[],

              const double &Close[],

              const int Rates_Total)

  {

//----

   int    SnakeWeight,i,w,ww,shift;

   double SnakeSum;

   for(shift=int(MathMin(Pos+SnakeRange+2,Rates_Total-1)); shift>Pos; shift--)

     {

      SnakeSum=NULL;

      SnakeWeight=NULL;

      i=NULL;

      w=int(MathMin(shift+SnakeRange,Rates_Total-1));

      ww=shift-int(SnakeRange);

      if(ww<Pos) ww=Pos;

      while(w>=shift)

        {

         i++;

         SnakeSum+=i*PriceSeries(IPC,w,Open,Low,High,Close);

         SnakeWeight+=i;

         w--;

        }

      while(w>=ww)

        {

         i--;

         SnakeSum+=i*PriceSeries(IPC,w,Open,Low,High,Close);

         SnakeWeight+=i;

         w--;

        }

      if(!SnakeWeight) SnakeWeight=1;

      AxisBuffer[shift]=SnakeSum/SnakeWeight;

     }

//----

   return;

  }

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

//| SmoothOverMart()                                                 |

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

void SmoothOverMart(int Pos,const int Rates_Total)

  { // @028< 8AB>@8N =0 ?>A;54=8E Pos...(Pos+SnakeRange+2) 10@0E

//----

   int    shift;

   double a,t,b;

   for(shift=int(MathMin(Pos+SnakeRange+2,Rates_Total-1)); shift>=Pos; shift--)

     {

      t=AxisBuffer[ArrayMaximum(AxisBuffer,shift,FilterPeriod)];

      b=AxisBuffer[ArrayMinimum(AxisBuffer,shift,FilterPeriod)];

      a=AxisBuffer[shift];

      BorderTopBuffer[shift]=(2*(1+MartFiltr)*a+(t-b))/2/(1+MartFiltr);

      BorderBotBuffer[shift]=(2*(1+MartFiltr)*a-(t-b))/2/(1+MartFiltr);

      MartBuffer[shift]=(2*(2+MartFiltr)*a-(t+b))/2/(1+MartFiltr);

     }

//----

   return;

  }

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

//| SmoothOverMart2()                                                |

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

void SmoothOverMart2(int shift,const int Rates_Total)

  { // 5 ?>4?@02;O5< 8AB>@8N =0 ?>A;54=8E 10@0E

//----

   double a,t,b;

   t = AxisBuffer[ArrayMaximum(AxisBuffer,shift,FilterPeriod)];

   b = AxisBuffer[ArrayMinimum(AxisBuffer,shift,FilterPeriod)];

   a = AxisBuffer[shift];

   BorderTopBuffer[shift]=(2*(1+MartFiltr)*a+(t-b))/2/(1+MartFiltr);

   BorderBotBuffer[shift]=(2*(1+MartFiltr)*a-(t-b))/2/(1+MartFiltr);

   MartBuffer[shift]=(2*(2+MartFiltr)*a -(t+b))/2/(1+MartFiltr);

//----

   return;

  }

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

//|  RightPrice creation                                             |

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

void CreateRightPrice(long chart_id,// chart ID

                      string   name,              // object name

                      int      nwin,              // window index

                      datetime time,              // price level time

                      double   price,             // price level

                      color    Color,             // Text color

                      string   Font               // Text font

                      )

//---- 

  {

//----

   ObjectCreate(chart_id,name,OBJ_ARROW_RIGHT_PRICE,nwin,time,price);

   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

   ObjectSetString(chart_id,name,OBJPROP_FONT,Font);

   ObjectSetInteger(chart_id,name,OBJPROP_BACK,true);

//----

  }

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

//|  RightPrice reinstallation                                       |

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

void SetRightPrice(long chart_id,// chart ID

                   string   name,              // object name

                   int      nwin,              // window index

                   datetime time,              // price level time

                   double   price,             // price level

                   color    Color,             // Text color

                   string   Font               // Text font

                   )

//---- 

  {

//----

   if(ObjectFind(chart_id,name)==-1) CreateRightPrice(chart_id,name,nwin,time,price,Color,Font);

   else ObjectMove(chart_id,name,0,time,price);

//----

  }

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

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