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();???????? ?????? } ? } ? ? ? ? ? ? ? ? |