希望祠.同舟.量化.侦云取势势能研究中心

 找回密码
 立即注册

《周易.系辞》

君子藏器于身,待时而动!

《吕氏.春秋》

君子谋时而动,顺势而为!

《晏子·霸业因时而生》

识时务者为俊杰,通机变者为英豪!

《周易.系辞》

天之所助,吉无不利,天之所助者.顺天者也!

《史记.太史公自序》
天之势,浩浩汤汤,顺之者昌,逆之者亡!

《寒窑赋》

天不得时,日月无光.地 不得时,草木不长.

《周易.系辞》

刚柔者,立本者也!变通者,识时者也!

圣人云:

君子务本,本立而道生!

 《势胜学》

不知势,无以为人也.势易而未觉,必败焉!

《周易·系辞上传》:

富有之谓大业,日新之谓盛德!

《礼记.大学》:
格物致知,正心诚意,修身齐家!

《礼记.中庸》:

凡事预则立,不预则废!

查看: 1447|回复: 1

[【优秀推荐】曲线完美收益中等] macd柱状线突破最近10根内最高值

[复制链接]

1653

主题

1887

帖子

1万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
11906

最佳新人活跃会员热心会员推广达人宣传达人荣誉版主勋章理事

QQ
侦云取势 发表于 2025-4-2 00:49:57 | 显示全部楼层 |阅读模式
写一个mt5ea:零轴突破+柱状线加速
逻辑

多单入场条件:
多单过滤条件1:macd柱状线突破最近10根内最高值
多单过滤条件2:k线在20ma均线上,
多单开单:满足多单条件1.2时,MACD柱状线连续3根递增(正值且逐根放大)时:买入多单
空单入场条件:
空单条件1:macd柱状线突破最近10根内最低值
空单条件2:k线在20ma均线下面
空单开单:满足空单条件1.2时,MACD柱状线连续3根递减(负值且逐根放大)时:买入空单

出场:柱状线连续2根递减时平仓
止损:入场K线最低价下方1.5倍ATR
//+------------------------------------------------------------------+
//|                                                  MACDBreakEA.mq5 |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>

//--- 输入参数
input int      MACDFast = 12;          // MACD快线周期
input int      MACDSlow = 26;          // MACD慢线周期
input int      MACDSignal = 9;         // MACD信号周期
input double   LotSize = 0.1;          // 交易手数
input int      TakeProfitPoints = 6000;// 止盈点数
input int      StopLossPoints = 5000;  // 止损点数

//--- 可自定义均线参数
input int      MAPeriod1 = 60;         // 均线1周期
input int      MAPeriod2 = 84;         // 均线2周期
input int      MAPeriod3 = 120;        // 均线3周期
input int      MAPeriod4 = 144;        // 均线4周期
input int      MAPeriod5 = 200;        // 均线5周期
input int      MAPeriod6 = 300;        // 均线6周期

//--- 指标句柄
int maHandles[6];
int macdHandle;
datetime lastBarTime;
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // 初始化均线指标数组
   int periods[6] = {MAPeriod1, MAPeriod2, MAPeriod3, MAPeriod4, MAPeriod5, MAPeriod6};

   for(int i=0; i<6; i++)
   {
      maHandles = iMA(_Symbol, _Period, periods, 0, MODE_SMA, PRICE_CLOSE);
      if(maHandles == INVALID_HANDLE)
      {
         Print("MA",i+1," 初始化失败,周期:",periods);
         return INIT_FAILED;
      }
   }

   macdHandle = iMACD(_Symbol, _Period, MACDFast, MACDSlow, MACDSignal, PRICE_CLOSE);
   if(macdHandle == INVALID_HANDLE)
   {
      Print("MACD初始化失败");
      return INIT_FAILED;
   }

   lastBarTime = 0;
   return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   for(int i=0; i<6; i++)
      IndicatorRelease(maHandles);
   IndicatorRelease(macdHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   if(!IsNewBar()) return;

   MqlRates rates[];
   double macd[], ma1[2], ma2[2], ma3[2], ma4[2], ma5[2], ma6[2];

   // 获取价格数据
   if(CopyRates(_Symbol, _Period, 0, 3, rates) != 3) return;

   // 获取MACD数据
   if(CopyBuffer(macdHandle, 0, 0, 13, macd) < 13) return;

   // 获取均线数据(修正数组传递方式)
   if(CopyBuffer(maHandles[0], 0, 0, 2, ma1) < 2) return;
   if(CopyBuffer(maHandles[1], 0, 0, 2, ma2) < 2) return;
   if(CopyBuffer(maHandles[2], 0, 0, 2, ma3) < 2) return;
   if(CopyBuffer(maHandles[3], 0, 0, 2, ma4) < 2) return;
   if(CopyBuffer(maHandles[4], 0, 0, 2, ma5) < 2) return;
   if(CopyBuffer(maHandles[5], 0, 0, 2, ma6) < 2) return;

   ArraySetAsSeries(rates, true);
   ArraySetAsSeries(macd, true);

   bool hasPosition = PositionSelect(_Symbol);

   // 多单逻辑
   if(CheckLongConditions(rates, macd, ma1[1], ma2[1], ma3[1], ma4[1], ma5[1], ma6[1]) && !hasPosition)
   {
      ExecuteOrder(ORDER_TYPE_BUY);
   }

   // 空单逻辑
   if(CheckShortConditions(rates, macd, ma1[1], ma2[1], ma3[1], ma4[1], ma5[1], ma6[1]) && !hasPosition)
   {
      ExecuteOrder(ORDER_TYPE_SELL);
   }
}
//+------------------------------------------------------------------+
//| 执行下单操作                                                     |
//+------------------------------------------------------------------+
void ExecuteOrder(ENUM_ORDER_TYPE orderType)
{
   if(orderType == ORDER_TYPE_BUY)
   {
      double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
      trade.Buy(LotSize, _Symbol, ask,
                NormalizeDouble(ask - StopLossPoints*point, _Digits),
                NormalizeDouble(ask + TakeProfitPoints*point, _Digits),
                "多单");
   }
   else if(orderType == ORDER_TYPE_SELL)
   {
      double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
      trade.Sell(LotSize, _Symbol, bid,
                 NormalizeDouble(bid + StopLossPoints*point, _Digits),
                 NormalizeDouble(bid - TakeProfitPoints*point, _Digits),
                 "空单");
   }
}
//+------------------------------------------------------------------+
//| 多单入场条件检查                                                 |
//+------------------------------------------------------------------+
bool CheckLongConditions(const MqlRates &rates[], const double &macd[],
                        double ma1, double ma2, double ma3,
                        double ma4, double ma5, double ma6)
{
   // 均线过滤条件:MAPeriod1 > MAPeriod2 > MAPeriod3 > MAPeriod4 > MAPeriod5 > MAPeriod6
   if(!(ma1 > ma2 && ma2 > ma3 && ma3 > ma4 && ma4 > ma5 && ma5 > ma6))
      return false;

   // MACD条件
   double maxMacd = GetArrayMax(macd, 2, 10);
   if(macd[1] <= maxMacd) return false;

   // 连续3根递增正值
   if(macd[1] <= macd[2] || macd[2] <= macd[3]) return false;
   if(macd[1] <= 0 || macd[2] <= 0 || macd[3] <= 0) return false;

   return true;
}
//+------------------------------------------------------------------+
//| 空单入场条件检查                                                 |
//+------------------------------------------------------------------+
bool CheckShortConditions(const MqlRates &rates[], const double &macd[],
                         double ma1, double ma2, double ma3,
                         double ma4, double ma5, double ma6)
{
   // 均线过滤条件:MAPeriod1 < MAPeriod2 < MAPeriod3 < MAPeriod4 < MAPeriod5 < MAPeriod6
   if(!(ma1 < ma2 && ma2 < ma3 && ma3 < ma4 && ma4 < ma5 && ma5 < ma6))
      return false;

   // MACD条件
   double minMacd = GetArrayMin(macd, 2, 10);
   if(macd[1] >= minMacd) return false;

   // 连续3根递减负值
   if(macd[1] >= macd[2] || macd[2] >= macd[3]) return false;
   if(macd[1] >= 0 || macd[2] >= 0 || macd[3] >= 0) return false;

   return true;
}
//+------------------------------------------------------------------+
//| 辅助函数:获取数组最大值                                         |
//+------------------------------------------------------------------+
double GetArrayMax(const double &arr[], int start, int count)
{
   double maxVal = -DBL_MAX;
   int end = start + count;
   if(end > ArraySize(arr)) end = ArraySize(arr);

   for(int i=start; i<end; i++)
      if(arr > maxVal) maxVal = arr;
   return maxVal;
}
//+------------------------------------------------------------------+
//| 辅助函数:获取数组最小值                                         |
//+------------------------------------------------------------------+
double GetArrayMin(const double &arr[], int start, int count)
{
   double minVal = DBL_MAX;
   int end = start + count;
   if(end > ArraySize(arr)) end = ArraySize(arr);

   for(int i=start; i<end; i++)
      if(arr < minVal) minVal = arr;
   return minVal;
}
//+------------------------------------------------------------------+
//| 检测新K线                                                       |
//+------------------------------------------------------------------+
bool IsNewBar()
{
   datetime currentTime = iTime(_Symbol, _Period, 0);
   if(currentTime != lastBarTime)
   {
      lastBarTime = currentTime;
      return true;
   }
   return false;
}
//+------------------------------------------------------------------+

观天之道,执天之行。
行胜于言,取时于天。

成功=艰苦劳动+正确方法+少说空话 --爱因斯坦
回复

使用道具 举报

1653

主题

1887

帖子

1万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
11906

最佳新人活跃会员热心会员推广达人宣传达人荣誉版主勋章理事

QQ
 楼主| 侦云取势 发表于 2025-4-2 01:15:09 | 显示全部楼层
//+------------------------------------------------------------------+
//|                                                  MACDBreakEA.mq5 |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>

//--- 输入参数
input int      MACDFast = 12;          // MACD快线周期
input int      MACDSlow = 26;          // MACD慢线周期
input int      MACDSignal = 9;         // MACD信号周期
input int      MACDBreakBars = 10;     // 突破周期(原10根)
input int      MACDConsecutiveBars = 3;// 连续递增/递减周期(原3根)
input double   LotSize = 0.1;          // 交易手数
input int      TakeProfitPoints = 6000;// 止盈点数
input int      StopLossPoints = 5000;  // 止损点数

//--- 可自定义均线参数
input int      MAPeriod1 = 60;         // 均线1周期
input int      MAPeriod2 = 84;         // 均线2周期
input int      MAPeriod3 = 120;        // 均线3周期
input int      MAPeriod4 = 144;        // 均线4周期
input int      MAPeriod5 = 200;        // 均线5周期
input int      MAPeriod6 = 300;        // 均线6周期

//--- 指标句柄
int maHandles[6];
int macdHandle;
datetime lastBarTime;
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // 初始化均线指标数组
   int periods[6] = {MAPeriod1, MAPeriod2, MAPeriod3, MAPeriod4, MAPeriod5, MAPeriod6};

   for(int i=0; i<6; i++)
   {
      maHandles = iMA(_Symbol, _Period, periods, 0, MODE_SMA, PRICE_CLOSE);
      if(maHandles == INVALID_HANDLE)
      {
         Print("MA",i+1," 初始化失败,周期:",periods);
         return INIT_FAILED;
      }
   }

   macdHandle = iMACD(_Symbol, _Period, MACDFast, MACDSlow, MACDSignal, PRICE_CLOSE);
   if(macdHandle == INVALID_HANDLE)
   {
      Print("MACD初始化失败");
      return INIT_FAILED;
   }

   lastBarTime = 0;
   return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   for(int i=0; i<6; i++)
      IndicatorRelease(maHandles);
   IndicatorRelease(macdHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   if(!IsNewBar()) return;

   MqlRates rates[];
   double macd[], ma1[2], ma2[2], ma3[2], ma4[2], ma5[2], ma6[2];

   // 计算需要获取的MACD数据量
   int requiredBars = MACDBreakBars + MACDConsecutiveBars + 1;

   // 获取价格数据
   if(CopyRates(_Symbol, _Period, 0, 3, rates) != 3) return;

   // 获取MACD数据
   if(CopyBuffer(macdHandle, 0, 0, requiredBars, macd) < requiredBars) return;

   // 获取均线数据
   if(CopyBuffer(maHandles[0], 0, 0, 2, ma1) < 2) return;
   if(CopyBuffer(maHandles[1], 0, 0, 2, ma2) < 2) return;
   if(CopyBuffer(maHandles[2], 0, 0, 2, ma3) < 2) return;
   if(CopyBuffer(maHandles[3], 0, 0, 2, ma4) < 2) return;
   if(CopyBuffer(maHandles[4], 0, 0, 2, ma5) < 2) return;
   if(CopyBuffer(maHandles[5], 0, 0, 2, ma6) < 2) return;

   ArraySetAsSeries(rates, true);
   ArraySetAsSeries(macd, true);

   bool hasPosition = PositionSelect(_Symbol);

   // 多单逻辑
   if(CheckLongConditions(rates, macd, ma1[1], ma2[1], ma3[1], ma4[1], ma5[1], ma6[1]) && !hasPosition)
   {
      ExecuteOrder(ORDER_TYPE_BUY);
   }

   // 空单逻辑
   if(CheckShortConditions(rates, macd, ma1[1], ma2[1], ma3[1], ma4[1], ma5[1], ma6[1]) && !hasPosition)
   {
      ExecuteOrder(ORDER_TYPE_SELL);
   }
}
//+------------------------------------------------------------------+
//| 执行下单操作                                                     |
//+------------------------------------------------------------------+
void ExecuteOrder(ENUM_ORDER_TYPE orderType)
{
   if(orderType == ORDER_TYPE_BUY)
   {
      double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
      trade.Buy(LotSize, _Symbol, ask,
                NormalizeDouble(ask - StopLossPoints*point, _Digits),
                NormalizeDouble(ask + TakeProfitPoints*point, _Digits),
                "多单");
   }
   else if(orderType == ORDER_TYPE_SELL)
   {
      double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
      trade.Sell(LotSize, _Symbol, bid,
                 NormalizeDouble(bid + StopLossPoints*point, _Digits),
                 NormalizeDouble(bid - TakeProfitPoints*point, _Digits),
                 "空单");
   }
}
//+------------------------------------------------------------------+
//| 多单入场条件检查                                                 |
//+------------------------------------------------------------------+
bool CheckLongConditions(const MqlRates &rates[], const double &macd[],
                        double ma1, double ma2, double ma3,
                        double ma4, double ma5, double ma6)
{
   // 均线过滤条件
   if(!(ma1 > ma2 && ma2 > ma3 && ma3 > ma4 && ma4 > ma5 && ma5 > ma6))
      return false;

   // MACD突破条件
   double maxMacd = GetArrayMax(macd, 1, MACDBreakBars);
   if(macd[0] <= maxMacd) return false;

   // 连续递增检查
   for(int i=0; i<MACDConsecutiveBars-1; i++)
   {
      if(macd <= macd[i+1]) return false;
      if(macd <= 0) return false;
   }
   if(macd[MACDConsecutiveBars-1] <= 0) return false;

   return true;
}
//+------------------------------------------------------------------+
//| 空单入场条件检查                                                 |
//+------------------------------------------------------------------+
bool CheckShortConditions(const MqlRates &rates[], const double &macd[],
                         double ma1, double ma2, double ma3,
                         double ma4, double ma5, double ma6)
{
   // 均线过滤条件
   if(!(ma1 < ma2 && ma2 < ma3 && ma3 < ma4 && ma4 < ma5 && ma5 < ma6))
      return false;

   // MACD突破条件
   double minMacd = GetArrayMin(macd, 1, MACDBreakBars);
   if(macd[0] >= minMacd) return false;

   // 连续递减检查
   for(int i=0; i<MACDConsecutiveBars-1; i++)
   {
      if(macd >= macd[i+1]) return false;
      if(macd >= 0) return false;
   }
   if(macd[MACDConsecutiveBars-1] >= 0) return false;

   return true;
}
//+------------------------------------------------------------------+
//| 辅助函数:获取数组最大值                                         |
//+------------------------------------------------------------------+
double GetArrayMax(const double &arr[], int start, int count)
{
   double maxVal = -DBL_MAX;
   int end = start + count;
   if(end > ArraySize(arr)) end = ArraySize(arr);

   for(int i=start; i<end; i++)
      if(arr > maxVal) maxVal = arr;
   return maxVal;
}
//+------------------------------------------------------------------+
//| 辅助函数:获取数组最小值                                         |
//+------------------------------------------------------------------+
double GetArrayMin(const double &arr[], int start, int count)
{
   double minVal = DBL_MAX;
   int end = start + count;
   if(end > ArraySize(arr)) end = ArraySize(arr);

   for(int i=start; i<end; i++)
      if(arr < minVal) minVal = arr;
   return minVal;
}
//+------------------------------------------------------------------+
//| 检测新K线                                                       |
//+------------------------------------------------------------------+
bool IsNewBar()
{
   datetime currentTime = iTime(_Symbol, _Period, 0);
   if(currentTime != lastBarTime)
   {
      lastBarTime = currentTime;
      return true;
   }
   return false;
}
//+------------------------------------------------------------------+

观天之道,执天之行。
行胜于言,取时于天。

成功=艰苦劳动+正确方法+少说空话 --爱因斯坦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /5 下一条

人生者,生存也。
生存所需,财(钱)官(权)也。
财自食伤(子孙福德爻)生,
印星生身荫护,官财印全,适合人间生存也。
若命缺财,缺官,则失去生存权。
一旦脱离父母养育,将天天面临,
从绝望中醒来,又从绝望中睡去。来去如烟!

Archiver|小黑屋|希望祠.同舟.量化

GMT+8, 2025-5-2 22:32 , Processed in 0.024611 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表