//+------------------------------------------------------------------+
//|                     coded by Propaganda                          |
//|                     code from MACD example and i-Session         |
//|                     the rest is my own                           |
//+------------------------------------------------------------------+
#define MAGICMA  20050610

#property indicator_chart_window

extern int    NumberOfDays = 50;        
extern string AsiaBegin    = "01:00";   
extern string AsiaEnd      = "10:00";   
extern color  AsiaColor    = Goldenrod;
extern string EurBegin     = "07:00";   
extern string EurEnd       = "16:00";   
extern color  EurColor     = Tan;       
extern string USABegin     = "14:00";   
extern string USAEnd       = "23:00";   
extern color  USAColor     = PaleGreen; 

//-------------------------------------
double TargetProfit = 30;
double StopLoss = 30;
int BreakOutBuffer = 5; //in pips
int DailyMoveDoneVaule = 300 ; //in pips
//-------------------------------------

double AsianSessionHigh = 0;
double AsianSessionLow = 1000;
int HaltTradingFlag = 0; 
int NoBreakoutCount = 0;
int ForcedOutOfTimeOrders = 0;
int DoOnceFlag = 0;
int DoOnceFlag2 = 0;
int TradesMade = 0;
int DailyMoveDone = 0;



//---- i-Sessons -----------------------------
void init() {
  DeleteObjects();
  for (int i=0; i<NumberOfDays; i++) {
    CreateObjects("AS"+i, AsiaColor);
    CreateObjects("EU"+i, EurColor);
    CreateObjects("US"+i, USAColor);
  }
}

void deinit() {
  ///DeleteObjects();
}

void CreateObjects(string no, color cl) {
  ObjectCreate(no, OBJ_RECTANGLE, 0, 0,0, 0,0);
  ObjectSet(no, OBJPROP_STYLE, STYLE_SOLID);
  ObjectSet(no, OBJPROP_COLOR, cl);
  ObjectSet(no, OBJPROP_BACK, True);
}

void DeleteObjects() {
  for (int i=0; i<NumberOfDays; i++) {
    ObjectDelete("AS"+i);
    ObjectDelete("EU"+i);
    ObjectDelete("US"+i);
  }
}

//-----------------------------------------



//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   
   int buys=0,sells=0;

//----
 
   for(int i=0;i<OrdersTotal();i++)
     {
    
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
     
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
        
         if(OrderType()==OP_BUY)  buys++;
        
         if(OrderType()==OP_SELL) sells++;
        
        }
     
     }
//---- return orders volume
  
   if(buys>0) return(buys);
  
   else       return(-sells);
  
  }

  
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+


void CheckForOpen()
  {


//if at the end of the trading day and no trades made then increment notradecounter
if ( TimeHour(TimeCurrent()) == 22 && DoOnceFlag == 0 ) {

   DoOnceFlag = 1;

   if (HaltTradingFlag == 0) NoBreakoutCount++; //No trade made.
}
//------------------


//reset vaules if before Asian session
if ( TimeHour(TimeCurrent()) == 0 ) {
   
   AsianSessionHigh = 0; 
   AsianSessionLow = 1000;

   DoOnceFlag = 0;
   DoOnceFlag2 = 0;

   HaltTradingFlag = 0;

}//end


//if in Asian session then calculate highs and lows.
if ( TimeHour(TimeCurrent()) >= 1 && TimeHour(TimeCurrent()) <= 10   ) {

   //if (Ask > AsianSessionHigh) AsianSessionHigh = Ask; 
   //if (Bid < AsianSessionLow) AsianSessionLow = Bid; 

   if (iHigh(NULL, 0, 0) > AsianSessionHigh) AsianSessionHigh = iHigh(NULL, 0, 0); 
   if (iLow(NULL, 0, 0) < AsianSessionLow) AsianSessionLow = iLow(NULL, 0, 0); 

} //end calculate asian high/lows


///do not allow any more trades if there are been a trade for the day
if (HaltTradingFlag == 1) return;
//-------------------------------------------------------------------


//only allow trading during certian hours  //return if not within EUR session or other
if ( TimeHour(TimeCurrent()) < 10 || TimeHour(TimeCurrent()) > 14 ) return; //14 for only the london session where there is no overlapping other session
//---------------------------------------------------------------------------


//SUSPEND TRADING if AVERAGE DAILY MOVE has been done
if (AsianSessionHigh - AsianSessionLow > (DailyMoveDoneVaule / 100)) {

   if (DoOnceFlag2 == 0) DailyMoveDone++;
   
   DoOnceFlag2 = 1;

   return;  
}
//-----------------------------------------------------


///CHECK FOR TRADE CONDITIONS

if (Bid > AsianSessionHigh + (BreakOutBuffer / 100)) {   

    OrderSend(Symbol(),OP_BUY, 1 ,Ask,3,0,0,"",MAGICMA,0,Blue); 

   TradesMade++;

}

if (Ask < AsianSessionLow - (BreakOutBuffer / 100)) {


   OrderSend(Symbol(),OP_SELL, 1 ,Bid,3,0,0,"",MAGICMA,0,Red);
   
   TradesMade++; 
  
}

  }

//-------------------------------------------------------------------------------------------  
  
  
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {

HaltTradingFlag = 1; 
   
   //Force close trade if at the end of the trading period
   if ( TimeHour(TimeCurrent()) == 23  ) { //16 = END OF LONDON ; 23 = end of USA
   
      ForcedOutOfTimeOrders++;

      OrderClose(OrderTicket(),OrderLots(),Bid,3,Black);
      OrderClose(OrderTicket(),OrderLots(),Ask,3,Black);
      
      return;
      
   } //end
      
    
      //Check for closing conditions  
     
      for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
     
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
     
     
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
                  
          if( Bid < OrderOpenPrice() - StopLoss / 100) { //STOP LOSS
      
             OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);   
             OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
   
         }  
          
          
           if( Ask > OrderOpenPrice() + TargetProfit / 100) { //TARGET PROFIT
      
             OrderClose(OrderTicket(),OrderLots(),Bid,3,Yellow);   
             OrderClose(OrderTicket(),OrderLots(),Ask,3,Yellow);
   
         }  
        
         break;
       
        }
     
      if(OrderType()==OP_SELL)
        {
              
        //iHigh(NULL, 0, 0)
              if( Ask > (OrderOpenPrice() + StopLoss / 100) ) { ///STOR LOSS
   
             OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);   
             OrderClose(OrderTicket(),OrderLots(),Ask,3,Red); 
   
   }   
        
        
          if( Bid < (OrderOpenPrice() - TargetProfit / 100) ) { ///TARGET PROFIT
   
             OrderClose(OrderTicket(),OrderLots(),Bid,3,Yellow);   
             OrderClose(OrderTicket(),OrderLots(),Ask,3,Yellow); 
   
   }  
                      
         break;
       
        }
     }
     

//----

  }
  
  

  
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+

void start()
  {

     Comment( 
      
            "StopLoss " +  StopLoss / 100  + "\n" +
            
            "TargetProfit " +  TargetProfit / 100  + "\n" +
            
            "TimeHour " +  TimeHour(TimeCurrent())   + "\n" +
            "TimeMinute " +  TimeMinute(TimeCurrent()) + "\n" +
            
            "AsianSessionHigh " +  AsianSessionHigh + "\n" +
            "AsianSessionLow " +  AsianSessionLow + "\n" + 
            
            "iHigh " +  iHigh(NULL, 0, 0) + "\n" +
            "iLow " +  iLow(NULL, 0, 0) + "\n" +
                                    
            "TradesMade " +  TradesMade + "\n" +
            
            "DailyMoveDone " +  DailyMoveDone + "\n" +
            
            "NoBreakoutCount " +  NoBreakoutCount + "\n" +
            
            "ForcedOutOfTimeOrders " +  ForcedOutOfTimeOrders
 
 
            
);


//---- calculate open orders by current symbol
 
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); //if not orders opened check for trade conditions
  
   else                                    CheckForClose(); //if there is a active order check for closing conditions

//----




return;   // use return here during visual mode to speed up back testing. comment out return to add i-Session indicator to graph in visual mode (slows down process)



//---- i-Session ----

  datetime dt=CurTime();

  for (int i=0; i<NumberOfDays; i++) {
    DrawObjects(dt, "AS"+i, AsiaBegin, AsiaEnd);
    DrawObjects(dt, "EU"+i, EurBegin, EurEnd);
    DrawObjects(dt, "US"+i, USABegin, USAEnd);
    dt=decDateTradeDay(dt);
    while (TimeDayOfWeek(dt)>5) dt=decDateTradeDay(dt);
  }
}

void DrawObjects(datetime dt, string no, string tb, string te) {
  datetime t1, t2;
  double   p1, p2;
  int      b1, b2;

  t1=StrToTime(TimeToStr(dt, TIME_DATE)+" "+tb);
  t2=StrToTime(TimeToStr(dt, TIME_DATE)+" "+te);
  b1=iBarShift(NULL, 0, t1);
  b2=iBarShift(NULL, 0, t2);
  p1=High[Highest(NULL, 0, MODE_HIGH, b1-b2, b2)];
  p2=Low [Lowest (NULL, 0, MODE_LOW , b1-b2, b2)];
  ObjectSet(no, OBJPROP_TIME1 , t1);
  ObjectSet(no, OBJPROP_PRICE1, p1);
  ObjectSet(no, OBJPROP_TIME2 , t2);
  ObjectSet(no, OBJPROP_PRICE2, p2);
}

datetime decDateTradeDay (datetime dt) {
  int ty=TimeYear(dt);
  int tm=TimeMonth(dt);
  int td=TimeDay(dt);
  int th=TimeHour(dt);
  int ti=TimeMinute(dt);

  td--;
  if (td==0) {
    tm--;
    if (tm==0) {
      ty--;
      tm=12;
    }
    if (tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm==10 || tm==12) td=31;
    if (tm==2) if (MathMod(ty, 4)==0) td=29; else td=28;
    if (tm==4 || tm==6 || tm==9 || tm==11) td=30;
  }
  return(StrToTime(ty+"."+tm+"."+td+" "+th+":"+ti));

//------------------------------------------------------------------

 
  } //end void start()  