求助帮忙修改错误代码

14 九月 2025, 04:29
Kechao Xie
0
139
//+------------------------------------------------------------------+
//| HalfTrend.mq4                                                    |
//| Copyright 2014, AlexSoftware                                      |
//| Based on Ozymandias.mq4                                          |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 DeepSkyBlue // up[]
#property indicator_width1 2
#property indicator_color2 Tomato // down[]
#property indicator_width2 2
#property indicator_color3 DeepSkyBlue // atrlo[]
#property indicator_width3 1
#property indicator_color4 Tomato // atrhi[]
#property indicator_width4 1
#property indicator_color5 DeepSkyBlue // arrup[]
#property indicator_width5 1
#property indicator_color6 Tomato // arrdwn[]
#property indicator_width6 1
#property indicator_color7 LemonChiffon // buy arrow
#property indicator_width7 1
#property indicator_color8 LightCoral // sell arrow
#property indicator_width8 1

extern int Amplitude = 2; // 振幅
extern int ShortPeriod = 5; // 短期均线周期
extern int MediumPeriod = 10; // 中期均线周期
extern int LongPeriod = 20; // 长期均线周期
extern bool ShowBars = false;
extern bool ShowArrows = true; // 默认显示箭头
extern bool alertsOn = false;
extern bool alertsMessage = true;

double up[], down[], atrlo[], atrhi[], trend[], arrup[], arrdwn[], buyArrow[], sellArrow[];
bool nexttrend; // 当前位置趋势
double minhighprice, maxlowprice;

//+------------------------------------------------------------------+
//| Initialization function                                           |
//+------------------------------------------------------------------+
int OnInit() {
    IndicatorBuffers(8); // 创建 8 个缓冲区

    SetIndexBuffer(0, up);
    SetIndexStyle(0, DRAW_LINE);
    SetIndexBuffer(1, down);
    SetIndexStyle(1, DRAW_LINE);
    SetIndexBuffer(2, atrlo);
    SetIndexBuffer(3, atrhi);
    SetIndexBuffer(4, arrup);
    SetIndexBuffer(5, arrdwn);
    SetIndexBuffer(6, buyArrow);
    SetIndexBuffer(7, sellArrow);

    SetIndexEmptyValue(0, 0.0);
    SetIndexEmptyValue(1, 0.0);
    SetIndexEmptyValue(2, 0.0);
    SetIndexEmptyValue(3, 0.0);
    SetIndexEmptyValue(4, EMPTY_VALUE);
    SetIndexEmptyValue(5, EMPTY_VALUE);
    SetIndexEmptyValue(6, EMPTY_VALUE);
    SetIndexEmptyValue(7, EMPTY_VALUE);

    if (ShowArrows) {
        SetIndexStyle(6, DRAW_ARROW);
        SetIndexArrow(6, 233); // 买入箭头
        SetIndexStyle(7, DRAW_ARROW);
        SetIndexArrow(7, 234); // 卖出箭头
    } else {
        SetIndexStyle(6, DRAW_NONE);
        SetIndexStyle(7, DRAW_NONE);
    }

    nexttrend = 1; // 初始为向上趋势
    minhighprice = High[0]; // 初始化为当前最高价
    maxlowprice = Low[0]; // 初始化为当前最低价

    return INIT_SUCCEEDED; // 返回初始化成功
}

//+------------------------------------------------------------------+
//| Main calculation 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 double &spread[]) {
    int counted_bars = IndicatorCounted();
    if (counted_bars < 0) return (-1);
    if (counted_bars > 0) counted_bars--;

    int limit = MathMin(rates_total - counted_bars, rates_total - 1);

    // 确保trend、up、down、atrlo、atrhi、arrup、arrdwn、buyArrow、sellArrow数组足够大
    ArrayResize(trend, rates_total);
    ArrayResize(up, rates_total);
    ArrayResize(down, rates_total);
    ArrayResize(atrlo, rates_total);
    ArrayResize(atrhi, rates_total);
    ArrayResize(arrup, rates_total);
    ArrayResize(arrdwn, rates_total);
    ArrayResize(buyArrow, rates_total);
    ArrayResize(sellArrow, rates_total); 

    ArrayInitialize(trend, 0);
    ArrayInitialize(up, 0);
    ArrayInitialize(down, 0);
    ArrayInitialize(atrlo, 0);
    ArrayInitialize(atrhi, 0);
    ArrayInitialize(arrup, EMPTY_VALUE);
    ArrayInitialize(arrdwn, EMPTY_VALUE);
    ArrayInitialize(buyArrow, EMPTY_VALUE);
    ArrayInitialize(sellArrow, EMPTY_VALUE);

    for (int i = limit - 1; i >= 0; i--) {

        // 计算低点和高点
        double lowprice_i = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, Amplitude, i));
        double highprice_i = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, Amplitude, i));
        double lowma = iMA(NULL, 0, Amplitude, 0, MODE_SMA, PRICE_LOW, i);
        double highma = iMA(NULL, 0, Amplitude, 0, MODE_SMA, PRICE_HIGH, i);
        double atr = iATR(NULL, 0, 14, i) / 2; // 使用14期ATR

        arrup[i] = EMPTY_VALUE;
        arrdwn[i] = EMPTY_VALUE;

        // 更新趋势
        if (nexttrend == 1) { // 向上趋势
            maxlowprice = MathMax(lowprice_i, maxlowprice);
            if (highma < maxlowprice && Close[i] < Low[i + 1]) {
                trend[i] = 1.0;
                nexttrend = 0;
                minhighprice = highprice_i;
            }
        } else { // 向下趋势
            minhighprice = MathMin(highprice_i, minhighprice);
            if (lowma > minhighprice && Close[i] > High[i + 1]) {
                trend[i] = 0.0;
                nexttrend = 1;
                maxlowprice = lowprice_i;
            }
        }

        // 计算上轨和下轨
        if (trend[i] == 0.0) {
            if (trend[i + 1] != 0.0) {
                up[i] = down[i + 1];
            } else {
                up[i] = MathMax(maxlowprice, up[i + 1]);
            }
            atrhi[i] = up[i] - atr;
            atrlo[i] = up[i];
            down[i] = 0.0;

            // 检查买入信号
            if (isBuySignal(i)) {
                buyArrow[i] = Low[i] - 2 * Point; // 设置买入信号箭头位置
            }
        } else {
            if (trend[i + 1] != 1.0) {
                down[i] = up[i + 1];
            } else {
                down[i] = MathMin(minhighprice, down[i + 1]);
            }
            atrhi[i] = down[i] + atr;
            atrlo[i] = down[i];
            up[i] = 0.0;

            // 检查卖出信号
            if (isSellSignal(i)) {
                sellArrow[i] = High[i] + 2 * Point; // 设置卖出信号箭头位置
            }
        }
    }

    manageAlerts();
    return rates_total;
}

//+------------------------------------------------------------------+
//| 检查买入信号                                                    |
//+------------------------------------------------------------------+
bool isBuySignal(int index) {
    double shortMA = iMA(NULL, 0, ShortPeriod, 0, MODE_SMA, PRICE_CLOSE, index);
    double mediumMA = iMA(NULL, 0, MediumPeriod, 0, MODE_SMA, PRICE_CLOSE, index);
    double longMA = iMA(NULL, 0, LongPeriod, 0, MODE_SMA, PRICE_CLOSE, index);

    return (shortMA > mediumMA && mediumMA > longMA); // 确认多头趋势
}

//+------------------------------------------------------------------+
//| 检查卖出信号                                                    |
//+------------------------------------------------------------------+
bool isSellSignal(int index) {
    double shortMA = iMA(NULL, 0, ShortPeriod, 0, MODE_SMA, PRICE_CLOSE, index);
    double mediumMA = iMA(NULL, 0, MediumPeriod, 0, MODE_SMA, PRICE_CLOSE, index);
    double longMA = iMA(NULL, 0, LongPeriod, 0, MODE_SMA, PRICE_CLOSE, index);

    return (shortMA < mediumMA && mediumMA < longMA); // 确认空头趋势
}

//+------------------------------------------------------------------+
//| 管理警报                                                        |
//+------------------------------------------------------------------+
void manageAlerts() {
    if (alertsOn) {
        // 添加报警逻辑
    }

}



帮助我把上方代码修改正确,斌且添加多周期共振出现买入和卖出信号并且在主图箭头显示