ib_insync,打印股票代码,即使我没有指定它

2024-09-29 23:19:40 发布

您现在位置:Python中文网/ 问答频道 /正文

刚开始使用ib_insync。我试图将滴答声数据放入数据框中

以下是相关代码:

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

当我在终端中运行此代码时,它会打印ticker,我不确定这里到底需要更改什么


Tags: 数据closeopenconnrealtimestampaskint
1条回答
网友
1楼 · 发布于 2024-09-29 23:19:40

如果您只想在不显示信息的情况下获取刻度,那么以下是一些您应该能够运行的示例代码:

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()

相关问题 更多 >

    热门问题