The ib-extensions Gem defines macros to ease the specification and verification of contracts. Include
 
require 'ib-api' 
require 'ib/verify' 
in your scripts.

Contract#verify

To ease the process of verification of given contract-attributes, a handy verify method is provided.

  w =  IB::Symbols::Stocks.wfc
  w.con_id                     => 0 
  ww = w.verify.first
  ww.con_id                    => 10375 
  ww.contract_detail.long_name   => "WELLS FARGO & CO" 

In its basic form IB::Contract.verify just sends a :RequestContractData-Message, fetches the response and extracts IB::Contracts.

Manipulations of the response

Contract#verify accepts a block to to manipulate the returned contract. The block has access to any returned IB::Contract-object.


 f =  Future.new symbol: 'DAX', expiry: 2021, exchange: 'DTB', currency: 'EUR', multiplier: 5
 dax_con_ids =  f.verify{|c| {c.expiry => c.con_id }}
 =>  [{"20201218"=>413140771}, {"20210319"=>428741696}, {"20210618"=>446220391}]

or – if you want to store the returned contracts


 f =  Future.new symbol: 'DAX', expiry: 2021, exchange: 'DTB', currency: 'EUR', multiplier: 5
 dax_contracts =  f.verify{|c| c.essential.to_yaml }
 pp.dax_contracts.last
  "--- !ruby/object:IB::Future\n" +
  "attributes:\n" +
  "  :symbol: DAX\n" +
  "  :con_id: 446220391\n" +
  "  :exchange: DTB\n" +
  "  :right: ''\n" +
  "  :currency: EUR\n" +
  "  :expiry: '20210618'\n" +
  "  :strike: 0.0\n" +
  "  :local_symbol: FDXM JUN 21\n" +
  "  :last_trading_day: '2021-06-18'\n" +
  "  :multiplier: 5\n" +
  "  :trading_class: FDXM\n" +
  "  :sec_type: FUT\n" +
  "  :created_at: 2020-10-27 07:41:52.297145470 +00:00\n" +
  "  :include_expired: false\n" +
  "description: DAX 30 Index (Deutsche Aktien Xchange 30)\n"


Asynchronous Verification

If a bunch of IB::Contracts should be verified, verify can be called for each. The entries are processed sequentially, which is slow.

Contract.verify( thread: true ) just submits requests. It returns the receiver-thread and performs the query asynchron.

Assuming, a watchlist was setup. (You can do this by running ruby input.rb in the examples directory of ib-extensions), then

 Symbols.allocate_collection :W500
 puts Symbols::W500.all.size      => 456

To verify all IB::Contracts asynchronous, collect any thread into an array and join it.

 verified_symbols = []
 Symbols::W500.map{|y| y.verify(thread: true){ |c| verified_symbols << c  }}.join
 # after a while
 puts verified_symbols.size      => 456

If the verification is performed asynchron, the thread itself is returned for joining. The results must compiled through the block (as shown above).

Tags: