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);
        
        if(ticket > 0)
        {
            Print('Orders placed successfully.');
        }
        else
        {
            int error = GetLastError();
            Print('Failed to place orders. Error code: ', error);
        }
    }
}

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;
            }
        }
    }
}
MT4 EA 代码: 一键轻仓,自动重启挂单策略

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

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