forked from lbwdruid/Algorithmic-Trading-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tutorial 10 US - API.py
39 lines (30 loc) · 930 Bytes
/
Tutorial 10 US - API.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
from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
ibConnection = None
def operate(orderId, ticker, action, quantity, price=None):
# 1. Contract
contract = Contract()
contract.m_symbol = ticker
contract.m_secType = 'STK'
contract.m_exchange = 'ISLAND'
contract.m_currency = 'USD'
# 2. Order
order = Order()
if price is not None:
order.m_orderType = 'LMT'
order.m_lmtPrice = price
else:
order.m_orderType = 'MKT'
order.m_totalQuantity = quantity
order.m_action = action
# 3. Place Order
ibConnection.placeOrder(orderId, contract, order)
# Step 1. Establish connetion
ibConnection = Connection.create(port=7497, clientId=999)
ibConnection.connect()
# Step 2. Buy 123 NVDA
#operate(orderId=10, ticker='TSLA', action='BUY', quantity=123)
operate(orderId=11, ticker='TSLA', action='SELL', quantity=100)
# Step 3. Disconnect
ibConnection.disconnect()