获取IB历史选项数据

时间:2018-05-17 20:17:28

标签: python api ibpy

在以下程序中,我可以获得合同详细信息,但我没有获得历史数据。不确定我做错了什么。

from ib.ext.Contract import Contract
from ib.ext.ContractDetails import ContractDetails
from ib.opt import ibConnection, message

import time
import datetime
from time import sleep, strftime

twsPort = 7496
gatewayPort = 4001

def watcher(msg):
    print("[watcher: ", msg, " ]")

def contractDetailsHandler(msg):
    print("[contractDetailsHandler:]")
    contractDetails = msg.contractDetails
    contract = msg.contractDetails.m_summary
    print(contractDetails.m_cusip, contractDetails.m_underConId, contractDetails.m_longName, contractDetails.m_industry,
          contractDetails.m_category, contractDetails.m_subcategory, contract.m_symbol, contract.m_secType, 
          contract.m_strike, contract.m_right, contract.m_exchange, 
          contract.m_currency, contract.m_secIdType, contract.m_secId, "\n")
    contracts.append(contractDetails.m_summary)

def contractDetailsEndHandler(msg):
    print("[contractDetailsEndHandler:]")


def contractHistDetailsHandler(msg):
    global DataWait
    print("[contractHistDetailsHandler:]")
    contracts.append(msg.historicalData)
    DataWait =  False


con = ibConnection()
con.host = "127.0.0.1"
con.port = gatewayPort
con.clientId = 5
con.registerAll(watcher)
con.register(contractDetailsHandler, 'ContractDetails')
con.register(contractDetailsEndHandler, 'ContractDetailsEnd')
con.register(contractHistDetailsHandler, message.historicalData)

con.connect()

contract = Contract()
contract.m_exchange     = "SMART"
contract.m_secType      = "OPT"
contract.m_symbol       = "SPY"
contract.m_currency     = "USD"
contract.m_strike       = 260
contract.m_right        = "PUT"
contract.m_expiry       = "20180615"
#contract.m_includeExpired = True

endtime = strftime('%Y%m%d %H:%M:%S')
#endtime = '20170102 01:00:00'

con.reqContractDetails(1, contract)

con.reqHistoricalData(2,contract,endtime,"14 D","30 min","MIDPOINT",0,1)

contracts = []

DataWait = True ;  i = 0
while DataWait and i < 90:
    i += 1 ; print(i),
    time.sleep(1)

time.sleep(1)

con.disconnect()
con.close()

print(contracts)

计划的输出:

mm2:python ivan python ib_hist.py
Server Version: 76
TWS Time at connection:20180521 19:38:15 CST
20180521 19:38:16
1
[watcher:  <managedAccounts accountsList=F........>  ]
[watcher:  <nextValidId orderId=1>  ]
2
3
4
5
6
7
8
9
[watcher:  <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:ushmds>  ]
[watcher:  <contractDetails reqId=1, contractDetails=<ib.ext.ContractDetails.ContractDetails object at 0x1024d8f60>>  ]
[contractDetailsHandler:]
 756733 SPDR S&P 500 ETF TRUST None None None SPY OPT 260.0 P SMART USD

[watcher:  <contractDetailsEnd reqId=1>  ]
[contractDetailsEndHandler:]
[watcher:  <error id=2, errorCode=165, errorMsg=Historical Market Data Service query message:HMDS server connection was successful.>  ]
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
[watcher:  <connectionClosed>  ]
[<ib.ext.Contract.Contract object at 0x1027ba080>]
mm2:python ivan$

2 个答案:

答案 0 :(得分:1)

您必须订阅实时数据。

你不能要求14分钟1分钟,它必须至少30分钟&#39;

HistDetailsHandler您使用msg.contractDetails时出错,msg是历史数据。

我真的不明白你在等待和使用全局变量时所做的事情。您有一个有效的合同,然后要求它,然后将其分配回自身,然后将其用于历史数据请求。

只需请求您确定已订阅并从中构建的历史数据。

答案 1 :(得分:-1)

我终于得到了一些历史数据。我不得不降低天数。我使用了5天30分钟的数据。花了很长时间才拿回信息,平均60秒。