// #property  copyright "Copyright © 2005, Yura Prokofiev"
// #property  link      "Yura.prokofiev@gmail.com"
// Modofied by ACS to create sound alert and Text Signals and option to calculate the proper way.
// 30-Nov-07. Smoothing made extern variable on 12-Apr-08.

// Modified by rsbgm to elliminate re-paint - 4/28/2008

#property  indicator_separate_window
#property  indicator_buffers 5
#property  indicator_color1  Black
#property  indicator_color2  Lime
#property  indicator_color3  Red
#property  indicator_width1 2
#property  indicator_width2 2
#property  indicator_width3 2

// For Trend aray
#define BULL 1111
#define BEAR 2222
#define BULB 0000

extern int period = 10;
extern double Smooth = 0.67;
//extern int option = 1;  // option 1 will make it NOT a repainter. Other options leave it as a repainter!
extern bool SoundAlert = true;
//extern string SoundFile = "alert";

double         Zero[];
double         AboveZero[];
double         BelowZero[];
int            Trend[];
double         TempValue[];

double alertTag;
string TextDir="SolarWind-ACS3";
//=====================================================================
void init() {
   IndicatorDigits(Digits+1);
   IndicatorShortName("SOLAR WIND-ACS3 ("+Symbol()+","+period+")");
   SetIndexBuffer(0,Zero); SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(1,AboveZero); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexLabel(1,"SolarWind-UP");
   SetIndexBuffer(2,BelowZero); SetIndexStyle(2,DRAW_HISTOGRAM); SetIndexLabel(2,"SolarWind-DN");
   SetIndexBuffer(3,Trend); SetIndexStyle(3,DRAW_NONE);
   SetIndexBuffer(4,TempValue); SetIndexStyle(4,DRAW_NONE);
return; }
//=====================================================================
int deinit() { ObjectDelete(TextDir); return(0); }
//=====================================================================
int start() {
   int    i,limit;
   int    counted_bars=IndicatorCounted();
   double prev,current;
   double Value=0,Value1=0,Fish=0,Fish1=0;
   double Eval;
   double price;
   double MinL=0;
   double MaxH=0;  

   if(counted_bars>0) counted_bars--; limit=Bars-counted_bars;

/*   if (option!=1) {
      for(int i=0; i<limit; i++) {
         MaxH = High[Highest(NULL,0,MODE_HIGH,period,i)];
         MinL = Low[Lowest(NULL,0,MODE_LOW,period,i)];
         price = (High[i]+Low[i])/2;
         Value = (1-Smooth)*2*((price-MinL)/(MaxH-MinL)-0.5) + Smooth*Value1;     
         Value = MathMin(MathMax(Value,-0.999),0.999); 
         Zero[i] = 0.5*MathLog((1+Value)/(1-Value))+ 0.5*Fish1;
         Value1 = Value;
         Fish1 = Zero[i];
      }
   }*/
   
   // Create history data
   for(i=limit-2; i>0; i--) {
      MaxH = High[Highest(NULL,0,MODE_HIGH,period,i)];
      MinL = Low[Lowest(NULL,0,MODE_LOW,period,i)];
      price = (High[i]+Low[i])/2;
      Value = (1-Smooth)*2*((price-MinL)/(MaxH-MinL)-0.5) + Smooth*Value1;     
      TempValue[i] = MathMin(MathMax(Value,-0.999),0.999); 
         
      Eval = 0.5*MathLog((1+TempValue[i])/(1-TempValue[i]))+ 0.5*Fish;
         
      if (Eval == 0) {
         Zero[i] = Eval;
         AboveZero[i] = NULL;
         BelowZero[i] = NULL;
         Trend[i] = BULB;
      }
      if (Eval > 0) {
         Zero[i] = NULL;
         AboveZero[i] = Eval;
         BelowZero[i] = NULL;
         Trend[i] = BULL;
      }
      if (Eval < 0) {
         Zero[i] = NULL;
         AboveZero[i] = NULL;
         BelowZero[i] = Eval;
         Trend[i] = BEAR;
      }
      
      Value1 = TempValue[i];
      Fish = Eval;
   }
   
   switch (Trend[1]) {
      case BULB:
         prev = Zero[1];
         break;
      case BULL:
         prev = AboveZero[1];
         break;
      case BEAR:
         prev = BelowZero[1];
         break;
      default:
         prev = 0;
         break;
   }
   
   // Constant computation for current candle
   MaxH = High[Highest(NULL,0,MODE_HIGH,period,0)];
   MinL = Low[Lowest(NULL,0,MODE_LOW,period,0)];
   price = (High[0]+Low[0])/2;
   Value = (1-Smooth)*2*((price-MinL)/(MaxH-MinL)-0.5) + Smooth*TempValue[1];
   TempValue[0] = MathMin(MathMax(Value,-0.999),0.999); 
         
   Eval = 0.5*MathLog((1+TempValue[0])/(1-TempValue[0]))+ 0.5*prev;
   
   if (Eval == 0) {
      Zero[0] = Eval;
      AboveZero[0] = NULL;
      BelowZero[0] = NULL;
      Trend[0] = BULB;
   }
   if (Eval > 0) {
      Zero[0] = NULL;
      AboveZero[0] = Eval;
      BelowZero[0] = NULL;
      Trend[0] = BULL;
   }
   if (Eval < 0) {
      Zero[0] = NULL;
      AboveZero[0] = NULL;
      BelowZero[0] = Eval;
      Trend[0] = BEAR;
   }
         
   current = Eval;
   
   if (SoundAlert == true && alertTag != Time[0]) {
      if (current > 0 && current > prev && prev <= 0) {
         Alert("Solar wind on ",Symbol()," crossed above 0.");
         alertTag = Time[0];
      }
      if (current < 0 && current < prev && prev >= 0) {
         Alert("Solar wind on ",Symbol()," crossed below 0.");
         alertTag = Time[0];
      }
   }

   return(0);
}

