MT4 专家交易系统:基于价格突破的网格交易策略

本篇博客分享一个适用于 MetaTrader 4 (MT4) 平台的专家交易系统 (EA) 代码示例,该策略基于价格突破进行网格交易。

策略概述

该策略的核心思想是在价格突破预设的间隔时进行交易。当价格向上突破一定点数时,进行买入操作;当价格向下突破一定点数时,进行卖出操作。

策略特点:

  • 网格交易: 在价格波动区间内,进行多次买入或卖出操作,以获取利润。* 价格突破: 利用价格突破作为交易信号,避免在震荡行情中频繁交易。

代码示例

以下是经过修改的代码,使用了适用于 MetaTrader 4(MT4)平台的函数和变量:MQL4extern int MagicNumber = 92133; // 魔术码extern double InitialLots = 0.5; // 初始手数extern int Gap = 100; // 间距 (点数)extern double AddLotsMultiplier = 0.6; // 补仓倍数extern int MaxAddLots = 5; // 最大补仓次数extern double ProfitTarget = 40; // 总盈利目标 (美元)

int totalOrders = 0; // 当前魔术码下的总订单数量double firstBuyPrice = 0; // 第一笔多单的价格double firstSellPrice = 0; // 第一笔空单的价格int buyOrders = 0; // 当前多单的笔数int sellOrders = 0; // 当前空单的笔数

// 检查当前魔术码下是否持仓bool CheckOpenOrders(){ totalOrders = 0; buyOrders = 0; sellOrders = 0;

for (int i = OrdersTotal() - 1; i >= 0; i--)    {        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))        {            if (OrderMagicNumber() == MagicNumber)            {                totalOrders++;                if (OrderType() == OP_BUY)                {                    buyOrders++;                    if (buyOrders == 1)                    {                        firstBuyPrice = OrderOpenPrice();                    }                }                else if (OrderType() == OP_SELL)                {                    sellOrders++;                    if (sellOrders == 1)                    {                        firstSellPrice = OrderOpenPrice();                    }                }            }        }    }

return (totalOrders > 0);}

// 挂单void PlaceOrder(int type, double lots){ double price = NormalizeDouble(Bid + Gap * _Point, Digits); double slippage = MarketInfo(_Symbol, MODE_STOPLEVEL);

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

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

// 补仓void AddPosition(int type, double lots, double openPrice){ double price = NormalizeDouble(Bid + Gap * _Point, Digits); double slippage = MarketInfo(_Symbol, MODE_STOPLEVEL); double addLots = lots * AddLotsMultiplier;

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

OrderSend(_Symbol, type, addLots, price, slippage, 0, 0, '', MagicNumber, 0, Blue);

if (type == OP_BUY)    {        firstBuyPrice = openPrice;    }    else if (type == OP_SELL)    {        firstSellPrice = openPrice;    }}

// 平仓void CloseAllPositions(){ for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber() == MagicNumber) { OrderClose(OrderTicket(), OrderLots(), Bid, 0, Red); } } }}

// 主函数int start(){ if (!CheckOpenOrders()) { double price = NormalizeDouble(Bid + Gap * _Point, Digits); double slippage = MarketInfo(_Symbol, MODE_STOPLEVEL);

    // 挂单多单和空单        PlaceOrder(OP_BUY, InitialLots);        PlaceOrder(OP_SELL, InitialLots);    }    else    {        double currentPrice = NormalizeDouble(Bid, Digits);

    // 多单补仓        if (buyOrders > 0 && currentPrice >= firstBuyPrice + Gap * buyOrders * _Point)        {            if (buyOrders < MaxAddLots)            {                AddPosition(OP_BUY, OrderLots(), firstBuyPrice);            }        }

    // 空单补仓        if (sellOrders > 0 && currentPrice <= firstSellPrice - Gap * sellOrders * _Point)        {            if (sellOrders < MaxAddLots)            {                AddPosition(OP_SELL, OrderLots(), firstSellPrice);            }        }

    // 检查持仓多单        if (buyOrders > 0 && currentPrice < firstBuyPrice - Gap * _Point)        {            PlaceOrder(OP_SELL, InitialLots);        }

    // 检查持仓空单        if (sellOrders > 0 && currentPrice > firstSellPrice + Gap * _Point)        {            PlaceOrder(OP_BUY, InitialLots);        }

    // 检查总盈利目标        double totalProfit = 0;        for (int i = OrdersTotal() - 1; i >= 0; i--)        {            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))            {                if (OrderMagicNumber() == MagicNumber)                {                    totalProfit += OrderProfit();                }            }        }

    if (totalProfit >= ProfitTarget)        {            CloseAllPositions();        }    }

return 0;}

参数说明

  • MagicNumber: 用于区分不同 EA 的唯一标识符。* InitialLots: 初始交易手数。* Gap: 网格间距,单位为点。* AddLotsMultiplier: 每次补仓时的交易手数倍增系数。* MaxAddLots: 最大补仓次数。* ProfitTarget: 总盈利目标,达到目标后平仓。

注意

  • 以上代码仅为示例,并未经过完整的测试和验证。在实际使用中,应仔细检查和调整代码以适应你的交易策略和需求。* 交易外汇和差 CFDs 涉及高风险,可能会损失您的全部投资资金。* 在使用本代码或任何其他交易系统之前,请确保您完全了解所涉及的风险,并根据您的风险承受能力做出明智的决定。

希望这篇博客对您有所帮助。祝您交易顺利!

MT4 专家交易系统:基于价格突破的网格交易策略

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

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