forked from mklechan/IBAlgoTrading
/
example_historicaldata.py
61 lines (49 loc) · 1.98 KB
/
example_historicaldata.py
1
2
3
4
5
6
7
8
9
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
''' Simple example of using the SWIG generated TWS wrapper to request historical
data from interactive brokers.
Note:
* Communication with TWS is asynchronous; requests to TWS are made through the
EPosixClientSocket class and TWS responds at some later time via the functions
in our EWrapper subclass.
* If you're using a demo account TWS will only respond with a limited time
period, no matter what is requested. Also the data returned is probably wholly
unreliable.
'''
from datetime import datetime, timedelta
import time
from swigibpy import EWrapper, EPosixClientSocket, Contract, Order
import sys
sys.path.append('/home/mchan/git/Artemis/SwigIbPy/')
from DataDownloadIBWrapper import ArtemisIBWrapper
#from ArtemisIBWrapper import ArtemisIBWrapper
###
# Instantiate our callback object
callback = ArtemisIBWrapper()
# Instantiate a socket object, allowing us to call TWS directly. Pass our
# callback object so TWS can respond.
tws = EPosixClientSocket(callback)
# Connect to tws running on localhost
tws.eConnect("", 7496, 46, poll_interval=1)
accountNumber = ''
contract = Contract()
contract.exchange = "SMART"
contract.symbol = "TOT"
contract.secType = "STK"
#contract.right = "PUT"
contract.currency = "USD"
#contract.secType = 'OPT'
#contract.strike = 24
#contract.expiry = '20121116'
today = datetime.today()
#tws.reqAccountUpdates(1, accountNumber)
#tws.reqAccountUpdates(0, accountNumber)
callback.histTickerID = contract.symbol
tws.reqHistoricalData(
1, #tickerId,
contract, #contract,
datetime(2012, 11, 2, 23, 59, 59).strftime("%Y%m%d %H:%M:%S"), #endDateTime,
"2 D", #durationStr,
"1 min", #barSizeSetting,
"TRADES", #whatToShow,
0, #useRTH,
1 #formatDate
)