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.

Order Closing Issue
 Post subject: Order Closing Issue Post rating: 0   Post Posted: Wed 17 Apr, 2013, 13:06 

User rating: 0
Joined: Wed 17 Apr, 2013, 13:04
Posts: 9
Hi All,

I am using JForex API to create our trading app. Currently using a demo account.
Now am facing a development issue. I am successfully submitting around 100 orders at a time.
Now I need a functionality to close all of them at a time. So I am using the below code,

for(int i=0; i<placedOrderList.size();i++){
try{
String labelName = placedOrderList.get(i);
if(labelName != null){
if (engine.getOrder(labelName) != null) {
engine.getOrder(labelName).close();
}
}
}catch(Exception e){
e.printStackTrace();
}
}

But its deleting a maximum of 42 records at a time and rest of the orders are opened on dukascopy. Please Help!!!

Thanks in advance


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Wed 17 Apr, 2013, 15:16 
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
For subsequent requests please post full example strategy which replicates the case and the messages/exception that you receive. For getting the stack traces you need to enable the Java console, see:
viewtopic.php?f=81&t=39073


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Wed 17 Apr, 2013, 17:30 

User rating: 0
Joined: Wed 17 Apr, 2013, 13:04
Posts: 9
Thanks for the quick reply.

The scenario is, I place 100 orders and I have stored labelname for all of them.
Now, suppose and 4:30 pm i want to delete all of them, I don't care about there current status just delete all of them.

Thats why i loop a list of 100 label names and trying to get IOrder by using,
IOrder order = engine.getOrder(labelName);
and close the same by,
order.close();

but this is not closing all of them. Sometime is close 40 orders and sometimes 10. Nothing consistent.
I can see the orders are present in the position structure (using JForex Demo App) but engine.getOrder(labelName) is not getting them to me all the time.

I know I am doing something wrong but unable to recognize :( . Please help!!!

Regards
Rupal


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Thu 18 Apr, 2013, 07:31 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please provide full example strategy which replicates the case and the messages/exception that you receive. Also do describe what you expect the strategy to do and what it does instead.


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Thu 18 Apr, 2013, 08:02 

User rating: 0
Joined: Wed 17 Apr, 2013, 13:04
Posts: 9
Hey,

Thanks a lot for the reply.

I am attaching my strategy java class (Sorry, it is very badly organized. Once I m done with the objective I will definitely organize the same). Please find the same.

Now what i am doing is,
- This java application runs every minute using windows scheduler.
- This checks the database for bid data for the current time. A bid data consists of every thing required to submit a bid. Along with that it has a "date_time" (on which the bid should be placed) and "close_date_time" (on which the bid should be closed)
- When it runs, first it looks for data with "date_time == currentDateTime" and then if found it submits the order in a loop.
- Next it looks for data with "close_date_time == currentDateTime" and if found it should close the orders. Here I am facing the problem.
- Generally i am not getting any exception but sometimes the process throws
Exception in thread "LocalCacheTimer" java.lang.ExceptionInInitializerError
at java.io.File.deleteOnExit(File.java:939)
at com.dukascopy.charts.data.datacache.by.m(Unknown Source)
at com.dukascopy.charts.data.datacache.by$f.run(Unknown Source)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
Caused by: java.lang.IllegalStateException: Shutdown in progress
at java.lang.Shutdown.add(Shutdown.java:62)
at java.lang.System$2.registerShutdownHook(System.java:1146)
at java.io.DeleteOnExitHook.<clinit>(DeleteOnExitHook.java:20)
... 5 more


We have successfully placed bid / orders. The only issue is with closing orders.

Please Help !!!


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Thu 18 Apr, 2013, 08:10 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
It does not compile:
ApplicationGlobal cannot be resolved to a variable
Please provide an example strategy, which replicates the issue. The more concise, the more efficiently we will be able to support you.


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Thu 18 Apr, 2013, 08:22 

User rating: 0
Joined: Wed 17 Apr, 2013, 13:04
Posts: 9
Hi,

Thanks for the quick response.

The strategy class is a part of a big application which is structured in packages and has inter related class.
It also fetch data from database.

Settings up the entire strategy is big task. If possible kindly go through the JForexScheduleOrder class to figure out the issue.

Please Help!!!

Thanks a lot


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Fri 19 Apr, 2013, 08:52 

User rating: 0
Joined: Wed 17 Apr, 2013, 13:04
Posts: 9
Hi,

I have created a example scheduler strategy class. Please find the attached java file. Kindly check the same.

Before starting the strategy, make sure to subscribe to Instrument.EURUSD. E.g.
Set<Instrument> instruments = new HashSet<Instrument>();
instruments.add(Instrument.EURUSD);
client.setSubscribedInstruments(instruments);
client.startStrategy(new ExampleScheduler());

This strategy class should perform the following tasks,
- When we run the strategy, it first check for existing orders in the dukascopy.
- If it finds orders over there should close all the orders.
- If it doesn't finds any orders in the dukascopy, then it will place 100 random orders.
- We assume that when we run the strategy for the first time, dukascopy has no placed orders. So for the first time it will place 100 random orders.
- In the next run it should close all the orders. But this is not working. It is closing only around 10 orders (not every time though).

I know I am wrong somewhere but unable to recognize the issue. Please Help!!!

Thanks
Rupal


Attachments:
File comment: Sample Strategy Class
ExampleScheduler.java [3.15 KiB]
Downloaded 370 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: Order Closing Issue Post rating: 0   Post Posted: Fri 19 Apr, 2013, 11:53 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
  1. Your strategy does not check the order state, see the last paragraph of
    https://www.dukascopy.com/wiki/#Close_Orders
  2. Your strategy does not wait for the order to get closed, which may be crucial, if you are getting close to having 100 positions or your strategy depends on the fact that the order has been closed/cancelled, see:
    https://www.dukascopy.com/wiki/#Close_Orders/Order_close_and_cancel_workflow


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Fri 19 Apr, 2013, 17:49 

User rating: 0
Joined: Wed 17 Apr, 2013, 13:04
Posts: 9
Hi,

Thanks for your response.

As per your suggestion, I am now using the following code to close orders.
                List orders = engine.getOrders();
         System.out.println("#################### "+orders.size());
         if(orders != null && orders.size() > 0){
            for(int i=0; i<orders.size(); i++){
               IOrder order = (IOrder) orders.get(i);
               if(order.getState() == IOrder.State.FILLED || order.getState() == IOrder.State.OPENED){
                   order.close();
                   order.waitForUpdate(2000, IOrder.State.CLOSED);
               } else {
                   console.getOut().println("Unable to close order in state: " + order.getState());
               }               
            }
         }


But, it still not working. Still it is closing 30 / 40 orders at one go. But there is a strange thing that i noticed.
I have placed 100 orders and i can see all of them in the dukascopy application.

But while running the closing order code, List orders = engine.getOrders(); is not getting all of them at once. Some time it returns 30 orders, some time 10 orders.

Don't know where i am going wrong at this point. Kindly help!!!

Thanks
Rupal


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Tue 23 Apr, 2013, 17:04 

User rating: 0
Joined: Wed 17 Apr, 2013, 13:04
Posts: 9
Hi,

Here is the ExampleStrategy class attached.

We are using List orders = engine.getOrders(); to get all the orders from dukascopy but this is not getting all of them at once. Some time it returns 30 orders, some time 10 orders.

This is the basic reason I found behind, all the orders not getting closed. Its simple, if it is not fetching all the orders then i can't close all of them.

Need a method which will basically bring all the available orders. Please Help!!!

Thanks
Rupal


Attachments:
File comment: Sample Strategy Class
ExampleScheduler.java [3.41 KiB]
Downloaded 378 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: Order Closing Issue Post rating: 0   Post Posted: Wed 24 Apr, 2013, 14:13 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
This place in your code is not correct:
if(order.getState() == IOrder.State.FILLED || order.getState() == IOrder.State.OPENED){
    order.close();
    order.waitForUpdate(2000, IOrder.State.CLOSED);

Order closing (in platform - position close) and cancelling (in platform - order cancel) are two different things and they have different workflows, please have a look again at the example:
https://www.dukascopy.com/wiki/#Close_Orders/Order_close_and_cancel_workflow
and
https://www.dukascopy.com/wiki/#Order_state/Conditional_order_states_diagram


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Fri 26 Apr, 2013, 12:12 

User rating: 0
Joined: Wed 17 Apr, 2013, 13:04
Posts: 9
Hi,

Thanks APISupport for your time and quick response.

Now in my code structure I am following the steps provided by you and for the last two tests it giving almost 99% success rate.

But, a few orders are still not closing, instead we are getting a response some thing like this,

Closing order BUY 1000 EUR/USD @ MKT is sent at 2013-04-26 09:45:20.561 GMT by the strategy "JForexScheduleOrder": from the local computer
Order ACCEPTED: #196216611 BUY 0.001 mil. EUR/USD @ MKT MAX SLIPPAGE 0.0005 - Position #50762938
Order ACCEPTED: #196216611 BUY 0.001 mil. EUR/USD @ MKT MAX SLIPPAGE 0.0005 - Position #50762938
Order 196216611 was REJECTED: no margin available.
Order 196216611 was REJECTED: no margin available.


This msg is coming after the executing of order.close(); method. Finally the order remains open.

Kindly Help!!!

Thanks
Rupal


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Tue 30 Apr, 2013, 16:17 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
This could have happened if the order close would have increased the exposure such that there would be no free margin available.
See more on following your account state:
https://www.dukascopy.com/wiki/#Account_Info


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Wed 01 May, 2013, 14:53 

User rating: 0
Joined: Wed 17 Apr, 2013, 13:04
Posts: 9
Hi,

Thanks for your time and support.

Is there any way to avoid Order 196216611 was REJECTED: no margin available. error while closing order?

Thanks
Rupal


 
 Post subject: Re: Order Closing Issue Post rating: 0   Post Posted: Thu 02 May, 2013, 14:51 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Make sure to have enough credit in your account for the given exposure levels.


 

Jump to:  

  © 1998-2026 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