place 2 pending orders

 

Hi,

I've looked forever but can't find a script to place 2 pending orders at my prices, a pending buy stop at one price and a pending sell stop at another.

Does such a script exist, most straddles work on "x pips away from the current price" but this is not the same as setting my own price with an expiry.

If anyone can give me a heads up on finding this script, if it exists, would really appreciate it, I feel like my eyes are going to pop out.

I did try and code it based on a news script but it was a bit of a disaster :-).

my 2 efforts are below (I am not a programmer so I have tried to mod exisitng scripts) if some one sees something in them that could be salvaged please let me know. I know how difficult programming is now.

Many thanks

Kevin


//+------------------------------------------------------------------+
//| Codersguru |
//| http://www.forex-tsd.com |
//+------------------------------------------------------------------+
//Trying to mod the news script to place 2 pending orders at top and bottom of channel.
//The news trade script works on the current price and places pending orders a set distance from the current price
// which isn't what I want to do
//
//so I have tweaked it to calculate the distance based on my user inputs.
//
//What I want this to do is place 2 pending orders at prices that I set - for breakout trading with timed finish.
//
//Ideally this will run as a script and not an EA as I have an EA I want to use for money management.
//
//
//

#property copyright "Codersguru"
#property link "http://www.forex-tsd.com"
#property show_inputs

extern double takeprofit = 20;
extern double lots = 1;
extern double high_price = 2.0000;
extern double low_price = 2.0000;
extern int EndHour = 15;
extern int EndMinute = 30;


int start()

{
int ticket;
string hcomment = "NewsScript";

ticket=OpenPendingOrder(OP_SELLSTOP,lots,(high_price-Bid),5,high_price,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,(high_price-Bid),5,low_price,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);
}


if(TimeHour(TimeCurrent())>=EndHour && TimeMinute(TimeCurrent())>=EndMinute)
{
CloseAll.TB();
}
Comment("StartHour",TimeCurrent(),"\n","StartMinute");


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");
}
this one might be closer, it compiles ok, just doesn't work


//+------------------------------------------------------------------+
//| Straddle.mq4 |
//| MQL4 |
//|
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
//Input Parameters----------------------------------------------------
extern double high_price = 2.0000;
extern double low_price = 2.0000;
// extern double StopLoss = 10;
extern double TakeProfit = 10;
extern double TrailingStop = 10;
extern double Lots = 0.10 ;
//extern int ShiftUP = 10;
//extern int ShiftDOWN = 10;
extern int StartHour;
extern int StartMinute;
extern int EndHour = 15;
extern int EndMinute = 30;
int Magic=10000000000000;

//Start Coding---------------------------------------------------------

int start()
{
if (OrdersTotal()==0)
{
if (TimeHour(TimeCurrent())==StartHour && TimeMinute(TimeCurrent())>=StartMinute)
{

OpenPendingOrder(OP_SELLSTOP,Lots,(high_price + 3),5,high_price,TakeProfit,"learn the syntax"+"_SELLSTOP",Magic,0);

OpenPendingOrder(OP_BUYSTOP,Lots,(high_price - 3),5,low_price,TakeProfit,"learn the syntax"+"_BUYSTOP",Magic,0);

}
}
if(TimeHour(TimeCurrent())>=EndHour && TimeMinute(TimeCurrent())>=EndMinute)
{
CloseAll.TB();
}
Comment("StartHour",TimeCurrent(),"\n","StartMinute");


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)
{

int err=0, c=0;

switch (pType)
{
case OP_BUYLIMIT:
{
for(c = 0 ; c < 5 ; c++)
{
OrderSend(Symbol(),OP_BUYLIMIT,pLots,Ask-(pLevel*Point),sp,(Ask-pLevel*Point)-sl*Point,(Ask-pLevel*Point)+tp*Point,pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(IsTradeContextBusy() || err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
}
case OP_BUYSTOP:
{
for(c = 0 ; c < 5 ; c++)
{
OrderSend(Symbol(),OP_BUYSTOP,pLots,Ask+(pLevel*Point),sp,(Ask+pLevel*Point)-sl*Point,(Ask+pLevel*Point)+tp*Point,pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(IsTradeContextBusy() || err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
}
case OP_SELLLIMIT:
{
for(c = 0 ; c < 5 ; c++)
{
OrderSend(Symbol(),OP_SELLLIMIT,pLots,Bid+(pLevel*Point),sp,(Bid+pLevel*Point)+sl*Point,(Bid+pLevel*Point)-tp*Point,pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(IsTradeContextBusy() || err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
}
case OP_SELLSTOP:
{
for(c = 0 ; c < 5 ; c++)
{
OrderSend(Symbol(),OP_SELLSTOP,pLots,Bid-(pLevel*Point),sp,(Bid-pLevel*Point)+sl*Point,(Bid-pLevel*Point)-tp*Point,pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(IsTradeContextBusy() || err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
}
}
}

int CloseAll.TB()
{
int i=0;
for(i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP||OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT)
OrderDelete(OrderTicket());
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Ask,3);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Bid,3);
}
}
return(0);
}

 

Hi Guru's

Could someone possibly help me with this bit of code:

I've defined as user input

extern double high_price = 2.0500;

and opp for low

extern double low_price = 2.0400;

using this I can calculate the distance from the current price that i want to place pending orders.

For some reason when I do the calculations for distanceh or distancel (see below) it goes wrong, it won't place an order BUT if I tell it the distance as external double distance = 15; it is ok.

Code:

int start()

{

int ticket,distanceh,distancel;

string hcomment = "place pending";

distanceh = (high_price - Bid); //works out distance from current price for pending buystop

distancel = (Ask - low_price); // vice versa for sell stop

ticket=OpenPendingOrder(OP_SELLSTOP,lots,distancel,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,distanceh,5,stoploss,takeprofit,hcomment);

if(ticket>0)

{

any help very appreciated. It doesn't seem to like the output from the calculation so not going any further. I'm also wondering if the price format has anything to do with it, i.e number of decimal places possibly

Many thanks

KevM

Reason: