Project_N_1/Include/CBollinger_bands.mqh

139 lines
12 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:18:18 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Bollinger.mqh |
//| Copyright 2021, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CBollingerBands
{
private:
int InitHandle(string symbol, ENUM_TIMEFRAMES period, int bbPeriod, int maShift, double bbDev, ENUM_APPLIED_PRICE appliedPrice, bool displayAtChart);
int bollingerRange;
public:
CBollingerBands();
CBollingerBands(string symbol, ENUM_TIMEFRAMES period, int bbPeriod, int maShift, double bbDeviation, ENUM_APPLIED_PRICE bollingerAppliedPrice, bool displayAtChart, int bbRange);
~CBollingerBands();
bool BollingerOpenBuySignal(int openBuyCount, double Ask);
bool BollingerOpenSellSignal(int openSellCount, double Bid);
bool BollingerCloseBuySignal(int openBuyCount, double Ask);
bool BollingerCloseSellSignal(int openSellCount, double Bid);
double upBand[];
double downBand[];
double mediumBand[];
int handle;
void FillBuffer(int BBHandle);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CBollingerBands::CBollingerBands()
{
this.handle = InitHandle(_Symbol, PERIOD_CURRENT, 30, 0, 1, PRICE_CLOSE, true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CBollingerBands::CBollingerBands(string symbol, ENUM_TIMEFRAMES period, int bbPeriod, int maShift, double bbDeviation, ENUM_APPLIED_PRICE bollingerAppliedPrice, bool displayAtChart, int bbRange)
{
this.handle = InitHandle(symbol, period, bbPeriod, maShift, bbDeviation, bollingerAppliedPrice, displayAtChart);
this.bollingerRange = bbRange;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CBollingerBands::~CBollingerBands()
{
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CBollingerBands::BollingerOpenBuySignal(int openBuyCount, double Ask)
{
double diff_Bollinger = upBand[0] - downBand[0]; // 07=8F0 <564C 25@E=8< 8 =86=8< Bollinger
if(openBuyCount == 0 && Ask <= downBand[0] && diff_Bollinger > bollingerRange * _Point)
return true;
return false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CBollingerBands::BollingerOpenSellSignal(int openSellCount, double Bid)
{
double diff_Bollinger = upBand[0] - downBand[0];
if(openSellCount == 0 && Bid >= upBand[0] && diff_Bollinger > bollingerRange * _Point)
return true;
return false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CBollingerBands::BollingerCloseBuySignal(int openBuyCount, double Ask)
{
if(openBuyCount > 0 && Ask >= upBand[0])
return true;
return false;
}
//+------------------------------------------------------------------+
bool CBollingerBands::BollingerCloseSellSignal(int openSellCount, double Bid)
{
if(openSellCount > 0 && Bid <= downBand[0])
return true;
return false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CBollingerBands::InitHandle(string symbol, ENUM_TIMEFRAMES period, int bbPeriod, int maShift, double bbDev, ENUM_APPLIED_PRICE appliedPrice, bool displayAtChart)
{
Print(displayAtChart);
int BBHandle = iBands(symbol, period, bbPeriod, maShift, bbDev, appliedPrice);
if(BBHandle < 0)
{
Print("H81:0 ?@8 A>740=88 BBHandle - ! >H81:8: ", _LastError, "!!");
return -1;
}
if (displayAtChart) ChartIndicatorAdd(ChartID(), 0, BBHandle);
return BBHandle;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CBollingerBands:: FillBuffer(int bollingerHandle)
{
ArraySetAsSeries(upBand, true);
ArraySetAsSeries(downBand, true);
ArraySetAsSeries(mediumBand, true);
ArrayResize(upBand, 5);
ArrayResize(downBand, 5);
ArrayResize(mediumBand, 5);
int bbands_up = CopyBuffer(bollingerHandle, 1, 0, 5, upBand);
int bbands_dn = CopyBuffer(bollingerHandle, 2, 0, 5, downBand);
int bbands_bs = CopyBuffer(bollingerHandle, 0, 0, 5, mediumBand);
if(bbands_bs < 0 || bbands_up < 0 || bbands_dn < 0)
{
Print("0==K5 Bollinger bands =5 703@C78;8AL", _LastError);
SendMail(_Symbol + "H81:0 703@C7:8 40==KE 8=48:0B>@0 Bollinger bands ", _Symbol + " H81:0 703@C7:8 40==KE 8=48:0B>@>2");
return;
}
}