To use the features described below, activate the option-chain plugin
 
require 'ib-api' 
ib =  IB::Connection.new
ib.activate_plugin 'option-chain'

Its a most common request:
Display five in-the-money- and five out-of-the-money options and their properties.

Procedure

  • Define an underlying and verify it
  • Get the at-the-money option for all expiries
  • Get in-the-money options for all expiries
  • Get out-of-the-money options for all expiries
  • Show Option-Greeks

_

Define the Underlying

We use a liquid US-Stock: Microsoft

ib =  IB::Connection.new
ib.activate_plugin :option_chain, :symbols
m  = IB::Symbols::Stocks.msft_conid
m.con_id                 =>  272093

Centerpiece: IB::Contract.option_chain

The method :option_chain fires a :RequestOptionChainDefinition-message, waits for a :OptionChainDefinition-Response and returns it as hash, which is easily explored by:

> z= m.option_chain 
> z.keys.to_f
 => [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 25.0]  # ==> possible strikes 
 > puts z[7.to_d].to_human
<Option: GE 20201120 put 7.0 SMART USD>
<Option: GE 20201218 put 7.0 SMART USD>
<Option: GE 20210115 put 7.0 SMART USD>
<Option: GE 20210319 put 7.0 SMART USD>
<Option: GE 20210618 put 7.0 SMART USD>
<Option: GE 20220121 put 7.0 SMART USD>
<Option: GE 20230120 put 7.0 SMART USD>


> z= ge.option_chain sort: :expiry
> z.keys
 => [2011, 2012, 2101, 2103, 2106, 2201, 2301]  ## => Expiry in Year/Month notation
> puts z[2012].to_human
<Option: GE 20201218 put 0.5 SMART USD>
<Option: GE 20201218 put 1.0 SMART USD>
(...)
<Option: GE 20201218 put 25.0 SMART USD>
 

Call and Put Options are available through the :right- parameter: {:put, :call, :straddle}.

To fetch ATM, ITM and OTM Options depending on the current market-price, parts of the result set have to be erased.

Get at-the-money Options

Lets start simple: Get At The Money-Options and their properties.

IB::Contract.atm_options takes two optional parameters:

  • :ref_price – if set it selects the options with strike next to the specified value. Otherwise it performs a IB::Contract.market_price-request and sets the :ref_price accordingly.

  • :right – :specify :call or straddle. :put is the default value.

> ge_options= ge.atm_options ref_price: 7.3
> ge_options.keys          
 => [2011, 2012, 2101, 2103, 2106, 2201, 2301]
> ge_options[2011].to_human
  => ["<Option: GE 20201120 put 7.5 SMART USD>"] 

In-the-Money and Out-of-the-Money Options

If the strike of a Put-Option is above the market-price, the option is characterized as »In-the-Money« Therefore

> ge_options= ge.itm_options(sort: :expiry)
> puts ge_options[2011].to_human
  <Option: GE 20201120 put 7.5 SMART USD>
  <Option: GE 20201120 put 8.0 SMART USD>
  <Option: GE 20201120 put 8.5 SMART USD>
  <Option: GE 20201120 put 9.0 SMART USD>
  <Option: GE 20201120 put 9.5 SMART USD>
  <Option: GE 20201120 put 10.0 SMART USD> 

> ge_options[2011].map{|o| o.request_greeks( thread: true ) }.join
> puts ge_options[2011].greek.implied_volatility.round(6.4)
 0.5574
 0.5700
 0.5928
 0.6208
 0.6208
 0.6208

Out-of-the-money Options work too:

> ge_options= ge.otm_options(sort: :expiry)
> puts ge_options[2011].to_human
<Option: GE 20201120 put 12.5 SMART USD>
<Option: GE 20201120 put 12.0 SMART USD>
<Option: GE 20201120 put 11.5 SMART USD>
<Option: GE 20201120 put 11.0 SMART USD>
<Option: GE 20201120 put 10.5 SMART USD>
<Option: GE 20201120 put 10.0 SMART USD>

Tags: