MergedEA_BB_RSI_MACD - 多策略交易专家顾问 (EA)
//+------------------------------------------------------------------+ //| MergedEA_BB_RSI_MACD.mq4 | //| 由 binjie09 基于 SampleEA.mq4 和 SampleEA_BB_SO.mq4 修改 | //| https://www.mql5.com | //+------------------------------------------------------------------+
// 版权声明和链接 #property copyright '个人开发.' #property link 'QQ1092168941'
// 版本号和严格模式 #property version '1.00' #property strict
//--------------------------第一套EA参数设置----------------------------- // 止损点数 extern double TakeProfit1 = 4000; // 每次交易手数 extern double Lots1 = 0.02; // 最大持仓手数 extern double MaxLots1 = 0.06; // RSI指标周期 extern int RSI_Period1 = 20; // MACD快线周期 extern int MACD_Fast_Period1 = 20; // MACD慢线周期 extern int MACD_Slow_Period1 = 26; // MACD信号线周期 extern int MACD_Signal_Period1 = 9;
//--------------------------第二套EA参数设置----------------------------- // 止损点数 extern double TakeProfit2 = 1500; // 每次交易手数 extern double Lots2 = 0.05; // 最大持仓手数 extern double MaxLots2 = 0.2; // 布林带周期 extern int BB_Period2 = 10; // 布林带偏离 extern int BB_Deviation2 = 2; // 随机振荡器K线周期 extern int SO_K_Period2 = 81; // 随机振荡器D线周期 extern int SO_D_Period2 = 9; // 随机振荡器减速 extern int SO_Slowing2 = 9; // 时间周期 extern int Timeframe2 = 15;
//--------------------------全局变量设置----------------------------- // 当前持仓手数 double accountLots = 0.0; // 总订单数量 int totalOrders = 0; // 当前小时开仓次数 int currentHourOrders = 0; // 上一个小时 datetime lastHour = -1;
// 剩余使用时间 int remainingTime = 10080; // 默认为7天
//--------------------------第一套EA函数----------------------------- void RunStrategy1() { double RSI = iRSI(NULL, PERIOD_H1, RSI_Period1, PRICE_CLOSE, 1); double MACD_Main, MACD_Signal; MACD_Main = iMACD(NULL, PERIOD_H1, MACD_Fast_Period1, MACD_Slow_Period1, MACD_Signal_Period1, PRICE_CLOSE, MODE_MAIN, 1); MACD_Signal = iMACD(NULL, PERIOD_H1, MACD_Fast_Period1, MACD_Slow_Period1, MACD_Signal_Period1, PRICE_CLOSE, MODE_SIGNAL, 1);
// 当前小时开仓次数超过10次,停止开仓
if (currentHourOrders >= 10) {
return;
}
if (RSI < 30 && MACD_Main > MACD_Signal) {
if (accountLots + Lots1 <= MaxLots1) {
double sl = 0;
double tp = Ask + TakeProfit1 * Point;
int ticket = OrderSend(Symbol(), OP_BUY, Lots1, Ask, 3, sl, tp, NULL, 0, 0, 'EA1 Buy');
if(ticket < 0) {
Print('EA1 Buy OrderSend failed with error: ', GetLastError());
} else {
accountLots += Lots1;
currentHourOrders++;
}
}
} else if (RSI > 70 && MACD_Main < MACD_Signal) {
if (accountLots + Lots1 <= MaxLots1) {
double sl = 0;
double tp = Bid - TakeProfit1 * Point;
int ticket = OrderSend(Symbol(), OP_SELL, Lots1, Bid, 3, sl, tp, NULL, 0, 0, 'EA1 Sell');
if(ticket < 0) {
Print('EA1 Sell OrderSend failed with error: ', GetLastError());
} else {
accountLots += Lots1;
currentHourOrders++;
}
}
}
}
//--------------------------第二套EA函数----------------------------- void RunStrategy2() { double BBUpper, BBMiddle, BBLower; BBUpper = iBands(NULL, Timeframe2 * PERIOD_M15, BB_Period2, BB_Deviation2, 0, PRICE_CLOSE, MODE_UPPER, 1); BBMiddle = iBands(NULL, Timeframe2 * PERIOD_M15, BB_Period2, BB_Deviation2, 0, PRICE_CLOSE, MODE_MAIN, 1); BBLower = iBands(NULL, Timeframe2 * PERIOD_M15, BB_Period2, BB_Deviation2, 0, PRICE_CLOSE, MODE_LOWER, 1); double SO_K, SO_D; SO_K = iStochastic(NULL, Timeframe2 * PERIOD_M15, SO_K_Period2, SO_D_Period2, SO_Slowing2, MODE_SMA, 0, MODE_MAIN, 1); SO_D = iStochastic(NULL, Timeframe2 * PERIOD_M15, SO_K_Period2,SO_D_Period2, SO_Slowing2, MODE_SMA, 0, MODE_SIGNAL, 1);
// 当前小时开仓次数超过10次,停止开仓
if (currentHourOrders >= 10) {
return;
}
if (Close[1] < BBLower && SO_K < 20 && SO_K > SO_D) { // 如果收盘价低于布林带下轨且随机振荡器K线值低于20且高于D线值
if (accountLots + Lots2 <= MaxLots2) { // 检查是否超过最大持仓手数
int ticket = OrderSend(Symbol(), OP_BUY, Lots2, Ask, 3, 0, Ask + TakeProfit2 * Point, NULL, 0, 0, 'EA2 Buy'); // 发送买单
if(ticket < 0) {
Print('EA2 Buy OrderSend failed with error: ', GetLastError());
} else {
accountLots += Lots2;
currentHourOrders++;
}
}
} else if (Close[1] > BBUpper && SO_K > 80 && SO_K < SO_D) { // 如果收盘价高于布林带上轨且随机振荡器K线值高于80且低于D线值
if (accountLots + Lots2 <= MaxLots2) { //检查是否超过最大持仓手数
int ticket = OrderSend(Symbol(), OP_SELL, Lots2, Bid, 3, 0, Bid - TakeProfit2 * Point, NULL, 0, 0, 'EA2 Sell'); // 发送卖单
if(ticket < 0) {
Print('EA2 Sell OrderSend failed with error: ', GetLastError());
} else {
accountLots += Lots2;
currentHourOrders++;
}
}
}
}
//--------------------------主函数开始----------------------------- int OnInit() { remainingTime = remainingTime - (TimeCurrent() / 60); // 计算剩余使用时间 return(INIT_SUCCEEDED); // 初始化成功 }
void OnDeinit(const int reason) { // EA卸载时执行的操作 }
// 每天开盘第一次调用OnTick时重置当前小时开仓次数 // 每个小时开始时,记录当前小时的时间,清空当前小时开仓次数 void resetCurrentHourOrders() { datetime currentHour = Time[0] / 3600; if (currentHour != lastHour) { currentHourOrders = 0; } lastHour = currentHour; }
void OnTick() { // 重置全局变量 totalOrders = OrdersTotal(); accountLots = 0.0;
// 统计当前持仓手数
for (int i = 0; i < totalOrders; i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol() == Symbol()) {
if (StringFind(OrderComment(), 'EA1') != -1) {
accountLots += OrderLots();
}
if (StringFind(OrderComment(), 'EA2') != -1) {
accountLots += OrderLots();
}
}
}
}
// 如果已达到最大持仓手数,退出函数
if (accountLots >= MaxLots1 + MaxLots2) {
return;
}
resetCurrentHourOrders(); // 重置当前小时开仓次数
RunStrategy1(); // 第一
RunStrategy2(); // 第二
// 检查剩余使用时间
if (remainingTime <= 0) {
Print('剩余使用时间已到,EA 停止运行');
return;
}
// 显示剩余使用时间
int hours = remainingTime / 60;
int minutes = remainingTime % 60;
Print('剩余使用时间:', hours, '小时', minutes, '分钟');
}
//--------------------------主函数结束-----------------------------
原文地址: https://www.cveoy.top/t/topic/nmSr 著作权归作者所有。请勿转载和采集!