forked from Jake0303/InteractiveBrokersPythonBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveBrokersPythonBot.py
More file actions
69 lines (68 loc) · 2.24 KB
/
InteractiveBrokersPythonBot.py
File metadata and controls
69 lines (68 loc) · 2.24 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
#Imports
import ibapi
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
#
from ibapi.contract import Contract
from ibapi.order import *
import threading
import time
#Vars
#Class for Interactive Brokers Connection
class IBApi(EWrapper,EClient):
def __init__(self):
EClient.__init__(self, self)
# Listen for realtime bars
def realtimeBar(self, reqId, time, open_, high, low, close,volume, wap, count):
super().realtimeBar(reqId, time, open_, high, low, close, volume, wap, count)
try:
bot.on_bar_update(reqId, time, open_, high, low, close, volume, wap, count)
except Exception as e:
print(e)
def error(self, id, errorCode, errorMsg):
print(errorCode)
print(errorMsg)
#Bot Logic
class Bot:
ib = None
def __init__(self):
#Connect to IB on init
self.ib = IBApi()
self.ib.connect("127.0.0.1", 7496,1)
ib_thread = threading.Thread(target=self.run_loop, daemon=True)
ib_thread.start()
time.sleep(1)
#Get symbol info
symbol = input("Enter the symbol you want to trade : ")
#Create our IB Contract Object
contract = Contract()
contract.symbol = symbol.upper()
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
# Request Market Data
self.ib.reqRealTimeBars(0, contract, 5, "TRADES", 1, [])
# TODO Submit ORDER
# Create Order Object
order = Order()
order.orderType = "MKT" # or LMT ETC....
order.action = "BUY" # or SELL ETC...
quantity = 1
order.totalQuantity = quantity
# Create Contract Object
contract = Contract()
contract.symbol = symbol
contract.secType = "STK" # or FUT ETC....
contract.exchange = "SMART"
contract.primaryExchange = "ISLAND"
contract.currency = "USD"
# Place the order
self.ib.placeOrder(2, contract, order)
#Listen to socket in seperate thread
def run_loop(self):
self.ib.run()
#Pass realtime bar data back to our bot object
def on_bar_update(self, reqId, time, open_, high, low, close, volume, wap, count):
print(close)
#Start Bot
bot = Bot()