Python 有没有合适的方法使用ib api生成OHLCV数据帧?

Python 有没有合适的方法使用ib api生成OHLCV数据帧?,python,pandas,jupyter,modularity,Python,Pandas,Jupyter,Modularity,这是打印数据的代码。但是,我不知道如何将这些数据收集到数据框中。我使用从ibapi interactive broker导入的reqHistoricalData从继承EClient和EWrapper的TestApp类函数请求数据 from ibapi.client import EClient from ibapi.wrapper import EWrapper from ibapi.contract import Contract from ibapi.order import Order f

这是打印数据的代码。但是,我不知道如何将这些数据收集到数据框中。我使用从ibapi interactive broker导入的reqHistoricalData从继承EClient和EWrapper的TestApp类函数请求数据

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()
输出为:

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
我要找的是:

           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

您可以将dataframe成员添加到TestApp,然后在每次调用historicalData时向其添加一行:

...
self.cols = ['date', 'open', 'high', 'low', 'close', 'volume']
self.df = pd.DataFrame(columns=self.cols)

def historicalData(self, reqId, bar):
    print (bar.date, bar.open, bar.high, bar.low, bar.close, bar.volume)
    self.df.loc[len(self.df)] = [bar.date, bar.open, bar.high, bar.low, bar.close, bar.volume]

您可能希望为每个reqId提供一个单独的数据帧

您可以向TestApp添加一个dataframe成员,然后在每次调用historicalData时向其添加一行:

...
self.cols = ['date', 'open', 'high', 'low', 'close', 'volume']
self.df = pd.DataFrame(columns=self.cols)

def historicalData(self, reqId, bar):
    print (bar.date, bar.open, bar.high, bar.low, bar.close, bar.volume)
    self.df.loc[len(self.df)] = [bar.date, bar.open, bar.high, bar.low, bar.close, bar.volume]

您可能希望为每个reqId提供一个单独的数据帧

我要做的是从模块队列创建一个队列

这就像是

def create_queue(self):
    my_queue = queue.Queue()
    self.my_hist_queue = my_queue
    return my_queue
然后,当我在包装器中定义historicalData时,我让它将其添加到队列中。就像

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})
然后,通过队列进行迭代并将历史数据放入字典列表是相对直接的。通过这种方式,熊猫可以轻松地将其转换为数据帧。我是这样做的

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)

我希望这有帮助!干杯

我要做的是从模块队列创建一个队列

这就像是

def create_queue(self):
    my_queue = queue.Queue()
    self.my_hist_queue = my_queue
    return my_queue
然后,当我在包装器中定义historicalData时,我让它将其添加到队列中。就像

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})
然后,通过队列进行迭代并将历史数据放入字典列表是相对直接的。通过这种方式,熊猫可以轻松地将其转换为数据帧。我是这样做的

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)
我希望这有帮助!干杯