Detected Country () : English
Non Farm Payroll News Trap EA - codingforex.com

Welcome to CodingForex.Com!

Knowledge portal where you can learn how to create forex indicators and robot.

Click button below to choose level that suite you:

Non Farm Payroll News Trap EA

Samples [2018-06-25 09:52:45] - by hillzx
Featured Image

Once a month on Friday at the beginning of the month, the majority of forex traders look forward to the release of Non Farm Payroll (NFP) economic news, usually the NFP news resulting in price spikes on the USD pair, this price spike is used by traders to profit from forex trading.

Because the NFP is closely related to fundamental economic factors, technical traders look for opportunities through the trap of pending orders by selling and buying at the same time by setting SellStop and BuyStop to profit.

In order to facilitate the process of trapping price spikes, we can create a simple EA to conduct pending orders selling and buying transactions simultaneously with just a click of a button. (See picture below):

newstrap2

There are 3 buttons:

  • Set Trap Button: Perform pending sell and sell transaction simultaneously.
  • Del Trap button: Cancel pending order transactions
  • Close button: Close all transactions



Well let's start making EA,

First we create an extern variable to accept the input:

extern double LotSize=0.01;
extern int Delay=300;
extern int StopLoss=300;
extern int TakeProfit=2500;
int Slippage=5;

 

Next we create the OnInit and OnDeinit modules, we create 3 buttons on OnInit, then delete all buttons on OnDeinit:

int OnInit()
{
//---
int x=10;

CreateButton("button1","Set Trap",x,20,80,30,Yellow,Green,White,12);
CreateButton("button2","Del Trap",x,20,80,30,Yellow,Maroon,White,12);
CreateButton("button3","Close "+Symbol(),x,20,150,30,White,Gray,White,12);

//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
DeleteButtons();
  }

 

Next we create the CreateButton and DeleteButton module, functions to create and delete buttons:

void CreateButton(string btnName,string btnText,int &x,int y,int w,int h,color clrText,color clrBg,color clrBorder,int fontSize)
{
   ObjectCreate(0,btnName,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,btnName,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(0,btnName,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(0,btnName,OBJPROP_XSIZE,w);
   ObjectSetInteger(0,btnName,OBJPROP_YSIZE,h);
   ObjectSetString(0,btnName,OBJPROP_TEXT,btnText);
   ObjectSetInteger(0,btnName,OBJPROP_COLOR,clrText);
   ObjectSetInteger(0,btnName,OBJPROP_BGCOLOR,clrBg);
   ObjectSetInteger(0,btnName,OBJPROP_BORDER_COLOR,clrBorder);
   ObjectSetInteger(0,btnName,OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,btnName,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,btnName,OBJPROP_STATE,false);
   ObjectSetInteger(0,btnName,OBJPROP_FONTSIZE,fontSize);
   x=x+w+10;
}
  
void DeleteButtons()
{
   for(int i=ObjectsTotal()-1; i>-1; i--)
     {
      if(StringFind(ObjectName(i),"button")>=0) ObjectDelete(ObjectName(i));
     }
}



Do not forget to capture click event:

  void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
      if(sparam=="button1") // Buy
        {
         Action_Button1();
         ObjectSetInteger(0,"button1",OBJPROP_STATE,false);
        }
      else if(sparam=="button2") // Sell
        {
         Action_Button2();
         ObjectSetInteger(0,"button2",OBJPROP_STATE,false);
        }
      else if(sparam=="button3") // Close This
        {
         Action_Button3();
         ObjectSetInteger(0,"button3",OBJPROP_STATE,false);
        }
  
  
  }

 

Next, create action for buttons :

void Action_Button1(){
   if(MessageBox("Set Trap "+ DoubleToStr(LotSize,2) + " Lot with Delay:" + Delay + " points, StopLoss:" + StopLoss + " points and TakeProfit:" + TakeProfit + " points ?","Order News Trap",MB_YESNO)==IDYES)
   {
      OrderBuyStop();
      OrderSellStop();
      PlaySound("ok.wav");
   }
}


void Action_Button2(){
   if(MessageBox("Delete News Trap Pending Orders ?","Delete Trap?",MB_YESNO)==IDYES)
   {
      ClosePO();
      PlaySound("ok.wav");
   }
}


void Action_Button3(){
   if(MessageBox("Close all floating orders on pair  "+ Symbol() + " ?","Close Orders",MB_YESNO)==IDYES)
   {
      CloseThis();
      PlaySound("ok.wav");
   }
}



Next, we create the code for the transaction OrderBuyStop, OrderSellStop, CloseThis, ClosePO:

void OrderBuyStop(){
int ticket;
   for(int i=0;i<3;i++)
   {
      double StopPrice = Ask + (Delay*Point);
      ticket=OrderSend(Symbol(),OP_BUYSTOP,LotSize,StopPrice,Slippage,StopPrice-StopLoss*Point,StopPrice+TakeProfit*Point,"HiLLzX News Trap Buy");
      if(ticket>0){break;}    
      RefreshRates();     
   }

}

void OrderSellStop(){
int ticket;
   for(int i=0;i<3;i++)
   {
      double StopPrice = Bid - (Delay*Point);
      ticket=OrderSend(Symbol(),OP_SELLSTOP,LotSize,StopPrice,Slippage,StopPrice+StopLoss*Point,StopPrice-TakeProfit*Point,"HiLLzX News Trap Sell");
      if(ticket>0){break;}    
      RefreshRates();     
   }
}

void CloseThis() {
   bool closed = false;
   for (int i = OrdersTotal(); i >=0; i--) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      while(IsTradeContextBusy()) Sleep(100);
      RefreshRates();
      if (OrderType() == OP_BUY && Symbol() == OrderSymbol() )
       {
        closed = OrderClose( OrderTicket(), OrderLots(), Bid, Slippage, White);
      }
      if (OrderType() == OP_SELL && Symbol() == OrderSymbol())
        {
        closed = OrderClose( OrderTicket(), OrderLots(), Ask, Slippage, White);
      }
   }
return;

}

void ClosePO() {
int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return;
}

 

EA has been made, after the button is executed then EA will install 2 pieces pending order at once accompanied by SL and TP (see picture):

newstrap1

 

If you need the full source code, please copy the code below:

//+------------------------------------------------------------------+
//|                                                    News Trap.mq4 |
//|                                                 hillzx@gmail.com |
//|                                                 hillzx@gmail.com |
//+------------------------------------------------------------------+
#property copyright "hillzx@gmail.com"
#property link      "hillzx@gmail.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

extern double LotSize=0.01;
extern int Delay=300;
extern int StopLoss=300;
extern int TakeProfit=2500;
int Slippage=5;

int OnInit()
{
//---
int x=10;

CreateButton("button1","Set Trap",x,20,80,30,Yellow,Green,White,12);
CreateButton("button2","Del Trap",x,20,80,30,Yellow,Maroon,White,12);
CreateButton("button3","Close "+Symbol(),x,20,150,30,White,Gray,White,12);

//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
DeleteButtons();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+

void CreateButton(string btnName,string btnText,int &x,int y,int w,int h,color clrText,color clrBg,color clrBorder,int fontSize)
{
   ObjectCreate(0,btnName,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,btnName,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(0,btnName,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(0,btnName,OBJPROP_XSIZE,w);
   ObjectSetInteger(0,btnName,OBJPROP_YSIZE,h);
   ObjectSetString(0,btnName,OBJPROP_TEXT,btnText);
   ObjectSetInteger(0,btnName,OBJPROP_COLOR,clrText);
   ObjectSetInteger(0,btnName,OBJPROP_BGCOLOR,clrBg);
   ObjectSetInteger(0,btnName,OBJPROP_BORDER_COLOR,clrBorder);
   ObjectSetInteger(0,btnName,OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,btnName,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,btnName,OBJPROP_STATE,false);
   ObjectSetInteger(0,btnName,OBJPROP_FONTSIZE,fontSize);
   x=x+w+10;
}
  
void DeleteButtons()
{
   for(int i=ObjectsTotal()-1; i>-1; i--)
     {
      if(StringFind(ObjectName(i),"button")>=0) ObjectDelete(ObjectName(i));
     }
}
  
  
  void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
      if(sparam=="button1") // Buy
        {
         Action_Button1();
         ObjectSetInteger(0,"button1",OBJPROP_STATE,false);
        }
      else if(sparam=="button2") // Sell
        {
         Action_Button2();
         ObjectSetInteger(0,"button2",OBJPROP_STATE,false);
        }
      else if(sparam=="button3") // Close This
        {
         Action_Button3();
         ObjectSetInteger(0,"button3",OBJPROP_STATE,false);
        }
  
  
  }
  
void Action_Button1(){
   if(MessageBox("Set Trap "+ DoubleToStr(LotSize,2) + " Lot with Delay:" + Delay + " points, StopLoss:" + StopLoss + " points and TakeProfit:" + TakeProfit + " points ?","Order News Trap",MB_YESNO)==IDYES)
   {
      OrderBuyStop();
      OrderSellStop();
      PlaySound("ok.wav");
   }
}


void Action_Button2(){
   if(MessageBox("Delete News Trap Pending Orders ?","Delete Trap?",MB_YESNO)==IDYES)
   {
      ClosePO();
      PlaySound("ok.wav");
   }
}


void Action_Button3(){
   if(MessageBox("Close all floating orders on pair  "+ Symbol() + " ?","Close Orders",MB_YESNO)==IDYES)
   {
      CloseThis();
      PlaySound("ok.wav");
   }
}

void OrderBuyStop(){
int ticket;
   for(int i=0;i<3;i++)
   {
      double StopPrice = Ask + (Delay*Point);
      ticket=OrderSend(Symbol(),OP_BUYSTOP,LotSize,StopPrice,Slippage,StopPrice-StopLoss*Point,StopPrice+TakeProfit*Point,"HiLLzX News Trap Buy");
      if(ticket>0){break;}    
      RefreshRates();     
   }

}

void OrderSellStop(){
int ticket;
   for(int i=0;i<3;i++)
   {
      double StopPrice = Bid - (Delay*Point);
      ticket=OrderSend(Symbol(),OP_SELLSTOP,LotSize,StopPrice,Slippage,StopPrice+StopLoss*Point,StopPrice-TakeProfit*Point,"HiLLzX News Trap Sell");
      if(ticket>0){break;}    
      RefreshRates();     
   }
}

void CloseThis() {
   bool closed = false;
   for (int i = OrdersTotal(); i >=0; i--) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      while(IsTradeContextBusy()) Sleep(100);
      RefreshRates();
      if (OrderType() == OP_BUY && Symbol() == OrderSymbol() )
       {
        closed = OrderClose( OrderTicket(), OrderLots(), Bid, Slippage, White);
      }
      if (OrderType() == OP_SELL && Symbol() == OrderSymbol())
        {
        closed = OrderClose( OrderTicket(), OrderLots(), Ask, Slippage, White);
      }
   }
return;

}

void ClosePO() {
int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return;
}


Let us try, do not forget to share and follow our facebook and our youtube channel, happy trading!






- Provided by HiLLzSoft Studio -