GRFLeadingEdger_Pivot

Author: Copyright © 2007, GammaRatForex
0 Views
0 Downloads
0 Favorites
GRFLeadingEdger_Pivot
ÿþ//+------------------------------------------------------------------+

//|                                        GRFLeadingEdger_Pivot.mq5 | 

//|                                  Copyright © 2007, GammaRatForex | 

//|                                   http://www.gammarat.com/Forex/ | 

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

#property copyright "Copyright © 2007, GammaRatForex"

#property link "http://www.gammarat.com/Forex/"

/*

 * LSQ line fitting to the a number of samples.

 * The trendline is the leading point in the fit;

 * the bands are calculated somewhat differently, check the math below and adapt to 

 * your own needs as appropriate

 * also the point estimate is given by the geometric mean

 * MathPow(HCCC,.025) (see function "get_avg" below) rather than 

 * more standard estimates.

 * It's computationally fairly intensive

 */

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

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

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

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

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

#property indicator_chart_window 

#property indicator_buffers 0

#property indicator_plots   0

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

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

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

#define INDICATOR_NAME      "GRFLeadingEdger_Pivot"     // <O 8=48:0B>@0

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

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

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

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

input string Symbols_Sirname="GRFLeadingEdger_Pivot_";  // 0720=85 4;O <5B>: 8=48:0B>@0

input uint Samples=60;

input int  LookAhead=0;

input double StdLevel1=2.0;

input double  StdLevel2=4.0;

input color Up_Color=clrSpringGreen;                    // &25B 25@E=59 ?>;>AK 8=48:0B>@0

input color Dn_Color=clrYellow;                         // &25B =86=59 ?>;>AK 8=48:0B>@0

input uint SignalBar=0;                                 // =><5@ 10@0 4;O ?>;CG5=8O 7=0G5=89 8=48:0B>@0

input uint SignalLen=40;                                // ;8=0 ?>;>A 8=48:0B>@0

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

double pStdLevel1,pStdLevel2;

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

int min_rates_total;

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

string UpName,DnName;

int PerSignalLen;

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

//|  !>740=85 B@5=4>2>9 ;8=88                                        |

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

void CreateRectangle

(

 long     chart_id,      // 845=B8D8:0B>@ 3@0D8:0

 string   name,          // 8<O >1J5:B0

 int      nwin,          // 8=45:A >:=0

 datetime time1,         // 2@5<O 1 F5=>2>3> C@>2=O

 double   price1,        // 1 F5=>2>9 C@>25=L

 datetime time2,         // 2@5<O 2 F5=>2>3> C@>2=O

 double   price2,        // 2 F5=>2>9 C@>25=L

 color    Color,         // F25B ;8=88

 int      style,         // AB8;L ;8=88

 int      width,         // B>;I8=0 ;8=88

 string   text           // B5:AB

 )

//---- 

  {

//----

   ObjectCreate(chart_id,name,OBJ_RECTANGLE,nwin,time1,price1,time2,price2);

   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

   ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);

   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);

   ObjectSetString(chart_id,name,OBJPROP_TEXT,text);

   ObjectSetInteger(chart_id,name,OBJPROP_BACK,true);

   ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,true);

   ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,true);

   ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,true);

   ObjectSetInteger(chart_id,name,OBJPROP_FILL,true);

//----

  }

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

//|  5@5CAB0=>2:0 B@5=4>2>9 ;8=88                                   |

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

void SetRectangle

(

 long     chart_id,      // 845=B8D8:0B>@ 3@0D8:0

 string   name,          // 8<O >1J5:B0

 int      nwin,          // 8=45:A >:=0

 datetime time1,         // 2@5<O 1 F5=>2>3> C@>2=O

 double   price1,        // 1 F5=>2>9 C@>25=L

 datetime time2,         // 2@5<O 2 F5=>2>3> C@>2=O

 double   price2,        // 2 F5=>2>9 C@>25=L

 color    Color,         // F25B ;8=88

 int      style,         // AB8;L ;8=88

 int      width,         // B>;I8=0 ;8=88

 string   text           // B5:AB

 )

//---- 

  {

//----

   if(ObjectFind(chart_id,name)==-1) CreateRectangle(chart_id,name,nwin,time1,price1,time2,price2,Color,style,width,text);

   else

     {

      ObjectSetString(chart_id,name,OBJPROP_TEXT,text);

      ObjectMove(chart_id,name,0,time1,price1);

      ObjectMove(chart_id,name,1,time2,price2);

      ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

     }

//----

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

//----

   ObjectsDeleteAll(0,Symbols_Sirname,-1,-1);

//----

   ChartRedraw(0);

  }

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

//| Custom indicator initialization function                         | 

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

void OnInit()

  {

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

   min_rates_total=int(Samples);

   pStdLevel1=StdLevel1*_Point;

   pStdLevel2=StdLevel2*_Point;



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

   UpName=Symbols_Sirname+"Upper Band";

   DnName=Symbols_Sirname+"Lower Band";

   PerSignalLen=int(SignalLen)*PeriodSeconds();



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

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

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

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

  }

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

//| 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[],

                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=85 ?5@5<5==KE A ?;020NI59 B>G:>9  

   double c0,c1,alpha,beta,s0,s1,c01,c11;

   static double base_det,a[2][2],b[2][2];

   static double Up1,Up2,Dn1,Dn2;

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

   int first,bar,kkk;



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

      ArrayInitialize(a,0);

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

        {

         a[0][0]+=iii*iii;

         a[0][1]+=iii;

         a[1][0]+=iii;

         a[1][1]++;

        }



      base_det=det2(a);

     }

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



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

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

     {

      c0=0;

      c1=0;

      for(kkk=0; kkk<int(Samples); kkk++)

        {

         double res=get_avg(bar-kkk,high,low,close);

         c0+=kkk*res;

         c1+=res;

        }



      ArrayCopy(b,a);

      b[0][0]=c0;

      b[1][0]=c1;

      alpha=det2(b)/base_det;



      ArrayCopy(b,a);

      b[0][1]=c0;

      b[1][1]=c1;

      beta=det2(b)/base_det;

      double Leading=(beta-alpha*LookAhead)*_Point;

      c0 = 0;

      c1 = 0;

      c11=0;

      c01=0;

      for(kkk=0; kkk<int(Samples); kkk++)

        {

         s0=get_avg(bar-kkk,high,low,close);

         s1=kkk*alpha+beta;

         double res=MathPow(s0-s1,2);



         if(s0<s1)

           {

            c0+=res;

            c01++;

           }

         else

           {

            c1+=res;

            c11++;

           }

        }



      if(!c01) c01=1;

      if(!c11) c11=1;



      c01=MathSqrt(1./(0.5/MathPow(Samples,2)+0.5/c01/c01));

      c11=MathSqrt(1./(0.5/MathPow(Samples,2)+0.5/c11/c11));

      c0=MathSqrt(c0/c01);

      c1=MathSqrt(c1/c11);



      if(MathAbs(StdLevel1)>0)

        {

         Up1=Leading+pStdLevel1*c0;

         Dn1=Leading-pStdLevel1*c1;

        }



      if(MathAbs(StdLevel2)>0)

        {

         Up2=Leading+pStdLevel2*c0;

         Dn2=Leading-pStdLevel2*c1;

        }



     }

   int bar0=rates_total-1;

   int bar1=rates_total-int(MathMax(SignalLen-1,0));

   datetime time0=time[bar0]+2*PeriodSeconds();

   datetime time1=time0-PerSignalLen;





//----  8AC5< =0 3@0D8:5 25@E=89 ?@O<>C3>;L=8: ?>;>AK >;;8=465@0

   SetRectangle(0,UpName,0,time1,Up1,time0,Up2,Up_Color,STYLE_SOLID,1,UpName);

//----  8AC5< =0 3@0D8:5 =86=89 ?@O<>C3>;L=8: ?>;>AK >;;8=465@0

   SetRectangle(0,DnName,0,time1,Dn1,time0,Dn2,Dn_Color,STYLE_SOLID,1,DnName);

//----

   ChartRedraw(0);

//----     

   return(rates_total);

  }

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

//|                                                                  |

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

double get_avg(int index,const double &High[],const double &Low[],const double &Close[])

  {

//----

   return(MathPow((High[index]*Low[index]*Close[index]*Close[index]),1/4.0)/_Point);

  }

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

//| Point and figure                                                 |

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

double det2(double &a[][2])

  {

//----

   return(a[0][0]*a[1][1]-a[1][0]*a[0][1]);

  }

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

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