Skip to content

Commit 46c6c98

Browse files
authored
Merge pull request #5 from chicago-joe/master
Updated to IB API version 9.76 and Python 3 syntax
2 parents 7e0dc30 + 6c4cd25 commit 46c6c98

File tree

12 files changed

+1133
-491
lines changed

12 files changed

+1133
-491
lines changed

.gitignore

Lines changed: 214 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,214 @@
1-
2-
*.pyc
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Example user template template
3+
### Example user template
4+
5+
# IntelliJ project files
6+
.idea
7+
*.iml
8+
out
9+
gen
10+
### VisualStudioCode template
11+
.vscode/*
12+
!.vscode/settings.json
13+
!.vscode/tasks.json
14+
!.vscode/launch.json
15+
!.vscode/extensions.json
16+
17+
### Python template
18+
# Byte-compiled / optimized / DLL files
19+
__pycache__/
20+
*.py[cod]
21+
*$py.class
22+
23+
# C extensions
24+
*.so
25+
26+
# Distribution / packaging
27+
.Python
28+
build/
29+
develop-eggs/
30+
dist/
31+
downloads/
32+
eggs/
33+
.eggs/
34+
lib/
35+
lib64/
36+
parts/
37+
sdist/
38+
var/
39+
wheels/
40+
pip-wheel-metadata/
41+
share/python-wheels/
42+
*.egg-info/
43+
.installed.cfg
44+
*.egg
45+
MANIFEST
46+
47+
# PyInstaller
48+
# Usually these files are written by a python script from a template
49+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
50+
*.manifest
51+
*.spec
52+
53+
# Installer logs
54+
pip-log.txt
55+
pip-delete-this-directory.txt
56+
57+
# Unit test / coverage reports
58+
htmlcov/
59+
.tox/
60+
.nox/
61+
.coverage
62+
.coverage.*
63+
.cache
64+
nosetests.xml
65+
coverage.xml
66+
*.cover
67+
.hypothesis/
68+
.pytest_cache/
69+
70+
# Translations
71+
*.mo
72+
*.pot
73+
74+
# Django stuff:
75+
*.log
76+
local_settings.py
77+
db.sqlite3
78+
79+
# Flask stuff:
80+
instance/
81+
.webassets-cache
82+
83+
# Scrapy stuff:
84+
.scrapy
85+
86+
# Sphinx documentation
87+
docs/_build/
88+
89+
# PyBuilder
90+
target/
91+
92+
# Jupyter Notebook
93+
.ipynb_checkpoints
94+
95+
# IPython
96+
profile_default/
97+
ipython_config.py
98+
99+
# pyenv
100+
.python-version
101+
102+
# pipenv
103+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
104+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
105+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
106+
# install all needed dependencies.
107+
#Pipfile.lock
108+
109+
# celery beat schedule file
110+
celerybeat-schedule
111+
112+
# SageMath parsed files
113+
*.sage.py
114+
115+
# Environments
116+
.env
117+
.venv
118+
env/
119+
venv/
120+
ENV/
121+
env.bak/
122+
venv.bak/
123+
124+
# Spyder project settings
125+
.spyderproject
126+
.spyproject
127+
128+
# Rope project settings
129+
.ropeproject
130+
131+
# mkdocs documentation
132+
/site
133+
134+
# mypy
135+
.mypy_cache/
136+
.dmypy.json
137+
dmypy.json
138+
139+
# Pyre type checker
140+
.pyre/
141+
142+
### JetBrains template
143+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
144+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
145+
146+
# User-specific stuff
147+
.idea/**/workspace.xml
148+
.idea/**/tasks.xml
149+
.idea/**/usage.statistics.xml
150+
.idea/**/dictionaries
151+
.idea/**/shelf
152+
153+
# Generated files
154+
.idea/**/contentModel.xml
155+
156+
# Sensitive or high-churn files
157+
.idea/**/dataSources/
158+
.idea/**/dataSources.ids
159+
.idea/**/dataSources.local.xml
160+
.idea/**/sqlDataSources.xml
161+
.idea/**/dynamic.xml
162+
.idea/**/uiDesigner.xml
163+
.idea/**/dbnavigator.xml
164+
165+
# Gradle
166+
.idea/**/gradle.xml
167+
.idea/**/libraries
168+
169+
# Gradle and Maven with auto-import
170+
# When using Gradle or Maven with auto-import, you should exclude module files,
171+
# since they will be recreated, and may cause churn. Uncomment if using
172+
# auto-import.
173+
# .idea/modules.xml
174+
# .idea/*.iml
175+
# .idea/modules
176+
# *.iml
177+
# *.ipr
178+
179+
# CMake
180+
cmake-build-*/
181+
182+
# Mongo Explorer plugin
183+
.idea/**/mongoSettings.xml
184+
185+
# File-based project format
186+
*.iws
187+
188+
# IntelliJ
189+
out/
190+
191+
# mpeltonen/sbt-idea plugin
192+
.idea_modules/
193+
194+
# JIRA plugin
195+
atlassian-ide-plugin.xml
196+
197+
# Cursive Clojure plugin
198+
.idea/replstate.xml
199+
200+
# Crashlytics plugin (for Android Studio and IntelliJ)
201+
com_crashlytics_export_strings.xml
202+
crashlytics.properties
203+
crashlytics-build.properties
204+
fabric.properties
205+
206+
# Editor-based Rest Client
207+
.idea/httpRequests
208+
209+
# Android studio 3.1+ serialized cache file
210+
.idea/caches/build_file_checksums.ser
211+
212+
.gitignore
213+
IB PairsTrading Algo
214+
IB PairsTrading Algo.pub

classes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
__author__ = 'jamesma'
2+
__author__ = 'chicago-joe'

classes/chart.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
"""
2-
Author: James Ma
3-
Email stuff here: jamesmawm@gmail.com
4-
"""
51
import pandas as pd
62
import matplotlib.pyplot as plt
73
import datetime as dt
84
import threading
9-
5+
from pandas.plotting import register_matplotlib_converters
6+
register_matplotlib_converters()
107

118
class Chart:
129

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
1-
"""
2-
Author: James Ma
3-
Email stuff here: jamesmawm@gmail.com
4-
"""
5-
6-
7-
class StockData:
8-
9-
def __init__(self, contract):
10-
self.contract = contract
11-
self.position = 0
12-
self.is_storing_data = False
13-
14-
self.market_price = 0
15-
self.market_value = 0
16-
self.average_cost = 0
17-
self.unrealized_pnl = 0
18-
self.realized_pnl = 0
19-
self.account_name = ""
20-
21-
def add_to_position(self, position):
22-
self.position += position
23-
24-
def update_position(self, position, market_price,
25-
market_value, average_cost, unrealized_pnl,
26-
realized_pnl, account_name):
27-
self.position = position
28-
self.market_price = float(market_price)
29-
self.market_value = float(market_value)
30-
self.average_cost = float(average_cost)
31-
self.unrealized_pnl = float(unrealized_pnl)
32-
self.realized_pnl = float(realized_pnl)
1+
2+
class ContractData:
3+
4+
def __init__(self, contract):
5+
self.contract = contract
6+
self.position = 0
7+
self.is_storing_data = True
8+
self.market_price = 0
9+
self.market_value = 0
10+
self.average_cost = 0
11+
self.unrealized_pnl = 0
12+
self.realized_pnl = 0
13+
self.account_name = ""
14+
15+
def add_to_position(self, position):
16+
self.position += position
17+
18+
def update_position(self, position, market_price,
19+
market_value, average_cost, unrealized_pnl,
20+
realized_pnl, account_name):
21+
self.position = position
22+
self.market_price = float(market_price)
23+
self.market_value = float(market_value)
24+
self.average_cost = float(average_cost)
25+
self.unrealized_pnl = float(unrealized_pnl)
26+
self.realized_pnl = float(realized_pnl)
3327
self.account_name = account_name

classes/ib_util.py

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
1-
"""
2-
Author: James Ma
3-
Email stuff here: jamesmawm@gmail.com
4-
"""
5-
from ib.ext.Contract import Contract
6-
from ib.ext.Order import Order
7-
import params.ib_data_types as DataType
8-
9-
10-
class IBUtil:
11-
12-
def __init__(self):
13-
pass
14-
15-
def create_stock_contract(self, stock):
16-
contract_tuple = (stock, 'STK', 'SMART', 'USD', '', 0.0, '')
17-
stock_contract = self.__make_ib_contract(contract_tuple)
18-
return stock_contract
19-
20-
@staticmethod
21-
def __make_ib_contract(contract_tuple):
22-
new_contract = Contract()
23-
new_contract.m_symbol = contract_tuple[0]
24-
new_contract.m_secType = contract_tuple[1]
25-
new_contract.m_exchange = contract_tuple[2]
26-
new_contract.m_currency = contract_tuple[3]
27-
new_contract.m_expiry = contract_tuple[4]
28-
new_contract.m_strike = contract_tuple[5]
29-
new_contract.m_right = contract_tuple[6]
30-
return new_contract
31-
32-
def create_stock_order(self, quantity, is_buy, is_market_order=True):
33-
order = Order()
34-
order.m_totalQuantity = quantity
35-
order.m_orderType = \
36-
DataType.ORDER_TYPE_MARKET if is_market_order else \
37-
DataType.ORDER_TYPE_LIMIT
38-
order.m_action = \
39-
DataType.ORDER_ACTION_BUY if is_buy else \
40-
DataType.ORDER_ACTION_SELL
41-
return order
1+
from ib.ext.Contract import Contract
2+
from ib.ext.Order import Order
3+
import ib_data_types as DataType
4+
5+
6+
class IBUtil:
7+
8+
def __init__(self):
9+
pass
10+
11+
def create_stock_contract(self, stock):
12+
contract_tuple = (stock, 'STK', 'SMART', 'USD', '', 0.0, '')
13+
stock_contract = self.__make_ib_contract(contract_tuple)
14+
return stock_contract
15+
16+
17+
def create_stock_order(self, quantity, is_buy, is_market_order=True):
18+
order = Order()
19+
order.m_totalQuantity = quantity
20+
order.m_orderType = \
21+
DataType.ORDER_TYPE_MARKET if is_market_order else \
22+
DataType.ORDER_TYPE_LIMIT
23+
order.m_action = \
24+
DataType.ORDER_ACTION_BUY if is_buy else \
25+
DataType.ORDER_ACTION_SELL
26+
return order
27+
28+
@staticmethod
29+
def __make_ib_contract(contract_tuple):
30+
new_contract = Contract()
31+
new_contract.m_symbol = contract_tuple[0]
32+
new_contract.m_secType = contract_tuple[1]
33+
new_contract.m_exchange = contract_tuple[2]
34+
new_contract.m_currency = contract_tuple[3]
35+
new_contract.m_expiry = contract_tuple[4]
36+
new_contract.m_strike = contract_tuple[5]
37+
new_contract.m_right = contract_tuple[6]
38+
return new_contract
39+
40+
@staticmethod
41+
def create_generic_contract(symbol, sec_type, exch, prim_exch, curr):
42+
contract = Contract()
43+
contract.m_symbol = symbol
44+
contract.m_secType = sec_type
45+
contract.m_exchange = exch
46+
contract.m_primaryExch = prim_exch
47+
contract.m_currency = curr
48+
return contract
49+
50+
@staticmethod
51+
def create_generic_order(order_type, quantity, action):
52+
order = Order()
53+
order.m_orderType = order_type
54+
order.m_totalQuantity = quantity
55+
order.m_action = action
56+
return order

0 commit comments

Comments
 (0)