//+------------------------------------------------------------------+
//|                                   Fisher Transform rsbgm v1a.mq4 |
//|                                              (Histogram Version) |
//|                                                    modified from |
//|                                                                  |
//|                                                   Stochastic.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp. mod by rsbgm"
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_width1 2
#property indicator_width2 2
//---- input parameters
extern int FPeriod=10;
extern string  comment = "Period SMA of FPeriod - set 0 to remove";
extern int SPeriod=3;
//---- buffers
double FishBuffer[];
double SignalBuffer[];
double CrossUp[];
double CrossDown[];
double ValueBuffer[];
//----
int draw_begin1=0;
int draw_begin2=0;
int draw_begin3=0;
double alertTag;
string ver="v1a";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
   SetIndexBuffer(3, FishBuffer);
   SetIndexBuffer(4, ValueBuffer);
//---- indicator lines
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexLabel(0,"SolarWind-UP");
   SetIndexBuffer(1, CrossDown);
   SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexLabel(1,"SolarWind-DN");
   SetIndexBuffer(2, SignalBuffer);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1); SetIndexLabel(2,"Signal");
//---- name for DataWindow and indicator subwindow label
   short_name="Fisher "+ver+" ("+FPeriod+","+SPeriod+")";
   IndicatorShortName(short_name);
//----
   draw_begin1=FPeriod;
   draw_begin2=FPeriod;
   draw_begin3=draw_begin1+SPeriod;
   SetIndexDrawBegin(0,draw_begin1);
   SetIndexDrawBegin(1,draw_begin2);
   SetIndexDrawBegin(2,draw_begin3);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Stochastic oscillator                                            |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k;
   int    counted_bars=IndicatorCounted();
   double price;
//----
   if(Bars<=draw_begin2) return(0);
//---- initial zero
   if(counted_bars<1)
     {
      for(i=1;i<=draw_begin1;i++) FishBuffer[Bars-i]=0;
      for(i=1;i<=draw_begin1;i++) CrossUp[Bars-i]=0;
      for(i=1;i<=draw_begin1;i++) CrossDown[Bars-i]=0;
      for(i=1;i<=draw_begin2;i++) SignalBuffer[Bars-i]=0;
      for(i=1;i<=draw_begin1;i++) ValueBuffer[Bars-i]=0;
     }
//---- Fish Line
   i=Bars-draw_begin1;
   if(counted_bars>draw_begin1) i=Bars-counted_bars-1;
   while(i>=0) {
      double   MaxH = High[Highest(NULL,0,MODE_HIGH,FPeriod,i)];
      double   MinL = Low[Lowest(NULL,0,MODE_LOW,FPeriod,i)];
      price=(High[i]+Low[i])/2;
      ValueBuffer[i]=(1-0.67)*2*((price-MinL)/(MaxH-MinL)-0.5)+0.67*ValueBuffer[i+1];
      if(ValueBuffer[i]>0.9999) ValueBuffer[i]=0.9999;
      if(ValueBuffer[i]<-0.9999) ValueBuffer[i]=-0.9999;
      FishBuffer[i]=0.5*MathLog((1+ValueBuffer[i])/(1-ValueBuffer[i]))+0.5*FishBuffer[i+1];
      
      if(FishBuffer[i]>0) {
         CrossUp[i] = FishBuffer[i];
         CrossDown[i] = NULL;
      }
      if(FishBuffer[i]<0) {
         CrossUp[i] = NULL;
         CrossDown[i] = FishBuffer[i];
      }
      
      i--;
   }
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
//---- Signal Line
   if(SPeriod!=0) {
      for(i=0; i<limit; i++)
         SignalBuffer[i]=iMAOnArray(FishBuffer,Bars,SPeriod,0,MODE_SMA,i);
   }
//---- Alert
   if(FishBuffer[0]>0 && FishBuffer[0]>FishBuffer[1] && FishBuffer[1]<0 && alertTag!=Time[0]) {
      Alert("Solar wind on ",Symbol()," crossed above 0.");
      alertTag = Time[0];
   }
   if(FishBuffer[0]<0 && FishBuffer[0]<FishBuffer[1] && FishBuffer[1]>0 && alertTag!=Time[0]) {
      Alert("Solar wind on ",Symbol()," crossed below 0.");
      alertTag = Time[0];
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+