//+------------------------------------------------------------------+
//|                                               Auction Market.mq4 |
//|                                           Author: Jamie Rancourt |
//|                                                                  |
//|                                                                  |
//| Auction Market is implemented based on the Auction Market Theory |
//| and Market Profile concepts.                                     |
//|                                                                  |
//| History:                                                         |
//|      1.0: 02.10.2011 - initial test version, IB and Double IB    |
//|                        lines drawn per session                   |
//+------------------------------------------------------------------+
//
// Notes:
// - IB and DIB lines will update the hour after the indicated session start time


#property indicator_chart_window
#property indicator_buffers 7

#define PREFIX "AMT-"

//---- input parameters
extern int       LondonStartHour=8;
extern int       USStartHour=13;
extern int       AsiaStartHour=0;
extern int       BarsBack = 500;
extern int       DaysBack = 14;
extern bool      ShowSessionIB=true;
extern color     TextColor=Gold;
extern color     IBHColor=Tomato;
extern color     IBLColor=DodgerBlue;
//extern color     DPOVvColor=Pink;
//extern color     DVAHColor=Red;
//extern color     DVALColor=Blue;
//---- buffers
double IBL[];
double IBH[];
double DIBL[];
double DIBH[];
double DPOCv[];
double DVAH[];
double DVAL[];
//variables
int barsToInclude;
double currAsiaIB=0,sumAsiaIB=0,countAsiaIB=0;
double currLoIB=0,sumLoIB=0,countLoIB=0;
double currUsIB=0,sumUsIB=0,countUsIB=0;
//int day;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,DPOCv);
   SetIndexLabel(0,"DPOCv");
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,DVAH);
   SetIndexLabel(1,"DVAH");
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,DVAL);
   SetIndexLabel(2,"DVAL");
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,IBL);
   SetIndexLabel(3,"IBL");
   SetIndexStyle(3,0,STYLE_SOLID,1,IBLColor);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,DIBL);
   SetIndexLabel(4,"DIBL");
   SetIndexStyle(4,0,STYLE_DOT,1,IBLColor);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,IBH);
   SetIndexLabel(5,"IBH");
   SetIndexStyle(5,0,STYLE_SOLID,1,IBHColor);
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,DIBH);
   SetIndexLabel(6,"DIBH");
   SetIndexStyle(6,0,STYLE_DOT,1,IBHColor);
   barsToInclude = DaysBack * (1440/(Period()*1.0));
   ObjectCreate(PREFIX+"IBH",OBJ_TEXT,0,0,0);
   ObjectCreate(PREFIX+"DIBH",OBJ_TEXT,0,0,0);
   ObjectCreate(PREFIX+"IBL",OBJ_TEXT,0,0,0);
   ObjectCreate(PREFIX+"DIBL",OBJ_TEXT,0,0,0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   DeleteObjectsByPrefix(PREFIX);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   int lastbar;
   if(Period() > 60) return(-1);
   if (counted_bars>0) counted_bars--;
   //day=-1;
   lastbar = MathMin(barsToInclude, Bars-counted_bars);
   IBLines(lastbar);	
   printInfo();

//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//text labels
void moveTextLabel(string name, double price) {
   int sPerBar = 60*Period();
   ObjectMove(PREFIX+name,0,Time[0]+5*sPerBar,price);
   ObjectSetText(PREFIX+name,name+"  "+DoubleToStr(price,Digits),8,"Arial",TextColor);
}

//Draw Initial Balance Lines
int IBLines(int lastbar) {
   int shift;
	for(shift=lastbar;shift>=0;shift--) {
     if(TimeHour(Time[shift]) == AsiaStartHour+1 && TimeMinute(Time[shift]) == 0) {
       IBH[shift]= High[iHighest(NULL,0,MODE_HIGH,60/Period(),shift+1)];  
       IBL[shift] = Low[iLowest(NULL,0,MODE_LOW,60/Period(),shift+1)]; 
       currAsiaIB = IBH[shift]-IBL[shift];
       sumAsiaIB += currAsiaIB;
       countAsiaIB++;
       DIBL[shift] = IBL[shift] - currAsiaIB;
       DIBH[shift] = IBH[shift] + currAsiaIB;
       
     }
     else if(TimeHour(Time[shift]) == LondonStartHour+1 && TimeMinute(Time[shift]) == 0) {
       IBH[shift]= High[iHighest(NULL,0,MODE_HIGH,60/Period(),shift+1)];  
       IBL[shift] = Low[iLowest(NULL,0,MODE_LOW,60/Period(),shift+1)]; 
       currLoIB = IBH[shift] - IBL[shift];
       sumLoIB += currLoIB;
       countLoIB++;
       DIBL[shift] = IBL[shift] - currLoIB;
       DIBH[shift] = IBH[shift] + currLoIB;
     }
     else if(TimeHour(Time[shift]) == USStartHour+1 && TimeMinute(Time[shift]) == 0) {      
       IBH[shift]= High[iHighest(NULL,0,MODE_HIGH,60/Period(),shift+1)];  
       IBL[shift] = Low[iLowest(NULL,0,MODE_LOW,60/Period(),shift+1)]; 
       currUsIB = IBH[shift] - IBL[shift];
       sumUsIB += currUsIB;
       countUsIB++;
       DIBL[shift] = IBL[shift] - currUsIB;
       DIBH[shift] = IBH[shift] + currUsIB;
     }
     else{
       IBH[shift]= IBH[shift+1];
       IBL[shift] = IBL[shift+1];
       DIBL[shift] = DIBL[shift+1];
       DIBH[shift] = DIBH[shift+1];
     }
     moveTextLabel("IBH",IBH[shift]);
     moveTextLabel("DIBH",DIBH[shift]);
     moveTextLabel("IBL",IBL[shift]);
     moveTextLabel("DIBL",DIBL[shift]);
   }
   return(0);
}

// Delete all objects with given prefix
void DeleteObjectsByPrefix(string Prefix) {
   int L = StringLen(Prefix);
   int i = 0; 
   while(i < ObjectsTotal()) {
       string ObjName = ObjectName(i);
       if(StringSubstr(ObjName, 0, L) != Prefix) { 
           i++; 
           continue;
       }
       ObjectDelete(ObjName);
   }
}

void printInfo() {
   int y = 10;
   double asiaDiff = ((currAsiaIB/(sumAsiaIB/countAsiaIB))-1)*100;
   y=createTextObject("IBtitle","","Session Initial Balances",y);
   y=createTextObject("desc","","Last      Avg     %diff",y);
   y=createTextObject("asia","","Asia: " +DoubleToStr(currAsiaIB/Point/10,1)+"    "+
                        DoubleToStr(sumAsiaIB/countAsiaIB/Point/10,1)+"    "+
                        DoubleToStr(((currAsiaIB/(sumAsiaIB/countAsiaIB))-1)*100,1),y);
   y=createTextObject("london","","London: " +DoubleToStr(currLoIB/Point/10,1)+"    "+
                        DoubleToStr(sumLoIB/countLoIB/Point/10,1)+"    "+
                        DoubleToStr(((currLoIB/(sumLoIB/countLoIB))-1)*100,1),y);
   y=createTextObject("us","","US: "+DoubleToStr(currUsIB/Point/10,1)+"    "+
                        DoubleToStr(sumUsIB/countUsIB/Point/10,1)+"    "+
                        DoubleToStr(((currUsIB/(sumUsIB/countUsIB))-1)*100,1),y);
   
}

int createTextObject(string label, string name, string value,int y,color col=NULL) {
   if(col == NULL) col = TextColor;
   ObjectCreate(label, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(label,name, 8, "Arial", col);
   ObjectSet(label, OBJPROP_CORNER, 1);
   ObjectSet(label, OBJPROP_XDISTANCE, 60);
   ObjectSet(label, OBJPROP_YDISTANCE, y); 
   
   ObjectCreate(label+"_1", OBJ_LABEL, 0, 0, 0);
   ObjectSetText(label+"_1",value, 8, "Arial", col);
   ObjectSet(label+"_1", OBJPROP_CORNER, 1);
   ObjectSet(label+"_1", OBJPROP_XDISTANCE, 10);
   ObjectSet(label+"_1", OBJPROP_YDISTANCE, y);  
   return(y+11);
}



