//+------------------------------------------------------------------+
//|                                             Alternating_Bars.mq4 |
//|                                                    Kenny Hubbard |
//|                                       http://www.compu-forex.com |
//+------------------------------------------------------------------+
#property copyright "Kenny Hubbard"
#property link      "http://www.compu-forex.com"

#define        NO_RESULT            -1

extern int     MagicNumber       = 100;
extern double  TakeProfit        = 5;
extern double  StopLoss          = 15;
extern double  Buffer            = 5;
extern double  Lots              = 0.01;
extern bool  ReverseTrades     = TRUE;

int   
   D_Factor = 1,
   LotDigits = 1;
   
double
   Slippage = 3;   
   
string
   Cmt = "Alternating Bars";
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
   if (Digits == 3 || Digits == 5) D_Factor = 10;
   TakeProfit *= Point * D_Factor;
   StopLoss   *= Point * D_Factor;
   Buffer     *= Point * D_Factor;
   if (MarketInfo(Symbol(),MODE_LOTSTEP) == 0.01) LotDigits = 2;
   if (MarketInfo(Symbol(),MODE_MINLOT) > Lots)Lots = MarketInfo(Symbol(),MODE_MINLOT);
   if (MarketInfo(Symbol(),MODE_MAXLOT) < Lots)Lots = MarketInfo(Symbol(),MODE_MAXLOT);
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
int
   Buy_or_Sell,
   ticket;
   color ArrowColor;
double
   Price,
   Temp_TP,
   Temp_SL;
bool
   Mod;
//----
   if (New_Bar()){
      if (OrdersTotal() > 0){
         for(int i=0;i<OrdersTotal()-1;i++){
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
               if(OrderMagicNumber() == MagicNumber){
                  switch(OrderType()){
                      case OP_BUY   : OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);   break;
                      case OP_SELL  : OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);   break;
                      default       : Print("Undefined OrderType for this strategy");            break;
                  }
               }
            }
            else Print("Error selecting order - " + GetLastError());
         }
      }
      Buy_or_Sell = Trade_Direction();
      if (Buy_or_Sell == OP_BUY){
         Price = Ask;
         Temp_TP = Ask + TakeProfit;
         Temp_SL = Bid - StopLoss;
         ArrowColor = Blue;
      }
      else{
         Price = Bid;
         Temp_TP = Bid - TakeProfit;
         Temp_SL = Ask + StopLoss;
         ArrowColor = Red;
      }
      ticket = OrderSend(Symbol(),Buy_or_Sell,NormalizeDouble(Lots,LotDigits),Price,Slippage,0,0,Cmt,MagicNumber,0,ArrowColor);
      if (ticket > 0){
         Mod = OrderModify(ticket,OrderOpenPrice(),NormalizeDouble(Temp_SL,Digits),NormalizeDouble(Temp_TP,Digits),0,ArrowColor);
         if(!Mod)Alert("Error entering Takeprofit or Stoploss - " + GetLastError());
      }
      else Alert("Error making trade -" + GetLastError());
      
   }


//----
   return(0);
}
//+------------------------------------------------------------------+
bool New_Bar()
{
   static datetime New_Time=0;
   if(New_Time!=Time[0]){
      New_Time=Time[0];
      return(true);
   }
   return(false);
}
//+------------------------------------------------------------------+
int Trade_Direction()
{
if(ReverseTrades==TRUE){
   if (Close[1] > Close[2] + Buffer) return(OP_SELL);
   if(Close[1] - Buffer < Close[2]) return(OP_BUY);
   }
   else{
   if (Close[1] > Close[2] + Buffer) return(OP_BUY);
   if(Close[1] - Buffer < Close[2]) return(OP_SELL);
}
   return(NO_RESULT);
}