brahma-investment-group / big-algo-framework

Another trading algo!
MIT License
31 stars 4 forks source link

Historical Data with IB #27

Open naga-9 opened 1 year ago

naga-9 commented 1 year ago

This is a code for historical data with IB. Need to generalize the formula, create a class using the same format as existing data functions.

'''
Fetch Historical tick data

Historical tick data can be fetched with a maximum of 1000 ticks at a time. 
Either the start time or the end time must be given, and one of them must remain empty:
'''

import datetime
from ib_insync import *
import pandas as pd

# Start IB session running
ib = IB()
ib.connect('127.0.0.1', 4002, clientId=2)
#ib.connect('127.0.0.1', 7496, clientId=1)

# Create some Forex contracts:
contracts = [Forex(pair) for pair in ('EURUSD', 'USDJPY', 'GBPUSD', 'USDCHF', 'USDCAD', 'AUDUSD')]
ib.qualifyContracts(*contracts)

eurusd = contracts[0]

start = ''
end = datetime.datetime.now()
ticks = ib.reqHistoricalTicks(eurusd, start, end, 1000, 'BID_ASK', useRth=False)

#print(ticks[-1])
#print(ticks)
print(util.df(ticks))

ib.disconnect()
ClimberMel commented 1 year ago

Can I get added to the project. I'd like to be able to assign this to myself. I'll clean it up to follow the abstract method and add in extra functions for Stock, Options, Futures, Forex)