侦云取势 发表于 2025-4-12 20:38:38

这个指标叫rainbowmma.ex5

这个指标叫rainbowmma.ex5 写成mt5ea

这个指标的参数用来过滤:1-13黄色;14-26蓝色;27-39白色;40到53绿色;53到66粉色,

多头:当灰色在蓝色在黄色上,粉色线呈现多头状态时开多单

空头 :当灰色在蓝色在黄色下,粉色线呈现空头状态时开空单

出场:盈利6000清仓 亏损5000清仓



//+------------------------------------------------------------------+
//|                                                   RainbowMMA_EA.mq5 |
//|                                    Copyright 2023, EarnForex.com |
//|                                        https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"

// 输入参数
input double LotSize = 0.1;          // 交易手数
input int    TakeProfit = 6000;      // 止盈点数
input int    StopLoss = 5000;      // 止损点数
input int    MagicNumber = 12345;    // 魔术码

// 指标句柄
int rainbowHandle;
// 定义颜色组范围
#define YELLOW_START 0
#define YELLOW_END 12
#define BLUE_START 13
#define BLUE_END 25
#define GRAY_START 26
#define GRAY_END 38
#define GREEN_START 39
#define GREEN_END 52
#define PINK_START 53
#define PINK_END 65

//+------------------------------------------------------------------+
//| 初始化函数                                                       |
//+------------------------------------------------------------------+
int OnInit()
{
   rainbowHandle = iCustom(_Symbol, _Period, "RainbowMMA");
   if(rainbowHandle == INVALID_HANDLE)
   {
      Print("无法加载RainbowMMA指标!");
      return(INIT_FAILED);
   }
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| 判断MA组排列方向函数                                             |
//+------------------------------------------------------------------+
bool IsGroupBullish(int startIdx, int endIdx)
{
   for(int i = startIdx; i < endIdx; i++)
   {
      double maCurrent = GetMAValue(i);
      double maNext = GetMAValue(i+1);
      if(maCurrent < maNext) return false;
   }
   return true;
}

bool IsGroupBearish(int startIdx, int endIdx)
{
   for(int i = startIdx; i < endIdx; i++)
   {
      double maCurrent = GetMAValue(i);
      double maNext = GetMAValue(i+1);
      if(maCurrent > maNext) return false;
   }
   return true;
}

//+------------------------------------------------------------------+
//| 获取MA值函数                                                   |
//+------------------------------------------------------------------+
double GetMAValue(int index)
{
   double buf[];
   ArraySetAsSeries(buf, true);
   if(CopyBuffer(rainbowHandle, index, 0, 3, buf) < 3) return 0;
   return buf;
}

//+------------------------------------------------------------------+
//| 交易执行函数                                                   |
//+------------------------------------------------------------------+
void CheckForSignal()
{
   // 获取各组的排列状态
   bool grayAboveBlue = GetMAValue(GRAY_START) > GetMAValue(BLUE_START);
   bool blueAboveYellow = GetMAValue(BLUE_START) > GetMAValue(YELLOW_START);
   bool pinkBullish = IsGroupBullish(PINK_START, PINK_END);

   bool grayBelowBlue = GetMAValue(GRAY_START) < GetMAValue(BLUE_START);
   bool blueBelowYellow = GetMAValue(BLUE_START) < GetMAValue(YELLOW_START);
   bool pinkBearish = IsGroupBearish(PINK_START, PINK_END);

   // 多头条件
   if(grayAboveBlue && blueAboveYellow && pinkBullish)
   {
      OpenTrade(ORDER_TYPE_BUY);
   }
   // 空头条件
   else if(grayBelowBlue && blueBelowYellow && pinkBearish)
   {
      OpenTrade(ORDER_TYPE_SELL);
   }
}

//+------------------------------------------------------------------+
//| 开仓函数                                                         |
//+------------------------------------------------------------------+
void OpenTrade(ENUM_ORDER_TYPE orderType)
{
   if(PositionsTotal() > 0) return;

   MqlTradeRequest request = {};
   MqlTradeResult result = {};
   request.action = TRADE_ACTION_DEAL;
   request.symbol = _Symbol;
   request.volume = LotSize;
   request.type = orderType;
   request.magic = MagicNumber;
   request.price = orderType == ORDER_TYPE_BUY ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) : SymbolInfoDouble(_Symbol, SYMBOL_BID);

   // 设置止盈止损
   if(TakeProfit > 0)
      request.tp = orderType == ORDER_TYPE_BUY ? request.price + TakeProfit * _Point : request.price - TakeProfit * _Point;
   if(StopLoss > 0)
      request.sl = orderType == ORDER_TYPE_BUY ? request.price - StopLoss * _Point : request.price + StopLoss * _Point;

   if(!OrderSend(request, result))
      Print("开仓失败,错误代码:", GetLastError());
}

//+------------------------------------------------------------------+
//| 平仓函数                                                         |
//+------------------------------------------------------------------+
void CloseAllTrades()
{
   for(int i = PositionsTotal()-1; i >= 0; i--)
   {
      ulong ticket = PositionGetTicket(i);
      if(PositionSelectByTicket(ticket))
      {
         MqlTradeRequest request = {};
         MqlTradeResult result = {};
         request.action = TRADE_ACTION_DEAL;
         request.symbol = _Symbol;
         request.volume = PositionGetDouble(POSITION_VOLUME);
         request.type = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) ? ORDER_TYPE_SELL : ORDER_TYPE_BUY;
         request.price = PositionGetDouble(POSITION_PRICE_CURRENT);
         request.magic = MagicNumber;

         if(!OrderSend(request, result))
            Print("平仓失败,错误代码:", GetLastError());
      }
   }
}

//+------------------------------------------------------------------+
//| 检查盈亏函数                                                   |
//+------------------------------------------------------------------+
void CheckProfitLoss()
{
   double profit = 0;
   for(int i = 0; i < PositionsTotal(); i++)
   {
      ulong ticket = PositionGetTicket(i);
      if(PositionSelectByTicket(ticket) && PositionGetInteger(POSITION_MAGIC) == MagicNumber)
      {
         profit += PositionGetDouble(POSITION_PROFIT);
      }
   }

   if(profit >= TakeProfit || profit <= -StopLoss)
      CloseAllTrades();
}

//+------------------------------------------------------------------+
//| OnTick函数                                                       |
//+------------------------------------------------------------------+
void OnTick()
{
   CheckForSignal();
   CheckProfitLoss();
}
//+------------------------------------------------------------------+

页: [1]
查看完整版本: 这个指标叫rainbowmma.ex5

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