MT4 EA 代码示例:修复 'MODE_PENDING' 未声明标识符错误在编写 MetaTrader 4 (MT4) 智能交易系统 (EA) 时,您可能会遇到 'MODE_PENDING' - undeclared identifier 的错误。这个错误通常是由于 MQL4 编译器无法识别 'MODE_PENDING' 常量导致的。错误原因:'MODE_PENDING' 常量用于访问挂单信息,但它并非 MQL4 语言的内置常量。要使用它,您需要包含 Trade/Trade.mqh 库文件。**解决方案:**在您的 EA 代码开头添加以下代码行:c#include <Trade/Trade.mqh>**完整代码示例:**以下是一个修复了 'MODE_PENDING' 错误的完整 MT4 EA 代码示例:c#include <Trade/Trade.mqh>// 定义可更改参数extern int MAGIC_NUMBER = 92133;extern double INITIAL_LOT_SIZE = 0.5;extern double TAKE_PROFIT_AMOUNT = 80.0;extern int SLIPPAGE = 4;extern int MAX_REBUY_ATTEMPTS = 4;extern double REBUY_MULTIPLIER = 2.0;extern int REBUY_SPACING = 100;// 初始化变量int totalProfit = 0;int consecutiveBuys = 0;int consecutiveSells = 0;double lastBuyLotSize = INITIAL_LOT_SIZE;double lastSellLotSize = INITIAL_LOT_SIZE;// 主循环void OnTick(){ double currentPrice = MarketInfo(Symbol(), MODE_BID); int openOrdersCount = OrdersTotal(); // 平仓并撤销所有挂单 if (totalProfit >= TAKE_PROFIT_AMOUNT) { CloseAllOrders(); DeleteAllPendingOrders(); Sleep(60000); // 1分钟延迟重新开始 totalProfit = 0; consecutiveBuys = 0; consecutiveSells = 0; lastBuyLotSize = INITIAL_LOT_SIZE; lastSellLotSize = INITIAL_LOT_SIZE; return; } // 如果没有持仓,则挂单 if (openOrdersCount == 0) { double buyPrice = currentPrice + 200 * Point; double sellPrice = currentPrice - 200 * Point; BuyOrder(buyPrice); SellOrder(sellPrice); } // 检查补仓条件 if (openOrdersCount > 0) { double lastOrderPrice = OrderClosePrice(); int lastOrderType = OrderType(); // 获取最近一笔订单的类型 // 多单补仓 if (lastOrderType == OP_BUY && currentPrice - lastOrderPrice >= REBUY_SPACING * Point) { if (consecutiveBuys < MAX_REBUY_ATTEMPTS) { consecutiveBuys++; lastBuyLotSize *= REBUY_MULTIPLIER; double buyPrice = currentPrice + 200 * Point; BuyOrder(buyPrice, lastBuyLotSize); } } // 空单补仓 if (lastOrderType == OP_SELL && lastOrderPrice - currentPrice >= REBUY_SPACING * Point) { if (consecutiveSells < MAX_REBUY_ATTEMPTS) { consecutiveSells++; lastSellLotSize *= REBUY_MULTIPLIER; double sellPrice = currentPrice - 200 * Point; SellOrder(sellPrice, lastSellLotSize); } } }}// 发送买入订单void BuyOrder(double price, double lotSize = INITIAL_LOT_SIZE){ int ticket = OrderSend(Symbol(), OP_BUY, lotSize, price, SLIPPAGE, 0, 0, 'Buy order', MAGIC_NUMBER, 0, Green); if (ticket > 0) { totalProfit += OrderProfit(); // 根据您的需求获取订单的盈利金额 }}// 发送卖出订单void SellOrder(double price, double lotSize = INITIAL_LOT_SIZE){ int ticket = OrderSend(Symbol(), OP_SELL, lotSize, price, SLIPPAGE, 0, 0, 'Sell order', MAGIC_NUMBER, 0, Red); if (ticket > 0) { totalProfit += OrderProfit(); // 根据您的需求获取订单的盈利金额 }}// 平仓所有订单void CloseAllOrders(){ int totalOrders = OrdersTotal(); for (int i = totalOrders - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber() == MAGIC_NUMBER) { if (OrderClose(OrderTicket(), OrderLots(), MarketInfo(Symbol(), MODE_BID), SLIPPAGE)) { totalProfit += OrderProfit(); // 根据您的需求获取订单的盈利金额 } } } }}// 撤销所有挂单void DeleteAllPendingOrders(){ int totalOrders = OrdersTotal(); for (int i = totalOrders - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_PENDING)) { if (OrderMagicNumber() == MAGIC_NUMBER) { OrderDelete(OrderTicket()); } }

MT4 EA 代码示例:修复 'MODE_PENDING' 未声明标识符错误

原文地址: https://www.cveoy.top/t/topic/dc57 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录