Code Monkey home page Code Monkey logo

pairs-ibkr's Introduction

Pairs-IBKR Python Pair Trading Bot using Interactive Brokers TWS API

For trading tickers in pairs (also possible to trade a single stock, fx, crypto).

  • Trades on Interactive Brokers using TWS-API
  • Integrated with Pairs-API
  • Recommended to use with IB Gateway
  • Compatible with TradingView webhooks

Watch Demo

Watch on YouTube

Use Cases

With Pairs-API you can:

  • integrate with Pairs-API trade platform
  • validate orders (ticker information, order size, dublicate orders, pair synchronization, minimum funds etc.)
  • send real time orders (Relative, Market, Limit) to Interactive Brokers
  • get filled order information
  • get account summary

Considerations

Considering for the next version:

  • improve error handling, add unit tests
  • using historical data from Interactive Brokers for:
    • automated pair selection by using historical data from IB
    • custom signal generator

Requirements

  • ibapi==9.76.1 (TWS API - manual installation for the newest version)
  • nest_asyncio
  • pytz
  • pandas
  • requests

Installation

Download and install IB API for the latest version: Installing & Configuring TWS for the Python API

inherited "ibapi" folder in the repository from TWS-API docs.

clone git repository:

$ git clone https://github.com/ozdemirozcelik/pairs-ibkr.git pairs-ibkr

create and activate virtual environment:

$ pip install virtualenv
(conda install virtualenv)

$ cd pairs-ibkr
cd pairs-ibkr (windows)

$ python -m venv ibkr-env
(conda create --name ibkr-env)

$ source ibkr-env/bin/activate
.\ibkr-env\scripts\activate (windows)
(conda activate ibkr-env)

install requirements:

$ pip install -r requirements.txt
(conda install --file requirements_conda.txt)

try this if conda fails to install requirements:

(conda config --append channels conda-forge)

check this out if you get an invalidversionspec error:

conda/conda#9519 (comment)

Configuration

  • configure config.ini file in line with Pairs-API and IB account details:
# DESCRIPTIONS:
# CHECK_FUNDS_FOR_TRADE=> Check available fund floor before sending an order
# AVAILABLE_FUND_FLOOR=> available funds contingency amount, less than this amount will result 'no trade'
# CONNECTION_PORT=> define port for socket connection
# SYNC_PAIR=> if True: change hedge parameter according to the active position of ticker 2 (sync from flat to pos only)
# NO_DUBS=> if True: do no create order if duplicate ticker1 is used
# PASSPHRASE=>  this is the passphrase for webhooks
# API_PUT_SIGNAL=> API resource to PUT (update) signals
# API_PUT_PRICE=> API resource to PUT (update) order fill prices
# API_GET_SIGNAL=>  API resource to GET signal rowid
# API_GET_SIGNAL_WAITING=>  API resource to GET signals with the status of waiting
# API_GET_SIGNAL_ROUTE=>  API resource to GET signals with the status of rerouted
# ACCOUNT_NUMBER=>  IB account number to get account and position details
# LOGFILE_NAME=>  folder and filename for logs

[environment]
# enable environment to work with
ENV : development
# ENV : stage
# ENV : production

[development]
CHECK_FUNDS_FOR_TRADE : False
AVAILABLE_FUND_FLOOR : 5000
CONNECTION_PORT : 7497
PASSPHRASE : webhook
SYNC_PAIR : True
API_PUT_SIGNAL : http://127.0.0.1:5000/v4/webhook
API_PUT_UPDATE : http://127.0.0.1:5000/v4/signal/updateorder
API_GET_SIGNAL : http://127.0.0.1:5000/v4/signal/
API_GET_PAIR : http://127.0.0.1:5000/v4/pair/
API_GET_TICKER : http://127.0.0.1:5000/v4/ticker/
API_UPDATE_PNL : http://127.0.0.1:5000/v4/ticker/updatepnl
API_PUT_PNL : http://127.0.0.1:5000/v4/regpnl
API_GET_PNL : http://127.0.0.1:5000/v4/pnl/1
API_GET_SIGNAL_WAITING : http://127.0.0.1:5000/v4/signals/status/waiting/0
API_GET_SIGNAL_ROUTE : http://127.0.0.1:5000/v4/signals/status/rerouted/0
ACCOUNT_NUMBER = DU######
LOGFILE_NAME : logs\logdev_

[stage]
...

[production]
...
  • double check your config file name. config.ini has the account number and webhook passphrase so pay attention before pushing to your public repository:
(app.py)
# get configuration variables
config = configparser.ConfigParser()
# change to your config file name
config.read("config_private.ini")
...

Contributing

Pull requests are welcome.

pairs-ibkr's People

Contributors

ozdemirozcelik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

pairs-ibkr's Issues

Rating for stocks

Use Beautiful Soup library to get the ratings from Trader Workstation

Filled Order Details

Try getting filled order details by using json_normalize().
Currently using split function.

followed the video to create order ticker of NFLX with 100 contracts

JSON
{
"rowid": "6",
"timestamp": "2023-06-30 01:51:29",
"ticker": "NFLX",
"ticker1": "NFLX",
"order_action": "buy",
"order_contracts": "100",
"order_price": "100",
"mar_pos": "long",
"mar_pos_size": "100",
"pre_mar_pos": "long",
"pre_mar_pos_size": "100",
"order_status": "created",
"order_comment": "Long(manual)",
"status_msg": "position size mismatch",
"passphrase": "webhook"
}

This is log file

2023-06-29 23:28:06,864 : INFO : main : received signal (Equation:NFLX)
2023-06-29 23:28:06,864 : WARNING : main : order positions mismatch
2023-06-29 23:28:07,018 : INFO : main : order information (position size mismatch) updated for rowid:6

Missing Parent Order

Missing parent order problem.
The problem persists for the second try.

Details and Assumptions

  • log file is attached
[https://app.zenhub.com/files/535878435/69c0d006-2193-4109-a0da-bdfa6a252505/download](https://app.zenhub.com/files/535878435/69c0d006-2193-4109-a0da-bdfa6a252505/download)

append -> concat

Change append to concat:
FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.