rterp / SumZeroTrading

A Java API for Developing Automated Trading Applications for the Equity, Futures, and Currency Markets
http://rterp.github.io/SumZeroTrading/
Other
145 stars 47 forks source link

Level1QuoteListener stop after placing the order #26

Closed vinceyap88 closed 7 years ago

vinceyap88 commented 8 years ago

Hi, I have tried to modify the TradingExampleWithOrderStatusListener.java by adding the Level1QuoteListener. After running it, the quotes can be received until placing the order. Although I have added the sleep code, the quote listener will stop after placing the order. Attached the modified code and also the log file for your investigation. Give me some hints on how to continue receiving the quotes after placing the order. Thank you.

Best Regards, Vincent Yap TradingExampleWithOrderStatusListener_java.txt tradingexamplelog.txt

rterp commented 8 years ago

Hi Vincent, I've been out of town, but I'll test your example this week to see what may be going on. I've seen sometimes if an exception is thrown by IB's API library it will automatically disconnect the API app from TWS, and just swallow the exception without any indication to the user that something went wrong.

Your code looks correct, and based on the logs, the order was successfully filled, so at this point not sure what may be wrong yet.

vinceyap88 commented 8 years ago

The problem happened in the hashCode of the Ticker. I find the workaround to assign decimalFormat to null as below: CurrencyTicker eurTicker = new CurrencyTicker(); eurTicker.setSymbol("EUR"); eurTicker.setCurrency("USD"); eurTicker.setExchange(Exchange.IDEALPRO); eurTicker.setDecimalFormat(null);

rterp commented 8 years ago

What was the issue exactly with the ticker's decimalFormat?

vinceyap88 commented 8 years ago

Not sure why, the ticker's decimalFormat's fields(maxIntegerDigits, minIntegerDigits, maxFractionDigits, minFractionDigits) will have value changed after placing the order. So the hashcode will be changed and the listener unable to retrieved from the QuoteEngine causing no more quotes received. I have highlighted in the attachment.

defect26-after-placing-order.pdf defect26-before-placing-order.pdf program_log.txt QuoteEngine-firelevel1Quote-listeners-null.pdf

rterp commented 7 years ago

I'm finding this is causing issues in other parts of the SumZero library as well. I can't find anywhere where the decimalFormat is being modified in the Order logic, or anywhere else for that matter. Will be adding some debug code to print a stack trace when the decimalFormat property on the ticker is modified.

rterp commented 7 years ago

Ok, this issue has been fixed. The problem is that the Ticker object contains a NumberFormat, and was using this object in its equals() and hashcode() methods. When an order is placed, the order is serialized to a file for persistence. Apparently the process of serializing the NumberFormat changes its hashcode, thus changing the hashcode of the Ticker. I've removed the NumberFormat from the calculation of the Ticker object and it seems to be working now.