'MODE_PENDING' - undeclared identifier: 7777.mq4 Error Fix and Complete Code
'MODE_PENDING' is an undeclared identifier in MQL4, it should be replaced with 'MODE_TRADES'. The code below is a complete solution to the 'MODE_PENDING' undeclared identifier error in the 7777.mq4 file. This error is common when working with MetaTrader 4 (MT4) Expert Advisors (EAs). This solution includes a corrected code snippet with clear explanations and addresses the underlying cause of the issue.
extern int MagicNumber = 92133; // 魔术码
extern double InitialLotSize = 0.5; // 初始手数
extern double TakeProfitAmount = 80; // 止盈金额
extern double MartingaleMultiplier = 2; // 补仓倍数
extern int MaxMartingaleAttempts = 4; // 补仓次数
extern int MartingaleDistance = 100; // 补仓间距
int totalOrders = 0; // 总订单数
bool hasPendingOrders = false; // 是否有挂单
void OnTick()
{
double currentPrice = MarketInfo(Symbol(), MODE_BID);
// 平仓逻辑
for (int i = totalOrders - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber)
{
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, White);
totalOrders--;
}
}
// 检查挂单是否存在
if (!hasPendingOrders)
{
// 挂单做多
double buyStopPrice = NormalizeDouble(currentPrice + 200 * Point, Digits);
int buyStopOrder = OrderSend(Symbol(), OP_BUYSTOP, InitialLotSize, buyStopPrice, 0, 0, buyStopPrice - MartingaleDistance * Point, 'Buy Stop Order', MagicNumber, 0, Green);
if (buyStopOrder > 0)
totalOrders++;
// 挂单做空
double sellStopPrice = NormalizeDouble(currentPrice - 200 * Point, Digits);
int sellStopOrder = OrderSend(Symbol(), OP_SELLSTOP, InitialLotSize, sellStopPrice, 0, 0, sellStopPrice + MartingaleDistance * Point, 'Sell Stop Order', MagicNumber, 0, Red);
if (sellStopOrder > 0)
totalOrders++;
hasPendingOrders = true;
}
// 补仓逻辑
for (int i = totalOrders - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber)
{
if (OrderType() == OP_BUY && currentPrice - OrderOpenPrice() >= MartingaleDistance * Point && OrderLots() < InitialLotSize * pow(MartingaleMultiplier, MaxMartingaleAttempts))
{
double lotSize = OrderLots() * MartingaleMultiplier;
double buyPrice = NormalizeDouble(currentPrice, Digits);
int buyOrder = OrderSend(Symbol(), OP_BUY, lotSize, buyPrice, 0, 0, 0, 'Martingale Buy Order', MagicNumber, 0, Green);
if (buyOrder > 0)
totalOrders++;
}
else if (OrderType() == OP_SELL && OrderOpenPrice() - currentPrice >= MartingaleDistance * Point && OrderLots() < InitialLotSize * pow(MartingaleMultiplier, MaxMartingaleAttempts))
{
double lotSize = OrderLots() * MartingaleMultiplier;
double sellPrice = NormalizeDouble(currentPrice, Digits);
int sellOrder = OrderSend(Symbol(), OP_SELL, lotSize, sellPrice, 0, 0, 0, 'Martingale Sell Order', MagicNumber, 0, Red);
if (sellOrder > 0)
totalOrders++;
}
}
}
// 检查总盈利达到止盈金额
double totalProfit = 0;
for (int i = totalOrders - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber)
{
totalProfit += OrderProfit();
}
}
if (totalProfit >= TakeProfitAmount)
{
// 平仓所有订单
for (int i = totalOrders - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber)
{
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, White);
totalOrders--;
}
}
// 重置挂单状态
hasPendingOrders = false;
Sleep(60000); // 1分钟延迟后重新开始运行
}
}
Explanation
-
The 'MODE_PENDING' undeclared identifier error is due to a misunderstanding of MQL4 syntax. 'MODE_PENDING' does not exist in MQL4. The correct value to use is 'MODE_TRADES' when accessing order information.
-
The corrected code replaces all occurrences of 'MODE_PENDING' with 'MODE_TRADES'.
Important Note:
While this solution fixes the immediate error, you should always thoroughly test and verify your EA before using it in a live trading environment. MQL4 is a powerful language, but errors can occur. Make sure to carefully analyze the logic and functionality of your code, especially when using advanced trading strategies like martingale.
Additional Tips for Robust Code
- Use error handling mechanisms. Check the return values of functions like OrderSend() to ensure that your orders were placed successfully.
- Document your code. Clear and concise comments can help you understand your code better and help others to understand it as well.
- Test your EA in a simulated environment before deploying it live.
原文地址: https://www.cveoy.top/t/topic/djic 著作权归作者所有。请勿转载和采集!