修复MQL4订单开仓价格获取错误,优化交易策略代码
修复MQL4订单开仓价格获取错误,优化交易策略代码
在之前的代码中,获取订单开仓价格的方式存在错误,导致交易策略无法正常执行。本文提供了一段修复后的代码,并对交易策略逻辑进行了一些优化。
原代码问题:
原代码中使用了OPEN函数尝试获取订单的开仓价格,但OPEN并非MQL4中的有效函数。
修复方案:
使用OrderOpenPrice()函数获取订单的开仓价格。
**优化后的代码:**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_ORDER && 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;}
代码说明:
- 使用
OrderOpenPrice()函数获取订单的开仓价格,替换原代码中错误的OPEN函数。2. 优化了交易策略逻辑,使其更清晰易懂。3. 添加了代码注释,方便理解代码功能。
注意:
以上代码仅供参考,实际使用时请根据您的需求进行调整和测试。
原文地址: https://www.cveoy.top/t/topic/dox6 著作权归作者所有。请勿转载和采集!