Author: Copyright 2020,fxMeter
Indicators Used
Bollinger bands indicator
0 Views
0 Downloads
0 Favorites
Bands(mtf)
ÿþ//+------------------------------------------------------------------+

//|                                                   Bands(mtf).mq4 |

//|                                           Copyright 2020,fxMeter |

//|                            https://www.mql5.com/zh/users/fxmeter |

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

//https://www.mql5.com/zh/code/29692

#property copyright "Copyright 2020,fxMeter"

#property link      "https://www.mql5.com/zh/users/fxmeter"

#property version   "1.00"

#property strict

#property indicator_chart_window

#property indicator_buffers 3

#property indicator_plots   3

//--- plot top

#property indicator_label1  "up"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot mid

#property indicator_label2  "mid"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_DOT

#property indicator_width2  1

//--- plot low

#property indicator_label3  "low"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2



input bool                 AutoMatchTF    = true;

extern ENUM_TIMEFRAMES     TimeFrame      = PERIOD_CURRENT;

input int                  Periods        = 12;

input double               Deviations     = 2.0;

input ENUM_APPLIED_PRICE   ApplyTo        = PRICE_CLOSE;





//--- indicator buffers

double         upBuffer[];

double         midBuffer[];

double         lowBuffer[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

{

//--- indicator buffers mapping

   SetIndexBuffer(0,upBuffer);

   SetIndexBuffer(1,midBuffer);

   SetIndexBuffer(2,lowBuffer);



   for(int i=0; i<3; i++)

   {

      SetIndexEmptyValue(i,0.0);

      SetIndexDrawBegin(i,Periods);

   }



//--- /f&TꁨR9SM‘hTg 

   if(AutoMatchTF)

   {

      if(Period()==1)TimeFrame =30;

      else if(Period()==5)TimeFrame =240;

      else if(Period()==15||Period()==30||Period()==60)TimeFrame=PERIOD_D1;

      else if(Period()==240)TimeFrame = PERIOD_W1;

      else if(Period()>=1440)TimeFrame = PERIOD_MN1;

   }

   else

     {

      if(TimeFrame<=Period())TimeFrame=0;

     }



   string name = StringFormat("Bands mtf(%s,%d,%.2f)",StringSubstr(EnumToString(TimeFrame),7), Periods,Deviations);

   IndicatorShortName(name);

   IndicatorDigits(Digits);



   if(Periods<=0||Deviations<=0)

   {

      return(INIT_FAILED);

   }



//---

   return(INIT_SUCCEEDED);

}

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                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[])

{

//---

   int limit=0;

   if(rates_total<Periods)return(0);

   if(prev_calculated>0) limit = rates_total - prev_calculated +1;

   else if(prev_calculated<=0)limit = rates_total;



   if(TimeFrame==0)

   {

      for(int i=0; i<limit; i++)

      {

         upBuffer[i]  = iBands(Symbol(),TimeFrame,Periods,Deviations,0,ApplyTo,MODE_UPPER,i);

         midBuffer[i] = iBands(Symbol(),TimeFrame,Periods,Deviations,0,ApplyTo,MODE_MAIN,i);

         lowBuffer[i] = iBands(Symbol(),TimeFrame,Periods,Deviations,0,ApplyTo,MODE_LOWER,i);

      }

   }

   else //èhTg

   {

      if(iBars(Symbol(),TimeFrame)<Periods)return(0);

      int max = iBarShift(NULL,0,iTime(NULL,TimeFrame,1));

      limit  = MathMax(limit,max);

      for(int i=0; i<limit; i++)

      {

         int shift = iBarShift(Symbol(),TimeFrame,time[i]);

         upBuffer[i]  = iBands(Symbol(),TimeFrame,Periods,Deviations,0,ApplyTo,MODE_UPPER,shift);

         midBuffer[i] = iBands(Symbol(),TimeFrame,Periods,Deviations,0,ApplyTo,MODE_MAIN,shift);

         lowBuffer[i] = iBands(Symbol(),TimeFrame,Periods,Deviations,0,ApplyTo,MODE_LOWER,shift);

      }

   }



//--- return value of prev_calculated for next call

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