Navigation:Home > Content >

NewsBreakv3.mq4

Time: 2012-09-21 | Download file:NewsBreakv3.mq4

//+------------------------------------------------------------------+
//|                                                  NewsBreakv3.mq4 |
//|                                       Copyright © 2006, kmrunner |
//|                                                  August 19, 2006 |
//+------------------------------------------------------------------+
#define MAGIC1  20060819
#define MAGIC2  12345678
#include 
#define     NL    "\n"

extern bool    wantNews1        = true;
extern int     NewsHour1        = 11;
extern int     NewsMinutes1     = 0;
extern bool    wantNews2        = true;
extern int     NewsHour2        = 12;
extern int     NewsMinutes2     = 30;
extern double  NewsLots         = 5;
extern int     NewsPips         = 15;
extern int     NewsTP           = 60;
extern int     NewsSL           = 55;
extern int     BEpips           = 60;
extern int     TrailingStop     = 35;
extern bool    wantNewsBO       = true; 
extern bool    wantSpreadBO     = true;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
  
//+------------------------------------------------------------------+
//| CheckForPendingClose1 conditions                                 |
//+------------------------------------------------------------------+ 
void CheckForPendingClose1()
  {
     int  totalorders = OrdersTotal();
     
     for(int i=totalorders-1;i>=0;i--)                              
      {
        OrderSelect( i, SELECT_BY_POS);
        if ( OrderSymbol()==Symbol() && OrderMagicNumber() == MAGIC1)  
         {  
          if ( OrderType() == OP_BUYSTOP ) bool result = OrderDelete( OrderTicket() );
          if ( OrderType() == OP_SELLSTOP )     result = OrderDelete( OrderTicket() );
         }
      } 
   return;
 }

//+------------------------------------------------------------------+
//| CheckForPendingClose2 conditions                                 |
//+------------------------------------------------------------------+ 
void CheckForPendingClose2()
  {
     int  totalorders = OrdersTotal();
     
     for(int i=totalorders-1;i>=0;i--)                              
      {
        OrderSelect( i, SELECT_BY_POS);
        if ( OrderSymbol()==Symbol() && OrderMagicNumber() == MAGIC2)  
         {  
          if ( OrderType() == OP_BUYSTOP ) bool result = OrderDelete( OrderTicket() );
          if ( OrderType() == OP_SELLSTOP )     result = OrderDelete( OrderTicket() );
         }
      } 
   return;
 }
 
//+------------------------------------------------------------------+
//| Break even code                                                  |
//+------------------------------------------------------------------+ 
 void DoBE(int byPips)
  {
    for (int i = 0; i < OrdersTotal(); i++) {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if ( OrderSymbol()==Symbol() && (OrderMagicNumber() == MAGIC2))  
        {
            if (OrderType() == OP_BUY) if (Bid - OrderOpenPrice() > byPips * Point) if (OrderStopLoss() < OrderOpenPrice()) {
              OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() +  Point, OrderTakeProfit(), Red);
            }
            if (OrderType() == OP_SELL) if (OrderOpenPrice() - Ask > byPips * Point) if (OrderStopLoss() > OrderOpenPrice()) { 
               OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() -  Point, OrderTakeProfit(), Red);
            }
        }
    }
  }

//+------------------------------------------------------------------+
//| Trailing stop code                                               |
//+------------------------------------------------------------------+ 
  void DoTrail()
  {
    for (int i = 0; i < OrdersTotal(); i++) {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if ( OrderSymbol()==Symbol() && (OrderMagicNumber() == MAGIC2))  
        {
          
          if (OrderType() == OP_BUY) {
             if(Bid-OrderOpenPrice()>Point*TrailingStop)
             {
                if(OrderStopLoss()(Point*TrailingStop))
             {
                if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                {
                   OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                   return(0);
                }
             }
          }
       }
    }
 }
    
//+------------------------------------------------------------------+
//| Check for NewsBreak1 order conditions                            |
//+------------------------------------------------------------------+
void CheckForNewsBreak1()
  {
   double h=iHigh(Symbol(),PERIOD_M1,0);
   double l=iLow(Symbol(),PERIOD_M1,0);
   double sp=Ask-Bid;
   double ho=h+sp+NewsPips*Point;
   double lo=l-sp-NewsPips*Point;
   double hsl=ho-NewsSL*Point;
   double lsl=lo+NewsSL*Point;
   double htp=ho+NewsTP*Point;
   double ltp=lo-NewsTP*Point;
   
 //---- breakout conditions
     
      OrderSend(Symbol(),OP_BUYSTOP,NewsLots,ho,3,hsl,htp,"",MAGIC1,0,Green);
      OrderSend(Symbol(),OP_SELLSTOP,NewsLots,lo,3,lsl,ltp,"",MAGIC1,0,Red);
     
 } 
     
//+------------------------------------------------------------------+
//| Check for NewsBreak2 order conditions                            |
//+------------------------------------------------------------------+
void CheckForNewsBreak2()
  {
   double h=iHigh(Symbol(),PERIOD_M1,0);
   double l=iLow(Symbol(),PERIOD_M1,0);
   double sp=Ask-Bid;
   double ho=h+sp+NewsPips*Point;
   double lo=l-sp-NewsPips*Point;
   double hsl=ho-NewsSL*Point;
   double lsl=lo+NewsSL*Point;
   double htp=ho+NewsTP*Point;
   double ltp=lo-NewsTP*Point;
   double lastclose = iClose(Symbol(), 1, 1);
 
     
      if(sp > 8 && Bid>=lastclose+NewsPips*Point || sp > 8 && Ask<=lastclose-NewsPips*Point)
       {
        OrderSend(Symbol(),OP_BUYSTOP,NewsLots,ho,3,hsl,htp,"",MAGIC2,0,PaleGreen);
        OrderSend(Symbol(),OP_SELLSTOP,NewsLots,lo,3,lsl,ltp,"",MAGIC2,0,Pink);
       }
       
 }
      
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
      
   double  i, OpenBuyLots1, BuyOrders1, OpenSellLots1, SellOrders1, OpenOrders1;
   double  CurrencyProfit1, BuyProfit1, SellProfit1, OpenLots1, TotalLotsOpen;
   double  OpenBuyLots2, BuyOrders2, OpenSellLots2, SellOrders2, OpenOrders2;
   double  CurrencyProfit2, BuyProfit2, SellProfit2, OpenLots2, CurrencyProfit;
   double  NewsTime1 = (NewsHour1*3600)+(NewsMinutes1*60)-90;
   double  NewsTime2 = (NewsHour2*3600)+(NewsMinutes2*60)-90;
   double  PendingBuyStop1, PendingSellStop1, PendingBuyStop2, PendingSellStop2;
   double  CurrentTime = (TimeHour(CurTime())*3600+TimeMinute(CurTime())*60);
   
   for(i = 0; i < OrdersTotal(); i++)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      TotalLotsOpen += OrderLots();
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC1)
        {
         if (OrderType() == OP_BUY)
          {
           OpenBuyLots1 += OrderLots();  
           BuyOrders1 ++;
           BuyProfit1 +=  OrderProfit();
          }
         if (OrderType() == OP_SELL)
          {
           OpenSellLots1 += OrderLots();  
           SellOrders1 ++;
           SellProfit1 +=  OrderProfit();
          }
          if (OrderType() == OP_BUYSTOP)
          {
           PendingBuyStop1 ++;
          }
          if (OrderType() == OP_SELLSTOP)
          {
           PendingSellStop1 ++;
          }
          OpenOrders1 = BuyOrders1 + SellOrders1;
          OpenLots1 = OpenBuyLots1 + OpenSellLots1;
          }
          
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC2)
        {
         if (OrderType() == OP_BUY)
          {
           OpenBuyLots2 += OrderLots();  
           BuyOrders2 ++;
           BuyProfit2 +=  OrderProfit();
          }
         if (OrderType() == OP_SELL)
          {
           OpenSellLots2 += OrderLots();  
           SellOrders2 ++;
           SellProfit2 +=  OrderProfit();
          }
          if (OrderType() == OP_BUYSTOP)
          {
           PendingBuyStop2 ++;
          }
          if (OrderType() == OP_SELLSTOP)
          {
           PendingSellStop2 ++;
          }
          OpenOrders2 = BuyOrders2 + SellOrders2;
          OpenLots2 = OpenBuyLots2 + OpenSellLots2;
        }
     }
   
   CurrencyProfit = BuyProfit1 + SellProfit1 + BuyProfit2 + SellProfit2;
   
   if(CurrentTime >= NewsTime1 + 120 && CurrentTime < NewsTime2 - 120) CheckForPendingClose1();
   if(CurrentTime >= NewsTime2 + 120 || CurrentTime < NewsTime1 - 120) CheckForPendingClose1();
   if(PendingBuyStop1 + PendingSellStop1 != 2)  CheckForPendingClose1();
   if(PendingBuyStop2 + PendingSellStop2 != 2)  CheckForPendingClose2();
     
   if (BEpips>0) DoBE(BEpips);
   if (TrailingStop>0) DoTrail();
   
   if(wantNews1 && CurrentTime >= NewsTime1 && CurrentTime <= NewsTime1 + 90 
   && PendingBuyStop1 == 0 && PendingSellStop1 == 0 && OpenOrders1 == 0) CheckForNewsBreak1();
   if(wantNews2 && CurrentTime >= NewsTime2 && CurrentTime <= NewsTime2 + 90 
   && PendingBuyStop1 == 0 && PendingSellStop1 == 0 && OpenOrders1 == 0) CheckForNewsBreak1();
   if(PendingBuyStop2 + PendingSellStop2 == 0) CheckForNewsBreak2();
                                       
 Comment(" TotalLotsOpen ", TotalLotsOpen,NL,
         " CurrencyProfit ", CurrencyProfit,NL,
         " NewsTime1 ", (NewsHour1*3600)+(NewsMinutes1*60),NL,
         " NewsTime2 ", (NewsHour2*3600)+(NewsMinutes2*60), NL,
         " Current Time is  ",TimeHour(CurTime()),":",TimeMinute(CurTime()),".",TimeSeconds(CurTime()));
   return;      
  }
//+------------------------------------------------------------------+

Recommend