Detected Country () : English
Create Net Trap Pending Order 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:

Create Net Trap Pending Order EA

Samples [2018-05-01 13:41:57] - by hillzx
Featured Image

Here's how to create an EA to create a pending order repetition with distance / delay between transactions, because the order is piled up then it looks like a net.

Input installed:

  1. LotSize 
  2. StopLoss 
  3. TakeProfit
  4. DelayBetweenOrder 
  5. Repetition 
  6. UseMartingale 

If UseMartingale = Off, then the lotsize lot is always the same

If UseMartingale = On, then the lotsize lot will keep doubling

nets input

 

There is a button feature to perform Buy and Sell transactions, as well as buttons to close transactions:

nets button

 

First create an extern variable:

extern double LotSize=0.01;
extern int StopLoss=0;
extern int TakeProfit=0;
extern int DelayBetweenOrder=100;
extern int Repetition=10;
extern bool UseMartingale=false;

int Slippage=5;

 

Then create OnInit, OnDeinit, and OnTick modules:

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

CreateButton("button1","Net Buy",x,20,120,30,Yellow,Green,White,12);
CreateButton("button2","Net Sell",x,20,120,30,Yellow,Maroon,White,12);
CreateButton("button3","Close PO "+Symbol(),x,20,160,30,White,Gray,White,12);
CreateButton("button4","Close "+Symbol(),x,20,160,30,White,Gray,White,12);

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



Create the CreateButton and DeleteButton module:

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

 

Create an OnChartEvent module 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);
        }
      else if(sparam=="button4") // Close This
        {
         Action_Button4();
         ObjectSetInteger(0,"button4",OBJPROP_STATE,false);
        }
 
  }



Create module Button1, Button2, Button3, Button4:

void Action_Button1(){
   if(MessageBox("Buy " + Repetition + "x " + DoubleToStr(LotSize,2) + " Lot, StopLoss:" + StopLoss + " points, TakeProfit:" + TakeProfit + " points ?","Order Buy",MB_YESNO)==IDYES)
   {
      int counter=1;
      double curLot=LotSize;
      
      for(int i=0;i<Repetition;i++)
      {
         OrderBuyLimit(Bid - ((counter*DelayBetweenOrder)*Point),curLot);
         counter+=1;
         if (UseMartingale){curLot=curLot*2;}
      }
      PlaySound("ok.wav");
   }
}


void Action_Button2(){
   if(MessageBox("Sell " + Repetition + "x " + DoubleToStr(LotSize,2) + " Lot, StopLoss:" + StopLoss + " points, TakeProfit:" + TakeProfit + " points ?","Order Sell",MB_YESNO)==IDYES)
   {
      int counter=1;
      double curLot=LotSize;
      
      for(int i=0;i<Repetition;i++)
      {
         OrderSellLimit(Ask + ((counter*DelayBetweenOrder)*Point),curLot);
         counter+=1;
         if (UseMartingale){curLot=curLot*2;}
      }
      PlaySound("ok.wav");
   }
}


void Action_Button3(){
   if(MessageBox("Close All Pending Orders for "+ Symbol() + " ?","Close Pending Orders",MB_YESNO)==IDYES)
   {
      CloseAllPO();
      PlaySound("ok.wav");
   }
}

void Action_Button4(){
   if(MessageBox("Close All Open Orders for "+ Symbol() + " ?","Close Orders",MB_YESNO)==IDYES)
   {
      CloseThis();
      PlaySound("ok.wav");
   }
}

 

Lastly create Transaction module:

void OrderBuyLimit(double price,double lot){
int ticket;
   for(int i=0;i<3;i++)
   {
      double sl;
      double tp;
      if (StopLoss==0) {
         sl = 0;
      }else {
         sl = price-StopLoss*Point;
      }
   
      if (TakeProfit==0) {
         tp = 0;
      }else {
         tp = price+TakeProfit*Point;
      }
      
      ticket=OrderSend(Symbol(),OP_BUYLIMIT,lot,price,Slippage,sl,tp,"HiLLzX Buy");
      if(ticket>0){break;}    
      RefreshRates();     
   }

}

void OrderSellLimit(double price,double lot){
int ticket;
   for(int i=0;i<3;i++)
   {
   
      double sl;
      double tp;
      if (StopLoss==0) {
         sl = 0;
      } else {
         sl = price+StopLoss*Point;
      }
      
      if (TakeProfit==0) {
         tp = 0;
      }else {
         tp = price-TakeProfit*Point;
      }
   
   
      ticket=OrderSend(Symbol(),OP_SELLLIMIT,lot,price,Slippage,sl,tp,"HiLLzX Sell");
      if(ticket>0){break;}    
      RefreshRates();     
   }
}

void CloseAllPO() {
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();
   
   if (OrderSymbol()==Symbol()){
    bool result = false;
    
    switch(type)
    {
      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
   }
  }
}

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;

}

nets marti off

nets marti on



OK good luck, if you need the full code, please grab the code below:

//+------------------------------------------------------------------+
//|                                           Nets Pending Order.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 StopLoss=0;
extern int TakeProfit=0;
extern int DelayBetweenOrder=100;
extern int Repetition=10;
extern bool UseMartingale=false;

int Slippage=5;

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

CreateButton("button1","Net Buy",x,20,120,30,Yellow,Green,White,12);
CreateButton("button2","Net Sell",x,20,120,30,Yellow,Maroon,White,12);
CreateButton("button3","Close PO "+Symbol(),x,20,160,30,White,Gray,White,12);
CreateButton("button4","Close "+Symbol(),x,20,160,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);
        }
      else if(sparam=="button4") // Close This
        {
         Action_Button4();
         ObjectSetInteger(0,"button4",OBJPROP_STATE,false);
        }
 
  }
  
void Action_Button1(){
   if(MessageBox("Buy " + Repetition + "x " + DoubleToStr(LotSize,2) + " Lot, StopLoss:" + StopLoss + " points, TakeProfit:" + TakeProfit + " points ?","Order Buy",MB_YESNO)==IDYES)
   {
      int counter=1;
      double curLot=LotSize;
      
      for(int i=0;i<Repetition;i++)
      {
         OrderBuyLimit(Bid - ((counter*DelayBetweenOrder)*Point),curLot);
         counter+=1;
         if (UseMartingale){curLot=curLot*2;}
      }
      PlaySound("ok.wav");
   }
}


void Action_Button2(){
   if(MessageBox("Sell " + Repetition + "x " + DoubleToStr(LotSize,2) + " Lot, StopLoss:" + StopLoss + " points, TakeProfit:" + TakeProfit + " points ?","Order Sell",MB_YESNO)==IDYES)
   {
      int counter=1;
      double curLot=LotSize;
      
      for(int i=0;i<Repetition;i++)
      {
         OrderSellLimit(Ask + ((counter*DelayBetweenOrder)*Point),curLot);
         counter+=1;
         if (UseMartingale){curLot=curLot*2;}
      }
      PlaySound("ok.wav");
   }
}


void Action_Button3(){
   if(MessageBox("Close All Pending Orders for "+ Symbol() + " ?","Close Pending Orders",MB_YESNO)==IDYES)
   {
      CloseAllPO();
      PlaySound("ok.wav");
   }
}

void Action_Button4(){
   if(MessageBox("Close All Open Orders for "+ Symbol() + " ?","Close Orders",MB_YESNO)==IDYES)
   {
      CloseThis();
      PlaySound("ok.wav");
   }
}


void OrderBuyLimit(double price,double lot){
int ticket;
   for(int i=0;i<3;i++)
   {
      double sl;
      double tp;
      if (StopLoss==0) {
         sl = 0;
      }else {
         sl = price-StopLoss*Point;
      }
   
      if (TakeProfit==0) {
         tp = 0;
      }else {
         tp = price+TakeProfit*Point;
      }
      
      ticket=OrderSend(Symbol(),OP_BUYLIMIT,lot,price,Slippage,sl,tp,"HiLLzX Buy");
      if(ticket>0){break;}    
      RefreshRates();     
   }

}

void OrderSellLimit(double price,double lot){
int ticket;
   for(int i=0;i<3;i++)
   {
   
      double sl;
      double tp;
      if (StopLoss==0) {
         sl = 0;
      } else {
         sl = price+StopLoss*Point;
      }
      
      if (TakeProfit==0) {
         tp = 0;
      }else {
         tp = price-TakeProfit*Point;
      }
   
   
      ticket=OrderSend(Symbol(),OP_SELLLIMIT,lot,price,Slippage,sl,tp,"HiLLzX Sell");
      if(ticket>0){break;}    
      RefreshRates();     
   }
}

void CloseAllPO() {
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();
   
   if (OrderSymbol()==Symbol()){
    bool result = false;
    
    switch(type)
    {
      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
   }
  }
}

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;

}


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






- Provided by HiLLzSoft Studio -