extern int MagicNumber = 92133; // 魔术码
extern double InitialLots = 0.5; // 初始手数
extern int Gap = 100; // 间距

// 检查当前魔术码下是否持仓
bool CheckOpenOrders()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderMagicNumber() == MagicNumber)
            {
                return true;
            }
        }
    }

    return false;
}

// 挂单
void PlaceOrder(int type, double lots, double price)
{
    double slippage = MarketInfo(_Symbol, MODE_STOPLEVEL);
    double stopLoss = 0;
    double takeProfit = 0;

    if (type == OP_BUY)
    {
        price += Gap * _Point;
        stopLoss = price - Gap * 2 * _Point;
        takeProfit = price + Gap * 2 * _Point;
    }
    else if (type == OP_SELL)
    {
        price -= Gap * _Point;
        stopLoss = price + Gap * 2 * _Point;
        takeProfit = price - Gap * 2 * _Point;
    }

    OrderSend(_Symbol, type, lots, price, slippage, stopLoss, takeProfit, '', MagicNumber, 0, Blue);
}

// 主函数
int start()
{
    double currentPrice = NormalizeDouble(Bid, Digits);

    // 检查当前魔术码是否持仓
    bool hasOpenOrders = CheckOpenOrders();

    if (!hasOpenOrders)
    {
        // 如果当前魔术码下没有持仓,则挂单多单和空单
        PlaceOrder(OP_BUY, InitialLots, currentPrice + Gap * _Point);
        PlaceOrder(OP_SELL, InitialLots, currentPrice - Gap * _Point);
    }
    else
    {
        // 如果当前魔术码下有持仓,检查持仓类型并根据条件做空或做多
        for (int i = OrdersTotal() - 1; i >= 0; i--)
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
                if (OrderMagicNumber() == MagicNumber)
                {
                    if (OrderType() == OP_BUY && currentPrice < OrderOpenPrice() - Gap * _Point)
                    {
                        PlaceOrder(OP_SELL, InitialLots, currentPrice);
                    }
                    else if (OrderType() == OP_SELL && currentPrice > OrderOpenPrice() + Gap * _Point)
                    {
                        PlaceOrder(OP_BUY, InitialLots, currentPrice);
                    }
                }
            }
        }
    }

    return 0;
}

这是一个简单的MQL4外汇EA策略示例,包含以下功能:

  • 使用 MagicNumber 区分不同EA的订单
  • 使用 InitialLots 设置初始交易手数
  • 使用 Gap 设置交易价格点位间距
  • 当没有持仓时,自动挂多单和空单
  • 当有持仓时,根据价格波动和持仓方向进行反向交易

请注意,这只是一个简单的策略示例,并未经过完整的测试和优化,实际使用中需要根据市场情况进行调整。

MQL4外汇EA策略:基于固定点位加减仓交易

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

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