139 lines
No EOL
12 KiB
MQL5
139 lines
No EOL
12 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| 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]; // Разница между верхним и нижним 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("Ошибка при создании BBHandle - № ошибки: ", _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("Данные Bollinger bands не загрузились", _LastError);
|
|
SendMail(_Symbol + "Ошибка загрузки данных индикатора Bollinger bands ", _Symbol + " Ошибка загрузки данных индикаторов");
|
|
return;
|
|
}
|
|
} |