Bollinger Bands là một công cụ phân tích kỹ thuật được sử dụng rộng rãi trong giao dịch chứng khoán và ngoại hối. Nó được phát triển bởi John Bollinger vào những năm 1980 và giúp nhà giao dịch xác định các vùng giá phù hợp để mua và bán.
Trong ngôn ngữ lập trình MQL4, mã Bollinger Bands là một chương trình phần mềm có tính năng tính toán và hiển thị các dải giá Bollinger trên biểu đồ. Mã này sử dụng các đường trung bình động và độ lệch chuẩn để tính toán các giá trị giới hạn cho các dải giá trên biểu đồ.
Với mã Bollinger Bands trên MQL4, nhà giao dịch có thể tùy chỉnh các tham số như độ rộng của dải giá, số lượng thanh tính toán, và các tham số phụ thuộc vào động lực học giá để tạo ra các đường Bollinger Bands phù hợp với chiến lược giao dịch của mình.
Điều này giúp nhà giao dịch dễ dàng xác định các vùng giá có tiềm năng để mua hoặc bán, giúp tăng hiệu quả giao dịch và giảm rủi ro. Mã Bollinger Bands trên MQL4 là một công cụ mạnh mẽ trong tay nhà giao dịch để phân tích và dự đoán hành động giá trên thị trường.
Chỉ báo Bollinger Bands có sẵng mặc định trên các nền tảng Metatrader, vậy tại sao tôi lại chia sẻ cái này.
Mục đích là với đoạn mã nguồn chúng ta có thể tùy biến lại chỉ báo Bollinger Bands tùy ý mà chỉ báo mặc định không làm được.
Ví dụ:
- Sử dụng đường trung bình có trọng số EMA thay vì sử dụng đường trung SMA trên Bollinger Bands mặc định, ta có thể sử dụng đường trung bình có trọng số. Điều này có thể giúp đưa ra dự báo chính xác hơn cho các xu hướng của giá.
- Thay đổi giá trị của độ lệch chuẩn: thay vì sử dụng độ lệch chuẩn có giá trị cố định, ta có thể thay đổi giá trị của nó để phù hợp với tình hình thị trường
Dưới đây là đoạn mã Bollinger Bands mặc định các bạn có thể sao chép về và code lại tùy ý.
#property copyright "Contact" #property link "https://www.nhutpham.com" #include <MovingAverages.mqh> #property indicator_chart_window #property indicator_buffers 3 #property indicator_color1 White #property indicator_color2 White #property indicator_color3 White //--- indicator parameters input int InpBandsPeriod=20; // Bands Period input int InpBandsShift=0; // Bands Shift input double InpBandsDeviations=2.2; // Bands Deviations //--- buffers double ExtMovingBuffer[]; double ExtUpperBuffer[]; double ExtLowerBuffer[]; double ExtStdDevBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit(void) { //--- 1 additional buffer used for counting. IndicatorBuffers(4); IndicatorDigits(Digits); //--- middle line SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,ExtMovingBuffer); SetIndexShift(0,InpBandsShift); SetIndexLabel(0,"Bands SMA"); //--- upper band SetIndexStyle(1,DRAW_LINE); SetIndexBuffer(1,ExtUpperBuffer); SetIndexShift(1,InpBandsShift); SetIndexLabel(1,"Bands Upper"); //--- lower band SetIndexStyle(2,DRAW_LINE); SetIndexBuffer(2,ExtLowerBuffer); SetIndexShift(2,InpBandsShift); SetIndexLabel(2,"Bands Lower"); //--- work buffer SetIndexBuffer(3,ExtStdDevBuffer); //--- check for input parameter if(InpBandsPeriod<=0) { Print("Wrong input parameter Bands Period=",InpBandsPeriod); return(INIT_FAILED); } //--- SetIndexDrawBegin(0,InpBandsPeriod+InpBandsShift); SetIndexDrawBegin(1,InpBandsPeriod+InpBandsShift); SetIndexDrawBegin(2,InpBandsPeriod+InpBandsShift); //--- initialization done return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Bollinger Bands | //+------------------------------------------------------------------+ 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 i,pos; //--- if(rates_total<=InpBandsPeriod || InpBandsPeriod<=0) return(0); //--- counting from 0 to rates_total ArraySetAsSeries(ExtMovingBuffer,false); ArraySetAsSeries(ExtUpperBuffer,false); ArraySetAsSeries(ExtLowerBuffer,false); ArraySetAsSeries(ExtStdDevBuffer,false); ArraySetAsSeries(close,false); //--- initial zero if(prev_calculated<1) { for(i=0; i<InpBandsPeriod; i++) { ExtMovingBuffer[i]=EMPTY_VALUE; ExtUpperBuffer[i]=EMPTY_VALUE; ExtLowerBuffer[i]=EMPTY_VALUE; } } //--- starting calculation if(prev_calculated>1) pos=prev_calculated-1; else pos=0; //--- main cycle for(i=pos; i<rates_total && !IsStopped(); i++) { ExtMovingBuffer[i]=SimpleMA(i,InpBandsPeriod,close); ExtStdDevBuffer[i]=StdDev_Func(i,close,ExtMovingBuffer,InpBandsPeriod); ExtUpperBuffer[i]=ExtMovingBuffer[i]+InpBandsDeviations*ExtStdDevBuffer[i]; ExtLowerBuffer[i]=ExtMovingBuffer[i]-InpBandsDeviations*ExtStdDevBuffer[i]; //--- } //---- OnCalculate done. Return new prev_calculated. return(rates_total); } //+------------------------------------------------------------------+ //| Calculate Standard Deviation | //+------------------------------------------------------------------+ double StdDev_Func(int position,const double &price[],const double &MAprice[],int period) { //--- variables double StdDev_dTmp=0.0; //--- check for position if(position>=period) { //--- calcualte StdDev for(int i=0; i<period; i++) StdDev_dTmp+=MathPow(price[position-i]-MAprice[position],2); StdDev_dTmp=MathSqrt(StdDev_dTmp/period); } //--- return calculated value return(StdDev_dTmp); }