简体   繁体   中英

ib_insync, prints ticker even though I have not specified it

Just started using ib_insync . I am trying to get the tick data into a dataframe.

Here is the relevant code:

def onPendingTickers(tickers, conn=conn):
    for t in tickers:
        # 'CREATE TABLE IF NOT EXISTS {} (timestamp timestamp, bid_qty INT, bid REAL, ask REAL, ' \
        # 'ask_qty INT, high REAL, low REAL, close REAL, open REAL, contractID INT)'
        # print(t)
        c.execute('INSERT INTO {} (timestamp, bid_qty, bid, ask, ask_qty, high, low, close, open, contractID)'
                  ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);'.format(t.contract.pair()),
                  (t.time, t.bidSize, t.bid, t.ask, t.askSize, t.high, t.low, t.close, t.open, t.contract.conId))
        # print(t.time, t.bidSize, t.bid, t.ask, t.askSize, t.high, t.low, t.close, t.open, t.contract.conId)
    conn.commit()

ib.pendingTickersEvent += onPendingTickers
ib.sleep(60*60)
ib.pendingTickersEvent -= onPendingTickers

When I run this code in a terminal, it prints the ticker , I am not sure what exactly needs to be changed here.

If you just want to get ticks without displaying the information, here's some sample code that you should be able to run:

from ib_insync import *
import pandas as pd
import numpy as np

# Connect to IB; args are (IP address, device number, client ID)
def ibConnect(port,clientID):
  connection = ib.connect('127.0.0.1', port, clientID)
  ib.sleep(0)
  return ()

# Disconnect from IB  
def ibDisconnect():
  ib.disconnect()
  ib.sleep(0)
  return

# Set up a futures contract
def ibFuturesContract(symbol, expirationDate, exchange):
  futuresContract = Future(symbol, expirationDate, exchange)
  return futuresContract

# Realtime Ticks Subscription
def ibGetTicker (contract):
  ticker = ib.ticker(contract)
  return [ticker]

ib = IB()
ibConnect(7496,300)
contract = ibFuturesContract('YM',20210618,'ECBOT')

# Start the real-time tick subscription
ib.reqMktData(contract, '', False, False)

# Real Time Ticks
global ticker
ticker = ibGetTicker(contract)

# Get just the last tick each second and put it into a data table
x = 0
while x < 10:
  ib.sleep(1)
  if ticker is not None:
    df = util.df(ticker)
    if (x == 0):
      dt = df
    else:
      dt = dt.append(df)
  x = x + 1

print (dt)  
ib.cancelMktData(contract)
ibDisconnect()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM