//+------------------------------------------------------------------+
//|                                                          NFP.mq4 |
//|                                            Copyright © 2011, AM2 |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, AM2"
#property link      "http://www.forexsystems.biz"
#define MAGIC  20110406
extern int StopLoss    = 300;    //Ñòîïëîññ îðäåðà
extern int TakeProfit  = 2000;   //Òåéêïðîôèò îðäåðà
extern int Trailing    = 300;    //Òðåéëèíãcòîï îðäåðà
extern int BULevel     = 300;    //Óðîâåíü áåçóáûòêà
extern int StartHour   = 14;     //×àñ íà÷àëà òîðãîâëè(òåðìèíàëüíîå âðåìÿ)
extern int EndHour     = 23;     //×àñ îêîí÷àíèÿ òîðãîâëè(òåðìèíàëüíîå âðåìÿ)
extern int Distance    = 300;    //Ðàññòîÿíèå îò öåíû äëÿ óñòàíîâêè îðäåðà
extern int Expiration  = 4;      //Âðåìÿ èñòå÷åíèÿ îðäåðà
extern double Lots     = 1;      //Ëîò
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   int b,s,p,res;
   datetime expiration = TimeCurrent()+3600*Expiration;
   double BuyPrice=fND(Open[0]+Distance*Point);
   double SellPrice=fND(Open[0]-Distance*Point);   
   for (int i=OrdersTotal()-1;i>=0;i--)
     {
      if (OrderSelect(i, SELECT_BY_POS))
        {  
         if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=MAGIC) continue;
         if (OrderType()==OP_BUYSTOP) b++;
         if (OrderType()==OP_SELLSTOP) s++;
         if (OrderType()==OP_BUY||OrderType()==OP_SELL) p++;
        }   
     }
   if(p>0) DelAllStop();
   if (BULevel!=0) BU(); 
   if(Trailing>0) Trail();
   if(Hour()==EndHour) ClosePositions();  
   if(Hour()>=StartHour && b<1 && p<1 && DayOfWeek()==5 && Day()<=7)
     {               
       res=OrderSend(Symbol(),OP_BUYSTOP,Lots,BuyPrice,0,fND(BuyPrice-StopLoss*Point),fND(BuyPrice+TakeProfit*Point),"",MAGIC,expiration,Blue);      
     }  
         
   if(Hour()>=StartHour && s<1 && p<1 && DayOfWeek()==5 && Day()<=7) 
     {               
       res=OrderSend(Symbol(),OP_SELLSTOP,Lots,SellPrice,0,fND(SellPrice+StopLoss*Point),fND(SellPrice-TakeProfit*Point),"",MAGIC,expiration,Red );
     }   
//----   
   return(0);
  }
//+------------------------------------------------------------------+
double fND(double d, int n=-1) 
  {  
   if (n<0) return(NormalizeDouble(d, Digits)); 
   return(NormalizeDouble(d, n)); 
  }
//+------------------------------------------------------------------+
void ClosePositions()
  {
   for (int i=OrdersTotal()-1;i>=0;i--)
    {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol())
       {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC)
          {
            if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);
            if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
          }
      }
    }
  }
//+------------------------------------------------------------------+
void DelAllStop()
{
  for (int i=0; i<OrdersTotal(); i++)
   {                                               
     if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
      {
         if (OrderSymbol()!=Symbol()||OrderMagicNumber()!=MAGIC) continue;
         if (OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP) OrderDelete(OrderTicket());
      }   
   }
}
//-------------------------------------------------------------------+
void Trail()
{
   for (int i=0; i<OrdersTotal(); i++) 
     {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if (OrderSymbol()==Symbol()||OrderMagicNumber()==MAGIC)
       if(OrderType()==OP_BUY)
         {
           if(Trailing>0)  
            {                 
              if(Bid-OrderOpenPrice()>Point*Trailing)
               {
                 if(OrderStopLoss()<Bid-Point*Trailing)
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*Trailing,OrderTakeProfit(),0,Green);
                  }
               }
            }
         }
         
        if(OrderType()==OP_SELL)
         {
           if(Trailing>0)  
            {                 
              if((OrderOpenPrice()-Ask)>(Point*Trailing))
               {
                 if((OrderStopLoss()>(Ask+Point*Trailing)) || (OrderStopLoss()==0))
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*Trailing,OrderTakeProfit(),0,Red);
                  }
               }
            }
         }   
   }
}
//+------------------------------------------------------------------+
void BU()
{
   for(int i=0; i<OrdersTotal(); i++) 
      {
       if(OrderSelect(i, SELECT_BY_POS))
        {      
         if(OrderType()==OP_BUY) 
          {
           if(OrderOpenPrice()<=(Bid-BULevel*Point)&&OrderOpenPrice()>OrderStopLoss())
            {      
             OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
            }
          } 
             
         if(OrderType() == OP_SELL) 
          {
           if(OrderOpenPrice()>=(Ask+BULevel*Point)&&OrderOpenPrice()<OrderStopLoss()) 
            {
             OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);
            }
          } 
        }
      }
}
//--------------------------------------------------------------------+
             
            
            
            
Comments