
//Good for GBP/USD 
//GBP/USD TimeToTrade=15 GMT, 

#define EAName "Anthony_M"

extern double Lots=0.1;
extern double MinLots=0;
extern double MaxLots=1;

extern int BarsBack=7;
extern int TimeToTrade=17; //GMT=15
extern int AndMinutes=0;
extern int PendingOrdersTime=10700; //time in milliseconds before deleting pending orders.(3hours=3x60x60=10800),(4hours=4x60x60=14400).
extern int CloseTimeOfOrder=13;   //if still open orders not touch loss or profit,time to close orders before new cycle starts again.
extern int Buffer=20;            //pips over pending order.
extern int TrailingStop=0;
extern int TrailingStep=0;
extern int BreakEven=0;       //if =1 enabled, if =0 disabled 
extern int MaxOrders=1;
extern int Slippage=3;
extern int Magic=2009;

datetime PreviousBar1;
datetime PreviousBar2;


//initialize
int start()
{
 if(OrderOpenTime()<=TimeCurrent()-PendingOrdersTime) //time to delete pending orders/
 if (OrderType()==OP_BUYSTOP || (OrderType()==OP_SELLSTOP))
 OrderDelete(OrderTicket());
//trailing stop

   if(TrailingStop>0)MoveTrailingStop();

//break even

   if(BreakEven>0)MoveBreakEven();
   


//signal conditions

   int limit=1;
   for(int i=1;i<=limit;i++)
   {
   
   //main signal
   RefreshRates();

double highest = High[iHighest(NULL,0,MODE_HIGH,BarsBack,1)] + (Buffer * Point);
   double lowest = Low[iLowest(NULL,0,MODE_LOW,BarsBack,1)] - (Buffer * Point);
 double hourtime=Hour();
double minutetime=Minute();
      string BUY="false";
      string SELL="false";

      if( hourtime==TimeToTrade && minutetime==AndMinutes )  BUY="true"; 
      if( hourtime==TimeToTrade && minutetime==AndMinutes ) SELL="true";
      
      string SignalBUY="false";
      string SignalSELL="false";
      
      if(BUY=="true") SignalBUY="true";
      if(SELL=="true")SignalSELL="true";
            
   }
//open orders
   double SL,TP,ILots;
   int Ticket;
   SL=0;TP=0;ILots=Lots;
     
   if((CountOrders(OP_BUY,Magic)+CountOrders(OP_SELL,Magic))<MaxOrders) 
   {  
      if(SignalBUY=="true"&&NewBarB())
      {         
        
         
         Ticket=OrderSend(Symbol(),OP_BUYSTOP,ILots,highest,Slippage,(lowest),TP,EAName,Magic,0,Blue); 
                        
      }

   
      if(SignalSELL=="true"&&NewBarS())
      {                
         
         
         Ticket=OrderSend(Symbol(),OP_SELLSTOP,ILots,lowest,Slippage,(highest),TP,EAName,Magic,0,Red);
                 
      }
   }
CloseBuyOrders(Magic);
CloseSellOrders(Magic);
  
//no more money warning
   int err=0;
   if(Ticket<0)
   {
      if(GetLastError()==134)
      {
         err=1;
         Print("no more money");
      }
      return (-1);
   }
   return(0);
}

//close buy orders

int CloseBuyOrders(int Magic)
{      
double profitt=(0.01*OrderOpenPrice());
double closetime=Hour();
   
  int total=OrdersTotal();

  for (int cnt=total-1;cnt>=0;cnt--)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbol())
    {
      if(OrderType()==OP_BUY)
       
      { if ( Bid>(OrderOpenPrice()+profitt)|| closetime==CloseTimeOfOrder) 
        OrderClose(OrderTicket(),OrderLots(),Bid,3);   break;
      }
    }
  }
  return(0);
}
//close sell orders
int CloseSellOrders(int Magic)
{   
double profitt=(0.01*OrderOpenPrice());
double closetime=Hour();
   
  int total=OrdersTotal();
  for(int cnt=total-1;cnt>=0;cnt--)
  { 
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbol())
    {
      if(OrderType()==OP_SELL)
       
      {if (   Ask<(OrderOpenPrice()-profitt) ||closetime==CloseTimeOfOrder) 
        OrderClose(OrderTicket(),OrderLots(),Ask,3); break;
      }
    }
  }
  return(0);
}
//count orders
int CountOrders(int Type,int Magic)
{
   int _CountOrd;
   _CountOrd=0;
   for(int i=0;i<OrdersTotal();i++)
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol())
      {
         if((OrderType()==Type&&(OrderMagicNumber()==Magic)||Magic==0))_CountOrd++;
      }
   }
   return(_CountOrd);
}
//trailing stop
void MoveTrailingStop()
{
   int cnt,total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol()&&OrderMagicNumber()==Magic)
      {
         if(OrderType()==OP_BUY)
         {
            if(TrailingStop>0&&Ask>NormalizeDouble(OrderOpenPrice(),Digits))  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-Point*(TrailingStop+TrailingStep),Digits))||(OrderStopLoss()==0))
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue);
                  return(0);
               }
            }
         }
         else 
         {
            if(TrailingStop>0&&Bid<NormalizeDouble(OrderOpenPrice(),Digits))  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)>(NormalizeDouble(Ask+Point*(TrailingStop+TrailingStep),Digits)))||(OrderStopLoss()==0))
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TrailingStop,Digits),OrderTakeProfit(),0,Red);
                  return(0);
               }
            }
         }
      }
   }
}
//breakeven
void MoveBreakEven()
{
double profits=(0.00618*OrderOpenPrice());
   int cnt,total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol()&&OrderMagicNumber()==Magic)
      {
         if(OrderType()==OP_BUY)
         {
            if(BreakEven>0)
            {
               if(NormalizeDouble((Bid-OrderOpenPrice()),Digits)>BreakEven*Point)
               {
                  if(NormalizeDouble((OrderStopLoss()-OrderOpenPrice()),Digits)<0)
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+profits,Digits),OrderTakeProfit(),0,Blue);
                     return(0);
                  }
               }
            }
         }
         else
         {
            if(BreakEven>0)
            {
               if(NormalizeDouble((OrderOpenPrice()-Ask),Digits)>BreakEven*Point)
               {
                  if(NormalizeDouble((OrderOpenPrice()-OrderStopLoss()),Digits)<0)
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-profits,Digits),OrderTakeProfit(),0,Red);
                     return(0);
                  }
               }
            }
         }
      }
   }
}
//allow one trade/bar

bool NewBarB()
{
   if(PreviousBar1<Time[0])
   {
      PreviousBar1=Time[0];
      return(true);
   }
   return(false);
}

bool NewBarS()
{
   if(PreviousBar2<Time[0])
   {
      PreviousBar2=Time[0];
      return(true);
   }
   return(false);
}



