// Hedge Hog.mq4
// Советник

/*
В начале часа StartHour открываются 1 ордер BUY и 1 ордер SELL
ExpirationDays: время жизни ордера в днях
TimeZoneOfData: время дилера минус время по Гринвичу (для Alpari = 1, для FXTeam = 2, для FIBO = 1, для LiteForex = 1)
*/

#property copyright   "mandorr@gmail.com"

extern int StartHour=0;
extern int TimeZone=1;
extern int OrderExpirationDays=2;
extern int TakeProfit=14;
extern int StopLoss=0;
extern double Lots=0.1;

int set_buy=0;
int set_sell=0;

void start()
   {
   if (Bars<100 || IsTradeAllowed()==false) return;
   if (TimeZone<-23 || TimeZone>23) return;
   TestOrderExpirationDays();
   int hour=Hour()-TimeZone;
   if (hour==StartHour)
      {
      if (set_buy ==0 && TotalBuy ()==0) { if (OpenBuy ()>0) {set_buy =1; return;} }
      if (set_sell==0 && TotalSell()==0) { if (OpenSell()>0) {set_sell=1; return;} }
      }
   else
      {
      set_buy=0;
      set_sell=0;
      }
   }

void TestOrderExpirationDays()
   {
   int set_double_buy =0;
   int set_double_sell=0;
   int ticket, slippage;
   double loss, profit;
   for (int i=OrdersTotal()-1; i>=0; i--)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if (OrderSymbol()!=Symbol()) continue;
      if (OrderType()!=OP_BUY && OrderType()!=OP_SELL) continue;
      if (CurTime()-OrderOpenTime()<86400*OrderExpirationDays) continue;
         {
         if (OrderType()==OP_BUY )
            { if (OrderClose(OrderTicket(),OrderLots(),Bid,slippage)) set_double_buy =1; }
         if (OrderType()==OP_SELL)
            { if (OrderClose(OrderTicket(),OrderLots(),Ask,slippage)) set_double_sell=1; }
         }
      }
   if (set_double_buy>0)
      {
      slippage=2;
      loss=0; if (StopLoss>0) loss=Ask-StopLoss*Point;
      profit=0; if (TakeProfit>0) profit=Ask+TakeProfit*Point;
      ticket=OrderSend(Symbol(),OP_BUY,2*Lots,Ask,slippage,loss,profit,"",0,0);
      if (ticket>0) set_buy=0;
      else Print("Open a order failed with error #",GetLastError());
      }
   if (set_double_sell>0)
      {
      slippage=2;
      loss=0; if (StopLoss>0) loss=Bid+StopLoss*Point;
      profit=0; if (TakeProfit>0) profit=Bid-TakeProfit*Point;
      ticket=OrderSend(Symbol(),OP_SELL,2*Lots,Bid,slippage,loss,profit,"",0,0);
      if (ticket>0) set_sell=0;
      else Print("Open a order failed with error #",GetLastError());
      }
   }

int OpenBuy()
   {
   int slippage=2;
   double loss=0; if (StopLoss>0) loss=Ask-StopLoss*Point;
   double profit=0; if (TakeProfit>0) profit=Ask+TakeProfit*Point;
   int ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,slippage,loss,profit,"",0,0);
   if (ticket==0) Print("Open a order failed with error #",GetLastError());
   return (ticket);
   }

int OpenSell()
   {
   int slippage=2;
   double loss=0; if (StopLoss>0) loss=Bid+StopLoss*Point;
   double profit=0; if (TakeProfit>0) profit=Bid-TakeProfit*Point;
   int ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,slippage,loss,profit,"",0,0);
   if (ticket==0) Print("Open a order failed with error #",GetLastError());
   return (ticket);
   }

int TotalBuy()
   {
   int count=0;
   for (int i=0; i<OrdersTotal(); i++)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if (OrderSymbol()==Symbol() && OrderType()==OP_BUY) count++;
      }
   return (count);
   }

int TotalSell()
   {
   int count=0;
   for (int i=0; i<OrdersTotal(); i++)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if (OrderSymbol()==Symbol() && OrderType()==OP_SELL) count++;
      }
   return (count);
   }

