How to use python ibpy to get Greeks?

Discussion in 'Automated Trading' started by Erick Gomez, Jul 11, 2015.

  1. Hi Everyone

    I haven't been able to find a good example using Interactive Brokers API with the python library ibpy to get the full detail of the Greeks I think that I need to use tickOptionComputation. What I need it is a list of the Puts and Calls for a certain expiration day with their respective Greeks it could be a pandas data frame or dictionary. I don't want to re-invent the wheel. I am sure that somebody might have some vanilla example like that.

    Many Thanks,
    EG
     
  2. u trust their own calculations? why don't you calculate your own greeks?
     
  3. Agree, But if I want to calculate them i need the black scholes data as well. Like the real-time underline price, BID/ASK, VIX those are not an issue I know that can retrieve with ibpy. I am not sure where to get the "RO" the interest rates? and the Dividends? so with all that I will have the price of the options. With that I will probably could calculate the Greeks.

    But that puts me back to square one. I still need to have a ibpy to get the values :(
     
  4. how is it that IB and my bank can have the same interest rates? and how is it possible they have dividends info when the company haven't announce it? as much as i don't trust brokers, i don't trust their data as far as things go. are you trying to get implied interest rates? The black-scholes formula is easy with a set of assumptions, but it's not the only pricing method. Some use binomial or even trinomial trees. Also, you cannot assume that the volatility term is constant.
     
  5. When you call reqMktData one of the overloaded callback methods called tickOptionComputation returns greeks.
     
    Erick Gomez likes this.
  6. @stevegee58 You are right, a quick look in google it shows me that tickOptionComputation will return the greeks. However I still not able to find a simple example in python everyting that i found it is in java and C++. I want to base my code on python. Before I start jumping and cracking something myself do you guys have a bare schema to pull the tickOptionComputation + the TickPrice call backs from ibpy? for the Bid/Ask of the option prices?
     
  7. I'm vaguely aware of ibpy and only use C++ so I'm of limited help.
    Frankly I don't think the Python adapter is used all that much; you're probably on your own figuring it out.
    There are several example programs included in the github repo. I'd use those as a starting point.
     
  8. I don't use ibypy eithier (I use swigibpy), nor do I get option greeks, so I hesitate to give you advice.

    I did look at ibpy when I started. This page https://www.quantstart.com/articles...he-Interactive-Brokers-API-to-Automate-Trades seemed to be the most helpful.

    If you were to use swigibpy then you should be able to modify https://github.com/robcarver17/ibswigsystematicexamples/blob/master/sysIB/wrapper_v3.py, as follows:

    Code:
    class IBWrapper(EWrapper): """
    Callback object passed to TWS, these functions will be called directly by the TWS or Gateway.
    """
    ## add the following methods to the class
       def init_optiondata(self, TickerId):
          if "data_optiondata" not in dir(self):
             optiondict=dict()
          else:
             optiondict=self.data_optiondata
       optiondict[TickerId]=[np.nan]*9
       setattr(self, "data_optiondata", tickdict)
    
    
       def tickOptionComputation(TickerID, TickType, impliedVol, delta, optPrice,  pvDividend, gamma, vega,  theta, undPrice):
          self.data_optiondata[TickerId]=[TickType, impliedVol, delta, optPrice,  pvDividend, gamma, vega,  theta, undPrice]
    
    ## modify this method of the client class
       def get_IB_market_data(self, ibcontract, seconds=30, tickerid=MEANINGLESS_ID):
           """
           Returns granular market data
           Returns a nested tuple of lists ([bid price, bid size, ask price, ask size], [TickType, impliedVol, delta, optPrice,  pvDividend, gamma, vega,  theta, undPrice])
        """
    
        ## initialise the tuple
        self.cb.init_tickdata(tickerid)
        self.cb.init_error()
        self.init_optiondata(self, tickerId)
    
        ### rest of the function here then replace this bet at the end ###
    
        marketdata=(self.cb.data_tickdata[tickerid], self.cb.data_optiondata[tickerid])
        ## marketdata should now contain some interesting information
        ## Note in this implementation we overwrite the contents with each tick; we could keep them
    
        if iserror:
             print "Error: "+self.cb.error_msg
             print "Failed to get any prices with marketdata"
        return marketdata
    
    
    
    I haven't tested this, and the usual caveats apply. Hopefully this is clear enough you could also use the other API calls (listed here under market data) for options.

    This may also work in ibpy. I don't know enough about it to guarantee that.
     
    Last edited: Jul 14, 2015
    Erick Gomez likes this.
  9. @globalarbtrader This is great. This is getting me the structure of what I need. From the data that come from the TickOptioncomputation
    TickType, impliedVol, delta, optPrice, pvDividend, gamma, vega, theta, undPrice

    I have the 2 following questions:

    1) Do you know if the implied Volatility it is for the contract or for the instrument?
    example the impliedvolatility = VIX for all of the contracts for the SPY? In their spec said that it is based on the tick price ask/bid etc but the numbers do not match with the VIX prices..

    2) I am getting a field name modelOptComp, askOptComp and bidOptComp. I am not sure if modelOptComp it is the Last market depth price. In the Spec it says the following however they dont clarify.

    Specifies the type of option computation. Pass the field value into TickType.getField(int tickType) to retrieve the field description. For example, a field value of 13 will map to modelOptComp, etc. l 10 = Bid l 11 = Ask l 12 = Last
     
  10. I am glad this is working for you. But hopefully there are some API options experts (non language dependent) who can answer your questions because as I said I don't trade options with IB.
     
    #10     Jul 14, 2015