Search code examples
pythonpandasjupytermodularity

Is there a proper way to produce a OHLCV pandas dataframe using ib api?


Here is the code that print out the data. However i don't see how to collect these data into a pandas dataframe. I used reqHistoricalData imported from ibapi (interactive broker) to request the data from TestApp class function which inherit EClient and EWrapper.

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order
from ibapi.ticktype import TickTypeEnum
import pandas as pd
import numpy as np
import os.path  # To manage paths
import sys  # To find out the script name (in argv[0])
from datetime import datetime
from time import sleep, strftime, localtime 
from socket import error as SocketError
import errno

class TestApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self,self)

    def error(self, reqId, errorCode, errorString):
        print ('Error: ', reqId, errorCode, ' ', errorString)

    def historicalData(self,reqId, bar):

         print (bar.date, bar.open, bar.high, bar.low, bar.close, bar.volume)


def create_contract(symbol, sec_type, exch, prim_exch, curr):

    contract = Contract()
    contract.symbol = symbol
    contract.secType = sec_type
    contract.exchange = exch
    contract.currency = curr
    contract.primaryExchange = prim_exch

    return contract

def create_order(order_type, quantity, action):

    order = Order()
    order.orderType = order_type
    order.totalQuantity = quantity
    order.action = action

    return order

app = TestApp()
app.connect('127.0.0.1', 7497, 0)

contract = create_contract('AAPL', 'STK', 'SMART', 'NASDAQ', 'USD')

app.reqHistoricalData(      reqId = 0, 
                            contract = contract, 
                            endDateTime = '', 
                            durationStr = '1 Y', 
                            barSizeSetting = '1 month', 
                            whatToShow = 'TRADES',
                            useRTH = 1, # =1 for RTH data
                            formatDate = 1,
                            keepUpToDate = False,
                            chartOptions = []
                         ) 

app.run()

and the output is:

20181031 222.52 224.23 206.09 218.86 1752000
20181130 219.07 222.36 170.26 178.58 7249186
20181231 184.39 184.94 146.6 157.74 6851826
20190131 154.89 169.0 142.0 166.44 6383564
20190228 166.93 175.87 165.93 173.15 3478346
20190329 174.28 197.69 169.5 189.95 4956586
20190430 191.64 208.48 188.38 200.67 3812115
20190531 209.88 215.31 174.99 175.07 5642571
20190628 175.58 201.57 170.27 197.92 3592406
20190731 203.28 221.37 198.41 213.04 3418242
20190830 213.82 218.03 192.58 208.74 5078104
20190930 206.42 226.42 204.22 223.97 3768842
20191023 225.13 243.18 215.13 242.51 3253952

What i am looking for:

           Open   High    Low  Close Volume
Date                                       
20181031 222.52 224.23 206.09 218.86 1752000
20181130 219.07 222.36 170.26 178.58 7249186
20181231 184.39 184.94 146.6 157.74 6851826
20190131 154.89 169.0 142.0 166.44 6383564
20190228 166.93 175.87 165.93 173.15 3478346
20190329 174.28 197.69 169.5 189.95 4956586
20190430 191.64 208.48 188.38 200.67 3812115
20190531 209.88 215.31 174.99 175.07 5642571
20190628 175.58 201.57 170.27 197.92 3592406
20190731 203.28 221.37 198.41 213.04 3418242
20190830 213.82 218.03 192.58 208.74 5078104
20190930 206.42 226.42 204.22 223.97 3768842
20191023 225.13 243.18 215.13 242.51 3253952

Solution

  • What I do is create a queue from the module queue.

    So that goes something like...

    def create_queue(self):
        my_queue = queue.Queue()
        self.my_hist_queue = my_queue
        return my_queue
    

    Then, when I define historicalData in the wrapper, I have it add it to the queue. It goes like...

    def historicalData(self, reqId, bar):
        print("HistoricalData. ", reqId,
              "Date:", bar.date,
              "Open:", bar.open,
              "High:", bar.high,
              "Low:", bar.low,
              "Close:", bar.close,
              "Volume:", bar.volume,
              "Count:", bar.barCount,
              "WAP:", bar.average)
        self.my_hist_queue.put({'Request ID': reqId,
                                'Date': bar.date,
                                'Open': bar.open,
                                'High': bar.high,
                                'Low': bar.low,
                                'Close': bar.close,
                                'Volume': bar.volume,
                                'Count': bar.barCount,
                                'WAP': bar.average})
    

    Then, it is relatively straight forward to iterate through the queue and put the historical data into a list of dictionaries. This way, pandas can easily convert it into a data frame. Here's how I do it...

    def create_dataframe:
        ticker_list = []
        hist_storage = self.create_queue()
        num_of_days = 5 #put however many days you want
        data = self.reqHistoricalData(101, Contract, '', '{} D'.format(num_of_days), '1 day', "TRADES", 1, 1, False, [])
        for i in range(number_of_days):
            ticker_list.append(hist_storage.get())
        df = pd.DataFrame(ticker_list)
        print(df)
    

    I hope this helps! Cheers!