|
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.
Why does this code not work ? |
skt77
|
Post subject: Why does this code not work ? |
Post rating: 0
|
Posted: Tue 18 Aug, 2015, 16:03
|
|
User rating: 0
Joined: Mon 11 May, 2015, 23:19 Posts: 12 Location: United Kingdom, London
|
try{ System.out.println("Going to check if order1 is null or not"); IOrder order1 = engine.getOrder(TicketLabel); System.out.println("Going to check if order1 is null or not id came back as "+order1.getId()+" time is "+order1.getCreationTime()); if(order1.equals(null)) { order1=engine.submitOrder(TicketLabel, instrument, myCommand, ordersize, myprice); order1.waitForUpdate(2000, IOrder.State.CREATED); //wait max 2 seconds for CREATED System.out.println(""+order1.getState()); }//if(order1==null) ends here order1.waitForUpdate(2000); } catch(JFException ex){System.out.println(""+ex.getMessage());} catch(Exception ex){System.out.println(""+ex.getMessage());} finally{} System.out.println("try catch finally ends");
The above piece of code produces this log file -
Going to check if order1 is null or not null try catch finally ends
It seems engine.getOrder call is not working as expected.
One of the things I could point out is that my code is a series of distinct classes and I keep passing the objects like engine, instrument and context from one class method to another through out the onTick call. Could that be an issue ? Is there a need to "refresh" the object in some way ?
|
|
|
|
 |
mcs
|
Post subject: Re: Why does this code not work ? |
Post rating: 0
|
Posted: Wed 19 Aug, 2015, 10:13
|
|
User rating: 4
Joined: Sat 12 May, 2012, 00:28 Posts: 21 Location: New Zealand, Auckland
|
Your code performs order1 = engine.getOrder() (line 3) which sometimes returns null, then it immediately calls order1.getId() in System.out.println() (line 4) and only then it checks if order1 is null (line 5). No wonder that when getOrder() returns null you get a NullPointerException on System.out.println().
Moreover, line 5 is incorrect as well. You shouldn't do 'if (order1.equals(null))', you should write 'if (order1 == null)' instead.
Calling any method on a null object results in a NullPointerException.
|
|
|
|
 |
skt77
|
Post subject: Re: Why does this code not work ? |
Post rating: 0
|
Posted: Wed 19 Aug, 2015, 10:20
|
|
User rating: 0
Joined: Mon 11 May, 2015, 23:19 Posts: 12 Location: United Kingdom, London
|
Thanks a lot for helping here.
Point taken but, if there is a null pointer exception, it should throw one and let me know ? I am using try / catch block also.
It just goes through and does not get caught seemingly ? What am I missing ?
Further, as a matter of good practice, should I be enclosing the server operations within the try / catch block or not ?
|
|
|
|
 |
mcs
|
Post subject: Re: Why does this code not work ? |
Post rating: 1
|
Posted: Wed 19 Aug, 2015, 11:36
|
|
User rating: 4
Joined: Sat 12 May, 2012, 00:28 Posts: 21 Location: New Zealand, Auckland
|
A NullPointerException is actually being thrown and caught by 'catch(Exception ex){System.out.println(""+ex.getMessage());}' code. However, getMessage() of a NullPointerException returns null So your println() call evaluates '"" + null' which is null and therefore prints null. Yes, you should enclose all server calls into try/catch blocks. Note there are other possible issues with your code: * submitOrder() can and will return null sometimes - need to check it before calling waitForUpdate() * waitForUpdate() can and will return null when there is a timeout, better to check it * a timeout of 2 seconds is too short (from practice), better to use at least 5 seconds * waitForUpdate() gets called twice on a new order Just for example, this is my production code for BuyLimit order creation: try { IOrder o = engine.submitOrder(oLabel, in, IEngine.OrderCommand.BUYLIMIT, oAmount, oPrice, Double.NaN, oSL, oTP, oGT); if (o != null) { msg = o.waitForUpdate(waitForUpdate, TimeUnit.MILLISECONDS); if (msg == null) {print("Submitting BuyLimit: WaitForUpdate() timeout!");} else { oState = o.getState(); if (oState == IOrder.State.FILLED) {print("BuyLimit filled on the spot");} else if ((oState != IOrder.State.OPENED) && (oState != IOrder.State.CREATED)) print("Looks like BuyLimit id " + o.getId() + " " + priceFormat.format(oAmount) + "@" + priceFormat.format(oPrice) + " order was rejected. Order state: " + oState + ", message type: " + msg.getType() + ", content: " + msg.getContent()); } } else print("Submitting BuyLimit: null order returned!"); } catch (Exception e) {console.getErr().println(e.getMessage()); e.printStackTrace(console.getErr()); print("Exception on creating BuyLimit order!!! " + e.getMessage());}
|
|
|
|
 |
skt77
|
Post subject: Re: Why does this code not work ? |
Post rating: 0
|
Posted: Wed 19 Aug, 2015, 12:02
|
|
User rating: 0
Joined: Mon 11 May, 2015, 23:19 Posts: 12 Location: United Kingdom, London
|
Thanks, it is really helpful.
"waitForUpdate() can and will return null when there is a timeout, better to check it"
This leads to interesting question - Do I necessarily need to wait for Update ? Does my waiting have any influence on what will happen to the order at the server side ? every onTick method call will first check if the order with the label TicketLabel is already present or not before sending a request to the server.
String TicketLabel="OrderAt0930AMGMT"; IOrder order1 = engine.getOrder(TicketLabel); if(order1==null){//create order with the label as TicketLabel}
Can I just send the order and move on, especially if my code is iterative and it comes back and checks during the next tick, if the order already got created or not ? Is there a risk in that there could be multiple orders getting created if it can take up to 5 seconds for Order to be reflected on the server, because presumably onTick may get called multiple times during the 5 sec window ?
Thanks for your ongoing help.
regards SKT
|
|
|
|
 |
mcs
|
Post subject: Re: Why does this code not work ? |
Post rating: 1
|
Posted: Wed 19 Aug, 2015, 12:27
|
|
User rating: 4
Joined: Sat 12 May, 2012, 00:28 Posts: 21 Location: New Zealand, Auckland
|
Good question!
What I ended up doing, is for every combination of operation type (SubmitBuyLimit, SubmitSellLimit, CloseBuyLimit, SetTPSell, CloseBuy, etc) and instrument (my strategy trades on many of them) I create a trading thread which performs that particular operation for that particular instrument. Each thread calls waitForUpdate() upon communicating with the server and blocks while main strategy thread continues processing ticks. The main thread doesn't submit new operations to the currently blocked thread, thus preventing sending the same commands to the server many times before trading timeout, but at the same time keeping a good degree of parallelism for trading (so it can submit both SellLimit and BuyLimit orders on the same instrument simultaneously, or submit several SellLimits on different instruments).
|
|
|
|
 |
skt77
|
Post subject: Re: Why does this code not work ? |
Post rating: 0
|
Posted: Wed 19 Aug, 2015, 14:01
|
|
User rating: 0
Joined: Mon 11 May, 2015, 23:19 Posts: 12 Location: United Kingdom, London
|
Awesome  !! Thanks.
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|