qtpylib icon indicating copy to clipboard operation
qtpylib copied to clipboard

QTPyLib 2.0 Library Structure

Open ranaroussi opened this issue 6 years ago • 11 comments

Thinking out loud... LMK what you think :)

qtpylib 2 0

ranaroussi avatar Sep 01 '19 08:09 ranaroussi

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)

ranaroussi avatar Sep 01 '19 08:09 ranaroussi

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")

ranaroussi avatar Sep 01 '19 08:09 ranaroussi

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()

ranaroussi avatar Sep 01 '19 08:09 ranaroussi

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

ranaroussi avatar Sep 01 '19 08:09 ranaroussi

Hi @ranaroussi ! Will paper trading be available? Cheers and thanks Andrew

andrewczgithub avatar Sep 05 '19 08:09 andrewczgithub

Of course!

ranaroussi avatar Sep 05 '19 09:09 ranaroussi

Cheers @ranaroussi !!! Looking forward to it!!

andrewczgithub avatar Sep 05 '19 20:09 andrewczgithub

Amazing @ranaroussi !! Do you possibly know when the 2.0 version will be released :). Rough estimates. Best, Andrew

andrewczgithub avatar Sep 07 '19 07:09 andrewczgithub

Your plans are great, hope you are working on this :)

ddon avatar Sep 17 '19 13:09 ddon

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.

traderjoe1968 avatar Oct 19 '19 01:10 traderjoe1968

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.

yatharthranjan avatar Sep 11 '20 10:09 yatharthranjan