//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                               Bogie v9-pivot.mq4 |
//|                              Copyright © 2006, Bogie Enterprises |
//|                                         
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Bogie Enterprises"

//       ====== Inputs ======

extern bool    UseMM          = 1;   // Money Management - 1 = true & 0 = false
extern double  Risk           = 2.0; // percent of account equity to risk per trade
extern double  Lots           = 0.1; // This lot size is used if UseMM = 0
extern double  TrailingStop   = 30; // 0 = no trailing stop
extern bool    UseStartTime   =  false;
extern double  DayStartHour   = 17; // local PC time
extern double  DayStartMinute =  2; // local PC time
double         TakeProfit;
extern bool    UseStopLoss    = false;          
extern double  StopLoss       = 300;
extern double  Spread         = 10;

int            MagicNumber;



//       ====== Program Start ======

int start()
  {
   double yesterday_high, yesterday_low, yesterday_close, today_open;
   double P, TO, G;
   double StopLoss2;

    
//         ====== Money Management for Lot Size routine ======  
  
   
    if(UseMM) 
      {
         Lots=AccountEquity()* Risk/100/1000;
         if( Lots>0.1)NormalizeDouble(Lots,1); else NormalizeDouble(Lots,2);
      }
    
    
//        ====== Pivot Calculation Routine ======
   
      yesterday_close=iClose(NULL,PERIOD_D1,0);
      today_open=iOpen(NULL,PERIOD_M1,0);
      yesterday_high=iHigh(NULL,PERIOD_D1,0);
      yesterday_low=iLow(NULL,PERIOD_D1,0);
      P = (yesterday_high + yesterday_low + yesterday_close + today_open) / 4;
      TO=today_open;
      
   P = NormalizeDouble((yesterday_high + yesterday_low + yesterday_close)/3,Digits);

  
  
  
//          ====== Trade Trigger Calculation Routine =======

   int Buy1=0,Sell1=0;
   int h=TimeHour(LocalTime());
   int m=TimeMinute(LocalTime());
  
 
   if(UseStartTime==true)
      {
         if(h==DayStartHour && m>=DayStartMinute )
         {
            if( TO - P <- (Spread*Point))  
               {  
               Buy1=1;
               Comment("Open Orders = ",MyOrdersTotal(),"\nToday Open = ",TO,"\nPivot = ",P,
               "\nGap = ",G,"  == BUY ==  "); 
               }
             
            if( TO - P > (Spread*Point)) 
               {
               Sell1=1;
               Comment("Open Orders = ",MyOrdersTotal(),"\nToday Open = ",TO,"\nPivot = ",P,
               "\nGap = ",G,"  == SELL ==  ");   
               }
               
               if( Buy1!=1 && Sell1!=1) Comment("Open Orders = ",MyOrdersTotal(),"\nToday Open = ",TO,"\nPivot = ",P,
               "\nGap = ",G);
         }
      }
      
    if(UseStartTime==false)
      {
            if( TO - P <- (Spread*Point))  
               {  
               Buy1=1;
               Comment("Open Orders = ",MyOrdersTotal(),"\nToday Open = ",TO,"\nPivot = ",P,
               "\nGap = ",G,"  == BUY ==  ");
               }
             
            if( TO - P > (Spread*Point)) 
               {
               Sell1=1;
               Comment("Open Orders = ",MyOrdersTotal(),"\nToday Open = ",TO,"\nPivot = ",P,
               "\nGap = ",G,"  == SELL ==  ");   
               }
               
            if( Buy1!=1 && Sell1!=1) Comment("Open Orders = ",MyOrdersTotal(),"\nToday Open = ",TO,"\nPivot = ",P,
            "\nGap = ",G);
      }
      
      if( Buy1==1) TakeProfit=P+(5*Point);
      if( Sell1==1) TakeProfit=P-(5*Point);
   
   
   
//          ====== Order Open Routine ======   

   int cnt, ticket, total, OrderIsOpen=0;
   total  = OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if((OrderSymbol()==Symbol())&& (OrderMagicNumber()==12345))
         {
         OrderIsOpen=1;
         break;
         }
      else
         continue; 
      }  
    if(MyOrdersTotal()==0 ) 
      {
      if(Buy1==1) 
         {
         if (UseStopLoss) StopLoss2 = Ask - StopLoss * Point; else StopLoss2 = 0.0;
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,StopLoss2,TakeProfit,"BUYSignal",12345,0,GreenYellow);
         if(ticket>0)
            {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            }
            else Print("Error opening BUY order : ",GetLastError()); 
            return(0);
         }

      if(Sell1==1)
         {
         if (UseStopLoss) StopLoss2 = Bid + StopLoss * Point; else StopLoss2 = 0.0;
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,StopLoss2,TakeProfit,"SELLSignal",12345,0,Red);
         if(ticket>0)
            {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
            }
            else Print("Error opening SELL order : ",GetLastError()); 
            return(0);
         }
      }
     
  
     
//          ====== Trailing Stop  Routine ======
  
   total = OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if((OrderType() <= OP_SELL) && (OrderSymbol() == Symbol()) && (OrderMagicNumber() == 12345))
         {
         if(OrderType() == OP_BUY)
            {
            if(TrailingStop > 0)
               {
               if((Bid-OrderOpenPrice()) > (Point*TrailingStop))
                  {
                  if((OrderStopLoss()) < (Bid-Point*TrailingStop))
                     {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*5,OrderTakeProfit(),0,GreenYellow);
                     return(0);
                     }
                  } 
               }                  
             }

            if(OrderType() == OP_SELL)
               {
               if(TrailingStop > 0)
                  {
                  if(OrderOpenPrice()-Ask>Point*TrailingStop)
                     {
                     if(OrderStopLoss()>Ask+Point*TrailingStop)
                        {
                        OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*5,OrderTakeProfit(),0,Red);
                        return(0);              
                        }
                     }     
                  }
                }
             }
          }

   return(0);
  }
   
   
   
//          ====== Sub-Routines ======
   
   int MyOrdersTotal() { // included by Renato
   int Mytotal=0; 
   for (int i=0; i<OrdersTotal(); i++) { 
     if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
       if((OrderSymbol()==Symbol())&& (OrderMagicNumber()==12345)) 
          Mytotal++; 
   }  
   return(Mytotal); 
} 
  
//+---------------------- The End -----------------------------