This article provides a solution to the 'CheckPositions' function not defined error in MQL4. The provided code examples can be used as a starting point for developing your own trading strategies.

  1. CheckPositions(MagicNumber): This function checks the number of positions for a given magic number.
int CheckPositions(int magicNumber)
{
    int totalPositions = 0;
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol())
        {
            if (OrderType() == OP_BUY || OrderType() == OP_SELL)
            {
                totalPositions += OrderLots();
            }
        }
    }
    return totalPositions;
}
  1. CheckPositionsType(MagicNumber, orderType): This function checks the number of positions for a specific order type (e.g., OP_BUY or OP_SELL) for a given magic number.
int CheckPositionsType(int magicNumber, int orderType)
{
    int totalPositions = 0;
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol() && OrderType() == orderType)
        {
            totalPositions += OrderLots();
        }
    }
    return totalPositions;
}
  1. GetFirstPositionPrice(MagicNumber, orderType): This function retrieves the price of the first position for a specific order type (e.g., OP_BUY or OP_SELL) for a given magic number.
double GetFirstPositionPrice(int magicNumber, int orderType)
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol() && OrderType() == orderType)
        {
            return OrderOpenPrice();
        }
    }
    return 0.0;  // If no matching order is found, return 0.0 or another appropriate value
}
  1. GetLastPositionVolume(MagicNumber, orderType): This function retrieves the volume (lots) of the last position for a specific order type (e.g., OP_BUY or OP_SELL) for a given magic number.
double GetLastPositionVolume(int magicNumber, int orderType)
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol() && OrderType() == orderType)
        {
            return OrderLots();
        }
    }
    return 0.0;  // If no matching order is found, return 0.0 or another appropriate value
}
  1. GetTotalProfit(MagicNumber): This function calculates the total profit for a given magic number.
double GetTotalProfit(int magicNumber)
{
    double totalProfit = 0.0;
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol())
        {
            totalProfit += OrderProfit();
        }
    }
    return totalProfit;
}
  1. DeletePendingOrders(MagicNumber, orderType): This function deletes all pending orders of a specified order type (e.g., OP_BUYSTOP or OP_SELLSTOP) for a given magic number.
void DeletePendingOrders(int magicNumber, int orderType)
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol() && OrderType() == orderType)
        {
            OrderDelete(OrderTicket());
        }
    }
}
  1. CloseAllPositions(MagicNumber): This function closes all positions for a given magic number.
void CloseAllPositions(int magicNumber)
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol())
        {
            if (OrderType() == OP_BUY || OrderType() == OP_SELL)
            {
                OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, clrNone);
            }
        }
    }
}

Remember to adjust and modify these functions according to your specific trading strategy needs.

'CheckPositions' Function Not Defined - MQL4 Code Example

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

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