Skip to content

Commit

Permalink
Merge pull request #5 from chicago-joe/master
Browse files Browse the repository at this point in the history
Updated to IB API version 9.76 and Python 3 syntax
  • Loading branch information
jamesmawm authored Jun 13, 2019
2 parents 7e0dc30 + 6c4cd25 commit 46c6c98
Show file tree
Hide file tree
Showing 12 changed files with 1,133 additions and 491 deletions.
216 changes: 214 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,214 @@

*.pyc
# Created by .ignore support plugin (hsz.mobi)
### Example user template template
### Example user template

# IntelliJ project files
.idea
*.iml
out
gen
### VisualStudioCode template
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

.gitignore
IB PairsTrading Algo
IB PairsTrading Algo.pub
1 change: 1 addition & 0 deletions classes/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__author__ = 'jamesma'
__author__ = 'chicago-joe'
7 changes: 2 additions & 5 deletions classes/chart.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
"""
Author: James Ma
Email stuff here: jamesmawm@gmail.com
"""
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
import threading

from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

class Chart:

Expand Down
58 changes: 26 additions & 32 deletions classes/stock_data.py → classes/contract_data.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
"""
Author: James Ma
Email stuff here: jamesmawm@gmail.com
"""


class StockData:

def __init__(self, contract):
self.contract = contract
self.position = 0
self.is_storing_data = False

self.market_price = 0
self.market_value = 0
self.average_cost = 0
self.unrealized_pnl = 0
self.realized_pnl = 0
self.account_name = ""

def add_to_position(self, position):
self.position += position

def update_position(self, position, market_price,
market_value, average_cost, unrealized_pnl,
realized_pnl, account_name):
self.position = position
self.market_price = float(market_price)
self.market_value = float(market_value)
self.average_cost = float(average_cost)
self.unrealized_pnl = float(unrealized_pnl)
self.realized_pnl = float(realized_pnl)

class ContractData:

def __init__(self, contract):
self.contract = contract
self.position = 0
self.is_storing_data = True
self.market_price = 0
self.market_value = 0
self.average_cost = 0
self.unrealized_pnl = 0
self.realized_pnl = 0
self.account_name = ""

def add_to_position(self, position):
self.position += position

def update_position(self, position, market_price,
market_value, average_cost, unrealized_pnl,
realized_pnl, account_name):
self.position = position
self.market_price = float(market_price)
self.market_value = float(market_value)
self.average_cost = float(average_cost)
self.unrealized_pnl = float(unrealized_pnl)
self.realized_pnl = float(realized_pnl)
self.account_name = account_name
97 changes: 56 additions & 41 deletions classes/ib_util.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,56 @@
"""
Author: James Ma
Email stuff here: jamesmawm@gmail.com
"""
from ib.ext.Contract import Contract
from ib.ext.Order import Order
import params.ib_data_types as DataType


class IBUtil:

def __init__(self):
pass

def create_stock_contract(self, stock):
contract_tuple = (stock, 'STK', 'SMART', 'USD', '', 0.0, '')
stock_contract = self.__make_ib_contract(contract_tuple)
return stock_contract

@staticmethod
def __make_ib_contract(contract_tuple):
new_contract = Contract()
new_contract.m_symbol = contract_tuple[0]
new_contract.m_secType = contract_tuple[1]
new_contract.m_exchange = contract_tuple[2]
new_contract.m_currency = contract_tuple[3]
new_contract.m_expiry = contract_tuple[4]
new_contract.m_strike = contract_tuple[5]
new_contract.m_right = contract_tuple[6]
return new_contract

def create_stock_order(self, quantity, is_buy, is_market_order=True):
order = Order()
order.m_totalQuantity = quantity
order.m_orderType = \
DataType.ORDER_TYPE_MARKET if is_market_order else \
DataType.ORDER_TYPE_LIMIT
order.m_action = \
DataType.ORDER_ACTION_BUY if is_buy else \
DataType.ORDER_ACTION_SELL
return order
from ib.ext.Contract import Contract
from ib.ext.Order import Order
import ib_data_types as DataType


class IBUtil:

def __init__(self):
pass

def create_stock_contract(self, stock):
contract_tuple = (stock, 'STK', 'SMART', 'USD', '', 0.0, '')
stock_contract = self.__make_ib_contract(contract_tuple)
return stock_contract


def create_stock_order(self, quantity, is_buy, is_market_order=True):
order = Order()
order.m_totalQuantity = quantity
order.m_orderType = \
DataType.ORDER_TYPE_MARKET if is_market_order else \
DataType.ORDER_TYPE_LIMIT
order.m_action = \
DataType.ORDER_ACTION_BUY if is_buy else \
DataType.ORDER_ACTION_SELL
return order

@staticmethod
def __make_ib_contract(contract_tuple):
new_contract = Contract()
new_contract.m_symbol = contract_tuple[0]
new_contract.m_secType = contract_tuple[1]
new_contract.m_exchange = contract_tuple[2]
new_contract.m_currency = contract_tuple[3]
new_contract.m_expiry = contract_tuple[4]
new_contract.m_strike = contract_tuple[5]
new_contract.m_right = contract_tuple[6]
return new_contract

@staticmethod
def create_generic_contract(symbol, sec_type, exch, prim_exch, curr):
contract = 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

@staticmethod
def create_generic_order(order_type, quantity, action):
order = Order()
order.m_orderType = order_type
order.m_totalQuantity = quantity
order.m_action = action
return order
Loading

0 comments on commit 46c6c98

Please sign in to comment.