An event-driven trading system
Project description
TradingSystem
An event-driven backtest/realtime quantitative trading system.
Architecture
Data Handler
- Handle live and historical OHLCV data
- Generate a BarEvent which contains ticker symbol and OHLCV data Strategy
- Get BarEvents and make trading decisions based on predefined rules
- Generate a SignalEvent which contains order type (market/limit order) and the direction (long/short) Order Handler
- Get SignalEvents and determine the quantities that should be bought/sold
- Generate an OrderEvent containing order type, direction, and quantities Broker
- Get OrderEvents and route orders to a simulated or real brokerage
- Once orders are filled, it creates a FillEvent which has executed price, commission, and the exchange where the order was filled Portfolio
- Manage the whole portfolio. It tracks PnL, position, and the average price of ticker symbols
- Get BarEvents and FillEvents to update the portfolio
Installation
- create a virtual environment
- pip install tradingsystem
Examples
For backtesting,
import queue
from tradingsystem.data_handler.historical_bar_handler import HistoricalBarHandler, get_data_from_csv, get_data_from_yahoo_finance
from tradingsystem.strategy.sma_crossover import SMACrossover
from tradingsystem.portfolio.portfolio import Portfolio
from tradingsystem.order_handler.max_order_handler import MaxOrderHandler
from tradingsystem.broker.simulated_broker import IBSimulatedBroker
from tradingsystem.statistics.statistics import Statistics
from tradingsystem.common import SessionType
from tradingsystem.simulated_trading_session import TradingSession
events_queue = queue.Queue()
init_asset_val = 10000
tickers_data = {}
amzn = get_data_from_yahoo_finance("AMZN", "2022-01-01", "2022-05-01", "1d")
ticker_list = ['AMZN']
tickers_data['AMZN'] = amzn
historical_bar_handler = HistoricalBarHandler(tickers_data, events_queue)
portfolio = Portfolio(init_asset_val)
order_handler = MaxOrderHandler(portfolio, events_queue, SessionType.BACKTEST)
sma_crossover = SMACrossover(ticker_list, 10, 20, events_queue, SessionType.BACKTEST, portfolio)
simulated_broker = IBSimulatedBroker(events_queue, historical_bar_handler)
stat = Statistics(init_asset_val)
trading_session = TradingSession(historical_bar_handler, sma_crossover, portfolio,
order_handler ,simulated_broker, events_queue, stat)
#start backtest
trading_session.start_trading()
For live trading in IB,
- Open Trader Workstation (TWS) or IB Gateway
- Change timezone to UTC in options
- Run the code below
import queue
from tradingsystem.data_handler.ib_real_time import IBRealTimeBarHandler
from tradingsystem.strategy.sma_crossover import SMACrossover
from tradingsystem.order_handler.max_order_handler import MaxOrderHandler
from tradingsystem.portfolio.portfolio import Portfolio
from tradingsystem.broker.ib_broker import IBBroker
from tradingsystem.statistics.statistics import Statistics
from tradingsystem.ibtws.twsclient import TWSClient
from tradingsystem.common import SessionType
from tradingsystem.trading_schedule.fx_schedule import FXSchedule
from datetime import datetime
from tradingsystem.ib_trading_session import TradingSession
#set up
events_queue = queue.Queue()
init_asset_val = 10000 #in USD
session_type = SessionType.LIVE
twsclient = TWSClient("127.0.0.1", 7497, 0)
trading_schedule = FXSchedule(2022)
symbol_list = ['EUR/USD']
ib_bar_handler = IBRealTimeBarHandler(twsclient, symbol_list, 300,
"MIDPOINT", True, events_queue,
)
portfolio = Portfolio(init_asset_val)
max_order_handler = MaxOrderHandler(portfolio, events_queue, session_type)
sma_crossover = SMACrossover(symbol_list, 10, 20, events_queue, session_type, portfolio)
ib_broker = IBBroker(twsclient, events_queue, symbol_list)
stat = Statistics(init_asset_val)
trading_end = datetime(2022,12,31, 0, 00)
trading_session = TradingSession(twsclient, ib_bar_handler, sma_crossover, portfolio,
max_order_handler, ib_broker, events_queue,
trading_schedule, trading_end, stat)
#start trading
trading_session.start_trading()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
tradingsystem-0.0.1.tar.gz
(105.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
tradingsystem-0.0.1-py3-none-any.whl
(135.8 kB
view details)
File details
Details for the file tradingsystem-0.0.1.tar.gz.
File metadata
- Download URL: tradingsystem-0.0.1.tar.gz
- Upload date:
- Size: 105.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6536bb1532f5c39abefdb3124db4d1883e6800556440b768d512bff95ac67df0
|
|
| MD5 |
1bf9ab59ceeff13ce2a154833aeec2fa
|
|
| BLAKE2b-256 |
8689e6ee9070156164d5faee085785bf9593baab938577eeb2f00a1676918812
|
File details
Details for the file tradingsystem-0.0.1-py3-none-any.whl.
File metadata
- Download URL: tradingsystem-0.0.1-py3-none-any.whl
- Upload date:
- Size: 135.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77f76b4c83520fc31343915d09e4dde02edcb4dcb99996b4b0cb4b9cb04dee90
|
|
| MD5 |
2738bd0d378ce96a5780ec55db620b3c
|
|
| BLAKE2b-256 |
2672acb2480cfbc6dddc0aafa33db47686864ade5e4b29041624616bb5ab0a00
|