找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
首页论坛EAHub 外汇论坛
查看: 8|回复: 0

MT5編碼請求幫忙!!

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-25 14:44:00 | 显示全部楼层 |阅读模式
最近正在學習寫EA,東拼西湊了一些內容

可是總是出現Compile總是出現錯誤,主要是跟「POSITION」函數有關

想請各位高手指導!!

EA的邏輯是雙均線+RSI過濾+MACD過濾

1.進場條件:

  (1)多單:

      當前無多單

      均線黃金交叉

      RSI 小於設定值

      MACD 黃金交叉

  (2)空單:

      當前無空單

      均線死亡交叉

      RSI 大於設定值

      MACD 死亡交叉

2.出場條件

      均線反向交叉 + MACD 反向交叉

      或虧損達到設定值

      或移動停利

3.手數手動設定

----------------------------------------------------------------------------------------------------------------

//+------------------------------------------------------------------+

//|                 MA_RSI_MACD_Strategy.mq5                         |

//+------------------------------------------------------------------+

#property strict


input double TradeLot        = 0.1;      // 固定手數

input int FastMAPeriod       = 12;

input int SlowMAPeriod       = 26;

input ENUM_MA_METHOD MaMethod = MODE_SMA;

input ENUM_APPLIED_PRICE MaPrice = PRICE_CLOSE;


input int RsiPeriod          = 14;

input double RsiBuyLevel     = 30.0;

input double RsiSellLevel    = 70.0;


input int MacdFast           = 12;

input int MacdSlow           = 26;

input int MacdSignal         = 9;


input double StopLoss        = 100;      // 點數

input double TrailingStop    = 80;       // 移動停利點數


input ENUM_TIMEFRAMES TF     = PERIOD_H1;


datetime lastTradeTime = 0;


//+------------------------------------------------------------------+

//| 取得指標值                                                      |

//+------------------------------------------------------------------+

bool IsGoldenCross()

{

   double fastPrev = iMA(_Symbol, TF, FastMAPeriod, 0, MaMethod, MaPrice, 1);

   double slowPrev = iMA(_Symbol, TF, SlowMAPeriod, 0, MaMethod, MaPrice, 1);

   double fastCurr = iMA(_Symbol, TF, FastMAPeriod, 0, MaMethod, MaPrice, 0);

   double slowCurr = iMA(_Symbol, TF, SlowMAPeriod, 0, MaMethod, MaPrice, 0);

   return (fastPrev < slowPrev && fastCurr > slowCurr);

}


bool IsDeathCross()

{

   double fastPrev = iMA(_Symbol, TF, FastMAPeriod, 0, MaMethod, MaPrice, 1);

   double slowPrev = iMA(_Symbol, TF, SlowMAPeriod, 0, MaMethod, MaPrice, 1);

   double fastCurr = iMA(_Symbol, TF, FastMAPeriod, 0, MaMethod, MaPrice, 0);

   double slowCurr = iMA(_Symbol, TF, SlowMAPeriod, 0, MaMethod, MaPrice, 0);

   return (fastPrev > slowPrev && fastCurr < slowCurr);

}


bool IsMACDGoldenCross()

{

   double macdPrev, signalPrev, macdCurr, signalCurr;

   iMACD(_Symbol, TF, MacdFast, MacdSlow, MacdSignal, PRICE_CLOSE, macdPrev, signalPrev, 1);

   iMACD(_Symbol, TF, MacdFast, MacdSlow, MacdSignal, PRICE_CLOSE, macdCurr, signalCurr, 0);

   return (macdPrev < signalPrev && macdCurr > signalCurr);

}


bool IsMACDDeathCross()

{

   double macdPrev, signalPrev, macdCurr, signalCurr;

   iMACD(_Symbol, TF, MacdFast, MacdSlow, MacdSignal, PRICE_CLOSE, macdPrev, signalPrev, 1);

   iMACD(_Symbol, TF, MacdFast, MacdSlow, MacdSignal, PRICE_CLOSE, macdCurr, signalCurr, 0);

   return (macdPrev > signalPrev && macdCurr < signalCurr);

}


double GetRSI()

{

   return iRSI(_Symbol, TF, RsiPeriod, PRICE_CLOSE, 0);

}


//+------------------------------------------------------------------+

//| 主邏輯:OnTick                                                   |

//+------------------------------------------------------------------+

void OnTick()

{

   // 檢查是否已經有單

   bool hasBuy = PositionSelect(_Symbol) && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY;

   bool hasSell = PositionSelect(_Symbol) && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL;


   double rsi = GetRSI();

   double sl = StopLoss * _Point;

   double tp = 0;


   // 進多單條件

   if (!hasBuy && IsGoldenCross() && rsi < RsiBuyLevel && IsMACDGoldenCross())

   {

      if (OrderSend(_Symbol, OP_BUY, TradeLot, Ask, 10, Ask - sl, tp, "Buy Order", 0, 0, clrBlue) > 0)

         lastTradeTime = TimeCurrent();

   }


   // 進空單條件

   if (!hasSell && IsDeathCross() && rsi > RsiSellLevel && IsMACDDeathCross())

   {

      if (OrderSend(_Symbol, OP_SELL, TradeLot, Bid, 10, Bid + sl, tp, "Sell Order", 0, 0, clrRed) > 0)

         lastTradeTime = TimeCurrent();

   }


   // 出場邏輯(包含反向交叉、移動停利)

   if (PositionSelect(_Symbol))

   {

      ulong ticket = PositionGetInteger(POSITION_TICKET);

      double entryPrice = PositionGetDouble(POSITION_PRICE_OPEN);

      double stopLoss = PositionGetDouble(POSITION_SL);

      double currentPrice = SymbolInfoDouble(_Symbol, PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY ? SYMBOL_BID : SYMBOL_ASK);

      double profit = PositionGetDouble(POSITION_PROFIT);


      // 出場條件

      bool exitBuy = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY &&

                      (IsDeathCross() && IsMACDDeathCross()));


      bool exitSell = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL &&

                       (IsGoldenCross() && IsMACDGoldenCross()));


      if (exitBuy || exitSell)

         PositionClose(ticket);

      

      // 移動停利

      if (TrailingStop > 0)

      {

         double trail = TrailingStop * _Point;

         double newSL;

         if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)

         {

            newSL = currentPrice - trail;

            if (newSL > stopLoss)

               PositionModify(ticket, newSL, 0);

         }

         else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)

         {

            newSL = currentPrice + trail;

            if (newSL < stopLoss)

               PositionModify(ticket, newSL, 0);

         }

      }

   }

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

本版积分规则

QQ|Archiver|手机版|小黑屋|EAHub外汇论坛

GMT+8, 2026-10-11 22:28 , Processed in 0.106492 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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