Keyboard Shortcuts
Likes
Search
Adding delta information to positions in ApiDemo (java)
Jos van de Werken
¿ªÔÆÌåÓýHello all, ? I¡¯m trying to add delta¡¯s to my positions in a adjusted ApiDemo example, and I¡¯m encountering some issues. I hope somebody can help me. ? I¡¯ve added a ¡°FishTailContractData¡± (FishTail is the name of my strategy) object to the existing PositionRow, a working copy of the ApiDemo program. This object can receive tickdata and remember the last ask, bid, markprice, but also delta, theta etc. of a contract. When the data has changed I would like to update the position panel using the FishTailContractDataHandler to invoke a screen-update and/or portefeuille-delta-calucations. ? ? My problem: When I receive a position from TWS I also get a Contract-object with the position. It contains everything including description() and conid() etc. etc. (approach1) When I pass this contract in the constructor of a new FishTailContractData object, I¡¯m will not receive any ticks from TWS. (approach2) When I use this contract in a contract-details-request, and use the returned ConrtractDetails.contract() from TWS, then I will receive ticks. However after the ¡°->¡± I cannot ¡°see¡± my current row, so I cannot connect the right row to the right ticks in the right FishTailContractData object. ? ? I¡¯m relatively new to java. I¡¯ve a general knowledge of object oriented programming, but sometimes it¡¯s hard to understand where an instance of an object is. Nevertheless I¡¯ve some automated option trading programs running on a daily basis in java, so I think everything can be learned! But now I¡¯m hoping for some help¡ ? ? ? ? public class FishTailContractData extends TopMktDataAdapter implements IOptHandler { ?????? Contract contract; ?????? double minPrice; ?????? double maxPrice; ?????? double ask; ?????? double bid; ?????? double last; ?????? double open; ?????? double close; ?????? double markPrice; ?????? double high; ?????? double low; ?????? double tickSize; ?????? int position; ?????? ?????? double impliedVol; ?????? double delta; ?????? double optPrice; ?????? double gamma; ?????? double vega; ?????? double theta; ?????? double undPrice; ?????? ?????? double deltaDollar=0; ?????? ?????? FishTailContractDataHandler fishTailContractDataHandler; ?????? ?????? boolean dataRequested=false;??? ?????? ?????? FishTailContractData(Contract newContract, FishTailContractDataHandler handler) {?????? ???????????? contract = newContract;?? ???????????? fishTailContractDataHandler = handler; ?????? } ?????? ?????? void setTickSize(double newTickSize) { ???????????? tickSize = newTickSize;???????? ?????? } ?????? ?????? void subscribeMarketData() { ???????????? ?????? ?????ApiDemo.INSTANCE.controller().reqTopMktData(contract, "221", false, false, this); ?????? ???? ApiDemo.INSTANCE.controller().reqOptionMktData(contract, "", false /* snapshot */, false /* regulatory snapshot */, this); ?????? ???? ?????? ?????dataRequested=true; ?????? ???? ?????? ?????System.out.printf("Data subscribed: %s / %s %s\n", contract.description(), this, contract.conid() ); ?????? } ?????? void unSubscribeMarketData() { ?????? ???? ApiDemo.INSTANCE.controller().cancelTopMktData(this); ?????? ???? ApiDemo.INSTANCE.controller().cancelOptionMktData(this); ?????? ???? dataRequested=false; ?????? }????? ?????? ?????? @Override public void tickPrice( TickType tickType, double price, TickAttrib attribs) { ???????????? switch( tickType) {???????????? ??????????????????? case BID: ??????????????????? case DELAYED_BID:????????????????????? ????????????????????????? bid = price;???????????????????? ????????????????????????? break; ??????????????????? case ASK: ??????????????????? case DELAYED_ASK:????????????????????? ????????????????????????? ask = price;???????????????????? ????????????????????????? break; ??????????????????? case LAST: ??????????????????? case DELAYED_LAST: ????????????????????????? last = price; ????????????????????????? break; ??????????????????? case CLOSE: ??????????????????? case DELAYED_CLOSE: ????????????????????????? close = price; ????????????????????????? break; ??????????????????? case OPEN: ??????????????????? case DELAYED_OPEN: ????????????????????????? open = price; ????????????????????????? break;??????????????????? ??????????????????? case MARK_PRICE:?????????? ????????????????????????? markPrice = price; ????????????????????????? if (last==0) last=markPrice; ????????????????????????? break; ??????????????????? case HIGH: ????????????????????????? high = price; ????????????????????????? break;?????????????????????????? ??????????????????? case LOW: ????????????????????????? low = price; ????????????????????????? break;????????????? ??????????????????? default:???????????????????????? ????????????????????????? break; ???????????? } ???????????? tickReceived(); ???????????? System.out.printf("Tick (%s) received for %s (%s): %.02f\n", tickType, contract.description(), contract.conid(), last); ?????? } ?????? ?????? void tickReceived() { ???????????? if (fishTailContractDataHandler != null) ???????????? ?????? fishTailContractDataHandler.fishTailContractDataReceived(); ?????? } ?????? ?????? double getPrice() {?????? ???????????? if (markPrice!=0) return roundToTickSize(markPrice); ???????????? if (last!=0) return roundToTickSize(last); ???????????? if ((ask!=0) && (bid!=0)) return roundToTickSize((bid+ask)/2); ???????????? if (bid!=0) return roundToTickSize(bid); ???????????? if (ask!=0) return roundToTickSize(ask); ???????????? if (close!=0) return roundToTickSize(close); ???????????? return 0; ?????? } ?????? ?????? public double roundToTickSize(double price) { ???????????? if (tickSize==0) { ??????????????????? return Math.round((price * (1/0.01))) * 0.01; ???????????? } else { ??????????????????? return Math.round((price * (1/tickSize))) * tickSize; ???????????? }?????????????????? ?????? } ? ?????? @Override ?????? public void tickOptionComputation(TickType tickType, double impliedVolNew, double deltaNew, double optPriceNew, ??????????????????? double pvDividendNew, double gammaNew, double vegaNew, double thetaNew, double undPriceNew) { ???????????? ???????????? if (delta>1000) return; //zie soms echte bagger binnenkomen??? ???????????? ???????????? impliedVol = impliedVolNew; ???????????? delta= deltaNew; ???????????? optPrice = optPriceNew; ???????????? gamma= gammaNew; ???????????? vega = vegaNew; ???????????? theta = thetaNew; ???????????? undPrice = undPriceNew; ???????????? System.out.printf("Option data tick received for %s (%s)? *? Delta %.03f Theta %.03f? *? TickType %s\n", contract.description(), contract.conid(), delta, theta, tickType);????????? ?????? } ?????? ?????? public void publicCalcGreeks() {?????? ???????????? deltaDollar = delta * position * ( contract.secType()==SecType.OPT ? 100 : 1); //to do: change 100 in multiplier ?????? } ?????? ?????? public interface FishTailContractDataHandler { ???????????? void fishTailContractDataReceived();???????? ?????? } ? } ? ? ? ? ? ? ? ? |
I use C++ so my answer more gving a hand to avoiding you to stay blind. ? Why is your class "static" ? It maybe that your class structure does feed another object than the one you look for. I dislike new/static together IHMO I would have remove the static attribute and handle the error will create at compile/run start, generating dynamically your object with new (which tailored for that) Just a thought |
Adding FishTailContractData to the existing PositionRow will not do the trick. Replace PositionRow by??FishTailContractData in the callback for positionMulti. It could look like this : ?row?=?new?FishTailContractData(m_model,?contract); ?m_map.put(key,?row); ?m_list.add(row); ApiDemo.INSTANCE.controller().reqContractDetails(contract,??list?-> { ?????contract.exchange(list.get(0).contract().exchange()); ?????row.subscribeMarketData(); }); ? You may need m_model in?FishTailContractData?to fire data changes is you want to use the table model.
?
Best,
Herman. |
Jos van de Werken
¿ªÔÆÌåÓýThanks Herman! ? Your answer helped me a lot: now it works! ? The main thing was that the returned ¡°contract¡± in @Override public void positionMulti(String account, String modelCode, Contract contract, double pos, double avgCost) { ? had no exchange. ? I expected ¡°conid¡± was filled and I expected that was enough for receiving ticks. (could I have known this?) ? Now I update the contract in my FishTailContractData class just before requesting tick. Just adding ¡°contract.exchange(¡°SMART¡±) seemed to work too. That is faster, but I¡¯m not sure whether that would be reliable in all situations. ? void subscribeMarketData() {?????????? ?????? ???? //!! Exchange will be filled !!!! in list.get(0).contract(), absent in original contract from PositionRow ?????? ???? ApiDemo.INSTANCE.controller().reqContractDetails( contract, list -> {? ??????? ???? ?????? ????? ?ApiDemo.INSTANCE.controller().reqTopMktData(list.get(0).contract(), "221", false, false, this); ?????? ??? ? ?ApiDemo.INSTANCE.controller().reqOptionMktData(list.get(0).contract(), "", false /* snapshot */, false /* regulatory snapshot */, this);???? ?????? ?????});??????????? ?????? ?????//ApiDemo.INSTANCE.controller().reqTopMktData(contract, "221", false, false, this); ?????? ???? //ApiDemo.INSTANCE.controller().reqOptionMktData(contract, "", false /* snapshot */, false /* regulatory snapshot */, this);?? ???? ?????? ?????dataRequested=true; ?????? } ? Thanks again Herman |
Jos,
It looks as if the exchange (FTA) for open positions is only missing for options, I tested with an option for the AEX index (ticker EOE). RDSA - a stock on the dutch exchange (AEB) - did have the exchange set. IMO this is a problem, the workaround with the reqContractDetails is ugly and time consuming, maybe others can jump in and explain why the exchange is missing. Best, Herman. |