//+--------------------------------------------------------------------------------+
//|                Float.mq4                                                       |
//|                Copyright � 2005  Barry Stander  Barry_Stander_4@yahoo.com      |
//|                http://www.4Africa.net/4meta/                                   |
//|                Float                                                           |
//+--------------------------------------------------------------------------------+

#property copyright "Float converted from MT3 to MT4"
#property link      "http://www.4Africa.net/4meta/"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red

extern int float=200,use_fibos=1,Backtesting=0,from = 0;
string short_name;

double f,c1,high_bar,Low_bar;
int bars_high,bars_low;
double cumulativeV,FLOATV,cumulativeV2,swing;
double newcv,CV,CV2;
double fib23,fib38,fib50,fib62,fib76;
double dinap0,dinap1,dinap2,dinap3,dinap4,dinap5;
double CVL,CVL1,CVL2,CVL3,CVL4;
double Buffer1[];
double Buffer2[];

int shift,swing_time;
int cvstart,cvend,bar;

void delete_objects(){
   string  buff_str = "";
   for(int i=ObjectsTotal()-1;i>=0;i--){
      buff_str = ObjectName(i);
      if(StringFind(buff_str,"float_",0)==0) ObjectDelete(buff_str);
      }
   Comment("");
}

int init(){
   IndicatorBuffers(2);
   delete_objects();
      
   short_name="Float";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   
   SetIndexStyle(0,DRAW_HISTOGRAM );
   SetIndexBuffer(0,Buffer1);
   SetIndexDrawBegin(0,Buffer1);
   
   SetIndexStyle(1,DRAW_LINE );
   SetIndexBuffer(1,Buffer2);
   SetIndexDrawBegin(1,Buffer2);

   return(0);
}
 
int deinit(){
   delete_objects();
   return(0);
}
  
void create_line(int from,double p1,int to,double p2,color c,int style,int w = 1){
   static int acc = 0;
   string name = "float_line_"+acc;acc++;
   datetime t0 = Time[from];
   if(from<0) t0 = Time[0]-from*Period()*60;
   datetime t1 = Time[to];
   if(to<0) t1 = Time[0]-to*Period()*60;
   
   ObjectCreate(name, OBJ_TREND  , 0, t0,p1,t1,p2);
   ObjectSet(name , OBJPROP_STYLE, style);
   ObjectSet(name , OBJPROP_COLOR , c );
   ObjectSet(name , OBJPROP_RAY , 0  );
   ObjectSet(name , OBJPROP_WIDTH , w );
}

void place_text(string text,int from,double p,color c){
   static int acc = 0;
   string name = "float_text_"+acc;acc++;
   ObjectCreate(name, OBJ_TEXT  , 0, Time[from],p );
   ObjectSetText(name , text, 8 , "Arial", c);
}

int start(){
   static int last = 0;
   if(last==Bars) return (0);
   last = Bars;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1); // Exit if na data
   
   delete_objects();
    
   cumulativeV=0;
   cumulativeV2=0;
      
      FLOATV=0;

         bars_high = Highest(NULL,0,MODE_HIGH,float,from+1);
         bars_low  = Lowest(NULL,0,MODE_LOW,float,from+1);
         high_bar = High[bars_high]; 
         Low_bar  =  Low[bars_low]; 
         swing = high_bar - Low_bar;
         swing_time = MathAbs(bars_low-bars_high);
         if( bars_high < bars_low ){
            cvstart=bars_low;
            cvend=bars_high;
            }
         else{
            cvstart=bars_high;
            cvend=bars_low;
            }

         for( shift=cvstart;shift>=cvend;shift--) FLOATV=FLOATV+Volume[shift];
         
         //find cumulative volume since last turnover
         for( shift=cvstart;shift>=from;shift--){
            cumulativeV=cumulativeV+Volume[shift];
            if( cumulativeV >= FLOATV ) cumulativeV=0;

            Buffer1[shift] = cumulativeV*0.001; //Blue
            Buffer2[shift] = FLOATV*0.001;      //Red
            }
         create_line(cvstart,high_bar,from+1,high_bar,Blue,STYLE_SOLID);            
         create_line(cvstart,Low_bar,from+1,Low_bar,Blue,STYLE_SOLID);            

         fib23=((high_bar-Low_bar)*0.236)+Low_bar;
         fib38=((high_bar-Low_bar)*0.382)+Low_bar;
         fib50=((high_bar-Low_bar)/2)+Low_bar;
         fib62=((high_bar-Low_bar)*0.618)+Low_bar;
         fib76=((high_bar-Low_bar)*0.764)+Low_bar;
         dinap0=(Low_bar+fib23)/2;
         dinap1=(fib23+fib38)/2;
         dinap2=(fib38+fib50)/2;
         dinap3=(fib50+fib62)/2;
         dinap4=(fib62+fib76)/2;
         dinap5=(high_bar+fib76)/2;

         create_line(cvstart,fib23,from+1,fib23,Green,STYLE_DASH);
         place_text("23.6",from+1,fib23,Green);

         create_line(cvstart,fib38,from+1,fib38,Green,STYLE_DASH);
         place_text("38.2",from+1,fib38,Green);
 
         create_line(cvstart,fib50,from+1,fib50,Red,STYLE_SOLID,2);
         place_text("50",from+1,fib50,Red);
 
         create_line(cvstart,fib62,from+1,fib62,Green,STYLE_DASH);
         place_text("61.8",from+1,fib62,Green);
 
         create_line(cvstart,fib76,from+1,fib76,Green,STYLE_DASH);
         place_text("76.4",from+1,fib76,Green);

         create_line(cvstart,dinap0,from+1,dinap0,Red,STYLE_DOT);
         create_line(cvstart,dinap1,from+1,dinap1,Red,STYLE_DOT);
         create_line(cvstart,dinap2,from+1,dinap2,Red,STYLE_DOT);
         create_line(cvstart,dinap3,from+1,dinap3,Red,STYLE_DOT);
         create_line(cvstart,dinap4,from+1,dinap4,Red,STYLE_DOT);
         create_line(cvstart,dinap5,from+1,dinap5,Red,STYLE_DOT);
 
         create_line(cvstart,high_bar,cvstart,Low_bar*Point,Blue,STYLE_SOLID);
         create_line(cvend,high_bar,cvend,Low_bar*Point,Blue,STYLE_SOLID);

   
   //vert float predictions. These are only time based.
   //see blue histogram for real float values.
   //if you change "trendline" to "Vline" it will draw through oscillators too.might be fun
   for(int i=1;i<10;i++){
      int x = cvend-(swing_time*i);
      create_line(x+5,high_bar,x+5,Low_bar,Red,STYLE_DOT);
      }
   Comment(
      "\n","high was   ",bars_high,"  bars ago",
      "\n","Low was    ",bars_low," bars ago","\n",
      "\n","Float time was  =      ", swing_time," bars",
      "\n","Float Vol. left    =     ",FLOATV-cumulativeV,
      "\n","Float Volume    =     ",FLOATV,
      );
 
   return(0);
}