//+------------------------------------------------------------------+
//|                                              eVOLution-dvoid.mq4 |
//|                                                   2012, someBody |
//|                                    http://trading-evolution.com/ |
//+------------------------------------------------------------------+
#property copyright " 2012, someBody"
#property link      "http://trading-evolution.com/"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 105
#property  indicator_color1  Silver

extern string FileNamePruductCode  = "";
//extern int barsToProcess=100;
extern color barColor = SteelBlue, oiColor=Yellow;
extern bool showOI = true;


string folder = "/evolution-dvoid/",objpref="evo-dvoid_";
int control;
string currency,currency1;
bool cur=TRUE,cross=FALSE;
int a,b,objwidth=4;
double tempvol,tempoi,volmax,oimax;

string ddate;
double volarr[], oiarr[];

bool initFinished=false;
color back_ground;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  

initFinished=false;

//----

   return(0);
}
  
  
void drawRectangle(string name, datetime time1, double price1, datetime time2, int counter)
{  
int windowIndex;
string oiname;

  
   windowIndex=WindowFind("trading-evolution.com : eVOLution-dvoid");
   
  
   
   oiname=objpref+"_oi_"+name;
   name=objpref+"_vol_"+name;
   
   if (Period()<1440)
   ObjectCreate(name, OBJ_RECTANGLE, windowIndex, time1, price1, time2, volarr[counter]*100/volmax);
   else
   ObjectCreate(name, OBJ_TREND, windowIndex, time1, price1, time1, volarr[counter]*100/volmax);
   
	ObjectSet(name, OBJPROP_COLOR, barColor);
	ObjectSet(name, OBJPROP_BACK, true);
	ObjectSet(name, OBJPROP_RAY, false);
	ObjectSet(name, OBJPROP_WIDTH,objwidth);
	ObjectSetText(name, "Volume="+DoubleToStr(volarr[counter],0));
	
	if (showOI)
	{
	  if (counter>1)
	  ObjectCreate(oiname,OBJ_TREND,windowIndex,time1-10,oiarr[counter-1]*100/oimax,time2,oiarr[counter]*100/oimax);
	  ObjectSet(oiname, OBJPROP_COLOR, oiColor);
	  ObjectSet(oiname, OBJPROP_BACK, true);
	  ObjectSet(oiname, OBJPROP_RAY, false);
	  ObjectSet(oiname, OBJPROP_WIDTH,2);
	  ObjectSetText(oiname, "OI="+DoubleToStr(oiarr[counter],0));
	}
	

}
   
   

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);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   deinition();
//----
   return(0);
  }

void deinition()
{

int total = ObjectsTotal();
   int count = 0;
   string prefixline = objpref;
   for (int i = total - 1; i >= 0; i--)
   {
      string prefix1 = ObjectName(i);
      if (StringFind(prefix1,prefixline) == 0)
      {ObjectDelete(prefix1);count++;}
	}
	   
}

void drawonce()
{

   int    counted_bars=IndicatorCounted();
//----
   
   if (!initFinished)
   {
   
   
   IndicatorShortName("trading-evolution.com : eVOLution-dvoid");
   
   
   
   if(cur==FALSE){Alert("Currency is not supported");return(0);}
   color tempColor;
   int total = ObjectsTotal();
   int count = 0;
      
   //product list from database: -EC -J1 -AD -BP -C1 -RY -NE -E1 -YM -NQ -ES -ZW -ZS -ZC -CL -SI -GC
   if (FileNamePruductCode =="") {   
   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 == "NQ" || currency1 == "Nasdaq")
   {currency ="nq";}
   else if(currency == "YM" || currency1 == "DowJon")
   {currency ="ym";}
   else if(currency=="J1" || currency1=="USDJPY")
   {currency ="j1";}
   else if(currency == "AD" || currency1 == "AUDUSD")
   {currency ="ad";}
   else if(currency == "GC" || currency1 == "XAGUSD")
   {currency ="gc";}
   else if(currency == "SI" || currency1 == "XAUUSD")
   {currency ="si";}
   else if(currency == "CL" || currency1 == "_CL")
   {currency ="cl";}
   else if(currency == "6S" || currency1 =="USDCHF")
   {currency ="e1";}
   else if(currency == "6C" || currency1 =="USDCAD")
   {currency ="c1";}
   else if(currency == "6N" || currency1 =="NZDUSD")
   {currency ="ne";}
   else if(currency == "6R" || currency1 =="EURJPY")
   {currency ="ry";}
   else if(currency == "ZS" || currency1 =="_ZS")
   {currency ="zs";}
   else if(currency == "ZC" || currency1 =="_ZC")
   {currency ="zc";}
   else if(currency == "ZW" || currency1 =="_ZW")
   {currency ="zw";}
   else{cur=FALSE;return(0);}
   
   }
   else currency=FileNamePruductCode ;
   
   
   string temp[10000],asd[4];
   int FileHandler = FileOpen( folder+"dvoid-"+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);
   ArrayResize(volarr,a);
   ArrayResize(oiarr,a);

   if(GlobalVariableGet(Symbol()+"_"+currency) != control){deinition();}
   
   for(b=1;b<a;b++)
   { 
      Explode(temp[b],",",asd);
      volarr[b]=StrToInteger(asd[1]);
      oiarr[b]=StrToInteger(asd[2]);
      
   }
   
   //getting maximum value from array
   volmax=volarr[(ArrayMaximum(volarr))];
   oimax=oiarr[(ArrayMaximum(oiarr))];
   
   for(b=1;b<a;b++)
   { 
      Explode(temp[b],",",asd);
      ddate=StringSubstr(asd[0],0,4)+"."+StringSubstr(asd[0],4,2)+"."+StringSubstr(asd[0],6,2);
      drawRectangle(ddate,StrToTime(ddate+" 00:00"),0,StrToTime(ddate+" 23:59"),b);
   }

   
//----
   initFinished=true;
   WindowRedraw();
   }


}



//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   drawonce();
   WindowRedraw();
   return(0);
  }
//+------------------------------------------------------------------+