Close one position open opposite position without hedge error

 

Does anyone know the best way to close a position and open the opposite position, without getting an "hedge error 149"'?

I only want one position open at a time.

Thanks!

 
Use OrderClose() then OrderSend() in the other direction.
 
phy wrote >>
Use OrderClose() then OrderSend() in the other direction.

Am using OrderClose() and OrderSend(). Appears that actual problem is that OrderClose() does not close, because Ticket # is 0 ( invalid ticket for OrderClose function ). Error 4051. Some how Ticket # is getting lost.

 
Yellowbeard:

Am using OrderClose() and OrderSend(). Appears that actual problem is that OrderClose() does not close, because Ticket # is 0 ( invalid ticket for OrderClose function ). Error 4051. Some how Ticket # is getting lost.

OrderSelect(0,SELECT_BY_POS);

int Ticket=OrderTicket();

OrderClose(Ticket, ......)

OrderSent(.....)



or,


OrderSelect(0,SELECT_BY_POS);

OrderClose(OrderTicket(),OrderLots(),......)

OrderSend(......)


I was getting Error 4051 a few hours ago and noticed that I had not actually obtained the Ticket number via the code shown in first example for my OrderModify(....), but in my OrderClose() function I use the second example and avoid collecting the ticket number.


FWIW

 
FXtrader2008 wrote >>

OrderSelect(0,SELECT_BY_POS);

int Ticket=OrderTicket();

OrderClose(Ticket, ......)

OrderSent(.....)

or,

OrderSelect(0,SELECT_BY_POS);

OrderClose(OrderTicket(),OrderLots(),......)

OrderSend(......)

I was getting Error 4051 a few hours ago and noticed that I had not actually obtained the Ticket number via the code shown in first example for my OrderModify(....), but in my OrderClose() function I use the second example and avoid collecting the ticket number.

FWIW

CLoser to goal but now getting " error 129 " for closing position.

 

hmmm


Without seeing your your OrderClose statement and the code that preceeds it I can only guess. Are you trying to close a BUY order with the "Ask" or a SELL order with the "Bid"?


Amended Post: Close BUY orders using the "Bid". Close SELL orders using the "Ask"

 
FXtrader2008 wrote >>

hmmm

Without seeing your your OrderClose statement and the code that preceeds it I can only guess. Are you trying to close a BUY order with the "Ask" or a SELL order with the "Bid"?

Amended Post: Close BUY orders using the "Bid". Close SELL orders using the "Ask"

Here's the code.

Lot = .01;

Ticket = OrderTicket();

TakePB = (Bid+10*Point);
TakePS = (Ask-10*Point);

For Close BUY - Open SELL:

{
RefreshRates();
OrderSelect(0,SELECT_BY_POS);
OrderClose(OrderTicket(),Lot,OrderType(),2);

{Alert ("Closed - Buy Order ",Ticket);}
Alert (GetLastError());


}


RefreshRates();
Ticket=OrderSend(Symbol(),OP_SELL,Lot,Bid,2,0,TakePS);


Alert ("Opened - Sell Order ",Ticket);
Alert (GetLastError());

For Close SELL - Open BUY:

{
RefreshRates();
OrderSelect(0,SELECT_BY_POS);
OrderClose(OrderTicket(),Lot,OrderType(),2);

{Alert ("Closed - Sell Order ",Ticket);}
Alert (GetLastError());


}



RefreshRates();
Ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,2,0,TakePB);


Alert ("Opened - Buy Order ",Ticket);
Alert (GetLastError());

 

Yellowbeard,


It appears your function parameters are indeed incorrect as Error 129 says.


The correct parameters for the OrderClose() function are as follows:


OrderClose( ticket, lots, price, slippage, color) and color is optional


You have "OrderType()" in the "price" parameter location. You need to have either "Ask" or "Bid" in the price parameter location.



You need two sets of OrderClose functions: one for closing BUY orders and one for closing SELL orders.


I suggest something like this:


OrderSelect(0,SELECT_BY_POS);

string ordertype=OrderType();


if(ordertype == "OP_BUY")

{

OrderClose(OrderTicket(),Lot,Bid,2);

}


if(ordertype == "OP_SELL")

{

OrderClose(OrderTicket(),Lot,Ask,2);

}

 
FXtrader2008 wrote >>

Yellowbeard,

It appears your function parameters are indeed incorrect as Error 129 says.

The correct parameters for the OrderClose() function are as follows:

OrderClose( ticket, lots, price, slippage, color) and color is optional

You have "OrderType()" in the "price" parameter location. You need to have either "Ask" or "Bid" in the price parameter location.

You need two sets of OrderClose functions: one for closing BUY orders and one for closing SELL orders.

I suggest something like this:

OrderSelect(0,SELECT_BY_POS);

string ordertype=OrderType();

if(ordertype == "OP_BUY")

{

OrderClose(OrderTicket(),Lot,Bid,2);

}

if(ordertype == "OP_SELL")

{

OrderClose(OrderTicket(),Lot,Ask,2);

}

Still have problem.

2009.07.16 20:42:22 '36498': order sell 1.00 EURUSD opening at 1.4129 sl: 0.0000 tp: 1.4121 failed [Hedge is prohibited]

2009.07.16 20:42:22 '36498': instant order sell 1.00 EURUSD at 1.4129 sl: 0.0000 tp: 1.4121

2009.07.16 20:37:49 '36498': order was opened : #8651373 buy 1.00 EURUSD at 1.4133 sl: 0.0000 tp: 1.4141

2009.07.16 20:37:46 '36498': request in process

2009.07.16 20:37:46 '36498': request was accepted by server

2009.07.16 20:37:46 '36498': instant order buy 1.00 EURUSD at 1.4133 sl: 0.0000 tp: 1.4141

Here is modified code:

{

RefreshRates();
OrderSelect(0,SELECT_BY_POS);

if(ordertype == "OP_SELL")
{OrderClose(OrderTicket(),Lot,Ask,2);}


{Alert ("Closed - Sell Order ",Ticket);}
Alert (GetLastError());


}


RefreshRates();
Ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,2,0,TakePB);

{Alert ("Opened - Buy Order ",Ticket);}
Alert (GetLastError());

 

It looks like you are going to have to do some sleuthing to figure this out. I suggest you add some temporary code to your EA that checks to see if there are any open orders before you open another order.


Place this temporary code before the OrderSend(.....) statement.


if(OrderTotal()>0)

{

OrderSelect(0,SELECT_BY_POS);

Print("Open order is: ",OrderType());

}



Debugging is tedious work sometimes. Also, have you checked to see if you have more that one trade open? Perhaps you are closing only one trade and there are more trades that are still open.

 
Yellowbeard wrote >>

Still have problem.

2009.07.16 20:42:22 '36498': order sell 1.00 EURUSD opening at 1.4129 sl: 0.0000 tp: 1.4121 failed [Hedge is prohibited]

2009.07.16 20:42:22 '36498': instant order sell 1.00 EURUSD at 1.4129 sl: 0.0000 tp: 1.4121

2009.07.16 20:37:49 '36498': order was opened : #8651373 buy 1.00 EURUSD at 1.4133 sl: 0.0000 tp: 1.4141

2009.07.16 20:37:46 '36498': request in process

2009.07.16 20:37:46 '36498': request was accepted by server

2009.07.16 20:37:46 '36498': instant order buy 1.00 EURUSD at 1.4133 sl: 0.0000 tp: 1.4141

Here is modified code:

{

RefreshRates();
OrderSelect(0,SELECT_BY_POS);

if(ordertype == "OP_SELL")
{OrderClose(OrderTicket(),Lot,Ask,2);}


{Alert ("Closed - Sell Order ",Ticket);}
Alert (GetLastError());


}


RefreshRates();
Ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,2,0,TakePB);

{Alert ("Opened - Buy Order ",Ticket);}
Alert (GetLastError());

Here is the error log for this time period. PLease note that the initial error of 4051 is because there aren't any orders open yet which the AE is trying to close.

When the BUY order is opened, there are no problems. ( Ticket #8651373 ) But when closing, the ticket # is " 0 " and the BUY order doesn't close. When the SELL order is attempted, I get the error 149.

20:37:22 ZEME EURUSD,M1: Alert: Attempt to close Sell 0. Waiting for response..

20:37:22 ZEME EURUSD,M1: Alert: Closed - Sell Order 0

20:37:22 ZEME EURUSD,M1: Alert: 4105

20:37:45 ZEME EURUSD,M1: open #8651373 buy 1.00 EURUSD at 1.4133 tp: 1.4141 ok

20:37:45 ZEME EURUSD,M1: Alert: Opened - Buy Order 8651373

20:37:45 ZEME EURUSD,M1: Alert: 0

20:42:21 ZEME EURUSD,M1: Alert: Attempt to close Buy 0. Waiting for response..

20:42:21 ZEME EURUSD,M1: Alert: Closed - Buy Order 0

20:42:21 ZEME EURUSD,M1: Alert: 4105

20:42:21 ZEME EURUSD,M1: Alert: Opened - Sell Order -1

20:42:21 ZEME EURUSD,M1: Alert: 149

Reason: