Coder Social home page Coder Social logo

santoshsrinivas79 / ib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from anthonyng2/ib

0.0 1.0 0.0 25.25 MB

Samples code demonstrating how to use IbPy to extract information from Interactive Brokers API

Python 1.96% Jupyter Notebook 98.04%

ib's Introduction

ib Files

Released 1 Feb 2016

Updated 12 May 2016 with Tutorial

  • ib_class.py : Demonstrates how to call IB using IbPy for account and other information. The codes are intentionally kept in self contained groups. E.g., Account and Portfolio, Orders etc.
  • IBWrapper.py : Wrapper functions to receive data from IB.

These codes have been tested on gateway Build 952.2f and TWS and ONLY on paper trading account. Please carry out your own test on your own account.It is best to be read in conjuction with the IB Java API manual.

IbPy - Interactive Brokers Python API

IbPy was originally written by Troy Melhase.

IB TWS and Gateway can be obtained via IB website

Introduction

Interactive Brokers offers a trading lab for education institution.

The instructor receives a "master" account with the ability to view all the students account information. However, as our class size often varies from 20 to 40, extracting key information from each account can be tedious and time consuming. Hence the reason for turning to IB API and writing these codes. The students' Net Liquidation Value (NLV) are download automatically and posted to plotly

It allows the students to view each others' NLV. Naturally, this encourages competition, learning and camaraderie as well as communication and sharing. You can find a sample notebook here

The following example is a great way to start. However, if you are comfortable with Python and Pandas, you can check out this Jupyter tutorial

Example

Below is a sample code snippets for Account and Portfolio Group. Best to leave the portion prior to Account and Portfolio alone as it sets up the necessary state to receive information from IB.

    import time
    from datetime import datetime
    from IBWrapper import IBWrapper, contract
    from ib.ext.EClientSocket import EClientSocket
    from ib.ext.ScannerSubscription import ScannerSubscription
    
    if __name__=="__main__":
        callback = IBWrapper()                # Instantiate IBWrapper 
        tws = EClientSocket(callback)         # Instantiate EClientSocket
        host = ""
        port = 7496
        clientId = 5000
        tws.eConnect(host, port, clientId)    # Connect to TWS
        tws.setServerLogLevel(5)
        accountName = "DU123456"              # Make sure you use your own 
        create = contract()                   # Instantiate contract class
        
        # Initiate attributes to receive data. At some point we need a separate class for this
        callback.initiate_variables()
        
        # Account and Portfolio ##############################################################
        # reqAccountUpdates   --->   updateAccountTime      self.update_AccountTime
        #                            updateAccountValue     self.update_AccountValue
        #                            updatePortfolio        self.update_Portfolio
        # accountDownloadEnd                                self.accountDownloadEnd_flag
        # reqAccountSummary   --->   accountSummary         self.account_Summary
        # cancelAccountSummary
        # accountSummaryEnd                                 self.account_SummaryEnd_flag
        # reqPositions        --->   position               self.update_Position
        # cancelPositions
        # positionEnd                                       self.positionEnd_flag
        ######################################################################################
        print "Testing Account and Portfolio \n"
        #tws.reqAccountUpdates(1, accountName)
        tws.reqAccountSummary(1,"All","NetLiquidation")
        #tws.reqPositions()
        time.sleep(2)

The following is the output

    Server Version: 76
    TWS Time at connection:20160202 09:33:21 SGT
    Testing Account and Portfolio 
    
    
    In [4]: callback.account_Summary
    Out[4]: 
    [(1, 'DI123456', 'NetLiquidation', '1012293.85', 'USD'),
     (1, 'DU123456', 'NetLiquidation', '803908.12', 'USD'),
    ...
     (1, 'DU123457', 'NetLiquidation', '831895.74', 'USD'),
     (1, 'DU123458', 'NetLiquidation', '1149361.71', 'USD'),
     (1, 'DU123459', 'NetLiquidation', '895145.61', 'USD'),
     (1, 'DU123460', 'NetLiquidation', '981381.33', 'USD')]

I'm very interested in your experience with the code. Please do drop me a note with any feedback you may have.

Anthony Ng

Look for me @ https://www.algo-hunter.com

ib's People

Contributors

anthonyng2 avatar

Watchers

 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.