
#property copyright "©2009-2010, trading-evolution.com , olegf0x , someBody edition"
#property  link  "http://trading-evolution.com/"
#property indicator_chart_window 




extern double PercentageDistribution = 30;
extern double HistogramVerticalShift = 0;
extern bool ShowDayBack=TRUE;
extern double  DayBack_zoom = 14;

extern bool ShowTwoDayBack=TRUE;
extern double TwoDayBack_zoom = 14;
extern bool ShowWeek=TRUE;
extern double Week_zoom = 14;
extern bool ShowWeekBack=TRUE;
extern double WeekBack_zoom = 14;
extern bool ShowContract=TRUE;
extern double Contract_zoom = 2;

extern color DayBackExt = Lime;
extern color DayBack = MediumSeaGreen;
extern color TwoDayBack = DarkGreen;
extern color TwoDayBackExt = Green;
extern color ThisWeek = SteelBlue;
extern color ThisWeekExt = DodgerBlue;
extern color WeekBack = Navy;
extern color WeekBackExt = Blue;
extern color Contract = Brown;
extern color ContractExt = Red;


// -----------------------------------------------------------------------------------------------------------------------------
string folder = "/evolution-histogram/";
int control;
string currency,currency1;
bool cur=TRUE,cross=FALSE;
double previosday[1][2],prepreviosday[1][2],thisweek[1][2],previosweek[1][2],contract[1][2];
double DayBack_extvolume,TwoDayBack_extvolume,Week_extvolume,WeekBack_extvolume,Contract_extvolume;
int a,b,c,d,e,f,g;
string prefix ="eVOLution_hist\n",date1,date2,date3,date4,date5;
string prevday,prevprevday,week,prevweek;

// -----------------------------------------------------------------------------------------------------------------------------
int init() 
{
 inition();
}
// -----------------------------------------------------------------------------------------------------------------------------
int deinit() 
{ if(UninitializeReason()==1 ||UninitializeReason()==2 || UninitializeReason()==4 ||UninitializeReason()==5)
  {
   deinition();
  } 
} 

// -----------------------------------------------------------------------------------------------------------------------------
int start()
{  
   if(cur==FALSE){Alert("Currency is not supported");return(0);}
   color tempColor;
   int total = ObjectsTotal();
   int count = 0;

   string prefixline = prefix+">";
   for (int i = total - 1; i >= 0; i--)
   {
       string prefix1 = ObjectName(i);
      if (StringFind(prefix1,prefixline) == 0)
      {ObjectDelete(prefix1);count++;}			
	}
      if (PercentageDistribution>0)
      {
      DayBack_extvolume=previosday[ArrayMaximum(previosday)][0]*PercentageDistribution/100;
      TwoDayBack_extvolume=prepreviosday[ArrayMaximum(prepreviosday)][0]*PercentageDistribution/100;
      Week_extvolume=thisweek[ArrayMaximum(thisweek)][0]*PercentageDistribution/100;
      WeekBack_extvolume=previosweek[ArrayMaximum(previosweek)][0]*PercentageDistribution/100;
      Contract_extvolume=contract[ArrayMaximum(contract)][0]*PercentageDistribution/100;
      }
      
  
  
      
   for(a=0;a<ArrayRange(previosday,0);a++)
   {  
      if(ShowDayBack==TRUE )
      {

      if (previosday[a][0]>DayBack_extvolume) tempColor=DayBackExt;
      else tempColor=DayBack;
      drawLine(prefixline+prevday+"\nVol: "+DoubleToStr(previosday[a][0],0),ObjectGet(prefix+"day back ",OBJPROP_TIME1) ,previosday[a][1],ObjectGet(prefix+"day back ",OBJPROP_TIME1)+previosday[a][0]*DayBack_zoom, tempColor);
      
      }
   
   }
   for(a=0;a<ArrayRange(prepreviosday,0);a++)
   {  
      if(ShowTwoDayBack==TRUE )
      {
      if (prepreviosday[a][0]>TwoDayBack_extvolume) tempColor=TwoDayBackExt;
      else tempColor=TwoDayBack;
      drawLine(prefixline+prevprevday+"\nVol: "+DoubleToStr(prepreviosday[a][0],0),ObjectGet(prefix+"two day back",OBJPROP_TIME1) ,prepreviosday[a][1],ObjectGet(prefix+"two day back",OBJPROP_TIME1)+prepreviosday[a][0]*TwoDayBack_zoom, tempColor);
      
      }
   
   }
   for(a=0;a<ArrayRange(thisweek,0);a++)
   {
      if(ShowWeek==TRUE )
      {
      if (thisweek[a][0]>Week_extvolume) tempColor=ThisWeekExt;
      else tempColor=ThisWeek;
      
      drawLine(prefixline+TimeToStr(StrToTime(week)+PERIOD_D1*60*1,TIME_DATE)+"-"+TimeToStr(StrToTime(week)+PERIOD_D1*60*6,TIME_DATE)+"\nVol: "+DoubleToStr(thisweek[a][0],0),ObjectGet(prefix+"this week",OBJPROP_TIME1) ,thisweek[a][1],ObjectGet(prefix+"this week",OBJPROP_TIME1)+thisweek[a][0]*Week_zoom, tempColor);}
   
   }
   for(a=0;a<ArrayRange(previosweek,0);a++)
   {
      if(ShowWeekBack==TRUE )
      {
      if (previosweek[a][0]>WeekBack_extvolume) tempColor=WeekBackExt;
      else tempColor=WeekBack;
      drawLine(prefixline+TimeToStr(StrToTime(prevweek)+PERIOD_D1*60*1,TIME_DATE)+"-"+TimeToStr(StrToTime(prevweek)+PERIOD_D1*60*6,TIME_DATE)+"\nVol: "+DoubleToStr(previosweek[a][0],0),ObjectGet(prefix+"week back",OBJPROP_TIME1) ,previosweek[a][1],ObjectGet(prefix+"week back",OBJPROP_TIME1)+previosweek[a][0]*WeekBack_zoom, tempColor);}
   
   }
   int bars_count=WindowBarsPerChart()-WindowFirstVisibleBar();
   datetime time = Time[0]+Period()*60*bars_count;
   for(a=0;a<ArrayRange(contract,0);a++)
   {  
      if(ShowContract==TRUE )
      {
      
      if (contract[a][0]>Contract_extvolume) tempColor=ContractExt;
      else tempColor=Contract;
      
      drawLine(prefixline+"CONTRACT\nVol: "+DoubleToStr(contract[a][0],0), time ,contract[a][1],time-contract[a][0]*Contract_zoom, tempColor);}
   
   }
   
      
     
      
      
    
}



void drawLine(string name, datetime time1, double price1, datetime time2, color Öâåò)
{  
   ObjectCreate(name, OBJ_TREND, 0, time1, price1+HistogramVerticalShift, time2, price1+HistogramVerticalShift);
	ObjectSet(name, OBJPROP_COLOR, Öâåò);
	ObjectSet(name, OBJPROP_BACK, true);
	ObjectSet(name, OBJPROP_STYLE,0);
   ObjectSet(name, OBJPROP_RAY, false);
   ObjectSet(name, OBJPROP_WIDTH, 3);
   
      
}




int Explode(string str, string levelrun, string& arr[])
{
   int i = 0;
   int po = (StringFind(str, levelrun));
   while(po != -1)
   {  
      if(po == 0) arr[i] = "0"; else arr[i] = StringSubstr(str, 0, po);
      i++;
      str = StringSubstr(str, po+StringLen(levelrun));
      po = StringFind(str, levelrun);
      if(po == -1 || str == "0") break;
   }
   arr[i] = str;

   return(i+1);
}



void inition()
{  
   currency =StringSubstr( Symbol(),0,2);
   currency1 = StringSubstr( Symbol(),0,6);
   if(currency =="6E" || currency1 =="EURUSD")
   {currency = "ec";}
   else if(currency =="6B" || currency1=="GBPUSD")
   {currency = "bp";}
   else if(currency =="ES" || currency1 =="S&P_50")
   {currency = "es";}
   else if(currency=="6J" || currency1=="USDJPY")
   {currency ="j1";}
   else if(currency == "6A" || currency1 == "AUDUSD")
   {currency ="ad";}
   else if(currency == "GC" || currency1 == "GOLD")
   {currency ="gc";}
   else if(currency == "6S" || currency1 =="USDCHF")
   {currency ="e1";}
   else if(currency == "6C" || currency1 =="USDCAD")
   {currency ="c1";}
   else{cur=FALSE;return(0);}
   string temp[10000],asd[4];
   int FileHandler = FileOpen( folder+"futuresvolume-"+currency+".csv", FILE_BIN|FILE_READ);
   if(FileHandler <0){
   if (GetLastError() == 4103/* CANNOT_OPEN_FILE */) Alert("No such file ");
      else Alert("Error opening file ");
      return (0);} 
   a= FileReadArray(FileHandler, temp, 0,10000); 
   if (a<1) {Alert("Error opening file ");return(0);}
   FileClose(FileHandler);
   ArrayResize(temp,a);
   Explode(temp[0],",",asd);
   control = StrToInteger(asd[2]);
   if(GlobalVariableGet(Symbol()+"_"+currency) != control){deinition();}
   
   prevday =TimeToStr( iTime(Symbol(),PERIOD_D1,1) ,TIME_DATE);
   prevprevday=TimeToStr(iTime(Symbol(),PERIOD_D1,2),TIME_DATE);
   week=TimeToStr(iTime(Symbol(),PERIOD_W1,0),TIME_DATE);
   prevweek =TimeToStr(iTime(Symbol(),PERIOD_W1,1),TIME_DATE);
   for(b=1;b<a;b++)
   { 
      Explode(temp[b],",",asd);
      if(StringLen(asd[1])==6)
      {
         if(prevday==("20"+StringSubstr(asd[1],0,2)+"."+StringSubstr(asd[1],2,2)+"."+StringSubstr(asd[1],4,2))&& StrToDouble(asd[3])>0)
         {  
            previosday[c][0]=StrToDouble(asd[2]);
            previosday[c][1]=StrToDouble(asd[3]);
            if(currency1 =="USDJPY")
            {previosday[c][1]=1/previosday[c][1] *100;}
            if(currency1 == "USDCAD")
            {previosday[c][1]=1/previosday[c][1];}
            if(currency1 == "USDCHF")
            {previosday[c][1]=1/previosday[c][1];}            
            
            c++;ArrayResize(previosday,c+1);
            continue;
         }
         if(prevprevday==("20"+StringSubstr(asd[1],0,2)+"."+StringSubstr(asd[1],2,2)+"."+StringSubstr(asd[1],4,2))&&StrToDouble(asd[3])>0)
         {
            prepreviosday[d][0]=StrToDouble(asd[2]);
            prepreviosday[d][1]=StrToDouble(asd[3]);
            if(currency1 =="USDJPY")
            {prepreviosday[d][1]=1/prepreviosday[d][1] *100;}
            if(currency1 == "USDCAD")
            {prepreviosday[d][1]=1/prepreviosday[d][1];}
            if(currency1 == "USDCHF")
            {prepreviosday[d][1]=1/prepreviosday[d][1];}
            
            
            d++;ArrayResize(prepreviosday,d+1);
            continue;
         }
      }
      if(StringLen(asd[1])==13)
      {
         if(week==("20"+StringSubstr(asd[1],0,2)+"."+StringSubstr(asd[1],2,2)+"."+StringSubstr(asd[1],4,2))&&StrToDouble(asd[3])>0)
         {
            thisweek[e][0]=StrToDouble(asd[2]);
            thisweek[e][1]=StrToDouble(asd[3]);
            if(currency1 =="USDJPY")
            {thisweek[e][1]=1/thisweek[e][1] *100;}
            if(currency1 == "USDCAD")
            {thisweek[e][1]=1/thisweek[e][1];}
            if(currency1 == "USDCHF")
            {thisweek[e][1]=1/thisweek[e][1];}
            
           
            e++;ArrayResize(thisweek,e+1);
            continue;
         }
         if(prevweek==("20"+StringSubstr(asd[1],0,2)+"."+StringSubstr(asd[1],2,2)+"."+StringSubstr(asd[1],4,2))&&StrToDouble(asd[3])>0)
         {
            previosweek[f][0]=StrToDouble(asd[2]);
            previosweek[f][1]=StrToDouble(asd[3]);
            if(currency1 =="USDJPY")
            {previosweek[f][1]=1/previosweek[f][1] *100;}
            if(currency1 == "USDCAD")
            {previosweek[f][1]=1/previosweek[f][1];}
            if(currency1 == "USDCHF")
            {previosweek[f][1]=1/previosweek[f][1];}
            
            
            f++;ArrayResize(previosweek,f+1);continue;
         }
      }
      
      if(asd[1]=="contract_period"&& StrToDouble(asd[3])>0)
      {
         contract[g][0]=StrToDouble(asd[2]);
         contract[g][1]=StrToDouble(asd[3]);
         if(currency1 =="USDJPY")
         {contract[g][1]=1/contract[g][1] *100;}
         if(currency1 == "USDCAD")
         {contract[g][1]=1/contract[g][1];}
         if(currency1 == "USDCHF")
         {contract[g][1]=1/contract[g][1];}
            
         
         g++;ArrayResize(contract,g+1);
      }
   }
   
  if(ShowDayBack==TRUE && ObjectFind(prefix+"day back ") == -1){
   string name = prefix+"day back ";     
   ObjectCreate(name,OBJ_VLINE,0,StrToTime(prevday),0);
   ObjectSet(name,OBJPROP_STYLE,1);
   ObjectSet(name,OBJPROP_COLOR,DayBack);}
   if(ShowTwoDayBack==TRUE && ObjectFind(prefix+"two day back")== -1 ){
   name = prefix+"two day back";
   ObjectCreate(name,OBJ_VLINE,0,StrToTime(prevprevday),0);
   ObjectSet(name,OBJPROP_STYLE,1);
   ObjectSet(name,OBJPROP_COLOR,TwoDayBack);}
   if(ShowWeek==TRUE && ObjectFind(prefix+"this week")== -1){
   name = prefix+"this week";
   ObjectCreate(name,OBJ_VLINE,0,StrToTime(week)+PERIOD_D1*60,0);
   ObjectSet(name,OBJPROP_STYLE,1);
   ObjectSet(name,OBJPROP_COLOR,ThisWeek);}
   if(ShowWeekBack==TRUE && ObjectFind(prefix+"week back")== -1){
   name = prefix +"week back";
   ObjectCreate(name,OBJ_VLINE,0,StrToTime(prevweek)+PERIOD_D1*60,0);
   ObjectSet(name,OBJPROP_STYLE,1);
   ObjectSet(name,OBJPROP_COLOR,WeekBack);}
  
 GlobalVariableSet(Symbol()+"_"+currency,control);
} 

void deinition()
{
   GlobalVariableDel(Symbol()+"_"+currency);
   int total = ObjectsTotal();
   int count = 0;
   string prefixline = prefix;
   for (int i = total - 1; i >= 0; i--)
   {
       string prefix1 = ObjectName(i);
      if (StringFind(prefix1,prefixline) == 0)
      {ObjectDelete(prefix1);count++;}			
	}
}
// -----------------------------------------------------------------------------------------------------------------------------

