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.

API call to engine.closeOrders() failing silently...
 Post subject: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Thu 07 Nov, 2013, 12:31 

User rating: 2
Joined: Thu 07 Nov, 2013, 12:15
Posts: 121
Hi

I'm writing an api-based visual order manager with a Swing form for our short-term trading strategy. Run into a strange issue.

In the code below, the calls in the format
engine.getOrder(activeBuyStop).close();
work as expected.

But the call to
engine.closeOrders(engine.getOrder(activeSellStop), engine.getOrder(activeBuyStop)); 
is failing silently.

It compiles and runs, and no exception is being thrown. There's nothing being written to Messages, and no message being posted to the onMessage callback.

I would close the two positions with two calls, but the docs say I'm only allowed 1 close call per second, and ideally I'd like avoid the delay.

Not sure how to debug this, given that the code is cut and pasted from the docs, and I'm getting no error feedback. Help much appreciated!

private class ClosePendingTask implements java.util.concurrent.Callable<Object>{
     
        public Object call() throws Exception {   
           
            if(activeBuyStop != null && activeSellStop != null){
                engine.closeOrders(engine.getOrder(activeSellStop), engine.getOrder(activeBuyStop));
            } else if (activeBuyStop != null){
                engine.getOrder(activeBuyStop).close();   
            } else if (activeSellStop != null){
                engine.getOrder(activeSellStop).close();
            }
 
            return true;           
        }
    }
 


Here's the onMessage code:
            case ORDER_CLOSE_OK : 
                print("Order closed: " + message.getOrder());
                break;
            case ORDER_CLOSE_REJECTED :
                print("Order close failed: " + message.getOrder());
                break;


 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Thu 07 Nov, 2013, 13:25 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please provide a full example strategy which replicates the case.


 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Tue 12 Nov, 2013, 10:25 

User rating: 2
Joined: Thu 07 Nov, 2013, 12:15
Posts: 121
Hi

Don't want to post it in public.

It works fine with the 2 separate calls to close(), but this is in a demo a/c. Do you really enforce the 1 second rule in the live account? I'm only ever making 2 calls at a time.


 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Tue 12 Nov, 2013, 10:38 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
scotpip wrote:
Don't want to post it in public.
We don't ask for you original strategy, rather for you to provide an example strategy that replicates the case.
scotpip wrote:
Do you really enforce the 1 second rule in the live account?
Yes.


 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Tue 12 Nov, 2013, 12:15 

User rating: 2
Joined: Thu 07 Nov, 2013, 12:15
Posts: 121
Hi

There are 2000 lines of code in the strategy including a nested Swing form - replicating the issue might be a big project.

Before I do that, I'd very much appreciate your suggestions for anything I could check that might be causing the issue. I've already posted the complete problem class above.

I'd imagine it's something to do with being called from a Callable class, but I'm confused by the fact that close() is working in the same class, while closeOrders() is failing.

Thanks


 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Tue 12 Nov, 2013, 12:40 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
scotpip wrote:
There are 2000 lines of code in the strategy including a nested Swing form - replicating the issue might be a big project.
If you claim that it is an API issue, then a simple strategy should be able to replicate this.
scotpip wrote:
I'd imagine it's something to do with being called from a Callable class, but I'm confused by the fact that close() is working in the same class, while closeOrders() is failing.
Consider a strategy which calls the IEngine.closeOrders() method from another thread:
private Instrument instrument = Instrument.EURUSD;

@Override
public void onStart(final IContext context) throws JFException {
    console = context.getConsole();
    engine = context.getEngine();

    context.setSubscribedInstruments(EnumSet.of(instrument), true);

    final IOrder o1 = engine.submitOrder("o1", instrument, OrderCommand.BUY, 0.001);
    o1.waitForUpdate(2000, IOrder.State.FILLED);
    final IOrder o2 = engine.submitOrder("o2", instrument, OrderCommand.BUY, 0.001);
    o2.waitForUpdate(2000, IOrder.State.FILLED);

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                context.executeTask(new Callable<Void>() { 
                    @Override
                    public Void call() throws Exception {
                        engine.closeOrders(o1, o2);
                        return null;
                    }
                });
            } catch (Exception e) {
                console.getErr().println(Thread.currentThread().getName() + " " + e);
            }
        }
    });
    thread.start();
}
Test it and you will see that it works as expected.


Attachments:
CloseMultipleOrders.java [2.46 KiB]
Downloaded 126 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Mon 18 Nov, 2013, 18:20 

User rating: 2
Joined: Thu 07 Nov, 2013, 12:15
Posts: 121
Well, I'm completely stuck with this. As I've said, closeOrder() works, but closeOrders() fails silently whatever I do, and I often have more than one order to close.

I've tried passing the Order objects into the Callable class every which way, and also using both sigs of the closeOrders() method. No joy.

Here is a strategy class which replicates the issue on my demo account:

1) Run strategy locally
2) click Open Positions button. 2 Pending orders are entered.
3) click Close Positions button. API method closeOrders() is called, but fails silently.

I tried the "final" trick that worked with my other Callables, but it doesn't work here.

I'd very much appreciate your help with this, as the API isn't giving me any feedback about why this is failing.

Geoff


Attachments:
TradeManagerDebug.java [11.41 KiB]
Downloaded 121 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 1   New post Posted: Mon 18 Nov, 2013, 20:02 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Without testing the attached strategy, and by just looking on the source:
There is a difference between submitted and filled order status. Any change if you replace ORDER_SUBMIT_OK with ORDER_FILL_OK in the corresponding onMessage() part?
You cannot close submitted but not yet filled orders, if I am not mistaken.

But even if this is the case, I would expect an exception being thrown if you try to close a non-filled order.


 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Tue 19 Nov, 2013, 11:22 

User rating: 2
Joined: Thu 07 Nov, 2013, 12:15
Posts: 121
Thanks for responding.

The orders I'm trying to close are pending stop orders. These only hit ORDER_FILL_OK when they get filled, I think.

Just to summarise, the thread, I can close them from my Callable class with:

engine.getOrder(activeBuyStop).close();
engine.getOrder(activeSellStop).close();


But I can't close them with

engine.closeOrders(myOrderList)


or with:

engine.closeOrders(engine.getOrder(activeSellStop), engine.getOrder(activeBuyStop)); 


or with any other variation of the same.

What I'm trying to get to the bottom of is why closeOrder() and closeOrders() are behaving differently with the same Order objects at the same point in the code.

The problem is that the API only allows 1 close() call per second, and I'd like to close them ASAP. If no one can help I'll have to just close them more slowly, but I'd much prefer to do it in a single call, as the slower approach will inevitably lose us $$$ at some stage...


 
The Best Answer  Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Tue 19 Nov, 2013, 14:59 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
First of all you have to draw a distinction between entry order and position - your code is attempting to cancel entry orders - not close positions - thus the button captions are misleading. Likewise you open entry orders - not positions. See the Order or Position section in https://www.dukascopy.com/wiki/#Orders_overview.

The javadocs clearly state that you can only close FILLED positions with the IEngine.closeOrders method. Whereas with IOrder.close you can both cancel an OPENED entry order and close a FILLED position. Your entry order multi-cancel attempt fails with an exception, but you don't see this since you neither handle the JFException in the Callable.call() method, nor call Future.get() to wait on the execution result.

scotpip wrote:
The problem is that the API only allows 1 close() call per second, and I'd like to close them ASAP. If no one can help I'll have to just close them more slowly, but I'd much prefer to do it in a single call, as the slower approach will inevitably lose us $$$ at some stage...
This is false. The restriction is one call per second per order. Hence you can cancel all of your entry orders without any timeout between the IOrder.close() calls.


 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Tue 19 Nov, 2013, 15:10 

User rating: 2
Joined: Thu 07 Nov, 2013, 12:15
Posts: 121
Thanks

This is very useful.

I can't help feeling that we could have got here rather earlier in the thread, but it's good to get this resolved.


 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Tue 19 Nov, 2013, 15:32 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
It got resolved the next day after you provided an example strategy which replicates the case. As we have said multiple times before - in non-trivial cases without a full example strategy we can only speculate where the problem lies.


 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 1   New post Posted: Tue 19 Nov, 2013, 22:43 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
scotpip wrote:
The problem is that the API only allows 1 close() call per second, and I'd like to close them ASAP. If no one can help I'll have to just close them more slowly, but I'd much prefer to do it in a single call, as the slower approach will inevitably lose us $$$ at some stage...


IT IS NOT TRUE that only one close() is allowed per second. The one-second mandatory delay applies to sequential
operations on the SAME order.

You may "mass close" any number of orders very quickly with no mandatory delays.
Be very careful that you are doing Order Entry related activities, either in an IStrategy
callback thread, or that you use the IContext.executeTask(Callable) to do all of this ! :)

ArrayList<IOrder> ordersToCloseList = new ArrayList<IOrder>();
// then add all FILLED orders you wish to close, by filtering available
// orders by Order Label, etc. and then you may do a single "mass closure"
// of dozens or hundreds of positions in one single statement.


                  engine.closeOrders(ordersToCloseList); // mass closure


Again, the delay is only a "per order" mandatory one-second delay.

HyperScalper


 
 Post subject: Re: API call to engine.closeOrders() failing silently... Post rating: 0   New post Posted: Thu 21 Nov, 2013, 10:26 

User rating: 2
Joined: Thu 07 Nov, 2013, 12:15
Posts: 121
Yup - my bad - I misread the docs!

And I need to get my head around Java error handling - I thought exceptions would bubble up and be handled even without explicit catching. Seems that they are just ignored.

You live and learn...


 

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