SAR指标代码求助

 
我希望绘制一个新指标,他的原理是:当价格大于SAR值时记录并绘制为均线,当价格小于SAR值时候记录并绘制为均线,加载到图表中使用不同颜色区分,这样图表上应该有两条均线,但是目前编写不顺利,附上源代码,请各位前来帮我修改指正。
//+------------------------------------------------------------------+
//|                                                  ConditionalSAR.mq5 |
//|                        Copyright 2023, Your Name |
//|                                       http://www.yourwebsite.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Your Name"
#property link      "http://www.yourwebsite.com/"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

//--- plot properties
#property indicator_label1 "SAR Above MA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed

#property indicator_label2 "SAR Below MA"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrGreen

// 输入参数
input double Start = 0.02;          // SAR的开始值
input double Increment = 0.02;      // SAR的增量
input double Maximum = 0.2;         // SAR的最大值
input int AvgPeriod = 30;           // 平均周期

// 定义指标缓冲区
double sarAboveMA[];
double sarBelowMA[];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    SetIndexBuffer(0, sarAboveMA);
    SetIndexBuffer(1, sarBelowMA);

    ArraySetAsSeries(sarAboveMA, true);
    ArraySetAsSeries(sarBelowMA, true);

    IndicatorSetString(INDICATOR_SHORTNAME, "Conditional SAR Avg");
    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 begin = prev_calculated > 1 ? prev_calculated - 1 : 0;
    if(rates_total < AvgPeriod) return 0;

    // Prepare the buffer to retrieve SAR data
    double sarBuffer[rates_total];
    int sarHandle = iSAR(_Symbol, _Period, Start, Increment);
    if(sarHandle == INVALID_HANDLE)
        return 0; // Exit if handle is invalid

    if(CopyBuffer(sarHandle, 0, 0, rates_total, sarBuffer) <= 0)
        return 0; // Exit if no data is copied

    for(int i = begin; i < rates_total; i++)
    {
        if(close[i] > sarBuffer[i])
        {
            sarAboveMA[i] = sarBuffer[i];
            sarBelowMA[i] = EMPTY_VALUE;
        }
        else if(close[i] < sarBuffer[i])
        {
            sarBelowMA[i] = sarBuffer[i];
            sarAboveMA[i] = EMPTY_VALUE;
        }
        else
        {
            sarAboveMA[i] = EMPTY_VALUE;
            sarBelowMA[i] = EMPTY_VALUE;
        }
    }

    return rates_total;
}
//+------------------------------------------------------------------+