MQL4 'CountOrders' 函数未定义错误解决方法
MQL4 'CountOrders' 函数未定义错误解决方法
在编写 MQL4 自动交易系统 (EA) 时,您可能会遇到 'CountOrders' 函数未定义的错误。这是因为早期的 MQL4 版本并没有内置这个函数。
为了解决这个问题,您需要手动定义 'CountOrders' 函数。以下是一段包含了 'CountOrders' 函数定义的完整 MQL4 代码示例:
//+------------------------------------------------------------------+
//| 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()
{
double totalProfit = 0;
// 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)
{
totalProfit += OrderProfit();
}
}
// Check if all positions have been closed
if (totalProfit >= PROFIT_TARGET)
{
CloseOrders();
return;
}
// Check if there are any open positions
bool hasLongPosition = false;
bool hasShortPosition = false;
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;
}
}
}
// Place pending orders if there are no open positions
if (!hasLongPosition && !hasShortPosition)
{
// Determine the prices for the pending orders
double askPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits);
double bidPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits);
double buyPrice = askPrice + MIN_FLUCTUATION;
double sellPrice = bidPrice - MIN_FLUCTUATION;
// Open buy order
if (!hasLongPosition)
{
if (CountOrders(OP_BUYSTOP) == 0)
{
// Delete sell order if exists
if (CountOrders(OP_SELLSTOP) > 0)
{
DeleteOrders(OP_SELLSTOP);
}
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)
{
// Delete buy order if exists
if (CountOrders(OP_BUYSTOP) > 0)
{
DeleteOrders(OP_BUYSTOP);
}
if (OpenSellOrder(INITIAL_LOTS, sellPrice))
{
Print('Opened sell order');
}
else
{
Print('Failed to open sell order');
}
}
}
}
}
//+------------------------------------------------------------------+
//| Open buy order function |
//+------------------------------------------------------------------+
boolean 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 |
//+------------------------------------------------------------------+
boolean 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());
}
}
}
}
//+------------------------------------------------------------------+
//| Delete orders of a specific type |
//+------------------------------------------------------------------+
void DeleteOrders(int orderType)
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER && OrderType() == orderType)
{
if (!OrderDelete(OrderTicket()))
{
Print('Failed to delete 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;
}
代码说明:
CountOrders(int orderType)函数用于计算指定类型的订单数量。orderType参数可以是以下值之一:OP_BUY: 买入订单OP_SELL: 卖出订单OP_BUYSTOP: 买入止损订单OP_SELLSTOP: 卖出止损订单OP_BUYLIMIT: 买入限价订单OP_SELLLIMIT: 卖出限价订单
使用方法:
要使用 CountOrders 函数,只需将其作为条件语句的一部分,例如:
// 如果没有买入止损订单,则打开一个新的买入止损订单
if (CountOrders(OP_BUYSTOP) == 0)
{
// 打开买入止损订单的代码
}
请注意,这只是一个简单的示例,并且可能需要根据您的具体需求进行调整和完善。编写自动交易系统需要特定的经验和知识,确保您充分了解和测试代码,以确保其符合您的交易策略和需求。
原文地址: https://www.cveoy.top/t/topic/b0nl 著作权归作者所有。请勿转载和采集!