Project_N_1/Bollinger_advisor_example.mq5

170 lines
15 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:18:18 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| 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>
//+------------------------------------------------------------------+
//| E>4=K5 ?0@0<5B@K 4;O >?B8<870F88 |
//+------------------------------------------------------------------+
input group "-------- 1I85 ?0@0<5B@K -------"
input bool NewBar = true;
input bool TimeCheck = true;
input ENUM_TIMEFRAMES per = PERIOD_M1; //5@8>4 4;O B5:CI59 B>@3>2;8
//input ENUM_TIMEFRAMES per2 = PERIOD_M5; //5@8>4 4;O 4>;3>A@>G=>3> B@5=40
//int Slippage = 30;
input int StartTime = 01;
input int EndTime = 23;
input bool Risk_lot = true;
input double Lot_percent = 5; // @8A: % 10;0=A0
input double SL = 150; //!B>? ;>AA
input double Lot1_ = 0.1; //@07<5@ ;>B0
input group "------------------ 0@0<0B@ ?>;>A >;;8=465@0 ------------------ "
input int BBPeriod = 30; // 5@8>4 4;O @0AG5B0 A@54=59 ;8=88
input double BBDev = 1; // >;-2> AB0=40@B=KE >B:;>=5=89
input double Bollinger_range = 20; // Bollinger range
input ENUM_APPLIED_PRICE Bollinger_Applied_price = PRICE_CLOSE; // "8? F5=K 8;8 handle
//int _bars = 0; // >;8G5AB2> 10@>2
input int BollingerMAshift = 0;
//input group "------------------ 0@0<0B@ ?>;>A >;;8=465@0 2 ------------------ "
//
//input int BBPeriod2 = 30; // 5@8>4 4;O @0AG5B0 A@54=59 ;8=88
//input double BBDev2 = 1; // >;-2> AB0=40@B=KE >B:;>=5=89
//input double Bollinger_range2 = 20; //
//input ENUM_APPLIED_PRICE Bollinger_Applied_price2 = PRICE_CLOSE; // "8? F5=K 8;8 handle
//int _bars2 = 0; // >;8G5AB2> 10@>2
input group "------------------ 0@0<5B@K AB>E0AB8G5A:>3> >AAF8;OB>@0 ------------------ "
input int InpPeriodK1 = 5; // K-?5@8>4 (:>;8G5AB2> 10@>2 4;O @0AG5B>2)
input int InpPeriodD1 = 3; // D-?5@8>4 (?5@8>4 ?5@28G=>3> A3;06820=8O)
input int InpSlowing1 = 3; // :>=G0B5;L=>5 A3;06820=85
input int low_lim = 20; // 86=OO 3@0=8F0
input int up_lim = 80; // 5@E=OO 3@0=8F0
input ENUM_MA_METHOD Stohastic_MA_Mode = MODE_EMA;
input ENUM_STO_PRICE Stohastic_STO_PRICE = STO_LOWHIGH;
//+------------------------------------------------------------------+
//| =8F80;870F8O M:75<?;O@>2 :;0AA>2 |
//+------------------------------------------------------------------+
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)
{
//---
}
//+------------------------------------------------------------------+
//| @5<5==>9 8=B5@20; B>@3>2;8 |
//+------------------------------------------------------------------+
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;
//+------------------------------------------------------------------+
//| K?>;=O5< =0 :064>< B8:5 |
//+------------------------------------------------------------------+
void OnTick()
{
// >;CG05< F5=K 0A: 8 184
double Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
// 0?>;=O5< 1CD5@K 0:BC0;L=K<8 40==K<8
Bollinger1.FillBuffer(Bollinger1.handle);
//Bollinger2.FillBuffer(Bollinger2.handle);
Stohastic.FillBuffer(Stohastic.handle);
// 07=8F0 <564C 25@E=8< 8 =86=8< Bollinger, ?@8<5@ 4>ABC?0 : A2>9AB2C :;0AA0.
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;
// 0 :064>< B8:5 ?@>25@O5< =0;8G85 >B:@KB>9 ?>78F88
genClass.CheckOpen(openBuyCount, openSellCount, time_openBuy, time_openSell);
//----------------------=0;87 =0 =>2>< 10@5---------------------------------------------
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_;
//----------------------B:@KB85 ?>78F88---------------------------------------------
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);
//---------------------0:@KB85 ?>78F88---------------------------------------------
if(Bollinger1.BollingerCloseBuySignal(openBuyCount,Ask))
genClass.CloseBuy();
if(Bollinger1.BollingerCloseSellSignal(openBuyCount,Bid))
genClass.CloseSell();
}
//+------------------------------------------------------------------+