'How to attach time values to live data being streamed in a dataframe?

I am able to create a two-column dataframe of stock prices being streamed from the Interactive Brokers Trader Workstation API. The two columns are row number and price. I would like to include the time at which each price was streamed.

The relevant code is:

    def tick_df(self, reqId, contract):
        self.bardata[reqId] = pd.DataFrame(columns=['price', 'time'])
        t = time.localtime()
        current_time = time.strftime("%H.%M.%S", t)
        self.bardata[reqId].time = current_time
        self.reqMktData(reqId, contract, "", False, False, [])
        return self.bardata[reqId]

    def tickPrice(self, reqId, tickType, price, attrib):
        if tickType == 2 and reqId == 102:
            self.bardata[reqId].loc[self.bardata[reqId].time] = price

df_stock = app.tick_df(102, app.stock)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source