QTPyLib 2.0 Library Structure
Thinking out loud... LMK what you think :)

Example strategy
from qtpylib.algo import Algo
class TestStrategy(Algo):
def on_bar(self, bars):
bars.assign('longma', bars["close"].rolling(50).mean())
bars.assign('shortma', bars["close"].rolling(10).mean())
signals = bars['shortma'] > bars['longma']
qty = self.broker.allocate(signals, self.prices)
self.broker.buy(qty, self.prices)
Instantiation
Instantiate strategy and backtest it with 2 stocks (pseudo-code):
from qtpylib.pipline import Ticker
strategy = TestStrategy(
config="algo.toml",
instruments = [
# Ticker(<name>, <broker identifier>, <blotter identifier>), ie:
Ticker("aapl", "AAPL.STK", "AAPL@NASDAQ"),
Ticker("tsla", "TSLA.STK", "TSLA@NASDAQ"),
...
]
strategy.backtest(start="2018-01-01", end=None, data="csv://path/to/csv_directory")
from qtpylib.pipeline import Universe
strategy = TestStrategy(
config="algo.toml",
instruments = Universe.SP500
]
strategy.run()
or, using a custom universe:
1. Create universe class
# custom_universe.py
from qtpylib.pipline import Universe
class FavoriteTechStocks_IB_IQFeed(Universe):
members = [
Ticker("aapl", "AAPL.STK", "NFLX@NASDAQ")
Ticker("goog", "GOOG.STK", "NFLX@NASDAQ")
Ticker("amzn", "AMZN.STK", "NFLX@NASDAQ")
Ticker("nflx", "NFLX.STK", "NFLX@NASDAQ")
]
2. Use it in your algo
from custom_universe import FavoriteTechStocks_IB_IQFeed
strategy = TestStrategy(
config="algo.toml",
instruments = FavoriteTechStocks_IB_IQFeed
]
strategy.run()
Example of algo configuration:
# algo.toml
title = "Algo Configuration"
timezone = "US/Central"
[services]
datastore = "mysql://user:password@localhost:3306/qtpylib"
blotter = "tcp://192.168.1.29:55555:55556"
broker = "ibgw://127.0.0.1:4001/UXXXXXXX"
[sms]
provider = "nexmo"
phones = []
[backtest]
output = "~/backtests/"
data = "yahoo"
cash = 1e6
[backtest.commission]
value = 0.001
type = "percent"
[backtest.slippage]
value = 2
type = "bps"
[events]
tick = 0
bars = "10T"
book = false
[history]
tick = 10
bars = 100
preload = true
futures = "continuous"
[schedule]
date_rules = "daily"
date_offset = 0
time_rules = "market" # premarket, postmarket
time_offset = 0
calendar = "NASDAQ"
half_days = true
Hi @ranaroussi ! Will paper trading be available? Cheers and thanks Andrew
Of course!
Cheers @ranaroussi !!! Looking forward to it!!
Amazing @ranaroussi !! Do you possibly know when the 2.0 version will be released :). Rough estimates. Best, Andrew
Your plans are great, hope you are working on this :)
Why don't you borrow some concepts from BackTrader. There is a lot to like about BT, but your distributed architecture is nice. A blend of some of BT's concepts with QTPyLib 2.0 would make an awesome framework. BT's concept of Commissions, Analysers, Observers could be useful.
Looks good. The abstraction of components will help extensibility of the library. For example, right now very deep coupling with IB which I don't plan on using. Backtesting also seems very useful. Hope these changes get incorporated soon. Thanks @ranaroussi for suggesting.