MQL4 代码错误 'OP_PENDING' - undeclared identifier 解决方法
MQL4 代码错误 'OP_PENDING' - undeclared identifier 解决方法
如果您在 MQL4 代码中遇到 'OP_PENDING' - undeclared identifier 错误,通常是因为您的 MetaTrader 4 平台版本过旧,不支持 OP_PENDING 常量。
解决方法:
-
升级 MetaTrader 4 平台: 尝试升级到最新版本的 MetaTrader 4。最新版本通常包含最新的 MQL4 定义和常量。
-
手动定义 OP_PENDING: 如果无法升级 MetaTrader 4,您可以在代码中手动定义
OP_PENDING常量。在代码的开头添加以下行:
MQL4 #define OP_PENDING 6
完整代码示例:
以下是一个完整的 MQL4 代码示例,其中包含 OP_PENDING 的正确用法:MQ4#property copyright 'Copyright 2023, Your Name'#property link 'https://www.example.com'#property version '1.00'
#define OP_PENDING 6
extern int magicNumber = 92133;extern double initialLots = 0.5;extern double takeProfitAmount = 80;extern int maxReentries = 4;extern int reentryPips = 100;extern int slippage = 4;
int totalOrders = 0;double totalProfit = 0;bool hasOpenPosition = false;
void OnTick(){ double currentPrice = Ask; double entryPrice;
// Check for open position if (hasOpenPosition) { // Check if take profit is reached if (totalProfit >= takeProfitAmount) { CloseAllOrders(); DeleteAllPendings(); Sleep(60000); // Wait for 1 minute before restart ResetVariables(); return; }
// Check for reentry conditions if (OrdersTotal() < maxReentries) { if (OrderSelect(totalOrders - 1, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol() && OrderMagicNumber() == magicNumber) { if (OrderType() == OP_BUY && currentPrice >= entryPrice + reentryPips * Point) { entryPrice = currentPrice; double lots = OrderLots() * 2; if (lots > MarketInfo(Symbol(), MODE_MINLOT)) OrderSend(Symbol(), OP_BUY, lots, currentPrice, slippage, 0, 0, 'Reentry', magicNumber, 0, Blue); } else if (OrderType() == OP_SELL && currentPrice <= entryPrice - reentryPips * Point) { entryPrice = currentPrice; double lots = OrderLots() * 2; if (lots > MarketInfo(Symbol(), MODE_MINLOT)) OrderSend(Symbol(), OP_SELL, lots, currentPrice, slippage, 0, 0, 'Reentry', magicNumber, 0, Red); } } } } else { // Check for initial entry conditions if (currentPrice >= currentPrice + 200 * Point) { entryPrice = currentPrice; OrderSend(Symbol(), OP_BUY, initialLots, currentPrice, slippage, 0, 0, 'Initial Entry', magicNumber, 0, Blue); hasOpenPosition = true; totalOrders++; } else if (currentPrice <= currentPrice - 200 * Point) { entryPrice = currentPrice; OrderSend(Symbol(), OP_SELL, initialLots, currentPrice, slippage, 0, 0, 'Initial Entry', magicNumber, 0, Red); hasOpenPosition = true; totalOrders++; } }}
void OnTrade(){ totalProfit = 0;
for (int i = 0; i < OrdersTotal(); i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == magicNumber && OrderType() != OP_PENDING) { totalProfit += OrderProfit(); } } }}
void CloseAllOrders(){ for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == magicNumber) { OrderClose(OrderTicket(), OrderLots(), Bid, slippage, Red); totalOrders--; } } }}
void DeleteAllPendings(){ for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol() == Symbol() && OrderType() == OP_PENDING && OrderMagicNumber() == magicNumber) { OrderDelete(OrderTicket()); } } }}
void ResetVariables(){ hasOpenPosition = false; totalProfit = 0; totalOrders = 0;}
通过以上步骤,您应该能够解决 MQL4 代码中出现的 'OP_PENDING' - undeclared identifier 错误。
原文地址: https://www.cveoy.top/t/topic/dof4 著作权归作者所有。请勿转载和采集!