ibapi

package module
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2022 License: MIT Imports: 11 Imported by: 2

README

Interactive Brokers API - Go

test codecov

This is a partial implementation of the Interactive Brokers API in Go. This implemention does not provide a one to one match of the official Interactive Brokers API, it attempts to provide an idiomatic Go API to the TWS functionality.

This implementation is a work in progress and focuses on parts of the API used by my application. The initial set of APIs implemented are around realtime market data. Next in line are APIs to support order execution and account management.

This implementation uses the TWS API 10.12 source as a reference.

The official API has some required complexity to be backwards compatible with older versions of the IB Gateway. To simplify things the API was implemented and test only against IB Gateway 10.12.

Installation

go get -u github.com/wboayue/ibapi

Usage

Market Data

Connect to gateway

port := 4002
clientId := 100
client, err := ibapi.Connect("localhost", port, client)
if err != nil {
    log.Printf("error connecting: %v", err)
    return
}

defer client.Close()

fmt.Printf("server version: %v\n", client.ServerVersion)
fmt.Printf("server time: %v\n", client.ServerTime)

Request real time bars

contract := ibapi.Contract{
    LocalSymbol: "ESH2",
    SecurityType: "FUT",
    Currency:     "USD",
    Exchange:     "GLOBEX",
}

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 60*time.Second) // stop streaming after 60 seconds
defer cancel()

bars, err := client.RealTimeBars(ctx, contract, "TRADES", false)
if err != nil {
    log.Printf("error connecting: %v", err)
    return
}

for bar := range bars {
    fmt.Println(bar)
}

Reference

Development

git tag                 # list tags
git tag vx.y.z          # create tag
git push origin vx.y.z  # push tags to origin
GOPROXY=proxy.golang.org go list -m github.com/wboayue/ibapi@vx.y.z     # publish to package repo

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bar

type Bar struct {
	Time   time.Time // The bar's date and time (either as a yyyymmss hh:mm:ss formatted string or as system time according to the request). Time zone is the TWS time zone chosen on login.
	Open   float64   // The bar's open price.
	High   float64   // The bar's high price.
	Low    float64   // The bar's low price.
	Close  float64   // The bar's close price.
	Volume int64     // The bar's traded volume if available (only available for TRADES)
	WAP    float64   // The bar's Weighted Average Price (only available for TRADES)
	Count  int       // The number of trades during the bar's timespan (only available for TRADES)
}

type BidAsk

type BidAsk struct {
	Time            time.Time       // The spread's date and time (either as a yyyymmss hh:mm:ss formatted string or as system time according to the request). Time zone is the TWS time zone chosen on login.
	BidPrice        float64         // tick-by-tick real-time tick bid price
	AskPrice        float64         // tick-by-tick real-time tick ask price
	BidSize         int64           // tick-by-tick real-time tick bid size
	AskSize         int64           // tick-by-tick real-time tick ask size
	BidAskAttribute BidAskAttribute // tick-by-tick real-time bid/ask tick attribs (bit 0 - bid past low, bit 1 - ask past high)
}

type BidAskAttribute

type BidAskAttribute struct {
	BidPastLow  bool
	AskPastHigh bool
}

type ComboLeg

type ComboLeg struct {
	ContractId int    // The Contract's IB's unique id.
	Ratio      int    // Select the relative number of contracts for the leg you are constructing. To help determine the ratio for a specific combination order, refer to the Interactive Analytics section of the User's Guide.
	Action     string //The side (buy or sell) of the leg:
	Exchange   string // The destination exchange to which the order will be routed.

	// Specifies whether an order is an open or closing order. For instituational customers to determine if this order is to open or close a position. 0 - Same as the parent security. This is the only option for retail customers.
	// 1 - Open. This value is only valid for institutional customers.
	// 2 - Close. This value is only valid for institutional customers.
	// 3 - Unknown.
	OpenClose int

	ShortSaleSlot      int    // For stock legs when doing short selling. Set to 1 = clearing broker, 2 = third party.
	DesignatedLocation string // When ShortSaleSlot is 2, this field shall contain the designated location.
	ExemptCode         int    // DOC_TODO.
}

type Contract

type Contract struct {
	ContractId int    // The unique IB contract identifier.
	Symbol     string // The underlying's asset symbol.

	// The security's type:
	// STK - stock (or ETF)
	// OPT - option
	// FUT - future
	// IND - index
	// FOP - futures option
	// CASH - forex pair
	// BAG - combo
	// WAR - warrant
	// BOND - bond
	// CMDTY - commodity
	// NEWS - news
	// FUND - mutual fund.
	SecurityType string

	// The contract's last trading day or contract month (for Options and Futures).
	// Strings with format YYYYMM will be interpreted as the Contract Month whereas YYYYMMDD will be interpreted as Last Trading Day.
	LastTradeDateOrContractMonth string

	Strike      float64 // The option's strike price.
	Right       string  // Either Put or Call (i.e. Options). Valid values are P, PUT, C, CALL.
	Multiplier  string  // The instrument's multiplier (i.e. options, futures).
	Exchange    string  // The destination exchange.
	Currency    string  // The underlying's currency.
	LocalSymbol string  // The contract's symbol within its primary exchange For options, this will be the OCC symbol.

	// The contract's primary exchange.
	// For smart routed contracts, used to define contract in case of ambiguity.
	// Should be defined as native exchange of contract, e.g. ISLAND for MSFT For exchanges which contain a period in name, will only be part of exchange name prior to period, i.e. ENEXT for ENEXT.BE.
	PrimaryExchange string

	TradingClass   string // The trading class name for this contract. Available in TWS contract description window as well. For example, GBL Dec '13 future's trading class is "FGBL".
	IncludeExpired bool   // If set to true, contract details requests and historical data queries can be performed pertaining to expired futures contracts. Expired options or other instrument types are not available.
	SecurityIdType string // Security's identifier when querying contract's details or placing orders ISIN - Example: Apple: US0378331005 CUSIP - Example: Apple: 037833100.
	SecurityId     string // Identifier of the security type.

	ComboLegsDescription string // Description of the combo legs.
	ComboLegs            []ComboLeg
	DeltaNeutralContract DeltaNeutralContract // elta and underlying price for Delta-Neutral combo orders. Underlying (STK or FUT), delta and underlying price goes into this attribute.
}

Describes an instrument's definition

type ContractDetails

type ContractDetails struct {
	Contract       Contract // A fully-defined Contract object.
	MarketName     string   // The market name for this product.
	MinTick        float64  // The minimum allowed price variation. Note that many securities vary their minimum tick size according to their price. This value will only show the smallest of the different minimum tick sizes regardless of the product's price. Full information about the minimum increment price structure can be obtained with the reqMarketRule function or the IB Contract and Security Search site.
	PriceMagnifier int      // Allows execution and strike prices to be reported consistently with market data, historical data and the order price, i.e. Z on LIFFE is reported in Index points and not GBP. In TWS versions prior to 972, the price magnifier is used in defining future option strike prices (e.g. in the API the strike is specified in dollars, but in TWS it is specified in cents). In TWS versions 972 and higher, the price magnifier is not used in defining futures option strike prices so they are consistent in TWS and the API.
	OrderTypes     string   //Supported order types for this product.
	ValidExchanges string   // Valid exchange fields when placing an order for this contract.
	// The list of exchanges will is provided in the same order as the corresponding MarketRuleIds list.
	UnderContractId        int        // For derivatives, the contract ID (conID) of the underlying instrument.
	LongName               string     // Descriptive name of the product.
	ContractMonth          string     // Typically the contract month of the underlying for a Future contract.
	Industry               string     // The industry classification of the underlying/product. For example, Financial.
	Category               string     // The industry category of the underlying. For example, InvestmentSvc.
	Subcategory            string     // The industry subcategory of the underlying. For example, Brokerage.
	TimeZoneId             string     // The time zone for the trading hours of the product. For example, EST.
	TradingHours           string     // The trading hours of the product. This value will contain the trading hours of the current day as well as the next's. For example, 20090507:0700-1830,1830-2330;20090508:CLOSED. In TWS versions 965+ there is an option in the Global Configuration API settings to return 1 month of trading hours. In TWS version 970+, the format includes the date of the closing time to clarify potential ambiguity, ex: 20180323:0400-20180323:2000;20180326:0400-20180326:2000 The trading hours will correspond to the hours for the product on the associated exchange. The same instrument can have different hours on different exchanges.
	LiquidHours            string     // The liquid hours of the product. This value will contain the liquid hours (regular trading hours) of the contract on the specified exchange. Format for TWS versions until 969: 20090507:0700-1830,1830-2330;20090508:CLOSED. In TWS versions 965+ there is an option in the Global Configuration API settings to return 1 month of trading hours. In TWS v970 and above, the format includes the date of the closing time to clarify potential ambiguity, e.g. 20180323:0930-20180323:1600;20180326:0930-20180326:1600.
	EvRule                 string     // Contains the Economic Value Rule name and the respective optional argument. The two values should be separated by a colon. For example, aussieBond:YearsToExpiration=3. When the optional argument is not present, the first value will be followed by a colon.
	EvMultiplier           int        // Tells you approximately how much the market value of a contract would change if the price were to change by 1. It cannot be used to get market value by multiplying the price by the approximate multiplier.
	AggGroup               int        // Aggregated group Indicates the smart-routing group to which a contract belongs. contracts which cannot be smart-routed have aggGroup = -1.
	SecIdList              []TagValue // A list of contract identifiers that the customer is allowed to view. CUSIP/ISIN/etc. For US stocks, receiving the ISIN requires the CUSIP market data subscription. For Bonds, the CUSIP or ISIN is input directly into the symbol field of the Contract class.
	UnderSymbol            string     // For derivatives, the symbol of the underlying contract.
	UnderSecType           string     // For derivatives, returns the underlying security type.
	MarketRuleIds          string     // The list of market rule IDs separated by comma Market rule IDs can be used to determine the minimum price increment at a given price.
	RealExpirationDate     string     // Real expiration date. Requires TWS 968+ and API v973.04+. Python API specifically requires API v973.06+.
	LastTradeTime          string     //Last trade time.
	StockType              string     // Stock type.
	Cusip                  string     // The nine-character bond CUSIP. For Bonds only. Receiving CUSIPs requires a CUSIP market data subscription.
	Ratings                string     // Identifies the credit rating of the issuer. This field is not currently available from the TWS API. For Bonds only. A higher credit rating generally indicates a less risky investment. Bond ratings are from Moody's and S&P respectively. Not currently implemented due to bond market data restrictions.
	DescAppend             string     // A description string containing further descriptive information about the bond. For Bonds only.
	BondType               string     // The type of bond, such as "CORP.".
	CouponType             string     // The type of bond coupon. This field is currently not available from the TWS API. For Bonds only.
	Callable               bool       // If true, the bond can be called by the issuer under certain conditions. This field is currently not available from the TWS API. For Bonds only.
	Putable                bool       // Values are True or False. If true, the bond can be sold back to the issuer under certain conditions. This field is currently not available from the TWS API. For Bonds only.
	Coupon                 float64    // The interest rate used to calculate the amount you will receive in interest payments over the course of the year. This field is currently not available from the TWS API. For Bonds only.
	Convertible            bool       // Values are True or False. If true, the bond can be converted to stock under certain conditions. This field is currently not available from the TWS API. For Bonds only.
	Maturity               string     // The date on which the issuer must repay the face value of the bond. This field is currently not available from the TWS API. For Bonds only. Not currently implemented due to bond market data restrictions.
	IssueDate              string     // The date the bond was issued. This field is currently not available from the TWS API. For Bonds only. Not currently implemented due to bond market data restrictions.
	NextOptionDate         string     // Only if bond has embedded options. This field is currently not available from the TWS API. Refers to callable bonds and puttable bonds. Available in TWS description window for bonds.
	NextOptionType         string     // Type of embedded option. This field is currently not available from the TWS API. Only if bond has embedded options.
	NextOptionPartial      bool       // Only if bond has embedded options. This field is currently not available from the TWS API. For Bonds only.
	Notes                  string     // If populated for the bond in IB's database. For Bonds only.
	MinSize                float64    // Order's minimal size.
	SizeIncrement          float64    // Order's size increment.
	SuggestedSizeIncrement float64    // Order's suggested size increment.
}

type DeltaNeutralContract

type DeltaNeutralContract struct {
	ContractId string  // The unique contract identifier specifying the security. Used for Delta-Neutral Combo contracts.
	Delta      float64 // The underlying stock or future delta. Used for Delta-Neutral Combo contracts.
	Price      float64 // The price of the underlying. Used for Delta-Neutral Combo contracts.
}

elta and underlying price for Delta-Neutral combo orders. Underlying (STK or FUT), delta and underlying price goes into this attribute.

type IbClient

type IbClient struct {
	ServerVersion    int        // IB server version
	ServerTime       time.Time  // IB server time
	NextValidOrderId int        // next valid order id
	ManagedAccounts  string     // Ids of managed accounts
	MessageBus       MessageBus // bus used to communicate with server
	// contains filtered or unexported fields
}

func Connect

func Connect(host string, port int, clientId int) (*IbClient, error)

Connect creates a socket connection to TWS/IBG.

Parameters:

host 	- host to connect to
port 	- port to connect to
client 	- client id. can connect up to 32 clients

func (*IbClient) Close

func (c *IbClient) Close() error

func (*IbClient) ContractDetails

func (c *IbClient) ContractDetails(ctx context.Context, contract Contract) ([]ContractDetails, error)

ContractDetails requests contract information. This method will provide all the contracts matching the contract provided. It can also be used to retrieve complete options and futures chains.

func (*IbClient) RealTimeBars

func (c *IbClient) RealTimeBars(ctx context.Context, contract Contract, whatToShow string, useRth bool) (<-chan Bar, error)

RealTimeBars requests real time bars. Currently, only 5 seconds bars are provided. This request is subject to the same pacing as any historical data request: no more than 60 API queries in more than 600 seconds. Real time bars subscriptions are also included in the calculation of the number of Level 1 market data subscriptions allowed in an account.

Parameters:

contract 	- the Contract for which the depth is being requested
whatToShow 	- TRADES, MIDPOINT, BID, ASK
useRth 		- use regular trading hours

func (*IbClient) TickByTickBidAsk

func (c *IbClient) TickByTickBidAsk(ctx context.Context, contract Contract) (chan BidAsk, error)

TickByTickBidAsk requests tick-by-tick bid/ask.

func (*IbClient) TickByTickTrades

func (c *IbClient) TickByTickTrades(ctx context.Context, contract Contract) (chan Trade, error)

TickByTickTrades requests tick by tick trades.

type MessageBus

type MessageBus interface {
	ReadPacket() (string, error)
	Write(string) error
	WritePacket(string) error
	Close() error
}

type MessageBusRecorder

type MessageBusRecorder struct {
	Bus MessageBus
}

MessageBusRecorder records the MessageBus interactions

func (*MessageBusRecorder) Close

func (b *MessageBusRecorder) Close() error

func (*MessageBusRecorder) ReadPacket

func (b *MessageBusRecorder) ReadPacket() (string, error)

func (*MessageBusRecorder) Write

func (b *MessageBusRecorder) Write(data string) error

func (*MessageBusRecorder) WritePacket

func (b *MessageBusRecorder) WritePacket(packet string) error

type TagValue

type TagValue struct {
	Tag   string
	Value string
}

type TcpMessageBus

type TcpMessageBus struct {
	// contains filtered or unexported fields
}

TcpMessageBus implements the MessageBus over TCP

func (*TcpMessageBus) Close

func (b *TcpMessageBus) Close() error

Close closes the network connection

func (*TcpMessageBus) Connect

func (b *TcpMessageBus) Connect(host string, port int, clientId int) error

Connect establises a connection to the remote host

func (*TcpMessageBus) ReadPacket

func (b *TcpMessageBus) ReadPacket() (string, error)

ReadPacket reads the next data packet from the message bus

func (*TcpMessageBus) Write

func (b *TcpMessageBus) Write(data string) error

WritePacket writes raw data to message bus

func (*TcpMessageBus) WritePacket

func (b *TcpMessageBus) WritePacket(data string) error

WritePacket writes a packet of data to message bus adding the length prefix

type Trade

type Trade struct {
	TickType          string         // tick type: "Last" or "AllLast"
	Time              time.Time      // The trade's date and time (either as a yyyymmss hh:mm:ss formatted string or as system time according to the request). Time zone is the TWS time zone chosen on login.
	Price             float64        // tick last price
	Size              int64          // tick last size
	TradeAttribute    TradeAttribute // tick attribs (bit 0 - past limit, bit 1 - unreported)
	Exchange          string         // tick exchange
	SpecialConditions string         // tick special conditions
}

type TradeAttribute

type TradeAttribute struct {
	PastLimit  bool
	Unreported bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL