修复 MT4 中 'OP_ORDER' 未声明标识符错误

在 MetaTrader 4 (MT4) 中,'OP_ORDER' 是一个未定义的标识符,会导致编译错误。本文将提供一个解决方案,并附带完整的 MQL4 代码示例。

错误原因:

'OP_ORDER' 本身并不是一个有效的 MQL4 预定义常量。正确的常量应该为 OP_BUYOP_SELL 等,用于表示订单类型。

解决方案:

请将代码中所有出现的 'OP_ORDER' 替换为正确的订单类型常量,例如 OP_BUYOP_SELL

完整代码示例:

以下是修复后的完整 MQL4 代码,该代码包含了初始代码的功能,并修复了 'OP_ORDER' 错误:MQ4extern 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;            int ticket = OrderSend(Symbol(), OP_BUY, initialLots, currentPrice, slippage, 0, 0, 'Initial Entry', magicNumber, 0, Blue);            if (ticket > 0)            {                hasOpenPosition = true;                totalOrders++;            }        }        else if (currentPrice <= currentPrice - 200 * Point)        {            entryPrice = currentPrice;            int ticket = OrderSend(Symbol(), OP_SELL, initialLots, currentPrice, slippage, 0, 0, 'Initial Entry', magicNumber, 0, Red);            if (ticket > 0)            {                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;}

注意: 以上代码仅为示例,实际使用时请根据您的需求进行修改和测试。

如果您在使用过程中遇到其他问题,请随时提出。

修复 MT4 中 'OP_ORDER' 未声明标识符错误

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

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