I would like to create one minute bars from live market data and append those bars to the end of a historical data data frame. So, ideally I would retrieve 1 minute bar data when the program started running and append one minute bars onto that data frame from the live market data. One issue I seem to be having is that I cannot rename the columns in my "nohlc_df". See code below:
import pandas as pd
from ib_insync import *
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=999)
pd.options.display.max_columns= None
pd.set_option('display.max_rows', 3000)
pd.set_option('display.max_columns', 3000)
contract=Future('ES', '20240315', 'CME')
bars1 = ib.reqHistoricalData(
contract, endDateTime='', durationStr='1 D',
barSizeSetting='1 min', whatToShow='MIDPOINT', useRTH=False)
column_select=[0,1,2,3,4]
df = util.df(bars1)
df=df.iloc[:, column_select]
df=df.set_index('date')
df=df.tz_convert(tz='America/Los_Angeles')
df1=df.tail(10)
print(df1)
ld_df=pd.DataFrame(columns=['time', 'last'])
market_data= ib.reqMktData(contract,'', False, False)
def onPendingTicker(ticker):
new_row={'time':market_data.time, 'last':market_data.last}
ld_df.loc[len(ld_df)]=new_row
df1=ld_df.set_index('time')
nohlc_df = df1.resample("1min").ohlc()
nohlc_df=nohlc_df.tz_convert(tz='America/Los_Angeles')
combined_ohlc_df=pd.concat([df1, nohlc_df.rename(columns={'last, low':'low', 'last, open':'open', 'last, high':'high'})], ignore_index=False)
print(combined_ohlc_df)
ib.pendingTickersEvent += onPendingTicker
ib.run()
Any help would be very much appreciated.