MQL4交易策略:基于固定间距加仓的交易机器人

本文将介绍一个基于固定间距加仓的MQL4交易机器人。该策略会在价格突破特定点数后进行加仓操作,并设置止损和止盈来管理风险。

代码解析

以下是该策略的MQL4代码:cppextern int MagicNumber = 92133; // 魔术码extern double InitialLotSize = 0.5; // 初始手数extern int Gap = 100; // 间距(点数)extern double AddLotMultiplier = 0.6; // 补仓倍数extern int MaxAddLotCount = 5; // 补仓次数extern double ProfitTarget = 40; // 盈利目标(美金)extern int Slippage = 3; // 滑点值

void OnTick(){ double latestPrice = MarketInfo(Symbol(), MODE_BID); // 获取最新价格 int totalOrders = OrdersTotal(); // 获取订单总数

// 检查当前魔术码是否持仓    bool hasPosition = false;    for (int i = 0; i < totalOrders; i++)    {        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber)        {            hasPosition = true;            break;        }    }

// 如果没有持仓    if (!hasPosition)    {        double entryPrice = latestPrice;        double lotSize = InitialLotSize;

    // 根据最新价格加100点挂单一次多单        double buyStopPrice = entryPrice + Gap * Point; // 注意: 将间距转换为点数        int buyStopOrderTicket = OrderSend(Symbol(), OP_BUYSTOP, lotSize, buyStopPrice, 0, 0, 0, 'Buy Stop', MagicNumber, 0, Blue);

    // 根据最新价格减100点挂单一次空单        double sellStopPrice = entryPrice - Gap * Point; // 注意: 将间距转换为点数        int sellStopOrderTicket = OrderSend(Symbol(), OP_SELLSTOP, lotSize, sellStopPrice, 0, 0, 0, 'Sell Stop', MagicNumber, 0, Red);                // 设置挂单的止损和止盈        if (buyStopOrderTicket > -1)        {            if (!OrderModify(buyStopOrderTicket, OrderOpenPrice(), NormalizeDouble(entryPrice - Gap * 2 * Point, Digits), OrderTakeProfit(), 0, Green)) // 注意: 将间距转换为点数            {                Print('Failed to modify buy stop order: ', GetLastError());            }        }        if (sellStopOrderTicket > -1)        {            if (!OrderModify(sellStopOrderTicket, OrderOpenPrice(), NormalizeDouble(entryPrice + Gap * 2 * Point, Digits), OrderTakeProfit(), 0, Green)) // 注意: 将间距转换为点数            {                Print('Failed to modify sell stop order: ', GetLastError());            }        }    }    else // 如果持仓    {        double firstOrderProfitTarget = 0;                for (int j = 0; j < totalOrders; j++)        {            if (OrderSelect(j, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber)            {                double orderProfit = OrderProfit();                                // 判断当前魔术码下的所有持仓订单                if (orderProfit >= ProfitTarget)                {                    if (!OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Red))                    {                        Print('Failed to close order: ', GetLastError());                    }                }                else                {                    if (j == 0) // 第一单                    {                        firstOrderProfitTarget = orderProfit;                                                // 如果持仓多单,最新价小于第一单多单100点时,以初始手数0.5手做空                        if (OrderType() == OP_BUY && latestPrice < OrderOpenPrice() - Gap * Point) // 注意: 将间距转换为点数                        {                            if (!OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Red))                            {                                Print('Failed to close initial buy order: ', GetLastError());                            }                                                        int shortOrderTicket = OrderSend(Symbol(), OP_SELL, InitialLotSize, 0, 0, 0, 0, 'Initial Short', MagicNumber, 0, Red);                            if (shortOrderTicket < 0)                            {                                Print('Failed to open initial short order: ', GetLastError());                            }                        }                                                // 如果持仓空单,最新价大于第一单空单100点时,以初始手数0.5手做多                        if (OrderType() == OP_SELL && latestPrice > OrderOpenPrice() + Gap * Point) // 注意: 将间距转换为点数                        {                            if (!OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red))                            {                                Print('Failed to close initial sell order: ', GetLastError());                            }                                                        int longOrderTicket = OrderSend(Symbol(), OP_BUY, InitialLotSize, 0, 0, 0, 0, 'Initial Long', MagicNumber, 0, Blue);                            if (longOrderTicket < 0)                            {                                Print('Failed to open initial long order: ', GetLastError());                            }                        }                    }                    else // 非第一单                    {                        int addLotCount = MathFloor(orderProfit / (Gap * Point)); // 补仓次数计数, 注意: 将间距转换为点数

                    if (OrderType() == OP_BUY) // 多单                        {                            for (int k = 1; k <= addLotCount && k <= MaxAddLotCount; k++)                            {                                double addLotSize = InitialLotSize * MathPow(AddLotMultiplier, k);                                double addLotEntryPrice = firstOrderProfitTarget + Gap * k * Point; // 注意: 将间距转换为点数                                int buyOrderTicket = OrderSend(Symbol(), OP_BUY, addLotSize, addLotEntryPrice, 0, 0, 0, 'Buy Add', MagicNumber, 0, Blue);                                if (buyOrderTicket < 0)                                {                                    Print('Failed to open buy add order: ', GetLastError());                                }                            }                        }                        else if (OrderType() == OP_SELL) // 空单                        {                            for (int k = 1; k <= addLotCount && k <= MaxAddLotCount; k++)                            {                                double addLotSize = InitialLotSize * MathPow(AddLotMultiplier, k);                                double addLotEntryPrice = firstOrderProfitTarget - Gap * k * Point; // 注意: 将间距转换为点数                                int sellOrderTicket = OrderSend(Symbol(), OP_SELL, addLotSize, addLotEntryPrice, 0, 0, 0, 'Sell Add', MagicNumber, 0, Red);                                if (sellOrderTicket < 0)                                {                                    Print('Failed to open sell add order: ', GetLastError());                                }                            }                        }                    }                }            }        }    }}

void OnStart(){ OnTick();}

代码逻辑

  1. 初始化参数: 代码首先定义了一些外部变量,包括魔术码、初始手数、价格间距、补仓倍数、最大补仓次数、盈利目标和滑点值。2. 检查持仓: 在每个价格变动时,OnTick() 函数会被调用。它首先检查当前魔术码是否有持仓订单。3. 无持仓时挂单: 如果没有持仓,根据当前价格和设定的间距计算出多单和空单的挂单价格,并使用 OrderSend() 函数发送挂单。同时,设置止损和止盈以控制风险。4. 有持仓时管理订单: 如果有持仓订单,代码会遍历所有订单,并根据盈利情况进行处理: * 如果订单盈利达到目标,则平仓。 * 如果是第一单,根据价格走势决定是否反向开仓。 * 如果是非第一单,根据盈利情况决定是否加仓。

策略优化

该策略可以通过调整参数来进行优化:

  • 间距: 可以根据不同的交易品种和市场波动性调整间距。* 补仓倍数: 可以根据风险偏好调整补仓倍数。* 最大补仓次数: 可以根据账户资金规模和风险承受能力调整最大补仓次数。* 盈利目标: 可以根据市场情况和个人交易目标调整盈利目标。

总结

该MQL4交易机器人实现了一个简单的基于固定间距加仓的交易策略。你可以根据自己的需求修改和优化代码,并将其应用于实际交易。请注意,任何交易策略都存在风险,请谨慎使用。

MQL4交易策略:基于固定间距加仓的交易机器人

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

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