The features described below are part of ib-api.
Include
 
require 'ib-api' 
in your scripts.

Specify a Contract

Any financial asset is a IB::Contract. Its defined by its attributes

 c =  IB::Contract.new 
 c.attributes
  => {:created_at=>2020-10-27 04:39:56 +0000, :con_id=>0, :right=>"", :include_expired=>false} 
 s =  IB::Stock.new  symbol: 'GE'
 s.attributes
 => {:created_at=>2020-10-27 04:40:29 +0000, :con_id=>0, :right=>"", :symbol=>'GE',  :include_expired=>false, :sec_type=>"STK", :currency=>"USD", :exchange=>"SMART"} 

Depending on the security-type different attributes are required to identify an asset. To ease the access to most common assets IB-Ruby defines default attributes (as shown above).

Required Attributes

We create contracts to fetch informations or to specify an asset to trade with.
These are basic requirements to achieve this goal:

Security Necessary Attributes Default Attributes Occasionally Required Attributes
Stocks :symbol, :exchange, :currency exchange: ‘SMART’, :currency: ‘USD’ :primary_exchange, :trading_class
Forex (+) exchange: ‘IDEALPRO’  
Future (+) :expiry   multiplier
Option (+) :strike, :right   multiplier, trading_class
Contract :con_id, :exchange    

Any contract is fully identified by its con_id and its exchange.

A IB::Stock may be defined by its :symbol and :currency or :exchange. In rare occasions, :primary_exchange and :trading_class have to be specified, too.

 a =  Stock.new symbol: 'aapl'
    # defines the _normal_ apple share, traded at several venues in _USD_.
 a =  Stock.new symbol: 'aapl', currency: 'CHF'
    # specifies the stock traded at the EBS in swiss franc.

A IB::Future requires :symbol, :exchange and :expiry.

 f= IB::Future.new symbol: 'ES', exchange: 'GLOBEX', expiry: 202103

An IB::Option needs the specification of :symbol, :exchange, :strike, :right and :expiry

RequestContractData

To verify the contract, a :RequestContractData-message is submitted to the TWS.

 ib = IB::Connection.current
 ib.send__message :RequestContractData, :contract => #{some contract}
         
 # wait until the :ContractDataEnd message returned
 ib.wait_for :ContractDataEnd

 the_contract =  ib.recieved[:ContractData].first

If this is performed with the Apple-Stock, as defined above:

 ib = IB::Connection.current
 ib.send_message :RequestContractData, :contract => IB::Stock.new( symbol: 'AAPL' )
 ib.received[:ContractData].first.to_human
 => "<Contract <Stock: AAPL USD NASDAQ>   
     <ContractDetails  APPLE INC, market-name:NMS, category:Computers, industry:Technology / Computers, ev_multiplier:, convertible:false, cupon:0.0, md_size_multiplier:100, min_tick:0.01, next_option_partial:false price_magnifier:1, puttable:false, sec_id-list:{:ISIN=>\"US0378331005\"}, valid exchanges: SMART,AMEX,NYSE,CBOE,PHLX,ISE,CHX,ARCA,ISLAND,DRCTEDGE,BEX,BATS,EDGEA,CSFBALGO,JEFFALGO,BYX,IEX,EDGX,FOXRIVER,PEARL,TPLUS1,NYSENAT,LTSE,MEMX,PSX, order types: ACTIVETIM,AD,ADJUST,ALERT,ALGO,ALLOC,AON,AVGCOST,BASKET,BENCHPX,COND,CONDORDER,DARKONLY,DARKPOLL,DAY,DEACT,DEACTDIS,DEACTEOD,DIS,GAT,GTC,GTD,GTT,HID,IBKRATS,ICE,IMB,IOC,LIT,LMT,LOC,MIT,MKT,MOC,MTL,NGCOMB,NODARK,NONALGO,OCA,OPG,OPGREROUT,PEGBENCH,POSTONLY,PREOPGRTH,REL,RPI,RTH,SCALE,SCALEODD,SCALERST,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,SWEEP,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF >>" 

The response is a IB::ContractData object. It holds any information about the successful request

 z= ib.received[:ContractData].first
 z.methods
 => [:contract_detail, :contract, :to_human, :buffer, :valid?, :load, data, ...
      :request_id, ...]
 z.contract.to_human
 => "<Stock: AAPL USD NASDAQ>"

Usually ContractData-Objects are just transfer container to initialize IB::Contracts.
For that purpose IB-Ruby provides Verify

Tags: