//+------------------------------------------------------------------+
//|                                               Candle_NoTrade.mq4 |
//|                                                       Vixen Girl |
//|                                                vixenme@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Vixen Girl"
#property link      "vixenme@gmail.com"

extern string     workPeriod                    ="D1";
extern int        SLlevel                       = 38;

extern int        MagicNo                       = 222222222;

extern double     TakeProfit                    = 180.0;
extern double     Lots                          = 0.1;
extern double     TrailingStop                  = 38.0;

extern string     OrdComment                    =  "BarByBar ^_+";

extern color      OpenBuy_Sig_Color             = DarkGoldenrod;
extern color      CloseBuy_Sig_Color            = Black;
extern color      OpenSell_Sig_Color            = Crimson;
extern color      CloseSell_Sig_Color           = Black;
extern color      Modified_Sig_Color            = DarkViolet;

extern bool       Enable_Sound                  = true;
extern string     Sound_OrderOpen_FileName      = "feelgood.wav";
extern string     Sound_OrderClose_FileName     = "jaildoorclose.wav";
extern string     Sound_OrderModify_FileName    = "kiss.wav";

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNo)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
  //----------------------------------------------------------------
  int Txt2Period(string text)
  {
      if       (text=="M1"    || text=="m1")int   Prd =1 ;
      else if  (text=="M5"    || text=="m5")      Prd =5 ;
      else if  (text=="M15"   || text=="m15")     Prd =15 ;
      else if  (text=="M30"   || text=="m30")     Prd =30 ;
      else if  (text=="H1"    || text=="h1")      Prd =60 ;
      else if  (text=="H4"    || text=="h4")      Prd =240 ;
      else if  (text=="D1"    || text=="d1")      Prd =1440 ;
      else if  (text=="W1"    || text=="w1")      Prd =10080 ;
      else if  (text=="MN"    || text=="mn")      Prd =43200 ;
      else     /*text==**/                        Prd = 0;
      return(Prd);
  }
  
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   //----

//---- Defind up-down PART
   int   wrkPeriod   =Txt2Period(workPeriod);

   double high  = iHigh(NULL,wrkPeriod,0);
   double low   = iLow(NULL,wrkPeriod,0);
   double close = iClose(NULL,wrkPeriod,0);
   double open  = iOpen(NULL,wrkPeriod,0);

  //////////////////////////////////////////////////////////////////
  //                                                              //
  //        YOUR OPEN BUY-SELL CONDITION COULD BE HERE            //
  //                                                              //
  //////////////////////////////////////////////////////////////////
   
   
   //---- Start Trade

   int cnt, ticket, total;
   
   int spread = MathRound((Ask-Bid)/Point);
   bool openenable = true;
      
      for(cnt=0;cnt<HistoryTotal();cnt++)
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
         if(OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNo)
         {                 
            int lastclosetime = OrderCloseTime();
            
               if(lastclosetime>iTime(Symbol(),wrkPeriod,0))
                        openenable = false;
               else     openenable = true;    
         }
      }
   //+------------------------------------------------------------------+
   //| Don't Trade If Bars<50 & TakeProfit<5 & No Avilable Margin Left  |
   //+------------------------------------------------------------------+
   if(Bars<50)Comment("bars less than ",50);
   else if(TakeProfit<5)Comment("TakeProfit less than ",5);
   else if(AccountFreeMargin()<(1000*Lots))
   {
      Comment("You have no money. Free Margin = ", AccountFreeMargin());
      Print("No money left. Free Margin = ", AccountFreeMargin());
   }
   //+------------------------------------------------------------------+
   else
   {
   //---- Start Trading
      //+--------//Check for opened order in the same symbol//-------------+
      if(CalculateCurrentOrders(Symbol())==0)
      {
         if(openenable)
         {   
            if(YOUR BUY CONDITION)
            {
               ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,spread,Ask-SLlevel*Point,Ask+TakeProfit*Point,OrdComment+":"+wrkPeriodTxt,MagicNo,0,OpenBuy_Sig_Color);
            }
            if(YOUR SELL CONDITION)
            {  
               ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,spread,Bid+SLlevel*Point,Bid-TakeProfit*Point,OrdComment+":"+wrkPeriodTxt,MagicNo,0,OpenSell_Sig_Color);
            }
         }   
         else ticket=-1;
         
         if(ticket>0)
         {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            {
               if (Enable_Sound) PlaySound(Sound_OrderOpen_FileName);
                  Comment("\nAlert : ", TimeToStr(LocalTime(),TIME_MINUTES),"O\'clock",
                           "\nOrder Opened : ",OrderOpenPrice());
                  Print("Order opened : ",OrderOpenPrice());
            return(0);
            }//end OrderSelect
         return(0);
         }//end ticket>0
      return(0);
      }//end of CalculateCurrentOrders()

   //+----------------------------------------------------------------+   
   }//end else (trading)
   
   //+--------//Check for closing and modifying condition//-----------+
   total = OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
   OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNo)
      {
         if(OrderType()==OP_BUY) // long position is opening
         {
            // should it be closed?
            if(YOUR CLOSE BUY CONDITION)
            {
                     OrderClose(OrderTicket(),OrderLots(),Bid,spread,CloseBuy_Sig_Color);
                     // close position
                     if (Enable_Sound) PlaySound(Sound_OrderClose_FileName);
                     Comment("\nLong Order Closed : ",OrderComment(),
                              "\nAt : ",OrderClosePrice(),
                              "\nProfit : ",OrderProfit(),
                              "\nTime : ", TimeToStr(LocalTime(),TIME_MINUTES));
            return(0); // exit
            }//end closebuy || closebrkbuy
      
            // check for trailing stop
            if(TrailingStop>0)
            {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
               {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Modified_Sig_Color);
                     if (Enable_Sound) PlaySound(Sound_OrderModify_FileName);
                     Comment("\nOrder Modify Alert : ",TimeToStr(LocalTime(),TIME_MINUTES),
                              "\nNew Stop Loss : ",OrderStopLoss(),
                              "\nNew Target Profit : ",OrderTakeProfit());
                     return(0);
                  }//end OrderStopLoss()<Bid-Point*TrailingStop
               }//end Bid-OrderOpenPrice()>Point*TrailingStop
            }//end TrailingStop>0
         }//end OrderType()==OP_BUY
   
         else // go to short position
         {
            // should it be closed?
            if(YOUR CLOSE SELL CONDITION)
            {
            // should it be closed?
                  OrderClose(OrderTicket(),OrderLots(),Ask,spread,CloseSell_Sig_Color);
                  // close position
                  if (Enable_Sound) PlaySound(Sound_OrderClose_FileName);
                  Comment("\nShort Order Closed : ",OrderComment(),
                           "\nAt : ",OrderClosePrice(),
                           "\nProfit : ",OrderProfit(),
                           "\nTime : ", TimeToStr(LocalTime(),TIME_MINUTES));
            return(0); // exit
            }//end closesell || closebrksell

            // check for trailing stop
            if(TrailingStop>0)
            {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Modified_Sig_Color);
                     if (Enable_Sound) PlaySound(Sound_OrderModify_FileName);
                        Comment("\nOrder Modify Alert : ",TimeToStr(LocalTime(),TIME_MINUTES),
                                 "\nNew Stop Loss : ",OrderStopLoss(),
                                 "\nNew Target Profit : ",OrderTakeProfit());
                  return(0);
                  }//end OrderStopLoss()
               }//end OrderOpenPrice()-Ask >(Point*TrailingStop)
            }//end TrailingStop>0
         }//end else
      }//end OrderType()<=OP_SELL && OrderSymbol()==Symbol()
   }//end for
   //+----------------------------------------------------------------END
 


//+------------------------------------------------------------------+

return(0);
}
//+------------------------------------------------------------------+
   


