// 定义指标变量 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指标柱状图 PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, Bars-1); PlotIndexSetString(0, PLOT_ARROW, "Arrow"); PlotIndexSetInteger(0, PLOT_ARROW_SHIFT, 0);

// 绘制趋势线 trendline = iMA(NULL, 0, 34, 1, MODE_EMA, MACD); PlotIndexSetInteger(1, PLOT_LINE_VALUE, int(trendline)); PlotIndexSetInteger(1, PLOT_LINE_BEGIN, Bars-1); PlotIndexSetString(1, PLOT_ARROW, "Arrow"); 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指标柱状图 PlotIndexSetInteger(2, PLOT_LINE_VALUE, int(K)); PlotIndexSetInteger(2, PLOT_LINE_BEGIN, Bars-1); PlotIndexSetString(2, PLOT_ARROW, "Arrow"); PlotIndexSetInteger(2, PLOT_ARROW_SHIFT, 0); PlotIndexSetString(2, PLOT_LABEL, "K"); PlotIndexSetInteger(3, PLOT_LINE_VALUE, int(D)); PlotIndexSetInteger(3, PLOT_LINE_BEGIN, Bars-1); PlotIndexSetString(3, PLOT_ARROW, "Arrow"); PlotIndexSetInteger(3, PLOT_ARROW_SHIFT, 0); PlotIndexSetString(3, PLOT_LABEL, "D"); PlotIndexSetInteger(4, PLOT_LINE_VALUE, int(J)); PlotIndexSetInteger(4, PLOT_LINE_BEGIN, Bars-1); PlotIndexSetString(4, PLOT_ARROW, "Arrow"); PlotIndexSetInteger(4, PLOT_ARROW_SHIFT, 0); PlotIndexSetString(4, PLOT_LABEL, "J");

// 绘制MACD指标柱状图 PlotIndexSetDouble(5, PLOT_LINE_BEGIN_VALUE, MACD); PlotIndexSetDouble(5, PLOT_LINE_END_VALUE, REF(MACD, 1)); PlotIndexSetInteger(5, PLOT_LINE_BEGIN, Bars-1); PlotIndexSetString(5, PLOT_ARROW, "Arrow"); PlotIndexSetInteger(5, PLOT_ARROW_SHIFT, 0); PlotIndexSetString(5, PLOT_LABEL, "MACD");

// 绘制AH指标线 PlotIndexSetDouble(6, PLOT_LINE_VALUE, AH); PlotIndexSetDouble(6, PLOT_LINE_BEGIN_VALUE, K); PlotIndexSetInteger(6, PLOT_LINE_BEGIN, Bars-1); PlotIndexSetString(6, PLOT_ARROW, "Arrow"); PlotIndexSetInteger(6, PLOT_ARROW_SHIFT, 0); PlotIndexSetString(6, PLOT_LABEL, "AH");

// 绘制MACD指标柱状图 PlotIndexSetDouble(7, PLOT_LINE_BEGIN_VALUE, MACD); PlotIndexSetDouble(7, PLOT_LINE_END_VALUE, REF(MACD, 1)); PlotIndexSetInteger(7, PLOT_LINE_BEGIN, Bars-1); PlotIndexSetString(7, PLOT_ARROW, "Arrow"); PlotIndexSetInteger(7, PLOT_ARROW_SHIFT, 0); PlotIndexSetString(7, PLOT_LABEL, "MACD");

// 设置EA交易参数和逻辑 input double Lots = 0.05; // 交易手数 input double StopLoss = 30; // 止损点数 input double TakeProfit = 60; // 止盈点数 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 && !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) { if (IsPositionExist(POSITION_TYPE_BUY)) { if (OrderClose(OrderTicket(), OrderLots(), Bid, m_slippage, 0, clrRed)) { // 平多头寸成功 } } } else if (trendline > 0) { if (IsPositionExist(POSITION_TYPE_SELL)) { if (OrderClose(OrderTicket(), OrderLots(), Ask, m_slippage, 0, clrRed)) { // 平空头寸成功 } } }

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;

把你回答的代码续写完成把下面的交易代码进行完善保持以下交易代码的逻辑并使其能像上面的代码一样在MT5中能进行测试通过并能进行EA交易 定义指标变量double DIFF DEA MACD AH AL trendline K D J; 计算DIFF、DEA、MACD指标DIFF = iMANULL 0 12 0 MODE_EMA PRICE_CLOSE - iMANULL 0 26 0 MODE_E

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

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