Giter Club home page Giter Club logo

ibapi's Introduction

Interactive Brokers API - GoLang Implement

  • Interactive Brokers API 9.80
  • pure golang Implement
  • Unofficial, use at you own risk

INSTALL

go get -u github.com/hadrianl/ibapi


USAGE

Implement IbWrapper to handle datas delivered via tws or gateway, Wrapper in demo is a default implement that just output data to std. Go to IbWrapper

  1. implement your own IbWrapper
  2. connect to TWS or Gateway
  3. handshake with TWS or Gateway
  4. run the loop
  5. do some request

Demo 1

import (
    . "github.com/hadrianl/ibapi"
    "time"
)

func main(){
    // internal api log is zap log, you could use GetLogger to get the logger
    // besides, you could use SetAPILogger to set you own log option
    // or you can just use the other logger  
    log := GetLogger().Sugar()
    defer log.Sync()
    // implement your own IbWrapper to handle the msg delivered via tws or gateway
    // Wrapper{} below is a default implement which just log the msg 
    ic := NewIbClient(&Wrapper{})

    // tcp connect with tws or gateway
    // fail if tws or gateway had not yet set the trust IP
    if err := ic.Connect("127.0.0.1", 4002, 0);err != nil {
        log.Panic("Connect failed:", err)
    }

    // handshake with tws or gateway, send handshake protocol to tell tws or gateway the version of client
    // and receive the server version and connection time from tws or gateway.
    // fail if someone else had already connected to tws or gateway with same clientID
    if err := ic.HandShake();err != nil {
        log.Panic("HandShake failed:", err)
    }

    // make some request, msg would be delivered via wrapper.
    // req will not send to TWS or Gateway until ic.Run()
    // you could just call ic.Run() before these
    ic.ReqCurrentTime()
    ic.ReqAutoOpenOrders(true)
    ic.ReqAccountUpdates(true, "")
    ic.ReqExecutions(ic.GetReqID(), ExecutionFilter{})

    // start to send req and receive msg from tws or gateway after this
    ic.Run()
    <-time.After(time.Second * 60)
    ic.Disconnect()
}

Demo 2 with context

import (
    . "github.com/hadrianl/ibapi"
    "time"
    "context"
)

func main(){
    var err error
    log := GetLogger().Sugar()
    defer log.Sync()
    ibwrapper := &Wrapper{}
    ctx, _ := context.WithTimeout(context.Background(), time.Second*30)
    ic := NewIbClient(ibwrapper)
    ic.SetContext(ctx)
    err = ic.Connect("127.0.0.1", 4002, 0)
    if err != nil {
        log.Panic("Connect failed:", err)
    }

    err = ic.HandShake()
    if err != nil {
        log.Panic("HandShake failed:", err)
    }

    ic.ReqCurrentTime()
    ic.ReqAutoOpenOrders(true)
    ic.ReqAccountUpdates(true, "")
    ic.ReqExecutions(ic.GetReqID(), ExecutionFilter{})

    ic.Run()
    err = ic.LoopUntilDone()  // block until ctx or ic is done
    log.Info(err)
}

Reference

  1. Offical Document
  2. Order Types
  3. Product
  4. Margin
  5. Market Data
  6. Commissions

ibapi's People

Contributors

hadrianl avatar sachinbhutani avatar travisgroth avatar trey2k avatar vkopachev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.