|
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property description "Bollinger Bands Squeeze Breakout Strategy"
#include <Trade/Trade.mqh>
//--- 输入参数
input int BandsPeriod = 20; // 布林带周期
input double BandsDeviations = 2.0; // 标准差倍数
input double WidthMultiplier = 1.1; // 宽度乘数
input int LookbackPeriod = 20; // 回溯周期
input double LotSize = 0.1; // 交易手数
input ulong MagicNumber = 123456; // 魔术码
input int Slippage = 3; // 滑点
input int TakeProfitPoints = 6000; // 止盈点数
input int StopLossPoints = 5000; // 止损点数
//--- 全局变量
int handleBands;
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
handleBands = iBands(_Symbol, _Period, BandsPeriod, 0, BandsDeviations, PRICE_CLOSE);
if(handleBands == INVALID_HANDLE) {
Print("Failed to initialize Bollinger Bands");
return(INIT_FAILED);
}
trade.SetExpertMagicNumber(MagicNumber);
trade.SetDeviationInPoints(Slippage);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 持仓检查函数 |
//+------------------------------------------------------------------+
bool HasPosition()
{
for(int i=PositionsTotal()-1; i>=0; i--)
if(PositionGetSymbol(i) == _Symbol && PositionGetInteger(POSITION_MAGIC) == MagicNumber)
return true;
return false;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
static datetime prevBarTime = 0;
datetime currentTime = iTime(_Symbol, _Period, 0);
if(currentTime == prevBarTime) return;
prevBarTime = currentTime;
// 获取布林带数据
double upper[], middle[], lower[];
ArraySetAsSeries(upper, true);
ArraySetAsSeries(middle, true);
ArraySetAsSeries(lower, true);
if(CopyBuffer(handleBands, 1, 0, LookbackPeriod+1, upper) <= 0 ||
CopyBuffer(handleBands, 0, 0, LookbackPeriod+1, middle) <= 0 ||
CopyBuffer(handleBands, 2, 0, LookbackPeriod+1, lower) <= 0)
{
Print("Error copying Bollinger Bands data!");
return;
}
// 计算当前布林带宽度
double currentWidth = upper[0] - lower[0];
// 寻找过去N个宽度中的最小值
double minWidth = DBL_MAX;
for(int i=1; i<=LookbackPeriod; i++) {
double w = upper[i] - lower[i];
if(w < minWidth) minWidth = w;
}
// 获取价格数据
double lastClose = iClose(_Symbol, _Period, 1);
// 交易信号检测
if(!HasPosition() && currentWidth < minWidth * WidthMultiplier)
{
// 多单入场条件
if(lastClose > upper[1])
{
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double sl = NormalizeDouble(ask - StopLossPoints * _Point, _Digits);
double tp = NormalizeDouble(ask + TakeProfitPoints * _Point, _Digits);
trade.Buy(LotSize, _Symbol, ask, sl, tp, "BB Buy Entry");
}
// 空单入场条件
else if(lastClose < lower[1])
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double sl = NormalizeDouble(bid + StopLossPoints * _Point, _Digits);
double tp = NormalizeDouble(bid - TakeProfitPoints * _Point, _Digits);
trade.Sell(LotSize, _Symbol, bid, sl, tp, "BB Sell Entry");
}
}
}这是一个mt5ea 加5跟均线过滤。5ma>10ma>15ma>20ma>60ma 只做多,做空刚好相反。 均线的数字要可选,修改后,100%输出源码,我复制使用
|
|