//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                           Bogie Tlatomi-v1.0.mq4 |
//|                              Copyright © 2006, Bogie Enterprises |
//|                                         
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Bogie Enterprises"

//       ====== Inputs ======

extern bool    UseMM          = 1;   // 1 = true & 0 = false
extern double  Risk           = 5; // percent of account equity to risk per trade
extern double  Lots           = 0.1;
extern bool    UseCloseSignal = true; // if true, TakeProfit & StopLoss input values are not used
extern double  TrailingStop   = 0; // 0 = no trailing stop
double         TakeProfit     = 200;
extern bool    StopLossMode   = false;          
extern double  StopLoss       = 100;
double         StopLossLevel;

int            MagicNumber;



//---- Входные параметры
extern int     AllBars=0;//Для скольки баров считать. 0 - для всех.
extern int     Otstup=30;//Отступ.
extern double  Per=9;//Период.
int         SH,NB,i,UD;
      double      R,SHMax,SHMin,SHMax2,SHMin2;
      double      BufD[];
      double      BufU[];
#define  SH_BUY   1
#define  SH_SELL  -1

int init()
   {
    

   return(0);
   }



//       ====== Program Start ======

int start()
  {
      
//         ====== Money Management of Lot Size routine ======  
  
   
    if(UseMM) {Lots=AccountEquity()* Risk/100/1000;

    if( Lots>0.1)NormalizeDouble(Lots,1); else NormalizeDouble(Lots,2);}
    
double i;
   int m,s,k;
   m=Time[0]+Period()*60-CurTime();
   i=m/60.0;
   s=m%60;
   m=(m-m%60)/60;
//   Comment( m + " minutes " + s + " seconds left to bar end");   
  
    

//          ====== Trade Trigger Calculation Routine =======
  
   int Buy1=0,Sell1=0;
   int SH;
   double S;
   UD=0;
   double myMA1 =iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0);
   double myMA2 =iMA(NULL,0,9,0,MODE_SMA,PRICE_CLOSE,0);

      SHMax = High[Highest(NULL,0,MODE_HIGH,Per,SH)];
      SHMax2 = High[Highest(NULL,0,MODE_HIGH,Per,2)];
      SHMin = Low[Lowest(NULL,0,MODE_LOW,Per,SH)];
      SHMin2 = Low[Lowest(NULL,0,MODE_LOW,Per,2)];
      if (Close[SH]<SHMin+(SHMax-SHMin)*Otstup/100 && UD!=SH_SELL) { UD=SH_SELL; }
      
      if (Close[SH]>SHMax-(SHMax-SHMin)*Otstup/100 && UD!=SH_BUY) { UD=SH_BUY; }
   

       
  
  if( UD > 0) 
      {
      if(myMA1 >= myMA2)
        {
        if(m ==0)
            {
            Buy1=1;
            }
         }
       }
       
  if( UD < 0) 
      {
      if(myMA1 <= myMA2)
        {
        if(m ==0)
            {
            Sell1=1;
            }
         }
       }
        
               
   Comment("Open Orders = ",MyOrdersTotal(),"   UD =  ",UD,"\n",m + " minutes " + s + " seconds left to bar end",
   "\nMA 5 = ",myMA1,"  MA 9 = ",myMA2);  
   
   bool CLOSEBuyOrder=false;
   bool CLOSESellOrder=false;
   if(UD < 0 ) CLOSEBuyOrder=true;
   if(UD > 0 ) CLOSESellOrder=true;  
      
   
//          ====== 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 (StopLossMode==true) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
         if (UseCloseSignal==true) TakeProfit= 0.0; else TakeProfit = Ask+TakeProfit*Point;
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,StopLossLevel,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 (StopLossMode==true) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
         if (UseCloseSignal==true) TakeProfit= 0.0; else TakeProfit = Bid-TakeProfit*Point;
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,StopLossLevel,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(UseCloseSignal==true && CLOSEBuyOrder == true )
                  {
                  OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); 
                  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);              
                        }
                     }     
                  }
                  if(UseCloseSignal==true && CLOSESellOrder == true )
                  {
                  OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); 
                  return(0); 
                  }
                }
             }
          }

   return(0);
  }
   
   
   
//          ====== Sub-Routines ======
   return(0);
   
   int MyOrdersTotal() { // included by Renato
   int Mytotal=0; 
   for (int w=0; w<OrdersTotal(); w++) { 
     if (OrderSelect(w, SELECT_BY_POS, MODE_TRADES)) 
       if((OrderSymbol()==Symbol())&& (OrderMagicNumber()==12345)) 
          Mytotal++; 
   }  
   return(Mytotal); 
} 
  
//+---------------------- The End -----------------------------