//+------------------------------------------------------------------+
//|                                             eVOLution-levels.mq4 |
//|                Copyright © 2009-2010, someBody, trading-evolution.com |
//|                                 http://www.trading-evolution.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009-2010, someBody, trading-evolution.com"
#property link      "http://www.trading-evolution.com"

#property indicator_chart_window

#import

extern bool soundAlert = False;
extern int NumberOfLevelsToShow = 3;
extern string CustomLevels1="";
extern string CustomLevels2="";
extern string CustomSymbol = "";
extern bool ShowPreviousDayVolume = True;
extern bool ShowDayBeforePreviousDayVolume = True;
extern bool ShowThisWeekVolume = True;
extern bool ShowPreviousWeekVolume = True;
extern bool ShowContractVolume = True;
extern color PreviousDayVolumeColor = LimeGreen;
extern color DayBeforePreviousDayVolumeColor = Green;
extern color ThisWeekVolumeColor = DodgerBlue;
extern color PrevousWeekVolumeColor = Blue;
extern color ContractVolumeColor = Red;
extern color CustomLevel1VolumeColor = PeachPuff;
extern color CustomLevel2VolumeColor = LightSteelBlue;
extern int FontSize = 10;
extern int textXshift = 10;
extern int textYshift = 10;
extern int FirstLevelWidth = 1;
extern int SecondLevelWidth = 1;
extern int ThirdLevelWidth = 1;
extern int FirstLevelStyle = 0;
extern int SecondLevelStyle = 4;
extern int ThirdLevelStyle = 4;
extern double LevelsVerticalShift = 0;
string varray[50],FileSymbol;
int clevel1count,clevel2count;


//---- input parameters
int          corner=0;




double ExtMapBuffer1[];
  
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   
   int top=FontSize*7+textYshift+15;
   int left = 90;

//---- evolution levels
   string filename,
   buff_str = "";
   int shft,
   handle,
   height,
   pointer;
   shft=10;
   
   FileSymbol=Symbol();
   if(StringLen(CustomSymbol)>0) FileSymbol = CustomSymbol;
      filename = "/evolution-levels/evolution-levels-"+FileSymbol+".csv";

//   filename = "/evolution-levels/evolution-levels-"+Symbol()+".csv";
   handle=FileOpen(filename, FILE_CSV|FILE_READ);
    if(handle<0)                        
     {
      if(GetLastError()==4103)         
         Alert("No such file ");
      else                            
         Alert("Error opening file ");
      return(0);                      
     }
   int i=0;
     while ( !FileIsEnding(handle)) 
      {
      varray[i] = FileReadString(handle);
      i++;
      }
   FileClose( handle );   

//---------------------------------------------------------------

if (ShowPreviousDayVolume){
   
   if (NumberOfLevelsToShow>0){
    
      drawLine("pd1",varray[4] , varray[1], StrToDouble(varray[5]), PreviousDayVolumeColor,0,0,FirstLevelWidth);
      if (NumberOfLevelsToShow>1) {
         drawLine("pd2",varray[6] , varray[1], StrToDouble(varray[7]), PreviousDayVolumeColor,4,0,SecondLevelWidth);
      }
     
      if (NumberOfLevelsToShow>2) {  
         drawLine("pd3",varray[8] , varray[1], StrToDouble(varray[9]), PreviousDayVolumeColor,4,0,ThirdLevelWidth);
      }
   }
   string spd;
   spd = "spd";
   ObjectCreate(spd, OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet(spd, OBJPROP_XDISTANCE, textXshift);
   ObjectSet(spd, OBJPROP_YDISTANCE, FontSize*2+textYshift);
   ObjectSetText(spd, "Total volume for "+varray[1]+" is: ______ "+StrToInteger(varray[2]), FontSize, "Arial", PreviousDayVolumeColor);
}

//-------- Day Bedore Previous Day

if (ShowDayBeforePreviousDayVolume){ 
   if (NumberOfLevelsToShow>0){
    
      drawLine("dbpd1",varray[42] , varray[39], StrToDouble(varray[43]), DayBeforePreviousDayVolumeColor,FirstLevelStyle,0,FirstLevelWidth);
      if (NumberOfLevelsToShow>1) {
         drawLine("dbpd2",varray[44] , varray[39], StrToDouble(varray[45]), DayBeforePreviousDayVolumeColor,SecondLevelStyle,0,SecondLevelWidth);
      }
     
      if (NumberOfLevelsToShow>2) {  
         drawLine("dbpd3",varray[46] , varray[39], StrToDouble(varray[47]), DayBeforePreviousDayVolumeColor,ThirdLevelStyle,0,ThirdLevelWidth);
      }
   }

   string sdbpd;
      sdbpd = "sdbpd";
      ObjectCreate(sdbpd, OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet(sdbpd, OBJPROP_XDISTANCE, textXshift);
      ObjectSet(sdbpd, OBJPROP_YDISTANCE, FontSize*3+textYshift+3);
      ObjectSetText(sdbpd, "Total volume for "+varray[39]+" is: ______ "+StrToInteger(varray[40]), FontSize, "Arial", DayBeforePreviousDayVolumeColor);

}

    
            
//----                                   ThisWeekVolume

if (ShowThisWeekVolume){

   if (NumberOfLevelsToShow>0){
    
      drawLine("tw1",varray[13] , varray[10], StrToDouble(varray[14]), ThisWeekVolumeColor,FirstLevelStyle,1,FirstLevelWidth);
      if (NumberOfLevelsToShow>1) {
         drawLine("tw2",varray[15] , varray[10], StrToDouble(varray[16]), ThisWeekVolumeColor,SecondLevelStyle,1,SecondLevelWidth);
      }
     
      if (NumberOfLevelsToShow>2) {  
         drawLine("tw3",varray[17] , varray[10], StrToDouble(varray[18]), ThisWeekVolumeColor,ThirdLevelStyle,1,ThirdLevelWidth);
      }
   }



  string stw;
      stw = "stw";
      ObjectCreate(stw, OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet(stw, OBJPROP_XDISTANCE, textXshift);
      ObjectSet(stw, OBJPROP_YDISTANCE, FontSize*4+textYshift+6);
      ObjectSetText(stw, "Total volume for "+varray[10]+" period is: ______ "+StrToInteger(varray[11]), FontSize, "Arial", ThisWeekVolumeColor);
    }
 
//----                                   PreviousWeekVolume
 
 if (ShowPreviousWeekVolume) {
 
 
 
    if (NumberOfLevelsToShow>0){
    
      drawLine("pw1",varray[24] , varray[21], StrToDouble(varray[25]), PrevousWeekVolumeColor,FirstLevelStyle,1,FirstLevelWidth);
      if (NumberOfLevelsToShow>1) {
         drawLine("pw2",varray[26] , varray[21], StrToDouble(varray[27]), PrevousWeekVolumeColor,SecondLevelStyle,1,SecondLevelWidth);
      }
     
      if (NumberOfLevelsToShow>2) {  
         drawLine("pw3",varray[28] , varray[21], StrToDouble(varray[29]), PrevousWeekVolumeColor,ThirdLevelStyle,1,ThirdLevelWidth);
      }
   }
   
 
  string spw;
      spw = "spw";
      ObjectCreate(spw, OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet(spw, OBJPROP_XDISTANCE, textXshift);
      ObjectSet(spw, OBJPROP_YDISTANCE, FontSize*5+textYshift+9);
      ObjectSetText(spw, "Total volume for "+varray[21]+" period is: ______ "+StrToInteger(varray[22]), FontSize, "Arial", PrevousWeekVolumeColor);

}

 //----                                   ContractVolume
 if (ShowContractVolume){
 
 
     if (NumberOfLevelsToShow>0){
    
      drawHLine("cv1",varray[33] , "090701", StrToDouble(varray[34]), ContractVolumeColor,FirstLevelStyle,FirstLevelWidth);
      if (NumberOfLevelsToShow>1) {
         drawHLine("cv2",varray[35] , "090701", StrToDouble(varray[36]), ContractVolumeColor,SecondLevelStyle,SecondLevelWidth);
      }
     
      if (NumberOfLevelsToShow>2) {  
         drawHLine("cv3",varray[37] , "090701", StrToDouble(varray[38]), ContractVolumeColor,ThirdLevelStyle,ThirdLevelWidth);
      }
   }

   string sc;
      sc = "sc";
      ObjectCreate(sc, OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet(sc, OBJPROP_XDISTANCE, textXshift);
      ObjectSet(sc, OBJPROP_YDISTANCE, FontSize*6+textYshift+12);
      ObjectSetText(sc, "Total volume for contract period is: ______ "+StrToInteger(varray[31])+"", FontSize, "Arial", ContractVolumeColor);
}





//-------- Custom Levels 1
if (StringLen(CustomLevels1)>2){
CustomLevels1=StringTrimLeft(CustomLevels1);
CustomLevels1=StringTrimRight(CustomLevels1);
pointer=0;
clevel1count=1;
for (i=1;i<=StringLen(CustomLevels1);i++){

   if (StringGetChar(CustomLevels1,i)==59){
   string ctemplevel1=StringSubstr(CustomLevels1,pointer,i-pointer);
    if (ObjectFind("clevel1"+clevel1count) != 0) 
     {
      ObjectCreate("clevel1"+clevel1count,OBJ_HLINE,0,Time[0],StrToDouble(ctemplevel1));
      ObjectSetText("clevel1"+clevel1count,"Vol",10,"Arial", CustomLevel1VolumeColor);
      ObjectSet("clevel1"+clevel1count,OBJPROP_COLOR,CustomLevel1VolumeColor);
      ObjectSet("clevel1"+clevel1count,OBJPROP_WIDTH,1);
      ObjectSet("clevel1"+clevel1count,OBJPROP_STYLE,STYLE_SOLID);
     }
     else
     {
     ObjectMove("clevel1"+clevel1count, 0,Time[0],StrToDouble(ctemplevel1));
     }  
   clevel1count=clevel1count+1;
   pointer=i+1;
   }
   
}
  
}



//-------- Custom Levels 2
if (StringLen(CustomLevels2)>2){
CustomLevels2=StringTrimLeft(CustomLevels2);
CustomLevels2=StringTrimRight(CustomLevels2);
pointer=0;
clevel2count=1;
for (i=1;i<=StringLen(CustomLevels2);i++){

   if (StringGetChar(CustomLevels2,i)==59){
   string ctemplevel2=StringSubstr(CustomLevels2,pointer,i-pointer);
    if (ObjectFind("clevel2"+clevel2count) != 0) 
     {
      ObjectCreate("clevel2"+clevel2count,OBJ_HLINE,0,Time[0],StrToDouble(ctemplevel2));
      ObjectSetText("clevel2"+clevel2count,"Vol",10,"Arial", CustomLevel2VolumeColor);
      ObjectSet("clevel2"+clevel2count,OBJPROP_COLOR,CustomLevel2VolumeColor);
      ObjectSet("clevel2"+clevel2count,OBJPROP_WIDTH,1);
      ObjectSet("clevel2"+clevel2count,OBJPROP_STYLE,STYLE_SOLID);
     }
     else
     {
     ObjectMove("clevel2"+clevel2count, 0,Time[0],StrToDouble(ctemplevel2));
     }  
   clevel2count=clevel2count+1;
   pointer=i+1;
   }
   
}
  
}


 //----    
      ObjectCreate("header", OBJ_LABEL, 0, 0, 0, 0, 0);
      ObjectSet("header", OBJPROP_XDISTANCE, textXshift);
      ObjectSet("header", OBJPROP_YDISTANCE, textYshift);
      ObjectSetText("header", ":: trading-evolution.com ::", 10, "Arial", PreviousDayVolumeColor);


//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
int i;
  
   ObjectDelete("pdf1");
      
   ObjectDelete("pd1");
   ObjectDelete("pd2");
   ObjectDelete("pd3");

   ObjectDelete("dbpd1");
   ObjectDelete("dbpd2");
   ObjectDelete("dbpd3");
   
   ObjectDelete("tw1");
   ObjectDelete("tw2");
   ObjectDelete("tw3");

   ObjectDelete("pw1");
   ObjectDelete("pw2");
   ObjectDelete("pw3");

   ObjectDelete("cv1");
   ObjectDelete("cv2");
   ObjectDelete("cv3");
  
   ObjectDelete("spd");
   ObjectDelete("sdbpd");
   ObjectDelete("stw");
   ObjectDelete("spw");
   ObjectDelete("sc");
   ObjectDelete("header");
   
 for (i=1;i<=clevel1count;i++)
 if (ObjectFind("clevel1"+i) == 0){
      ObjectDelete("clevel1"+i);

 }
 
for (i=1;i<=clevel2count;i++)
 if (ObjectFind("clevel2"+i) == 0){
 ObjectDelete("clevel2"+i);
 } 
 
   ObjectDelete( "nyl" );
   ObjectDelete( "nyt" );
    
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----



//----------------- evolution

  if (soundAlert==True){
   if ((Ask == StrToDouble(varray[5])) || (Ask == StrToDouble(varray[7])) || (Ask == StrToDouble(varray[9])) || (Ask == StrToDouble(varray[14])) || (Ask == StrToDouble(varray[16])) || (Ask == StrToDouble(varray[18])) || (Ask == StrToDouble(varray[25]))|| (Ask == StrToDouble(varray[27]))|| (Ask == StrToDouble(varray[29])) || (Ask == StrToDouble(varray[34])) || (Ask == StrToDouble(varray[36])) || (Ask == StrToDouble(varray[38])))
   {
      PlaySound("alert.wav");
   }
   }

   return(0);
   
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+


//-----


void drawLine(string name, string vol, string mydate, double price1, color col, int style, bool rayflag, int width)
{  

string time1="20"+StringSubstr(mydate, 0, 2)+"."+StringSubstr(mydate, 2, 2)+"."+StringSubstr(mydate, 4, 2)+" 00:00";
string time2=TimeToStr(StrToTime("20"+StringSubstr(mydate, 0, 2)+"."+StringSubstr(mydate, 2, 2)+"."+StringSubstr(mydate, 4, 2)+" 00:00")+266400);
if (TimeDayOfWeek(StrToTime(time2))==1) time2=TimeToStr(StrToTime(time2)+172800);
else if (TimeDayOfWeek(StrToTime(time2))==0) time2=TimeToStr(StrToTime(time2)+172800);



    if (ObjectFind(name) != 0) {
     ObjectDelete(name);
    } 
   //Print(time1);
   ObjectCreate(name, OBJ_TREND, 0, StrToTime(time1), price1+LevelsVerticalShift, StrToTime(time2), price1+LevelsVerticalShift);
	ObjectSet(name, OBJPROP_COLOR, col);
	ObjectSet(name, OBJPROP_BACK, false);
	ObjectSet(name, OBJPROP_STYLE,style);
   ObjectSet(name, OBJPROP_RAY, rayflag);
   ObjectSet(name, OBJPROP_WIDTH, width);
   ObjectSetText(name,"Vol="+vol,10);
 
           
}

void drawHLine(string name, string vol, string mydate, double price1, color col, int style, int width)
{  

string time1="20"+StringSubstr(mydate, 0, 2)+"."+StringSubstr(mydate, 2, 2)+"."+StringSubstr(mydate, 4, 2)+" 00:00";

    if (ObjectFind(name) != 0) {
     ObjectDelete(name);
    } 

   ObjectCreate(name, OBJ_HLINE, 0, StrToTime(time1), price1+LevelsVerticalShift);
	ObjectSet(name, OBJPROP_COLOR, col);
	ObjectSet(name, OBJPROP_BACK, false);
	ObjectSet(name, OBJPROP_STYLE,style);
   ObjectSet(name, OBJPROP_WIDTH, width);
   ObjectSetText(name,"Vol="+vol,10);
 

           
}

