完善后的EA交易代码如下所示:

// 定义指标变量
double DIFF, DEA, MACD, AH, AL, trendline, K, D, J;

// 计算DIFF、DEA、MACD指标
DIFF = iMA(NULL, 0, 12, 0, MODE_EMA, PRICE_CLOSE) - iMA(NULL, 0, 26, 0, MODE_EMA, PRICE_CLOSE);
DEA = iMA(NULL, 0, 9, 0, MODE_EMA, DIFF);
MACD = 2 * (DIFF - DEA / 2);

// 计算AH、AL指标
AH = iHighest(NULL, 0, MODE_MAIN, 13, 1);
AL = iLowest(NULL, 0, MODE_MAIN, 13, 0.3);

// 绘制MACD指标柱状图
for (int i = 0; i < Bars; i++) {
    if (MACD[i] > 0) {
        PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, i);
        break;
    }
}
PlotIndexSetInteger(0, PLOT_ARROW, 233);
PlotIndexSetInteger(0, PLOT_ARROW_SHIFT, 0);

// 绘制趋势线
trendline = iMA(NULL, 0, 34, 1, MODE_EMA, MACD);
for (int i = 0; i < Bars; i++) {
    if (trendline[i] > trendline[i - 1]) {
        PlotIndexSetDouble(1, PLOT_LINE_VALUE, trendline[i]);
        PlotIndexSetInteger(1, PLOT_LINE_BEGIN, i);
        break;
    }
}
PlotIndexSetInteger(1, PLOT_ARROW, 233);
PlotIndexSetInteger(1, PLOT_ARROW_SHIFT, 0);
PlotIndexSetString(1, PLOT_LABEL, "Trend Line");

// 计算K、D、J指标
K = iMA(NULL, 0, 2, 1, MODE_SMA, MACD);
D = iMA(NULL, 0, 2, 1, MODE_SMA, K);
J = iMA(NULL, 0, 2, 1, MODE_SMA, D);

// 绘制K、D、J指标柱状图
for (int i = 0; i < Bars; i++) {
    if (K[i] > REF(K, 1)[i]) {
        PlotIndexSetDouble(2, PLOT_LINE_VALUE, K[i]);
        PlotIndexSetInteger(2, PLOT_LINE_BEGIN, i);
        break;
    }
}
PlotIndexSetInteger(2, PLOT_ARROW, 233);
PlotIndexSetInteger(2, PLOT_ARROW_SHIFT, 0);
PlotIndexSetString(2, PLOT_LABEL, "K");
for (int i = 0; i < Bars; i++) {
    if (D[i] > REF(D, 1)[i]) {
        PlotIndexSetDouble(3, PLOT_LINE_VALUE, D[i]);
        PlotIndexSetInteger(3, PLOT_LINE_BEGIN, i);
        break;
    }
}
PlotIndexSetInteger(3, PLOT_ARROW, 233);
PlotIndexSetInteger(3, PLOT_ARROW_SHIFT, 0);
PlotIndexSetString(3, PLOT_LABEL, "D");
for (int i = 0; i < Bars; i++) {
    if (J[i] > REF(J, 1)[i]) {
        PlotIndexSetDouble(4, PLOT_LINE_VALUE, J[i]);
        PlotIndexSetInteger(4, PLOT_LINE_BEGIN, i);
        break;
    }
}
PlotIndexSetInteger(4, PLOT_ARROW, 233);
PlotIndexSetInteger(4, PLOT_ARROW_SHIFT, 0);
PlotIndexSetString(4, PLOT_LABEL, "J");

// 绘制MACD指标柱状图
for (int i = 1; i < Bars; i++) {
    if (REF(MACD, 1)[i] < MACD[i]) {
        PlotIndexSetDouble(5, PLOT_LINE_BEGIN_VALUE, MACD[i]);
        PlotIndexSetDouble(5, PLOT_LINE_END_VALUE, REF(MACD, 1)[i]);
        PlotIndexSetInteger(5, PLOT_LINE_BEGIN, i);
        break;
    }
}
PlotIndexSetInteger(5, PLOT_ARROW, 233);
PlotIndexSetInteger(5, PLOT_ARROW_SHIFT, 0);
PlotIndexSetString(5, PLOT_LABEL, "MACD");

// 绘制AH指标线
for (int i = 0; i < Bars; i++) {
    if (MACD[i] == AH) {
        PlotIndexSetDouble(6, PLOT_LINE_VALUE, AH);
        PlotIndexSetDouble(6, PLOT_LINE_BEGIN_VALUE, K[i]);
        PlotIndexSetInteger(6, PLOT_LINE_BEGIN, i);
        break;
    }
}
PlotIndexSetInteger(6, PLOT_ARROW, 233);
PlotIndexSetInteger(6, PLOT_ARROW_SHIFT, 0);
PlotIndexSetString(6, PLOT_LABEL, "AH");

// 绘制MACD指标柱状图
for (int i = 2; i < Bars; i++) {
    if (REF(MACD, 2)[i] > MACD[i]) {
        PlotIndexSetDouble(7, PLOT_LINE_BEGIN_VALUE, MACD[i]);
        PlotIndexSetDouble(7, PLOT_LINE_END_VALUE, REF(MACD, 1)[i]);
        PlotIndexSetInteger(7, PLOT_LINE_BEGIN, i);
        break;
    }
}
PlotIndexSetInteger(7, PLOT_ARROW, 233);
PlotIndexSetInteger(7, PLOT_ARROW_SHIFT, 0);
PlotIndexSetString(7, PLOT_LABEL, "MACD");

// 设置EA交易参数和逻辑
input double   Lots        = 0.05;              // 交易手数
input ulong    m_magic=42828093;             // 魔术数字
ulong          m_slippage=10;                // 滑点
bool isLong = false;
bool isShort = false;

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

if (isLong && OrderSend(Symbol(), OP_BUY, Lots, Ask, m_slippage, 0, 0, "Buy Order", m_magic)) {
    // 开多头寸成功
} else if (isShort && OrderSend(Symbol(), OP_SELL, Lots, Bid, m_slippage, 0, 0, "Sell Order", m_magic)) {
    // 开空头寸成功
}

if (trendline < 0 && OrderClose(OrderTicket(), OrderLots(), Bid, m_slippage, 0, clrRed)) {
    // 平仓成功
}
``

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

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