FineTuningMACandle

Author: Copyright © 2018, gumgum
0 Views
0 Downloads
0 Favorites
FineTuningMACandle
ÿþ//+------------------------------------------------------------------+

//|                                           FineTuningMACandle.mq5 | 

//|                                         Copyright © 2018, gumgum |

//|                                                                  |

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

#property copyright "Copyright © 2018, gumgum"

#property link      ""

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

#property version   "1.00"

//---- >?8A0=85 8=48:0B>@0

#property description "!25G=>9 3@0D8: =0 1>;55 B>=:>< CA@54=5=88 B09<A5@89"

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

#property indicator_chart_window 

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

#property indicator_buffers 5

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

#property indicator_plots   1

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

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

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

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

#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1   clrDeepPink,clrGray,clrCornflowerBlue

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

#property indicator_label1  "FineTuningMA Open;FineTuningMA High;FineTuningMA Low;FineTuningMA Close"

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

//|  %+  " + "                 |

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

input uint   FTMA=10;

input double rank1=2;

input double rank2=2;

input double rank3=2;

input double shift1=1;

input double shift2=1;

input double shift3=1;

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 uint Gap=10;      // @07<5@ =5CG8BK205<>3> 3M?0 2 ?C=:B0E

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

//---- >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 ExtOpenBuffer[];

double ExtHighBuffer[];

double ExtLowBuffer[];

double ExtCloseBuffer[];

double ExtColorBuffer[];

//----

double PM[];

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

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

//| FineTuningMA indicator initialization function                   | 

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

void OnInit()

  {

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

   dPriceShift=_Point*PriceShift;



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

   ArrayResize(PM,FTMA);



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

   double sum=0;

   for(int h=0; h<int(FTMA); h++)

     {

      PM[h]=shift1+MathPow(h/(FTMA-1.0),rank1)*(1.0-shift1);

      PM[h]=(shift2+MathPow(1-(h/(FTMA-1.0)),rank2)*(1.0-shift2))*PM[h];



      if((h/(FTMA-1.))<0.5) PM[h]=(shift3+MathPow((1-(h/(FTMA-1.0))*2.0),rank3)*(1.0-shift3))*PM[h];

      else PM[h]=(shift3+MathPow((h/(FTMA-1.0))*2.0-1.0,rank3)*(1.0-shift3))*PM[h];



      sum+=PM[h];

     }



   double sum1=0;

   for(int h=0; h<int(FTMA); h++)

     {

      PM[h]=PM[h]/sum;

      sum1+=PM[h];

     }



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

   min_rates_total=int(FTMA)+1;

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

   SetIndexBuffer(0,ExtOpenBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtHighBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtLowBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtCloseBuffer,INDICATOR_DATA);

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

   SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX);

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);

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

   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);

//--- 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,"FineTuningMACandle("+string(FTMA)+")");



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

  }

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

//| FineTuningMA 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 F5;KE ?5@5<5==KE 8 ?>;CG5=85 C65 ?>AG8B0==KE 10@>2

   int first,bar;

   double O,H,L,C;



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

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

     {      

      O=NULL;

      for(int index=0; index<int(FTMA); index++) O+=PM[index]*open[bar-index];

      H=NULL;

      for(int index=0; index<int(FTMA); index++) H+=PM[index]*high[bar-index];

      L=NULL;

      for(int index=0; index<int(FTMA); index++) L+=PM[index]*low[bar-index];

      C=NULL;

      for(int index=0; index<int(FTMA); index++) C+=PM[index]*close[bar-index];

      

      //---- 8A?@02;O5< 7=0G5=8O ?>;CG5==KE A25G5:

      double Max=MathMax(O,C);

      Max=MathMax(Max,L);

      Max=MathMax(Max,H);

      double Min=MathMin(O,C);

      Min=MathMin(Min,L);

      Min=MathMin(Min,H);

      ExtOpenBuffer[bar]=O;

      ExtHighBuffer[bar]=Max;

      ExtLowBuffer[bar]=Min;

      ExtCloseBuffer[bar]=C;

      //---- C40;O5< =5ACI5AB2CNI85 35?K

      if(MathAbs(open[bar]-close[bar])<=Gap) ExtOpenBuffer[bar]=ExtCloseBuffer[MathMax(bar-1,0)];

      //---- :@0A8< A25G8

      if(ExtOpenBuffer[bar]<ExtCloseBuffer[bar]) ExtColorBuffer[bar]=2.0;

      else if(ExtOpenBuffer[bar]>ExtCloseBuffer[bar]) ExtColorBuffer[bar]=0.0;

      else ExtColorBuffer[bar]=1.0;      

      //---- A428305< A25G8 ?> 25@B8:0;8

      ExtOpenBuffer[bar]+=dPriceShift;

      ExtHighBuffer[bar]+=dPriceShift;

      ExtLowBuffer[bar]+=dPriceShift;

      ExtCloseBuffer[bar]+=dPriceShift;

     }

//----     

   return(rates_total);

  }

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

//| PriceSeries() function                                           |

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

double PriceSeries

(

 uint applied_price,// &5=>20O :>=AB0=B0

 uint   bar,// =45:A A42830 >B=>A8B5;L=> B5:CI53> 10@0 =0 C:070==>5 :>;8G5AB2> ?5@8>4>2 =0704 8;8 2?5@Q4).

 const double &Open[],

 const double &Low[],

 const double &High[],

 const double &Close[]

 )

//PriceSeries(applied_price, bar, open, low, high, close)

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

  {

//----

   switch(applied_price)

     {

      //---- &5=>2K5 :>=AB0=BK 87 ?5@5G8A;5=8O ENUM_APPLIED_PRICE

      case  PRICE_CLOSE: return(Close[bar]);

      case  PRICE_OPEN: return(Open [bar]);

      case  PRICE_HIGH: return(High [bar]);

      case  PRICE_LOW: return(Low[bar]);

      case  PRICE_MEDIAN: return((High[bar]+Low[bar])/2.0);

      case  PRICE_TYPICAL: return((Close[bar]+High[bar]+Low[bar])/3.0);

      case  PRICE_WEIGHTED: return((2*Close[bar]+High[bar]+Low[bar])/4.0);



      //----                            

      case  8: return((Open[bar] + Close[bar])/2.0);

      case  9: return((Open[bar] + Close[bar] + High[bar] + Low[bar])/4.0);

      //----                                

      case 10:

        {

         if(Close[bar]>Open[bar])return(High[bar]);

         else

           {

            if(Close[bar]<Open[bar])

               return(Low[bar]);

            else return(Close[bar]);

           }

        }

      //----         

      case 11:

        {

         if(Close[bar]>Open[bar])return((High[bar]+Close[bar])/2.0);

         else

           {

            if(Close[bar]<Open[bar])

               return((Low[bar]+Close[bar])/2.0);

            else return(Close[bar]);

           }

         break;

        }

      //----

      default: return(Close[bar]);

     }

//----

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