在下面的程序中,我能够获得合同详细信息,但我没有获得历史数据。不知道我做错了什么。
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$