Navigation:Home > Content >

NewsBreakv9r1.mq4

Time: 2018-12-08 | Download file:NewsBreakv9r1.mq4

//+------------------------------------------------------------------+
//|                                                  NewsBreakv8.mq4 |
//|                                       Copyright © 2006, kmrunner |
//|                                                  August 19, 2006 |
//+------------------------------------------------------------------+
#define MAGIC1  11111111
#define MAGIC2  22222222
#define MAGIC3  33333333
#define MAGIC4  44444444
#define MAGIC5  55555555

#include 
#define     NL    "\n"

extern int     RiskPercent    = 100;
 
extern bool    wantNews1      = true;
extern bool    wantOCONews1   = true;
extern int     NewsHour1      = 12;
extern int     NewsMinutes1   = 30;

extern bool    wantNews2      = true;
extern bool    wantOCONews2   = true;
extern int     NewsHour2      = 12;
extern int     NewsMinutes2   = 31;

extern int     secondsafter   = 120;
extern int     secondsbefore  = 90;
extern double  NewsLots       = 5;
extern int     NewsPips       = 5;
extern int     NewsTP         = 85;
extern int     NewsSL         = 60;
extern int     NewsLookBack   = 5;
extern int     BEpips         = 60;
extern int     TrailingStop   = 40;

extern bool    wantSpreadBO1  = false;
extern bool    wantSpreadBO2  = false;
extern int     VarSpread      = 8;
extern int     BBTF           = 1;
extern int     BarsForRange   = 15;

extern bool    wantBBbuys     = true;
extern double  BBBuyLots      = 1;
extern int     BuyBarPips     = 12;
extern int     BuyBarTP       = 25;
extern int     BuyBarSL       = 125;

extern bool    wantBBsells    = true;
extern double  BBSellLots     = 0.5;
extern int     SellBarPips    = 12;
extern int     SellBarTP      = 25;
extern int     SellBarSL      = 125;
extern int     BOBEpips       = 60;
extern int     BOTrailingStop = 40;

//+------------------------------------------------------------------+
//| 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;
 }
//+------------------------------------------------------------------+
//| CheckForPendingClose1 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;
 }
 //+------------------------------------------------------------------+
//| CheckForPendingClose1 conditions                                 |
//+------------------------------------------------------------------+ 
void CheckForPendingClose3()
  {
     int  totalorders = OrdersTotal();
     
     for(int i=totalorders-1;i>=0;i--)                              
      {
        OrderSelect( i, SELECT_BY_POS);
        if (OrderSymbol()==Symbol() && OrderMagicNumber() == MAGIC3)  
         {  
          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() == MAGIC1) || (OrderMagicNumber() == MAGIC2) || (OrderMagicNumber() == MAGIC3) || (OrderMagicNumber() == MAGIC4) || (OrderMagicNumber() == MAGIC5))  
        {
            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() == MAGIC1) || (OrderMagicNumber() == MAGIC2)|| (OrderMagicNumber() == MAGIC3)|| (OrderMagicNumber() == MAGIC4)|| (OrderMagicNumber() == MAGIC5))  
        {
          
          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=High[Highest(NULL,0,MODE_HIGH,NewsLookBack,0)];
   double l=Low[Lowest(NULL,0,MODE_LOW,NewsLookBack,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 NewsBreak1 order conditions                            |
//+------------------------------------------------------------------+
void CheckForNewsBreak2()
  {
   double h=High[Highest(NULL,0,MODE_HIGH,NewsLookBack,0)];
   double l=Low[Lowest(NULL,0,MODE_LOW,NewsLookBack,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,"",MAGIC2,0,Green);
      OrderSend(Symbol(),OP_SELLSTOP,NewsLots,lo,3,lsl,ltp,"",MAGIC2,0,Red);
 } 
     
//+------------------------------------------------------------------+
//| Check for SpreadBreak1 order conditions                          |
//+------------------------------------------------------------------+
void CheckForSpreadBreak1()
  {
   double h=High[Highest(NULL,0,MODE_HIGH,NewsLookBack,0)];
   double l=Low[Lowest(NULL,0,MODE_LOW,NewsLookBack,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 NewsTime1 = (NewsHour1*3600)+(NewsMinutes1*60);
   double NewsTime2 = (NewsHour2*3600)+(NewsMinutes2*60);
   double openbar1  = iBarShift(NULL, 0, NewsTime1);
   double openbar2  = iBarShift(NULL, 0, NewsTime2);
  
      if(sp > VarSpread && Bid>=openbar1+NewsPips*Point || sp > VarSpread && Ask<=openbar1-NewsPips*Point)
       {
        OrderSend(Symbol(),OP_BUYSTOP,NewsLots,ho,3,hsl,htp,"",MAGIC3,0,PaleGreen);
        OrderSend(Symbol(),OP_SELLSTOP,NewsLots,lo,3,lsl,ltp,"",MAGIC3,0,Pink);
       }
      if(sp > VarSpread && Bid>=openbar2+NewsPips*Point || sp > VarSpread && Ask<=openbar2-NewsPips*Point)
       {
        OrderSend(Symbol(),OP_BUYSTOP,NewsLots,ho,3,hsl,htp,"",MAGIC3,0,PaleGreen);
        OrderSend(Symbol(),OP_SELLSTOP,NewsLots,lo,3,lsl,ltp,"",MAGIC3,0,Pink);
       }
       
  }

//+------------------------------------------------------------------+
//| Check for SpreadBreak2 order conditions                          |
//+------------------------------------------------------------------+
void CheckForSpreadBreak2()
  {
   double hprice=High[Highest(NULL,PERIOD_M1,MODE_HIGH,4,0)];
   double lprice=Low[Lowest(NULL,PERIOD_M1,MODE_LOW,4,0)];
   double h=iHigh(NULL,PERIOD_M1,0);
   double l=iLow(NULL,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;
     
      if(sp > VarSpread  && Bid>=lprice+(NewsPips*Point))
       {
        OrderSend(Symbol(),OP_BUY,NewsLots-(10*Point),Ask,3,hsl,htp,"",MAGIC4,0,Blue);
       }
       
      if(sp > VarSpread  && Ask<=hprice-(NewsPips*Point))
       {
       OrderSend(Symbol(),OP_SELL,NewsLots-(10*Point),Bid,3,lsl,ltp,"",MAGIC4,0,Orange);
       }
 }
//+------------------------------------------------------------------+
//| Check for BB Buy conditions                                      |
//+------------------------------------------------------------------+
void CheckForBBbuys()
  {
   double hprice=High[Highest(NULL,0,MODE_HIGH,BarsForRange,0)];
   double lprice=Low[Lowest(NULL,0,MODE_LOW,BarsForRange,0)];
   
    if(Bid-iOpen(NULL,BBTF,0) >= BuyBarPips*Point) OrderSend(Symbol(),OP_BUY,BBBuyLots,Ask,3,Ask-BuyBarSL*Point,Ask+BuyBarTP*Point,"",MAGIC5,0,LimeGreen);
   }

//+------------------------------------------------------------------+
//| Check for BB Sell conditions                                     |
//+------------------------------------------------------------------+
void CheckForBBsells()
  {
   double hprice=High[Highest(NULL,0,MODE_HIGH,BarsForRange,0)];
   double lprice=Low[Lowest(NULL,0,MODE_LOW,BarsForRange,0)];
   
    if(iOpen(NULL,BBTF,0)-Ask >= SellBarPips*Point) OrderSend(Symbol(),OP_SELL,BBSellLots,Bid,3,Bid+SellBarSL*Point,Bid-SellBarTP*Point,"",MAGIC5,0,Magenta);
   } 
        
//+------------------------------------------------------------------+
//| 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  BuyProfit3, SellProfit3, BuyOrders3, SellOrders3, OpenOrders3;
   double  NewsTime1 = (NewsHour1*3600)+(NewsMinutes1*60)-90;
   double  NewsTime2 = (NewsHour2*3600)+(NewsMinutes2*60)-90;
   double  PendingBuyStop1, PendingSellStop1, PendingBuyStop2, PendingSellStop2;
   double  PendingBuyStop3, PendingSellStop3, buy1, sell1, buy2, sell2, buy3, sell3;
   double  CurrentTime = (TimeHour(CurTime())*3600+TimeMinute(CurTime())*60);
   bool    needpending1 = false;
   bool    needpending2 = false;
   bool    needbuy3     = false;
   bool    needsell3    = false;
   bool    needbuy4     = false;
   bool    needsell4    = false;
   bool    needbuy5     = false;
   bool    needsell5    = false;
   int     barminutes   = Period();
            
   for(i = 0; i < OrdersTotal(); i++)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC1)
        {
         if (OrderType() == OP_BUY) datetime  buyclosedtime1 = OrderCloseTime();
         if (OrderType() == OP_SELL) datetime  sellclosedtime1 = OrderCloseTime();
        }
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC2)
        {
         if (OrderType() == OP_BUY) datetime  buyclosedtime2 = OrderCloseTime();
         if (OrderType() == OP_SELL) datetime  sellclosedtime2 = OrderCloseTime();
        }
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC3)
        {
         if (OrderType() == OP_BUY) datetime  buyclosedtime3 = OrderCloseTime();
         if (OrderType() == OP_SELL) datetime  sellclosedtime3 = OrderCloseTime();
        }
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC4)
        {
         if (OrderType() == OP_BUY) datetime  buyclosedtime4 = OrderCloseTime();
         if (OrderType() == OP_SELL) datetime  sellclosedtime4 = OrderCloseTime();
        }
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC5)
        {
         if (OrderType() == OP_BUY) datetime  buyclosedtime5 = OrderCloseTime();
         if (OrderType() == OP_SELL) datetime  sellclosedtime5 = OrderCloseTime();
        }
      }
      
      for(i = 0; i < OrdersTotal(); i++)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC1)
        {
         if (OrderType() == OP_BUY)
          {
          buy1 ++;
          datetime  buytradetime1 = OrderOpenTime();
          }
         if (OrderType() == OP_SELL)
          {
          sell1 ++;
          datetime  selltradetime1 = OrderOpenTime();
          }
         if (OrderType() == OP_BUYSTOP) datetime PendingBuyTime1 = OrderOpenTime();
         if (OrderType() == OP_SELLSTOP) datetime PendingSellTime1 = OrderOpenTime();
         if (OrderType() == OP_BUYSTOP) PendingBuyStop1 ++;
         if (OrderType() == OP_SELLSTOP) PendingSellStop1 ++;
        }
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC2)
        {
if (OrderType() == OP_BUY)
          {
          buy2 ++;
          datetime  buytradetime2 = OrderOpenTime();
          }
         if (OrderType() == OP_SELL)
          {
          sell2 ++;
          datetime  selltradetime2 = OrderOpenTime();
          } 
         if (OrderType() == OP_BUYSTOP) datetime PendingBuyTime2 = OrderOpenTime();
         if (OrderType() == OP_SELLSTOP) datetime PendingSellTime2 = OrderOpenTime();
         if (OrderType() == OP_BUYSTOP) PendingBuyStop2 ++;
         if (OrderType() == OP_SELLSTOP) PendingSellStop2 ++;
        }
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC3)
        {
if (OrderType() == OP_BUY)
          {
          buy3 ++;
          datetime  buytradetime3 = OrderOpenTime();
          }
         if (OrderType() == OP_SELL)
          {
          sell3 ++;
          datetime  selltradetime3 = OrderOpenTime();
          }
         if (OrderType() == OP_BUYSTOP) PendingBuyStop3 ++;
         if (OrderType() == OP_SELLSTOP) PendingSellStop3 ++;

        }
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC4)
        {
         if (OrderType() == OP_BUY) datetime  buytradetime4 = OrderOpenTime();
         if (OrderType() == OP_SELL) datetime  selltradetime4 = OrderOpenTime();
        }
       if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC5)
        {
         if (OrderType() == OP_BUY) datetime  buytradetime5 = OrderOpenTime();
         if (OrderType() == OP_SELL) datetime  selltradetime5 = OrderOpenTime();
        } 
      }
   
   if(BEpips>0) DoBE(BEpips);
   if(TrailingStop>0) DoTrail();
   if(BOBEpips>0) DoBE(BOBEpips);
   if(BOTrailingStop>0) DoTrail();

         
   if (CurrentTime >= NewsTime1 && CurrentTime < NewsTime1+ secondsbefore 
   && PendingBuyStop1 ==0 && PendingSellStop1 ==0) needpending1 = true;
   if (CurrentTime >= NewsTime1 && CurrentTime < NewsTime1+ secondsbefore
   && PendingBuyStop1 ==0 && PendingSellStop1 ==0) needpending1 = true;
   if (CurrentTime >= NewsTime2 && CurrentTime < NewsTime2+ secondsbefore
   && PendingBuyStop2 ==0 && PendingSellStop2 ==0) needpending2 = true;
   if (CurrentTime >= NewsTime2 && CurrentTime < NewsTime2+ secondsbefore
   && PendingBuyStop2 ==0 && PendingSellStop2 ==0) needpending2 = true;
   
   if (CurrentTime >= (NewsTime1 + secondsbefore + secondsafter) && CurrentTime < (NewsTime1 + 600)) CheckForPendingClose1();
   if (CurrentTime >= (NewsTime2 + secondsbefore + secondsafter)) CheckForPendingClose2();
   if (wantOCONews1 && buy1>0 || sell1>0 && PendingBuyStop1 ==0 && PendingSellStop1 >= 1 
   || PendingBuyStop1 >= 1 && PendingSellStop1 == 0)  CheckForPendingClose1();
   if (wantOCONews2 && buy2>0|| sell2>0 && PendingBuyStop2 ==0 && PendingSellStop2 >= 1 
   || PendingBuyStop2 >=1 && PendingSellStop2 == 0)  CheckForPendingClose2();
   if (PendingBuyStop1 + PendingSellStop1 != 2) CheckForPendingClose1();
   if (PendingBuyStop2 + PendingSellStop2 != 2) CheckForPendingClose2();
   if (PendingBuyStop3 + PendingSellStop3 != 2) CheckForPendingClose3();
   if (buy1>0 || sell1>0 && PendingBuyStop1 + PendingSellStop1 != 2) CheckForPendingClose1();
   if (buy2>0 || sell2>0 && PendingBuyStop2 + PendingSellStop2 != 2) CheckForPendingClose2();
   if (buy3>0 || sell3>0 && PendingBuyStop3 + PendingSellStop3 != 2) CheckForPendingClose3();
   
   if(buytradetime3  < Time[1]+ (barminutes*60)) needbuy3  = true;
   if(selltradetime3 < Time[1]+ (barminutes*60)) needsell3 = true;
   if(buytradetime4  < Time[1]+ (barminutes*60)) needbuy4  = true;
   if(selltradetime4 < Time[1]+ (barminutes*60)) needsell4 = true;
   if(buytradetime5  < Time[1]+ (barminutes*60)) needbuy5  = true;
   if(selltradetime5 < Time[1]+ (barminutes*60)) needsell5 = true;

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()));

   if((AccountMargin()/AccountBalance())>RiskPercent/100) return;
   
   if(wantNews1 && needpending1 && CurrentTime >= NewsTime1 && CurrentTime <= NewsTime1 + secondsbefore 
   && PendingBuyStop1 == 0 && PendingSellStop1 == 0) CheckForNewsBreak1();
   if(wantNews2 && needpending2 && CurrentTime >= NewsTime2 && CurrentTime <= NewsTime2 + secondsbefore 
   && PendingBuyStop2 == 0 && PendingSellStop2 == 0) CheckForNewsBreak2();
   if(PendingBuyStop3 + PendingSellStop3 == 0) CheckForSpreadBreak1();
   
   if(needbuy3) CheckForSpreadBreak1();
   if(needsell3) CheckForSpreadBreak1();
   if(needbuy4) CheckForSpreadBreak2();
   if(needsell4) CheckForSpreadBreak2();

   if(wantBBbuys && needbuy5) CheckForBBbuys();
   if(wantBBsells && needsell5) CheckForBBsells();
                                       
   return;      
  }
//+------------------------------------------------------------------+

Recommend