
#property copyright "Adeel 1H v1.01"

extern int    EA_magic          = 20090305;

extern double POS_tp            = 30;
extern double POS_sl            = 30;
extern double POS_slippage      = 0;
extern double BuyStop_pips_above  = 4;
extern double SellStop_pips_below  = 2;
extern double Move_to_BE        = 15;

extern int    Start_Hour        = 5;
extern int    End_Hour          = 17;

extern bool          MoneyManagement   = false;
extern double        minLots           = 0.1;
extern double        maxLots           = 100;
extern int           Risk_percent      = 3;

double   Lots;

int      positions, orders;
int prevtime;

void count_positions()
{
    for( int i = 0 ; i < OrdersTotal() ; i++ )
        {
        if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false )
            break;
            
        if( OrderMagicNumber() != EA_magic )
            continue;
            
        if( OrderSymbol() != Symbol() )
            continue;
 
        if( OrderType() == OP_BUY || OrderType() == OP_SELL)
           {
              positions++;
              if( OrderProfit() > Move_to_BE*OrderLots()*MarketInfo(Symbol(),MODE_TICKVALUE) && OrderOpenPrice()!=OrderStopLoss())
                 {
                 OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
//                 OrderClose(OrderTicket(),OrderLots()/2,Ask,POS_slippage+MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE);
                 }
           }
        else
           if( OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP)
               {
               if(TimeCurrent()-OrderOpenTime()>=3600)
                  {
                  if(!OrderDelete(OrderTicket()))
                     Print(GetLastError());
                  }
               else
                  orders++;
               }
        }
}

int init()
{
    return(0);
}

int start()
{
    double pos_sl,  pos_tp, pos_entry;
    int    ticket; 

   positions = 0; orders = 0;
   count_positions();

   if( orders > 0)
      return(0);
   
   if(iTime(Symbol(),PERIOD_H1,0) == prevtime) 
       return(0);
   prevtime = iTime(Symbol(),PERIOD_H1,0);
   if(!IsTradeAllowed()) 
     {
       prevtime = iTime(Symbol(),PERIOD_H1,1);
       return(0);
     }

   if(TimeHour(iTime(Symbol(),PERIOD_H1,0))< Start_Hour || TimeHour(iTime(Symbol(),PERIOD_H1,0)) > End_Hour)
      return(0);
      
   Call_MM();
   
    if( iClose(Symbol(),PERIOD_H1,1)<iOpen(Symbol(),PERIOD_H1,1))
      {    
            if( positions > 0 && OrderType() == OP_SELL)
               OrderClose(OrderTicket(),OrderLots(),Ask,POS_slippage+MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE);
            pos_entry = iHigh(Symbol(),PERIOD_H1,1)+BuyStop_pips_above*Point;
            pos_tp = pos_entry + POS_tp*Point;
            pos_sl = pos_entry - POS_sl*Point;
            ticket = OrderSend( Symbol(), OP_BUYSTOP, Lots, pos_entry, POS_slippage, pos_sl, pos_tp, "adeel 1H", EA_magic, 0, CLR_NONE );
            if( ticket < 0 )
                Sleep(5000);
      }
      else
      if( iClose(Symbol(),PERIOD_H1,1)>iOpen(Symbol(),PERIOD_H1,1))
         {
            if( positions > 0 && OrderType() == OP_BUY)
               OrderClose(OrderTicket(),OrderLots(),Ask,POS_slippage+MarketInfo(Symbol(),MODE_SPREAD),CLR_NONE);
            pos_entry = iLow(Symbol(),PERIOD_H1,1)-SellStop_pips_below*Point;
            pos_tp = pos_entry - POS_tp*Point;
            pos_sl = pos_entry + POS_sl*Point;
            ticket = OrderSend( Symbol(), OP_SELLSTOP, Lots, pos_entry, POS_slippage, pos_sl, pos_tp, "adeel 1H", EA_magic, 0, CLR_NONE );
            if( ticket < 0 )
                Sleep(5000);
          }
    return(0);
}

void Call_MM()
{
   if( MoneyManagement == false )
      Lots = minLots;
   else
      {
      Lots=AccountFreeMargin()/100000*Risk_percent;

      Lots=MathMin(maxLots,MathMax(minLots,Lots));
      if(minLots<0.1) 
        Lots=NormalizeDouble(Lots,2);               //   1k
      else
        {
        if(minLots<1) Lots=NormalizeDouble(Lots,1); //  10k
        else          Lots=NormalizeDouble(Lots,0); // 100k
        }
      }
   return(0);
}