Laden historischer Daten, die ich von IB API an Echtzeit n Python angehängt habePython

Python-Programme
Anonymous
 Laden historischer Daten, die ich von IB API an Echtzeit n Python angehängt habe

Post by Anonymous »

Wie kann ich historische Daten aus der IB -API laden und an Echtzeit aktuelle Balken 1 min -Diagramm anhängen? < /p>
def get_front_month_contract(symbol, exchange="CME", currency="USD", secType="FUT"):
ib = IB()
st.info("Connecting to IBKR Gateway via IB Insync...")
try:
ib.connect('--------', 48879, clientId=1)
except Exception as e:
st.error(f"Error connecting to IBKR Gateway: {e}")
return None
contract = Future()
contract.symbol = symbol
contract.secType = secType
contract.exchange = exchange
contract.currency = currency
st.info(f"Requesting contract details for {symbol} futures...")
details = ib.reqContractDetails(contract)
if not details:
st.error(f"No contract details found for {symbol}.")
ib.disconnect()
return None
today = datetime.today()
valid_contracts = []
for d in details:
expiry_str = d.contract.lastTradeDateOrContractMonth
expiry_date = parse_expiry(expiry_str)
if expiry_date and expiry_date > today:
valid_contracts.append((expiry_date, d.contract))
if not valid_contracts:
st.error(f"No active futures contracts found for {symbol}.")
ib.disconnect()
return None
valid_contracts.sort(key=lambda x: x[0])
front_month_contract = valid_contracts[0][1]
st.info(f"Selected front month contract: {front_month_contract.localSymbol}")
ib.disconnect()
return front_month_contract
< /code>
def get_historical_data (Vertrag, Dauer, barSizesetting, whattoshow = 'trades', userth = true):
ib = ib ()
Versuchen Sie:
ib.connect ('127.0.0.1', 4002, clientID = 10)
Ausnahme der Ausnahme als e:
st.Error(f"Error, der sich für historische Daten verbindet: {e} ")
return pd.dataframe ()
bars = ib.reqhistoricalData (
Contract,
EndDatetime = '',
Durationsstr = Durationsstr,
barsizeSeting = barsizeSeting,
whattoshow = whattoshow,
userth = userth,
formatdate = 1
)
ib.disconnect()
Wenn nicht Balken:
St.Error (ERROR (ERBEHALTEN ( "Keine historischen Daten, die von ib zurückgegeben wurden.")
Return pd.dataframe ()
data = []
Für Balken in Balken:
data.append( <
"DateTime": pd.to_datetime (bar.date),
"Öffnen": bar.open,
"High": bar.High,
"Low": bar.low, < Br /> "Close": bar.close,
"Volume": bar.volume
})
df = pd.dataframe (data)
df.set_index("datetime ", inplace = true)
return < /p>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post