以下是修复后的代码,以实现在持仓数量为 0 且挂单数量为 0 时触发以最新价加 200 微点挂多单,以及以最新价减 200 微点挂空单的逻辑:

extern int magicNumber = 92133;
extern double initialLotSize = 0.5;
extern double profitTarget = 80.0;
extern int maxReentries = 4;
extern int pipsThreshold = 100;

bool hasOpenPositions = false;
bool hasPendingOrders = false;

void OnTick()
{
    // 获取当前魔术码下的持仓订单和挂单
    int ordersCount = OrdersTotal();
    int historyCount = OrdersHistoryTotal();
    RefreshStatus();

    // 当前没有持仓且没有挂单时,触发挂单
    if (!hasOpenPositions && !hasPendingOrders)
    {
        double currentBid = MarketInfo(Symbol(), MODE_BID);
        double currentAsk = MarketInfo(Symbol(), MODE_ASK);

        // 触发挂单做多
        double buyStopPrice = currentAsk + pipsToPoints(200);
        int ticket = OrderSend(Symbol(), OP_BUYSTOP, initialLotSize, buyStopPrice, 0, 0, 0, 'Buy Stop Order', magicNumber, 0, Blue);

        // 触发挂单做空
        double sellStopPrice = currentBid - pipsToPoints(200);
        ticket = OrderSend(Symbol(), OP_SELLSTOP, initialLotSize, sellStopPrice, 0, 0, 0, 'Sell Stop Order', magicNumber, 0, Red);
    }

    // 获取当前魔术码下所有订单的总盈利
    double totalProfit = 0.0;

    for (int i = 0; i < ordersCount; i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderMagicNumber() == magicNumber)
            {
                totalProfit += OrderProfit();
            }
        }
    }

    // 当总盈利达到止盈金额时进行清仓
    if (totalProfit >= profitTarget)
    {
        for (int i = ordersCount - 1; i >= 0; i--)
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
                if (OrderMagicNumber() == magicNumber)
                {
                    bool closeSuccess = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 4, Red);
                    if (closeSuccess)
                    {
                        Print('Order closed successfully.');
                    }
                    else
                    {
                        int error = GetLastError();
                        Print('Failed to close order. Error code: ', error);
                    }
                }
            }
        }

        for (int i = historyCount - 1; i >= 0; i--)
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
            {
                if (OrderMagicNumber() == magicNumber)
                {
                    bool deleteSuccess = OrderDelete(OrderTicket());
                    if (deleteSuccess)
                    {
                        Print('Order deleted successfully.');
                    }
                    else
                    {
                        int error = GetLastError();
                        Print('Failed to delete order. Error code: ', error);
                    }
                }
            }
        }

        Sleep(60000); // 1分钟后重新开始判断是否有持仓订单和挂单
        RefreshStatus();

        // 触发新的挂单逻辑,与上面类似
        if (!hasOpenPositions && !hasPendingOrders)
        {
            double currentBid = MarketInfo(Symbol(), MODE_BID);
            double currentAsk = MarketInfo(Symbol(), MODE_ASK);

            // 触发挂单做多
            double buyStopPrice = currentAsk + pipsToPoints(200);
            int ticket = OrderSend(Symbol(), OP_BUYSTOP, initialLotSize, buyStopPrice, 0, 0, 0, 'Buy Stop Order', magicNumber, 0, Blue);

            // 触发挂单做空
            double sellStopPrice = currentBid - pipsToPoints(200);
            ticket = OrderSend(Symbol(), OP_SELLSTOP, initialLotSize, sellStopPrice, 0, 0, 0, 'Sell Stop Order', magicNumber, 0, Red);
        }
    }

    // 其他逻辑...
}

double pipsToPoints(int pips)
{
    return pips * Point;
}

void RefreshStatus()
{
    hasOpenPositions = false;
    hasPendingOrders = false;

    int ordersCount = OrdersTotal();
    int historyCount = OrdersHistoryTotal();

    for (int i = 0; i < ordersCount; i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderMagicNumber() == magicNumber && OrderType() <= OP_SELL)
            {
                hasOpenPositions = true;
                break;
            }
        }
    }

    for (int i = 0; i < historyCount; i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
            if (OrderMagicNumber() == magicNumber && OrderType() <= OP_SELLSTOP)
            {
                hasPendingOrders = true;
                break;
            }
        }
    }
}

这次的代码在 OnTick 函数中添加了判断持仓和挂单状态的逻辑,并在需要时触发新的挂单操作。当没有持仓订单且没有挂单时,会根据最新价加 200 微点触发挂多单和挂空单。请注意,这只是一个示例代码,您可能需要根据您的实际需求进行适当的修改。

MT4 EA: 触发双向挂单 - 在无持仓且无挂单时挂多单和空单

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

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