//+--------------------------------------+
//| Regularized Moving Average Oscillator|
//|                                      |
//|                                      |  
//+--------------------------------------+
//Modified, 10/jan/2024, by jeanlouie, www.forexfactory.com/jeanlouie
// - previously chatgpt
// - would not show history
// - plot was inverted

#property version     "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 White
#property indicator_type2 DRAW_NONE
#property strict

// Input parameters
input int EMAPeriod = 10;  // Period for EMA calculation
input int NormalizePeriod = 20; // Normalization Period

// Indicator buffers
double RMVABuffer[];
double EMAPeriodArray[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    // Indicator buffers
    SetIndexBuffer(0, RMVABuffer);
    SetIndexStyle(0, DRAW_HISTOGRAM);
    SetIndexLabel(0, "RMVA");
    SetIndexBuffer(1, EMAPeriodArray);
    SetIndexLabel(1, NULL);

    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[])
{
   for(int z=MathMax(rates_total-1-prev_calculated,0); z>=0; z--){
      EMAPeriodArray[z] = iMA(NULL, 0, EMAPeriod, 0, MODE_EMA, PRICE_CLOSE, z);
   }
   for(int z=MathMax(rates_total-1-prev_calculated,0); z>=0; z--){
      double EMA_P = iMA(NULL, 0, EMAPeriod, 0, MODE_EMA, PRICE_CLOSE, z);
      double EMA_EMA_P = iMAOnArray(EMAPeriodArray, 0, NormalizePeriod, 0, MODE_EMA, z);
      double StdDev_EMA_EMA_P = iStdDevOnArray(EMAPeriodArray, 0, NormalizePeriod, 0, MODE_SMA, z);
      if(StdDev_EMA_EMA_P!=0)RMVABuffer[z] = (EMA_P-EMA_EMA_P) / StdDev_EMA_EMA_P;
      else RMVABuffer[z]=0;
   }


//    if (rates_total <= EMAPeriod || rates_total <= NormalizePeriod || EMAPeriod <= 1 || NormalizePeriod <= 1)
//        return (0);
//
//    // Calculate EMA of EMAPeriod
//    double EMA_P = iMA(NULL, 0, EMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 0);
//
//    // Temporary array for storing EMA values
//    ArraySetAsSeries(RMVABuffer, true);
//    double EMAPeriodArray[];
//    ArrayResize(EMAPeriodArray, NormalizePeriod);
//    for (int i = 0; i < NormalizePeriod; i++)
//    {
//        EMAPeriodArray[i] = iMA(NULL, 0, EMAPeriod, 0, MODE_EMA, PRICE_CLOSE, i);
//    }
//
//    // Calculate EMA of EMA_P with NormalizePeriod
//    double EMA_EMA_P = iMAOnArray(EMAPeriodArray, 0, NormalizePeriod, 0, MODE_EMA, 0);
//
//    // Calculate standard deviation of EMA of EMA_P with NormalizePeriod
//    double StdDev_EMA_EMA_P = iStdDevOnArray(EMAPeriodArray, 0, NormalizePeriod, 0, MODE_SMA, 0);
//
//    // Calculate RMVA value
//    double RMVA = (EMA_EMA_P - EMA_P) / StdDev_EMA_EMA_P;
//
//    // Set RMVA value to the buffer for drawing
//    RMVABuffer[0] = RMVA;

    return(rates_total);
}