MT5 EA交易代码框架及MACD策略实现

本文将提供一个MT5 EA交易代码框架,并基于该框架实现一个MACD交易策略。代码经过优化,可在MT5中进行测试和EA交易。

一、MT5 EA交易代码框架

以下是一个基本的MT5 EA交易代码框架,包含了必要的函数和变量定义:cpp#property copyright 'Created by ChatGPT'#property link 'https://www.example.com'#property version '1.000'//---#include <Trade\PositionInfo.mqh>#include <Trade\Trade.mqh>#include <Trade\SymbolInfo.mqh>

CPositionInfo m_position; // trade position objectCTrade m_trade; // trading objectCSymbolInfo m_symbol; // symbol info object

//--- input parametersinput double Lots = 0.05; // 交易手数input ulong m_magic=42828093; // 魔术数字ulong m_slippage=10; // 滑点

//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+int OnInit(){ //--- if(!m_symbol.Name(Symbol())) // 设置交易品种名称 return(INIT_FAILED);

RefreshRates();

//---    m_trade.SetExpertMagicNumber(m_magic);

if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK))        m_trade.SetTypeFilling(ORDER_FILLING_FOK);    else if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC))        m_trade.SetTypeFilling(ORDER_FILLING_IOC);    else        m_trade.SetTypeFilling(ORDER_FILLING_RETURN);

m_trade.SetDeviationInPoints(m_slippage);

return(INIT_SUCCEEDED);}

//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+void OnDeinit(const int reason){ //---}

//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+void OnTick(){ // 在此添加您的交易逻辑}

//+------------------------------------------------------------------+//| 刷新交易品种行情数据 |//+------------------------------------------------------------------+bool RefreshRates(void){ //--- 刷新行情数据 if(!m_symbol.RefreshRates()) { Print('RefreshRates error'); return(false); }

//--- 保护返回值不为'零'    if(m_symbol.Ask()==0 || m_symbol.Bid()==0)        return(false);

return(true);}

//+------------------------------------------------------------------+//| 检查指定的成交模式是否允许 | //+------------------------------------------------------------------+ bool IsFillingTypeAllowed(int fill_type){ int filling = m_symbol.TradeFillFlags(); return((filling & fill_type) == fill_type);}

二、MACD交易策略实现

以下代码在上述框架基础上,实现了MACD交易策略:cpp#property copyright 'Created by ChatGPT'#property link 'https://www.example.com'#property version '1.000'//---#include <Trade\PositionInfo.mqh>#include <Trade\Trade.mqh>#include <Trade\SymbolInfo.mqh>

CPositionInfo m_position; // trade position objectCTrade m_trade; // trading objectCSymbolInfo m_symbol; // symbol info object

//--- input parametersinput double Lots = 0.05; // 交易手数input ulong m_magic=42828093; // 魔术数字ulong m_slippage=10; // 滑点input int FastMA = 12; // 快速均线周期input int SlowMA = 26; // 慢速均线周期input int SignalMA = 9; // 信号线周期input int TrendMA = 34; // 趋势线周期input double StopLoss = 20; // 止损点数input double TakeProfit = 40; // 止盈点数

//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+int OnInit(){ //--- if(!m_symbol.Name(Symbol())) // 设置交易品种名称 return(INIT_FAILED);

RefreshRates();

//---    m_trade.SetExpertMagicNumber(m_magic);

if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK))        m_trade.SetTypeFilling(ORDER_FILLING_FOK);    else if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC))        m_trade.SetTypeFilling(ORDER_FILLING_IOC);    else        m_trade.SetTypeFilling(ORDER_FILLING_RETURN);

m_trade.SetDeviationInPoints(m_slippage);

return(INIT_SUCCEEDED);}

//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+void OnDeinit(const int reason){ //---}

//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+void OnTick(){ // 计算指标 double DIFF = iMA(NULL, 0, FastMA, 0, MODE_EMA, PRICE_CLOSE) - iMA(NULL, 0, SlowMA, 0, MODE_EMA, PRICE_CLOSE); double DEA = iMA(NULL, 0, SignalMA, 0, MODE_EMA, DIFF); double MACD = 2 * (DIFF - DEA / 2); double trendline = iMA(NULL, 0, TrendMA, 1, MODE_EMA, MACD);

// 判断交易信号    bool isLong = false;    bool isShort = false;

if (trendline > 0 && REF(MACD, 1) < MACD && MACD > 0) {        isLong = true;        isShort = false;    } else if (trendline < 0 && REF(MACD, 1) > MACD && MACD < 0) {        isLong = false;        isShort = true;    }

// 执行交易    if (isLong && !IsPositionExist(POSITION_TYPE_BUY)) {        if (OrderSend(Symbol(), OP_BUY, Lots, Ask, m_slippage, Ask - StopLoss * Point, Ask + TakeProfit * Point, 'Buy Order', m_magic)) {            // 开多头寸成功        }    } else if (isShort && !IsPositionExist(POSITION_TYPE_SELL)) {        if (OrderSend(Symbol(), OP_SELL, Lots, Bid, m_slippage, Bid + StopLoss * Point, Bid - TakeProfit * Point, 'Sell Order', m_magic)) {            // 开空头寸成功        }    }

// 平仓    if (trendline < 0 && IsPositionExist(POSITION_TYPE_BUY)) {        if (OrderClose(OrderTicket(), OrderLots(), Bid, m_slippage, 0, clrRed)) {            // 平多头寸成功        }    } else if (trendline > 0 && IsPositionExist(POSITION_TYPE_SELL)) {        if (OrderClose(OrderTicket(), OrderLots(), Ask, m_slippage, 0, clrRed)) {            // 平空头寸成功        }    }}

//+------------------------------------------------------------------+//| 刷新交易品种行情数据 |//+------------------------------------------------------------------+bool RefreshRates(void){ //--- 刷新行情数据 if(!m_symbol.RefreshRates()) { Print('RefreshRates error'); return(false); }

//--- 保护返回值不为'零'    if(m_symbol.Ask()==0 || m_symbol.Bid()==0)        return(false);

return(true);}

//+------------------------------------------------------------------+//| 检查指定的成交模式是否允许 | //+------------------------------------------------------------------+ bool IsFillingTypeAllowed(int fill_type){ int filling = m_symbol.TradeFillFlags(); return((filling & fill_type) == fill_type);}

//+------------------------------------------------------------------+//| 检查是否存在指定类型的持仓 |//+------------------------------------------------------------------+bool IsPositionExist(int positionType) { int total = OrdersTotal(); for (int i = 0; i < total; i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol() && OrderMagicNumber() == m_magic) { if (positionType == POSITION_TYPE_BUY && OrderType() == OP_BUY) { return true; } else if (positionType == POSITION_TYPE_SELL && OrderType() == OP_SELL) { return true; } } } return false;}

三、代码说明

  1. 指标计算: 代码首先计算了DIFF、DEA、MACD、trendline指标。2. 交易信号: 根据MACD和trendline指标,判断做多或做空信号。3. 执行交易: 根据交易信号,执行开仓操作,设置止损和止盈。4. 平仓: 根据趋势线方向,判断是否平仓。5. IsPositionExist函数: 用于检查当前是否存在指定类型的持仓。

您可以根据自己的需求修改交易逻辑和参数,并使用MT5进行测试和EA交易

MT5 EA交易代码框架及MACD策略实现

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

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