以下是根据你提供的交易函数和交易逻辑编写的EA交易代码mq5:

//+------------------------------------------------------------------+
//|                                         Trend_MACD_Trading.mq5  |
//|                                     Copyright © 2021 MetaQuotes  |
//|                                       https://www.mql5.com/en/  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021 MetaQuotes"
#property link      "https://www.mql5.com/en/"
#property version   "1.00"
#property strict

// 输入参数
input int EMA1_Period = 12;
input int EMA2_Period = 26;
input int EMA3_Period = 9;
input int EMA4_Period = 34;

// 全局变量
double diff, dea, macd, ah, al, trendLine, k, d, j;
bool isLong = false;
bool isShort = false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    // 设置EMA指标参数
    SetIndexBuffer(0, diff);
    SetIndexBuffer(1, dea);
    SetIndexBuffer(2, macd);
    SetIndexBuffer(3, ah);
    SetIndexBuffer(4, al);
    SetIndexBuffer(5, trendLine);
    SetIndexBuffer(6, k);
    SetIndexBuffer(7, d);
    SetIndexBuffer(8, j);
    
    SetIndexStyle(0, DRAW_LINE);
    SetIndexStyle(1, DRAW_LINE);
    SetIndexStyle(2, DRAW_HISTOGRAM);
    SetIndexStyle(3, DRAW_LINE);
    SetIndexStyle(4, DRAW_LINE);
    SetIndexStyle(5, DRAW_LINE);
    SetIndexStyle(6, DRAW_LINE);
    SetIndexStyle(7, DRAW_LINE);
    SetIndexStyle(8, DRAW_LINE);
    
    SetIndexDrawBegin(0, EMA2_Period);
    SetIndexDrawBegin(1, EMA2_Period + EMA3_Period - 1);
    SetIndexDrawBegin(2, EMA2_Period + EMA3_Period - 1);
    SetIndexDrawBegin(3, EMA2_Period + EMA3_Period - 1);
    SetIndexDrawBegin(4, EMA2_Period + EMA3_Period - 1);
    SetIndexDrawBegin(5, EMA2_Period + EMA3_Period - 1);
    SetIndexDrawBegin(6, EMA2_Period + EMA3_Period - 1);
    SetIndexDrawBegin(7, EMA2_Period + EMA3_Period - 1);
    SetIndexDrawBegin(8, EMA2_Period + EMA3_Period - 1);
    
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    int limit = rates_total - prev_calculated;
    
    // 计算指标数值
    for(int i = 0; i < limit; i++)
    {
        diff[i] = iMA(NULL, 0, EMA1_Period, 0, MODE_EMA, PRICE_CLOSE, i) - iMA(NULL, 0, EMA2_Period, 0, MODE_EMA, PRICE_CLOSE, i);
        dea[i] = iMAOnArray(diff, rates_total, EMA3_Period, 0, MODE_EMA, i);
        macd[i] = 2 * (diff[i] - dea[i] / 2);
        ah[i] = iHighest(NULL, 0, MODE_MAIN, EMA2_Period, i);
        al[i] = iLowest(NULL, 0, MODE_MAIN, MathRound(EMA2_Period * 0.3), i);
        trendLine[i] = iMAOnArray(macd, rates_total, EMA4_Period, 0, MODE_EMA, i);
        k[i] = iMAOnArray(macd, rates_total, 2, 1, MODE_SMA, i);
        d[i] = iMAOnArray(k, rates_total, 2, 1, MODE_SMA, i);
        j[i] = iMAOnArray(d, rates_total, 2, 1, MODE_SMA, i);
    }
    
    // 判断交易条件
    if(trendLine[limit - 1] > trendLine[limit - 2] && macd[limit - 1] > 0 && macd[limit - 2] < 0)
    {
        isLong = true;
        isShort = false;
    }
    
    if(trendLine[limit - 1] < trendLine[limit - 2] && macd[limit - 1] < 0 && macd[limit - 2] > 0)
    {
        isLong = false;
        isShort = true;
    }
    
    // 执行交易操作
    if(isLong)
    {
        // 做多操作
        OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, 0, 0, "Long Order", 0, 0, clrRed);
    }
    else if(isShort)
    {
        // 做空操作
        OrderSend(Symbol(), OP_SELL, 0.01, Bid, 3, 0, 0, "Short Order", 0, 0, clrGreen);
    }
    
    return(rates_total);
}

请注意,以上代码仅为示例代码,可能需要根据具体需求进行修改和优化。此外,代码中的交易操作仅为示例,实际交易时应根据具体情况进行调整和完善。

把以下交易函数和交易逻辑写成可以在MT5中运行的EA交易代码mq5:交易函数DIFF=EMACLOSE12 - EMACLOSE26;DEA=EMADIFF9;MACD=2DIFF-DEA2;AH=HHVMACD131;AL=LLVMACD1303;STICKLINEMACD0 MACD010COLOR0000AALINETHICK1;STICKLINEMACD0 MACD010COLOR0088

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

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