-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIBPy
More file actions
108 lines (89 loc) · 3.63 KB
/
Copy pathIBPy
File metadata and controls
108 lines (89 loc) · 3.63 KB
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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#Installing IBPy
source activate PyAlgo#starting env
sudo apt-get install git-core# already installed for GIT
cd /home/octo/IBAPI_py# directory created
git clone https://github.com/blampe/IbPy# download gitclone
cd IbPy#(PyAlgo)octo@ALPHA:~/IBAPI_py/IbPy$
python setup.py install
#Another pack https://github.com/Komnomnomnom/swigibpy
#swigibpy is working and ipynb notebook at desktop/project folder
REF:
#https://www.quantstart.com/articles/Using-Python-IBPy-and-the-Interactive-Brokers-API-to-Automate-Trades
#http://www.elitetrader.com/et/index.php?threads/ib-api-howtos-and-guidelines-for-beginners.279658/page-5
#http://stackoverflow.com/questions/25299539/getting-parameters-of-listed-options-futures-in-interactive-brokers-api/34754857#34754857
#http://stackoverflow.com/questions/34561626/ibpy-getting-portfolio-information-interactive-broker-python/34755680#34755680
#https://www.quantopian.com/posts/ibpython3
#https://code.google.com/archive/p/ibpy/wikis/GettingStarted.wiki
#https://pypi.python.org/pypi/ib/0.8.0
####
IbPy code #triggering is not okay
#####
# LOAD the ib.ext and ib.opt Libraries
from time import sleep
from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
#a basic function to capture error messages
def error_handler(msg):
print "IB: Server Error = ", msg
def reply_handler(msg):
"""Handles of server replies"""
print "Server Response: %s, %s" % (msg.typeName, msg)
def print_portfolio_position(msg):
print "Position:", msg.contract.m_symbol, msg.position, msg.marketPrice, msg.contract.m_currency, msg.contract.m_secType
def make_contract(symbol, sec_type, exch, prim_exch, curr):
"""
symbol - The ticker symbol for the contract
sec_type - The security type for the contract ('STK' is 'stock')
exch - The exchange to carry out the contract on
prim_exch - The primary exchange to carry out the contract on
curr - The currency in which to purchase the contract"""
Contract.m_symbol = symbol
Contract.m_secType = sec_type
Contract.m_exchange = exch
Contract.m_primaryExch = prim_exch
Contract.m_currency = curr
return Contract
def make_order(action,quantity, price = None):
"""Create an Order object (Market/Limit) to go long/short.
order_type - 'MKT', 'LMT' for Market or Limit orders
quantity - Integral number of assets to order
action - 'BUY' or 'SELL'"""
if price is not None:
order = Order()
order.m_orderType = 'LMT'
order.m_totalQuantity = quantity
order.m_action = action
order.m_lmtPrice = price
else:
order = Order()
order.m_orderType = 'MKT'
order.m_totalQuantity = quantity
order.m_action = action
return order
cid = 303
while __name__ == "__main__":
conn = Connection.create(port=7496, clientId=123)
conn.connect()
conn.register(error_handler, 'Error')
conn.registerAll(reply_handler)
# Map server replies to "print_portfolio_position" function for "UpdatePortfolio" client requests
conn.register(print_portfolio_position, 'UpdatePortfolio')
conn.register(reply_handler, 'UpdateAccountValue')
# Make client request for AccountUpdates (includes request for Portfolio positions)
conn.reqAccountUpdates(1, '')
# Stop client request for AccountUpdates
conn.reqAccountUpdates(0, '')
sleep(5)
oid = cid
cont = make_contract('SQQQ', 'STK', 'SMART', 'SMART', 'USD')
#offer = make_order('BUY', 1, 200)
offer = make_order('BUY', 1,'MKT')
conn.placeOrder(oid, cont, offer)
conn.disconnect()
x = raw_input('enter to resend')
cid += 1
####
####
swigibpy CODE
###