以下是根据您的要求修改后的 MQ4 代码:

//+------------------------------------------------------------------+
//|                                                         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()
{
    bool hasLongPosition = false;
    bool hasShortPosition = false;

    // 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)
        {
            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)
    {
        double buyPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits) + MIN_FLUCTUATION;
        double sellPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits) - MIN_FLUCTUATION;
        
        // Open buy order
        if (OpenBuyOrder(INITIAL_LOTS, buyPrice))
        {
            // Open sell order
            if (OpenSellOrder(INITIAL_LOTS, sellPrice))
            {
                Print('Opened buy and sell orders');
            }
            else
            {
                Print('Failed to open sell order');
                CloseOrders(); // Close buy order on failure
            }
        }
        else
        {
            Print('Failed to open buy order');
        }
    }

    // Check if any positions have reached the profit target
    double totalProfit = 0;
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER)
        {
            totalProfit += OrderProfit();
        }
    }
    if (totalProfit >= PROFIT_TARGET)
    {
        CloseOrders();
    }
}

//+------------------------------------------------------------------+
//| Open buy order function                                          |
//+------------------------------------------------------------------+
bool 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                                         |
//+------------------------------------------------------------------+
bool 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());
            }
        }
    }
}

请注意,这只是一个简单的示例,并且可能需要根据您的具体需求进行调整和完善。编写自动交易系统需要特定的经验和知识,确保您充分了解和测试代码,以确保其符合您的交易策略和需求。

MetaTrader 4 脚本:挂单策略改进 - 仅触发一次并平仓后重新挂单

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

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