Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python控制台上检索和显示来自TWS的历史数据?_Python_Ibpy_Tws - Fatal编程技术网

如何在python控制台上检索和显示来自TWS的历史数据?

如何在python控制台上检索和显示来自TWS的历史数据?,python,ibpy,tws,Python,Ibpy,Tws,我能够成功地建立从python到TWS的连接,但是我无法获取数据,或者不确定数据是否已获取但未显示 我是新手,你能帮我从TWS获取历史数据吗?这对我很有用 from ibapi.wrapper import EWrapper from ibapi.client import EClient from ibapi.utils import iswrapper from ibapi.common import * from ibapi.contract import * from ibapi.tic

我能够成功地建立从python到
TWS
的连接,但是我无法获取数据,或者不确定数据是否已获取但未显示

我是新手,你能帮我从
TWS
获取历史数据吗?

这对我很有用

from ibapi.wrapper import EWrapper
from ibapi.client import EClient
from ibapi.utils import iswrapper
from ibapi.common import *
from ibapi.contract import *
from ibapi.ticktype import *

class TestApp(EWrapper, EClient):
  def __init__(self):
    EClient.__init__(self,self)

  def error(self, reqId: TickerId, errorCode:int, errorString:str):
    print('Error:', reqId, " ", errorCode, " ", errorString)

  def contractDetails(self, reqId:int, contractDetails:ContractDetails):
    print("contractDetails: ", reqId, " ", contractDetails)

  def marketDataType(self, reqId: TickerId, marketDataType: int):
    super().marketDataType(reqId, marketDataType)
    print("MarketDataType. ", reqId, "Type:", marketDataType)
  @iswrapper
  def tickPrice(self, reqId: TickerId, tickType: TickType, price: float, attrib: TickAttrib):
    super().tickPrice(reqId, tickType, price, attrib)
    print("Tick Price. Ticker Id:", reqId, "tickType:", tickType, "Price:", price, "CanAutoExecute:", attrib.canAutoExecute, "PastLimit", attrib.pastLimit)
  @iswrapper
  def tickSize(self, reqId: TickerId, tickType: TickType, size: int):
    super().tickSize(reqId, tickType, size)
    print("Tick Size. Ticker Id:", reqId, "tickType:", tickType, "Size:", size)

  @iswrapper
  def tickString(self, reqId: TickerId, tickType: TickType, value: str):
    super().tickString(reqId, tickType, value)
    print("Tick string. Ticker Id:", reqId, "Type:", tickType, "Value:", value)

  @iswrapper
  def tickGeneric(self, reqId: TickerId, tickType: TickType, value: float):
    super().tickGeneric(reqId, tickType, value)
    print("Tick Generic. Ticker Id:", reqId, "tickType:", tickType, "Value:", value)

def main():
  app = TestApp()
  app.connect("127.0.0.1", 4001, 0)

  contract = Contract();
  contract.symbol = "VIX";
  contract.secType = "FUT";
  contract.exchange = "CFE";
  contract.currency = "USD";
  contract.lastTradeDateOrContractMonth = "20170621";

  app.reqMktData(1001, contract, "", False, False, [])

  app.run()
  sleep(10)
  app.disconnect()


if __name__ == '__main__':
  main()

你试过什么?请发布您的可复制代码,以便人们能够更好地帮助您。有关详细信息,请阅读。