 //+------------------------------------------------------------------+
//|                                Bands Breakin fixed stoploss.mq4 |
//+------------------------------------------------------------------+

#property copyright "Denny"
#property link      "DDD@gmail.com"



#define MAGIC 35583

//---- input parameters


extern int       Mulai_Jam       = 1;
extern int       Akhir_Jam       = 24;
extern int       Bollinger_Period = 20;
extern int       TakeProfit = 50;
extern double    StopLoss = 200; 
extern int       TimePeriod = 5; // in minutes
extern int       Slippage = 3;
extern bool      UseFixedLots = false;
extern int       FixedLotSize = 1;
extern double    VAR = 0.1; // .1 means the value-at-risk is 10% of the account (i.e., if the stoploss is hit, this amount will be lost)

double entry = 0;

int Magic=69;
double clo,del,sel;
int ThisBarTrade= 0;
 int     max_ord         = 1;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
             
   

}
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}
  
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
   int buys=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
        {
         if(OrderType()==OP_BUY)  buys++;
        
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   
}
  
double StopLoss()
{
   return (StopLoss * Point);
}
  
double LotSize()
{
   if (UseFixedLots == true) return (FixedLotSize);

   double size;
   
   size = (AccountEquity() * VAR) / (StopLoss() / Point);
   
   if (size < 0.01) size = 0.01;
   if (size > 200) size = 200;
   return (size);
}

bool EnterLong() 
{
   double  lowboll;
  
  
   lowboll = iBands(NULL,TimePeriod,Bollinger_Period,2,0,PRICE_HIGH,MODE_LOWER,1);
   
   bool CanTrade = False;

   if(TimeCurrent() < StrToTime("01:00")) CanTrade = True;
   
   // Enter long  Ask is outside of lowwer band, 
   if ((Ask  <= lowboll)) 
   {
      return (true);
   }
   else return (false);
}



//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   int res;
     
 if(Jam_trade()==1)  
   // long conditions
   
   
   if (EnterLong())
        if (Bars != ThisBarTrade)
       {
          ThisBarTrade = Bars;
     if (jumlahorder(0)<=max_ord) OrderSend(Symbol(),OP_BUY,LotSize(),Ask,Slippage,Bid-StopLoss(),Ask+TakeProfit*Point,"",MAGIC);
      entry = Ask;
      return;
   }
   
  
 
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
{  
   for (int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if (OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;
      
      if (OrderType()==OP_BUY) {
         // close long conditions
        
            
            }
            break;
         }
      }
     
     
      
      
   


  
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

if(Hour()==Akhir_Jam)
if(jumlahorder(2)>=1 || jumlahorder(3)>=1){
closeall(2);
closeall(3);
 }
if(Hour()==Akhir_Jam)
if(jumlahorder(0)>=1 || jumlahorder(1)>=1){
closeall(0);
closeall(1);
 }
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
   
   
   
   
//----




   return(0);
  
  }
//+------------------------------------------------------------------+
void closeall(int m)
{
 for (int i = OrdersTotal() - 1; i >= 0; i--) {
  sel=OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
  if (OrderSymbol() != Symbol() || OrderMagicNumber()!=Magic||  OrderType()!=m) continue;
  if (OrderType() > 1) del=OrderDelete(OrderTicket());
  if (OrderType() == 0) clo=OrderClose(OrderTicket(), OrderLots(), Bid, 3, CLR_NONE);
  if (OrderType() == 1)clo=OrderClose(OrderTicket(), OrderLots(), Ask, 3, CLR_NONE);
 }
}
int Jam_trade()
{
   bool trade = false;
   if(Mulai_Jam > Akhir_Jam){
     if (Hour() >= Mulai_Jam || Hour() < Akhir_Jam) trade = true;
   } else
     if (Hour() >= Mulai_Jam && Hour() < Akhir_Jam) trade = true;

   return (trade);
}

int jumlahorder( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
  {
      sel=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
     total++;
  }

}
int Forceclose()
{
if(TimeCurrent()-OrderOpenTime()>=3600 && jumlahorder(0)>=1 ||jumlahorder(1)>=1)
closeall(0);
closeall(1);
}