Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

How to close multiple open orders
 Post subject: How to close multiple open orders Post rating: 0   Post Posted: Sun 23 Sep, 2012, 22:03 

User rating: 0
Joined: Tue 24 Apr, 2012, 07:11
Posts: 3
Hi Support,
Please help to show how I may close multiple open orders:
The current code I have for closeorder is below, but this only closes the most recent order but the previous open orders are not closed and left open.

private void closeOrder(IOrder order) throws JFException {
if (order != null && isActive(order)) {
order.close();
}
}

Thank you.
Best regards,
Terry


 
 Post subject: Re: How to close multiple open orders Post rating: 0   Post Posted: Mon 24 Sep, 2012, 00:06 
User avatar

User rating: 94
Joined: Mon 06 Feb, 2012, 12:22
Posts: 357
Location: Portugal, Castelo Branco
Hi terryl888:

Maybe if you create a function to close all open orders, and not only one, something like:

private void closeAllOrders(){
    List<IOrder> openOrders = Engine.getOrders();
    for (IOrder order : openOrders){
        order.close();
    }
}


Not tested this code, but is something like it.

Hope that helps

JL


 
 Post subject: Re: How to close multiple open orders Post rating: 0   Post Posted: Mon 24 Sep, 2012, 06:37 

User rating: 0
Joined: Tue 24 Apr, 2012, 07:11
Posts: 3
Hi JL,

Okay, let me try it out.
Thanks for your advice.

Cheers,
Terry


 
 Post subject: Re: How to close multiple open orders Post rating: 0   Post Posted: Mon 24 Sep, 2012, 14:24 

User rating: 0
Joined: Tue 24 Apr, 2012, 07:11
Posts: 3
Dear JL,
Good day. I have tried the code, it did not work.
I am not good at programming actually and not familiar with java coding though I have some experience with C++ long time ago.
Just to give an idea, the close order command is below:

// CLOSE ORDER
if(longOrder != null && closeLong) {
closeOrder(longOrder); //this jumps to closeOrder - the problem it only close the latest open order while the previous are still open
longOrder = null; // then it sets longOrder=null (meaning there are no more open long positions [but actually the previous long orders were not closed yet]
}
if(shortOrder != null && closeShort) {
closeOrder(shortOrder);
shortOrder = null;
}
}

To place order:
// PLACE ORDER
if (longSign) { // if longSign is true - then open a buy
// if (longOrder == null) { // the longOrder==null is to check if existing there is already an open Long (I have disable so it can open more than 1 position for test)
longOrder = submitOrder(OrderCommand.BUY, instrument);
}

} else if (shortSign) { // if shortSign is true - then open a sell
// if (shortOrder == null) {
shortOrder = submitOrder(OrderCommand.SELL, instrument);
}
}
}

And the closeOrder command is: // i think this code only closes the current position but the other open positions are not closed

private void closeOrder(IOrder order) throws JFException {
if (order != null && isActive(order)) {
order.close();
}
}

And isActive(order): // i think it returns a true if order is "true" and the order state is not closed & not cancelled
private boolean isActive(IOrder order) throws JFException {
if (order != null && order.getState() != IOrder.State.CLOSED && order.getState() != IOrder.State.CANCELED) {
return true; // return isActive == true
}
return false; // else return isActive == false
}

I believe the problem is the closeOrder command, how to retrieve the existing open positions, is it Iorder.State.FILLED? And how to command those positions (Long or Short) to close at the same time.. Please help to advise.. Thank you very much.

Best regards,
Terry


 
 Post subject: Re: How to close multiple open orders Post rating: 0   Post Posted: Mon 24 Sep, 2012, 14:43 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See:
https://www.dukascopy.com/wiki/#Order_Management/Print_and/or_close_orders_by_group


 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com