以下代码包含了EMA交叉策略、4小时趋势过滤、止盈止损、趋势跟踪、止盈保护等功能。
//+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ input int EMA12_Period = 12; // 12周期EMA input int EMA36_Period = 36; // 36周期EMA input int EMA13_Period = 13; // 4小时图EMA13 input int EMA30_Period = 30; // 4小时图EMA30 input int StopLoss_MinDistance = 30; // 最小止损距离,点数 input double RiskRewardRatio = 3.0; // 盈亏比,默认3倍 input bool Enable4HTrendFilter = false; // 是否启用4小时趋势过滤(默认关闭) input double TrendProtectionThreshold = 0.5; // 盈利保护阈值 (默认0.5, 即3+0.5=3.5倍时进行保护) input double MaxProfitMultiplier = 5.0; // 盈利达到5倍时平仓(可配置) double EMA12, EMA36, EMA13, EMA30, EMA15; // 当前周期和4小时图的EMA double OpenPrice = 0; double StopLoss = 0; double TakeProfit = 0; int ticket; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // 初始化 Print("Expert Initialized!"); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { Print("Expert Deinitialized!"); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // 获取当前周期和4小时图的EMA值 EMA12 = iMA(Symbol(), 0, EMA12_Period, 0, MODE_EMA, PRICE_CLOSE, 0); EMA36 = iMA(Symbol(), 0, EMA36_Period, 0, MODE_EMA, PRICE_CLOSE, 0); // 获取4小时图上的EMA值 EMA13 = iMA(Symbol(), PERIOD_H4, EMA13_Period, 0, MODE_EMA, PRICE_CLOSE, 0); EMA30 = iMA(Symbol(), PERIOD_H4, EMA30_Period, 0, MODE_EMA, PRICE_CLOSE, 0); EMA15 = iMA(Symbol(), 0, 15, 0, MODE_EMA, PRICE_CLOSE, 0); // 当前周期15EMA // 4H趋势过滤 bool is4HTrendUp = EMA13 > EMA30; // 4小时趋势向上 bool is4HTrendDown = EMA13 < EMA30; // 4小时趋势向下 // EMA交叉条件 bool isCrossUp = EMA12 > EMA36 && iMA(Symbol(), 0, EMA12_Period, 0, MODE_EMA, PRICE_CLOSE, 1) <= iMA(Symbol(), 0, EMA36_Period, 0, MODE_EMA, PRICE_CLOSE, 1); bool isCrossDown = EMA12 < EMA36 && iMA(Symbol(), 0, EMA12_Period, 0, MODE_EMA, PRICE_CLOSE, 1) >= iMA(Symbol(), 0, EMA36_Period, 0, MODE_EMA, PRICE_CLOSE, 1); // 当前是否处于盈利保护范围 bool isProfitProtection = (OrderProfit() >= StopLoss_MinDistance * RiskRewardRatio + TrendProtectionThreshold); // 仅在4H趋势符合时执行交易 if (Enable4HTrendFilter) { if (isCrossUp && is4HTrendUp) { OpenBuyOrder(); } else if (isCrossDown && is4HTrendDown) { OpenSellOrder(); } } else { if (isCrossUp) { OpenBuyOrder(); } else if (isCrossDown) { OpenSellOrder(); } } // 如果有开仓单,检查是否符合止盈保护条件 if (OrderSelect(ticket, SELECT_BY_TICKET)) { if (OrderType() == OP_BUY) { if (OrderProfit() > RiskRewardRatio * StopLoss_MinDistance) { ProtectTakeProfitForBuy(); } } else if (OrderType() == OP_SELL) { if (OrderProfit() > RiskRewardRatio * StopLoss_MinDistance) { ProtectTakeProfitForSell(); } } } } //+------------------------------------------------------------------+ //| 开仓买单的函数 | //+------------------------------------------------------------------+ void OpenBuyOrder() { double stopLossLevel = NormalizeDouble(EMA36 + StopLoss_MinDistance * Point, MarketInfo(Symbol(), MODE_DIGITS)); double takeProfitLevel = NormalizeDouble(stopLossLevel + (StopLoss_MinDistance * RiskRewardRatio * Point), MarketInfo(Symbol(), MODE_DIGITS)); // 下单 ticket = OrderSend(Symbol(), OP_BUY, 0.1, Ask, 2, stopLossLevel, takeProfitLevel, "EMA Cross Buy", 0, 0, Green); if (ticket < 0) { Print("Error opening buy order: ", ErrorDescription(GetLastError())); } } //+------------------------------------------------------------------+ //| 开仓卖单的函数 | //+------------------------------------------------------------------+ void OpenSellOrder() { double stopLossLevel = NormalizeDouble(EMA36 - StopLoss_MinDistance * Point, MarketInfo(Symbol(), MODE_DIGITS)); double takeProfitLevel = NormalizeDouble(stopLossLevel - (StopLoss_MinDistance * RiskRewardRatio * Point), MarketInfo(Symbol(), MODE_DIGITS)); // 下单 ticket = OrderSend(Symbol(), OP_SELL, 0.1, Bid, 2, stopLossLevel, takeProfitLevel, "EMA Cross Sell", 0, 0, Red); if (ticket < 0) { Print("Error opening sell order: ", ErrorDescription(GetLastError())); } } //+------------------------------------------------------------------+ //| 买单的止盈保护函数 | //+------------------------------------------------------------------+ void ProtectTakeProfitForBuy() { if (OrderProfit() > RiskRewardRatio * StopLoss_MinDistance) { double newTakeProfit = NormalizeDouble(OrderOpenPrice() + StopLoss_MinDistance * RiskRewardRatio * Point, MarketInfo(Symbol(), MODE_DIGITS)); OrderModify(ticket, OrderOpenPrice(), OrderStopLoss(), newTakeProfit, 0, CLR_NONE); } } //+------------------------------------------------------------------+ //| 卖单的止盈保护函数 | //+------------------------------------------------------------------+ void ProtectTakeProfitForSell() { if (OrderProfit() > RiskRewardRatio * StopLoss_MinDistance) { double newTakeProfit = NormalizeDouble(OrderOpenPrice() - StopLoss_MinDistance * RiskRewardRatio * Point, MarketInfo(Symbol(), MODE_DIGITS)); OrderModify(ticket, OrderOpenPrice(), OrderStopLoss(), newTakeProfit, 0, CLR_NONE); } }
下一篇:没有了!