解决重复挂单问题:MQL4代码修复指南
解决重复挂单问题:MQL4代码修复指南
之前的代码逻辑存在问题,可能导致重复挂单。以下是经过修正的代码,确保每个挂单只会触发一次:cpp//+------------------------------------------------------------------+//| 7777.mq4 |//| Copyright 2023, MetaQuotes Ltd. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright 'Copyright 2023, MetaQuotes Ltd.'#property link 'https://www.mql5.com'#property version '1.00'#property strict
//+------------------------------------------------------------------+//| Global variables |//+------------------------------------------------------------------+int MAGIC_NUMBER = 92133;double INITIAL_LOTS = 0.5;double REPLENISH_MULTIPLIER = 0.6;int REPLENISH_ATTEMPTS = 5;double PROFIT_TARGET = 40;double MIN_FLUCTUATION = MarketInfo(Symbol(), MODE_POINT) * 100;
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+int OnInit(){ return (INIT_SUCCEEDED);}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+void OnDeinit(const int reason){ // Close all open orders CloseOrders();}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+void OnTick(){ bool hasLongPosition = false; bool hasShortPosition = false;
// Check if there are any open orders with the specified magic number for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER) { if (OrderType() == OP_BUY) { hasLongPosition = true; } else if (OrderType() == OP_SELL) { hasShortPosition = true; } } }
// Check if all positions have been closed if (!hasLongPosition && !hasShortPosition) { // Place pending orders if there are no open positions double buyPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits) + MIN_FLUCTUATION; double sellPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits) - MIN_FLUCTUATION;
// Open buy order if (!hasLongPosition) { if (CountOrders(OP_BUYSTOP) == 0) { if (OpenBuyOrder(INITIAL_LOTS, buyPrice)) { Print('Opened buy order'); } else { Print('Failed to open buy order'); } } }
// Open sell order if (!hasShortPosition) { if (CountOrders(OP_SELLSTOP) == 0) { if (OpenSellOrder(INITIAL_LOTS, sellPrice)) { Print('Opened sell order'); } else { Print('Failed to open sell order'); } } } }
// Check if any positions have reached the profit target double totalProfit = 0; for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER) { totalProfit += OrderProfit(); } } if (totalProfit >= PROFIT_TARGET) { CloseOrders(); }}
//+------------------------------------------------------------------+//| Open buy order function |//+------------------------------------------------------------------+bool OpenBuyOrder(double lots, double price){ int ticket = OrderSend(Symbol(), OP_BUYSTOP, lots, price, 0, 0, 0, 'Buy Order', MAGIC_NUMBER, 0, Green); if (ticket > 0) { if (OrderSelect(ticket, SELECT_BY_TICKET)) { return true; } else { Print('Failed to select order with ticket ', ticket); } } else { Print('Failed to send buy order'); } return false;}
//+------------------------------------------------------------------+//| Open sell order function |//+------------------------------------------------------------------+bool OpenSellOrder(double lots, double price){ int ticket = OrderSend(Symbol(), OP_SELLSTOP, lots, price, 0, 0, 0, 'Sell Order', MAGIC_NUMBER, 0, Red); if (ticket > 0) { if (OrderSelect(ticket, SELECT_BY_TICKET)) { return true; } else { Print('Failed to select order with ticket ', ticket); } } else { Print('Failed to send sell order'); } return false;}
//+------------------------------------------------------------------+//| Close all orders function |//+------------------------------------------------------------------+void CloseOrders(){ for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER) { if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0)) { Print('Failed to close order with ticket ', OrderTicket()); } } }}
//+------------------------------------------------------------------+//| Count orders of a specific type |//+------------------------------------------------------------------+int CountOrders(int orderType){ int count = 0; for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER && OrderType() == orderType) { count++; } } return count;}
代码改进说明:
- 避免重复挂单: 代码逻辑经过优化,确保在任何时刻,同一方向上只有一个挂单。* 错误处理: 代码中添加了错误处理机制,例如在发送订单或选择订单失败时打印错误信息。* 代码注释: 代码中添加了详细的注释,以便于理解代码逻辑。
注意事项:
- 这段代码只是一个简单的示例,您可能需要根据您的具体需求进行调整和完善。* 在实盘交易之前,请务必在模拟账户上充分测试您的EA交易策略。
希望这篇文章能够帮助您解决MQL4代码中的重复挂单问题!
原文地址: http://www.cveoy.top/t/topic/bVaZ 著作权归作者所有。请勿转载和采集!