Hi folks!
This is not a trading article (I repeat and repeating it "I'm not a good trade but a good programmer!")! this article is about a program that suggested by one of my clients to deal with News time.
In the News time we are not sure what's going on and where exactly the market will go. So, the idea behind our program today is placing two pending orders above and below the current market price and give the user the ability to set the distance between the pending order price and the market price.
Besides, we are going to create a script to delete the order that have been created by the first script, and only these orders will be deleted.
Let's start with the News script:
News script:
The News script asking the user to set the distance of the pending orders (how many pips below and above the current price) you want to place you pending BUYSTOP and SELLSTOP orders! (Figure 1). And you can set too Take Profit, Stop Loss and Lots values.
The script then will place the orders for you (Figure 2) and wait the market to go to its direction. Or you can use News_Delete script to delete these orders.
Let's give the code of News script a look
News script code:
This is the code of the News script:
//+------------------------------------------------------------------+
//| Codersguru |
//| http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Codersguru"
#property link "http://www.forex-tsd.com"
#property show_inputs
extern double distance = 15;
extern double stoploss = 15;
extern double takeprofit = 20;
extern double lots = 1;
int start()
{
int ticket;
string hcomment = "NewsScript";
ticket=OpenPendingOrder(OP_SELLSTOP,lots,distance,5,stoploss,takeprofit,hcomment);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Pending " + GetOrderType(OrderType()) + " order opened : ",OrderOpenPrice());
}
else Print("Error opening Pending " + GetOrderType(OrderType()) + " order : ",GetLastError());
ticket=OpenPendingOrder(OP_BUYSTOP,lots,distance,5,stoploss,takeprofit,hcomment);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Pending " + GetOrderType(OrderType()) + " order opened : ",OrderOpenPrice());
}
else Print("Error opening Pending " + GetOrderType(OrderType()) + " order : ",GetLastError());
return(0);
}
//+------------------------------------------------------------------+
int OpenPendingOrder(int pType=OP_BUYLIMIT,double pLots=1,double pLevel=5,int sp=0, double sl=0,double tp=0,string pComment="",int pMagic=123,datetime pExpiration=0,color pColor=Yellow)
{
switch (pType)
{
case OP_BUYLIMIT:
return(OrderSend(Symbol(),OP_BUYLIMIT,pLots,Ask-pLevel*Point,sp,(Ask-pLevel*Point)-sl*Point,(Ask-pLevel*Point)+tp*Point,pComment,pMagic,pExpiration,pColor));
break;
case OP_BUYSTOP:
return(OrderSend(Symbol(),OP_BUYSTOP,pLots,Ask+pLevel*Point,sp,(Ask+pLevel*Point)-sl*Point,(Ask+pLevel*Point)+tp*Point,pComment,pMagic,pExpiration,pColor));
break;
case OP_SELLLIMIT:
return(OrderSend(Symbol(),OP_SELLLIMIT,pLots,Bid+pLevel*Point,sp,(Bid+pLevel*Point)+sl*Point,(Bid+pLevel*Point)-tp*Point,pComment,pMagic,pExpiration,pColor));
break;
case OP_SELLSTOP:
return(OrderSend(Symbol(),OP_SELLSTOP,pLots,Bid-pLevel*Point,sp,(Bid-pLevel*Point)+sl*Point,(Bid-pLevel*Point)-tp*Point,pComment,pMagic,pExpiration,pColor));
break;
}
}
string GetOrderType( int type)
{
if(type == OP_BUY) return ("Buying position");
if(type == OP_SELL) return ("Selling position");
if(type == OP_BUYLIMIT) return ("Buy Limit pending position");
if(type == OP_BUYSTOP) return ("Buy Stop pending position");
if(type == OP_SELLLIMIT) return ("Sell Limit pending position");
if(type == OP_SELLSTOP) return ("Sell Stop pending position");
}
As you see above there are two function in this script:
The GetOrderType() returns the order type in text representation for easy printing.
The main function is OpenPendingOrder() which takes all the needed information of the pending order you want to open (the order type, how much lots, the distance, the stop loss value, the take profit value, the comment, the magic number, the expiration time and the color of the arrow).
The function place the order and returns the ticket number of the order in successes and 0 otherwise.
How to delete these orders?
The second script will delete only the orders have been placed by our News script. The News_Delete identfy these orders by its type (they are SELLSTOP or BUYSTOP orders) and its comment "NewsScript".
This is the code of the NewsDelete script:
//+------------------------------------------------------------------+
//| Codersguru |
//| http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Codersguru"
#property link "http://www.forex-tsd.com"
int start()
{
int cnt, ticket, total;
string hcomment = "NewsScript";
total = OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
if((OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP) && OrderComment() == hcomment)
OrderDelete(OrderTicket());
}
return(0);
}
//+------------------------------------------------------------------+
You can download the both of scripts and enjoy your News trading!
For any comments drop me a mail.
Coders' Guru
New bar without using Time array