python 获取IB历史数据 ib_insync 库的使用
原创
©著作权归作者所有:来自51CTO博客作者fox64194167的原创作品,请联系作者获取转载授权,否则将追究法律责任
python 获取IB历史数据 ib_insync 库的使用
import pandas as pd
from ib_insync import *
# util.startLoop() # uncomment this line when in a notebook
ib = IB()
ib.connect('127.0.0.1', 7496, clientId=1)
code = "PDD"
contract = Stock(code, exchange="SMART")
bars = ib.reqHistoricalData(contract, endDateTime='20190314 18:30:00', durationStr='1 D',
barSizeSetting='30 secs', whatToShow='MIDPOINT', useRTH=True)
# convert to pandas dataframe:
df = util.df(bars)
print(df[['date', 'open', 'high', 'low', 'close']])
date open high low close
0 2019-03-13 21:30:00 27.280 27.840 27.185 27.775
1 2019-03-13 21:30:30 27.775 27.830 27.535 27.745
2 2019-03-13 21:31:00 27.745 27.890 27.725 27.880
3 2019-03-13 21:31:30 27.880 27.895 27.755 27.755
4 2019-03-13 21:32:00 27.755 27.845 27.725 27.845
5 2019-03-13 21:32:30 27.845 27.895 27.805 27.850
6 2019-03-13 21:33:00 27.850 27.855 27.725 27.725
7 2019-03-13 21:33:30 27.725 27.725 27.210 27.225