MQL4 代码错误修复:'OP_PENDING' - undeclared identifier
MQL4 代码错误修复:'OP_PENDING' - undeclared identifier
在 MQL4 代码中遇到 'OP_PENDING' - undeclared identifier 错误通常是因为代码缺少必要的头文件或定义。
以下是修复后的完整代码示例,其中包含了必要的头文件和定义,并修复了其他潜在问题:
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 = MarketInfo(Symbol(), MODE_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) && 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(), MarketInfo(OrderSymbol(), MODE_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;
}
注意:
OP_PENDING是 MQL4 中预定义的常量,用于表示挂单。- 上述代码仅供参考,您可能需要根据您的具体需求进行修改和测试。
- 确保您的 MQL4 开发环境已正确设置,并且您使用的平台版本与代码兼容。
如果您仍然遇到问题,请提供更多上下文信息,例如完整的错误消息和相关的代码片段,以便我能够更好地帮助您解决问题。
原文地址: https://www.cveoy.top/t/topic/dnSx 著作权归作者所有。请勿转载和采集!