Project_N_1/Bollinger_advisor_example.mq5
super.admin fca2fc1d82 convert
2025-05-30 16:18:18 +02:00

169 lines
15 KiB
MQL5

//+------------------------------------------------------------------+
//| bollinger_signal_tester.mq5 |
//| Copyright 2021, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#include <General_class_.mqh>
#include <Indicators_class.mqh>
#include <ExternalIndicators_class.mqh>
#include <CBollinger_bands.mqh>
#include <CStohastic_oscillator.mqh>
//+------------------------------------------------------------------+
//| Входные параметры для оптимизации |
//+------------------------------------------------------------------+
input group "-------- Общие параметры -------"
input bool NewBar = true;
input bool TimeCheck = true;
input ENUM_TIMEFRAMES per = PERIOD_M1; //Период для текущей торговли
//input ENUM_TIMEFRAMES per2 = PERIOD_M5; //Период для долгосрочного тренда
//int Slippage = 30;
input int StartTime = 01;
input int EndTime = 23;
input bool Risk_lot = true;
input double Lot_percent = 5; // риск % баланса
input double SL = 150; //Стоп лосс
input double Lot1_ = 0.1; //размер лота
input group "------------------ Параматр полос Боллинджера ------------------ "
input int BBPeriod = 30; // Период для расчета средней линии
input double BBDev = 1; // Кол-во стандартных отклонений
input double Bollinger_range = 20; // Bollinger range
input ENUM_APPLIED_PRICE Bollinger_Applied_price = PRICE_CLOSE; // Тип цены или handle
//int _bars = 0; // Количество баров
input int BollingerMAshift = 0;
//input group "------------------ Параматр полос Боллинджера 2 ------------------ "
//
//input int BBPeriod2 = 30; // Период для расчета средней линии
//input double BBDev2 = 1; // Кол-во стандартных отклонений
//input double Bollinger_range2 = 20; //
//input ENUM_APPLIED_PRICE Bollinger_Applied_price2 = PRICE_CLOSE; // Тип цены или handle
//int _bars2 = 0; // Количество баров
input group "------------------ Параметры стохастического оссцилятора ------------------ "
input int InpPeriodK1 = 5; // K-период (количество баров для расчетов)
input int InpPeriodD1 = 3; // D-период (период первичного сглаживания)
input int InpSlowing1 = 3; // Окончательное сглаживание
input int low_lim = 20; // Нижняя граница
input int up_lim = 80; // Верхняя граница
input ENUM_MA_METHOD Stohastic_MA_Mode = MODE_EMA;
input ENUM_STO_PRICE Stohastic_STO_PRICE = STO_LOWHIGH;
//+------------------------------------------------------------------+
//| Инициализация экземпляров классов |
//+------------------------------------------------------------------+
CGeneral_class genClass;
CIndicators_class indicators;
CExternalIndicators_class externalIndcators;
CBollingerBands Bollinger1(_Symbol, per, BBPeriod, BollingerMAshift, BBDev, Bollinger_Applied_price, true, Bollinger_range);
//CBollingerBands Bollinger2(_Symbol, per2, BBPeriod2, 0, BBDev2, Bollinger_Applied_price2, true, Bollinger_range2);
CStohastic Stohastic();
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
a_trade.SetExpertMagicNumber(Magic);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Временной интервал торговли |
//+------------------------------------------------------------------+
string Start_Time = IntegerToString(StartTime,2);
string End_Time = IntegerToString(EndTime,2);
double lot_b = 0;
double lot_s = 0;
//int stopLoss = per == PERIOD_M1 ? 150 : 200;
//+------------------------------------------------------------------+
//| Выполняем на каждом тике |
//+------------------------------------------------------------------+
void OnTick()
{
// Получаем цены аск и бид
double Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
// Заполняем буферы актуальными данными
Bollinger1.FillBuffer(Bollinger1.handle);
//Bollinger2.FillBuffer(Bollinger2.handle);
Stohastic.FillBuffer(Stohastic.handle);
// Разница между верхним и нижним Bollinger, пример доступа к свойству класса.
double diffBollinger = Bollinger1.upBand[0] - Bollinger1.downBand[0];
//double diffBollinger2 = Bollinger2.upBand[0] - Bollinger2.downBand[0];
// double startPositionSize = 0.1;
int openBuyCount = 0, openSellCount = 0;
datetime time_openBuy = 0, time_openSell = 0;
// На каждом тике проверяем наличие открытой позиции
genClass.CheckOpen(openBuyCount, openSellCount, time_openBuy, time_openSell);
//----------------------Анализ на новом баре---------------------------------------------
if(NewBar)
{
if(!genClass.isNewBar(per))
return;
}
if(TimeCheck)
{
if(!genClass.CheckTimePeriod(Start_Time,End_Time))
return;
}
double risk_lotBuy = genClass.LotSize(SL, Lot_percent);
if(Risk_lot)
lot_b = risk_lotBuy;
else
lot_b = Lot1_;
double risk_lotSell = genClass.LotSize(SL, Lot_percent);
if(Risk_lot)
lot_s = risk_lotSell;
else
lot_s = Lot1_;
//----------------------Открытие позиции---------------------------------------------
if(Bollinger1.BollingerOpenBuySignal(openBuyCount, Ask) && Stohastic.IsLowLimit())
a_trade.Buy(risk_lotBuy,_Symbol,Ask,SL*_Point,0);
if(Bollinger1.BollingerOpenSellSignal(openSellCount,Bid) && Stohastic.IsUpLimit())
a_trade.Sell(risk_lotSell,_Symbol,Bid,SL*_Point,0);
//---------------------Закрытие позиции---------------------------------------------
if(Bollinger1.BollingerCloseBuySignal(openBuyCount,Ask))
genClass.CloseBuy();
if(Bollinger1.BollingerCloseSellSignal(openBuyCount,Bid))
genClass.CloseSell();
}
//+------------------------------------------------------------------+