//+------------------------------------------------------------------+
//|                                                       RedK_EVEREX|
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_maximum 50000
#property indicator_buffers 11
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 Lime
#property indicator_color4 Lime
#property indicator_color5 Yellow

input int            length     = 10;
input ENUM_MA_METHOD MA_Type    = MODE_SMA;
input int            smooth     = 3;
input int            sig_length = 5;
input ENUM_MA_METHOD S_Type     = MODE_SMA;
input int            lookback   = 20;
input string         lkbk_Calc  = "Simple";
input bool           showBias   = false;
input int            B_Length   = 30;
input ENUM_MA_METHOD B_Type     = MODE_SMA;
input bool           showEVEREX = true;
input int            bandscale  = 100; // Note: Band scale options ["100", "200", "400"] not directly available in MQL4

double VolaBuffer[];
double PriceaBuffer[];
double RROFBuffer[];
double SignalBuffer[];
double Bv[];
double BarSpread_abs[];
double SrcShift_abs[];
double bulls[];
double bears[];
double RROF[];
double RROF_s[];

//double GetAverage(double data[], int len, ENUM_MA_METHOD MAOption) {
//   double value = 0;
//   switch (MAOption) {
//      case MODE_SMA: value = iMA(NULL, 0, len, 0, MODE_SMA, PRICE_CLOSE, 0); break;
//      case MODE_EMA: value = iMA(NULL, 0, len, 0, MODE_EMA, PRICE_CLOSE, 0); break;
//      case MODE_SMMA: value = iMA(NULL, 0, len, 0, MODE_SMMA, PRICE_CLOSE, 0); break;
//      case MODE_LWMA: value = iMA(NULL, 0, len, 0, MODE_LWMA, PRICE_CLOSE, 0); break;
//   }
//   return value;
//}

double Normalize(double Value, double Avg) {
   double X = Value / Avg;
   double Nor = 0.1; // Default value
   if (X > 1.50) Nor = 1.00;
   else if (X > 1.20) Nor = 0.90;
   else if (X > 1.00) Nor = 0.80;
   else if (X > 0.80) Nor = 0.70;
   else if (X > 0.60) Nor = 0.60;
   else if (X > 0.40) Nor = 0.50;
   else if (X > 0.20) Nor = 0.25;
   return (Nor);
}

int init() {
   IndicatorBuffers(11);
   SetIndexBuffer(0, VolaBuffer);
   SetIndexBuffer(1, PriceaBuffer);
   SetIndexBuffer(2, RROFBuffer);
   SetIndexBuffer(3, SignalBuffer);
   SetIndexBuffer(4, Bv);
   SetIndexBuffer(5, BarSpread_abs);
   SetIndexBuffer(6, SrcShift_abs);
   SetIndexBuffer(7, bulls);
   SetIndexBuffer(8, bears);
   SetIndexBuffer(9, RROF);
   SetIndexBuffer(10, RROF_s);
   SetIndexLabel(0, "Volume Normalized");
   SetIndexLabel(1, "Price Normalized");
   SetIndexLabel(2, "RROF Smooth");
   SetIndexLabel(3, "Signal Line");
   IndicatorShortName("RedK_EVEREX");
   return(0);
} 

int start(){int i,limit = Bars-IndicatorCounted(); double Vola,Vola_n_pre,Vola_n;

   // Volume "effort" Calculation
   for(i=limit;i>=0;i--){Bv[i]=Volume[i];}
   for(i=limit;i>=0;i--){Vola=iMAOnArray(Bv,0,length,0,MA_Type,i);
                         Vola_n_pre = Normalize(Bv[i], Vola) * 100;
                         Vola_n = Vola_n_pre; // Handle case of no volume data

   // Price "result" calculation
   double BarSpread = Close[i] - Open[i];
   double BarRange = High[i] - Low[i];
   double R2 = High[i+1] - Low[i+1];
   double SrcShift = Close[i] - Close[i+1];

   //double sign_shift = MathSign(SrcShift);
   double sign_shift; if(SrcShift<0){sign_shift=-1;}else{sign_shift=1;}
   
   
   //double sign_spread = MathSign(BarSpread);
   double sign_spread; if(BarSpread<0){sign_spread=-1;}else{sign_spread=1;}

   double barclosing = 2 * (Close[i] - Low[i]) / BarRange * 100 - 100;
   double s2r = BarSpread / BarRange * 100;
          BarSpread_abs[i] = MathAbs(BarSpread);
   
   double BarSpread_avg = iMAOnArray(BarSpread_abs, 0, lookback, 0, MA_Type, i);
   double BarSpread_ratio_n = Normalize(BarSpread_abs[i], BarSpread_avg) * 100 * sign_spread;

   double barclosing_2 = 2 * (Close[i] - Low[i+1]) / R2 * 100 - 100;
   double Shift2Bar_toR2 = SrcShift / R2 * 100;
          SrcShift_abs[i] = MathAbs(SrcShift);
   double srcshift_avg = iMAOnArray(SrcShift_abs, 0, lookback, 0, MA_Type, i);
   double srcshift_ratio_n = Normalize(SrcShift_abs[i], srcshift_avg) * 100 * sign_shift;

   double Pricea_n = (barclosing + s2r + BarSpread_ratio_n + barclosing_2 + Shift2Bar_toR2 + srcshift_ratio_n) / 6;

   double bar_flow = Pricea_n * Vola_n / 100;

   // Bulls and Bears calculation
          bulls[i] = MathMax(bar_flow, 0);
          bears[i] = -1 * MathMin(bar_flow, 0);
   double bulls_avg = iMAOnArray(bulls, 0, length, 0, MA_Type, i);
   double bears_avg = iMAOnArray(bears, 0, length, 0, MA_Type, i);
   double dx = bulls_avg / bears_avg;
          RROF[i] = 2 * (100 - 100 / (1 + dx)) - 100;
          RROF_s[i] = iMAOnArray(RROF, 0, smooth, 0, MODE_SMA, i);

   // Signal line calculation
   double Signal = iMAOnArray(RROF_s, 0, sig_length, 0, S_Type, i);

   // Storing values in buffers
   VolaBuffer[i] = Vola_n;
   PriceaBuffer[i] = Pricea_n;
   RROFBuffer[i] = RROF_s[i];
   SignalBuffer[i] = Signal;
}
   return(0);
}