pandas 尝试使用python和IB API获取多个证券的历史数据-循环之间未清除df

ogq8wdun  于 2022-12-10  发布在  Python
关注(0)|答案(1)|浏览(171)

我尝试通过IB API获取多个产品的历史数据,并将每个产品存储在 Dataframe 中(我需要将其保存在单独的csv文件中)。
这是我的代码,主要问题是循环之间的 Dataframe 没有清除,当进入第二个循环时,df包含2个产品的数据,第三个包含3个产品的数据。我不确定在哪里/如何清除df。

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import pandas as pd
import threading
import time

class IBapi(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
        self.data = []

    def historicalData(self, reqId, bar):
        self.data.append([bar.date, bar.open, bar.high, bar.low, bar.close, bar.volume])
           
    def error(self, reqId, errorCode, errorString):
        print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)

    def historicalDataEnd(self, reqId: int, start: str, end: str):
        print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
        self.df = pd.DataFrame(self.data)

def run_loop():
    app.run()
    
app = IBapi()

#Create contract object
ES_contract = Contract()
ES_contract.symbol = 'ES'
ES_contract.secType = 'FUT'
ES_contract.exchange = 'GLOBEX'
ES_contract.lastTradeDateOrContractMonth  = '202209'

#Create contract object
VIX_contract = Contract()
VIX_contract.symbol = 'VIX'
VIX_contract.secType = 'IND'
VIX_contract.exchange = 'CBOE'
VIX_contract.currency = 'USD'

#Create contract object
DAX_contract = Contract()
DAX_contract.symbol = 'DAX'
DAX_contract.secType = 'FUT'
DAX_contract.exchange = 'EUREX'
DAX_contract.currency = 'EUR'
DAX_contract.lastTradeDateOrContractMonth  = '202209'
DAX_contract.multiplier = '25'

products={'ES': ES_contract, 'VIX': VIX_contract,  'DAX': DAX_contract}

nid=1

app.connect('127.0.0.1', 4001, 123)
#Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()
time.sleep(1) #Sleep interval to allow time for connection to server

def fetchdata_function(name,nid):
    df=pd.DataFrame()
    #Request historical candles
    app.reqHistoricalData(nid, products[name], '', '1 W', '5 mins', 'TRADES', 0, 2, False, [])
    time.sleep(10) #sleep to allow enough time for data to be returned
    df = pd.DataFrame(app.data, columns=['Date', 'Open', 'High', 'Low', 'Close', 'Volume'])
    df['Date'] = pd.to_datetime(df['Date'],unit='s')
    df=df.set_index('Date')
    df.to_csv('1week'+str(name)+'5min.csv')  
    print(df)

names=['ES', 'DAX', 'VIX']

for name in names:     
    fetchdata_function(name,nid)
    nid=nid+1   
    
app.disconnect()
rdlzhqv9

rdlzhqv91#

创建一个字典并将app.data作为键值对附加到historicaldata回调函数中。

相关问题