0

I am streaming live price data using the IB API, and I want to put it in a dataframe for analysis. My data consists of a price being live streamed with no timestamp.

I think I need to create new rows using row numbers that are automatically added, and have the prices inserted in the price column.

I have tried defining the dataframe and telling the price where to go as follows:

def tick_df(self, reqId,
            contract):  # this stores price dataframe by creating an empty dataframe and setting the index to the time column
    self.bardata[reqId] = pd.DataFrame(columns=['index', 'price'])
    self.reqMktData(reqId, contract, "", False, False, [])
    self.bardata[reqId].index = [x for x in range(1, len(self.bardata[reqId].values) + 1)]
    return self.bardata[reqId]

def tickPrice(self, reqId, tickType, price, attrib):  # this function prints the price
    if tickType == 2 and reqId == 102:
        self.bardata[reqId].loc[self.bardata[reqId].index] = price

I have been using a methodology similar to here (https://github.com/PythonForForex/Interactive-brokers-python-api-guide/blob/master/GOOG_five_percent.py). However, as I am only streaming a price, I am unable to use the timestamp for creating new rows.

  • 1
    What is the structure you need to keep with price data? is it a set of (time_stamp, price) tuples? Is that what you need to keep updated with your tick data streaming?. How often you perform calculations on your data? after each tick? or at fix frequency, for example one second, one minute? – Marcello Chiuminatto Jan 10 '22 at 19:45
  • Sorry, I don't really understand enough about my code and Python to fully answer your question, but I'll try my best. I'm just getting a set of (price) tuples, no time information. I'll perform calculations on the data after each tick. – Bryce Turner Jan 10 '22 at 19:56

1 Answers1

1

I don't know if this is what you need. In a loop I generate random price that I append to a data frame.

import numpy as np
import pandas as pd

_price = 1.1300 # first price in the series
_std = 0.0005  # volatility (stadard deviation)
df = pd.DataFrame(columns=['price'])

for i in range(1000):
    _wn = np.random.normal(loc=0, scale=_std, size=1)  # random white noise
    _price = _price + _wn[0]  # random price
    df = df.append({'price':_price}, ignore_index=True)

df

I work with FOREX time series and I do not conceive time series without time so, just in case you have the same 'problem', I'm including a version with time stamp:

import numpy as np
import pandas as pd
from datetime import datetime

_price = 1.1300  # first price in the series
_std = 0.0005  # volatility (stadard deviation)
df = pd.DataFrame(columns=['price', 'time'])

for i in range(1000):
    _wn = np.random.normal(loc=0, scale=_std, size=1)  # random white noise
    _price = _price + _wn[0] # random price
    _time = datetime.now()
    df = df.append({'price':_price, 'time':_time}, ignore_index=True)
    
df

Please let me know if this is what you needed.

  • Thanks, Marcello! I'll confirm whether it works after I can see how to integrate this into my existing code – Bryce Turner Jan 10 '22 at 20:39
  • 1
    I ended up using a method suggested by Josh (https://stackoverflow.com/questions/62416071/storing-api-data-into-a-dataframe) as it seemed to be the simplest for me to understand and implement, but thanks very much for your help anyway, Marcello! – Bryce Turner Jan 17 '22 at 01:32