| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
QTPyLib (Quantitative Trading Python Library) is a simple, event-driven algorithmic trading library written in Python, that supports backtesting, as well as paper and live trading via Interactive Brokers.
I developed QTPyLib because I wanted for a simple, yet powerful, trading library that will let me focus on the trading logic itself and ignore everything else.
Read about the future of QTPyLib here: https://aroussi.com/post/the-future-of-qtpylib
There are 5 main components to QTPyLib:
To get started, you need to first create a Blotter script:
# blotter.py
from qtpylib.blotter import Blotter
class MainBlotter(Blotter):
pass # we just need the name
if __name__ == "__main__":
blotter = MainBlotter()
blotter.run()Then, with IB TWS/GW running, run the Blotter from the command line:
$ python blotter.pyIf your strategy needs order book / market depth data, add the --orderbook flag to the command:
$ python blotter.py --orderbookWhile the Blotter running in the background, write and execute your algorithm:
# strategy.py
from qtpylib.algo import Algo
class CrossOver(Algo):
def on_start(self):
pass
def on_fill(self, instrument, order):
pass
def on_quote(self, instrument):
pass
def on_orderbook(self, instrument):
pass
def on_tick(self, instrument):
pass
def on_bar(self, instrument):
# get instrument history
bars = instrument.get_bars(window=100)
# or get all instruments history
# bars = self.bars[-20:]
# skip first 20 days to get full windows
if len(bars) < 20:
return
# compute averages using internal rolling_mean
bars['short_ma'] = bars['close'].rolling(window=10).mean()
bars['long_ma'] = bars['close'].rolling(window=20).mean()
# get current position data
positions = instrument.get_positions()
# trading logic - entry signal
if bars['short_ma'].crossed_above(bars['long_ma'])[-1]:
if not instrument.pending_orders and positions["position"] == 0:
# buy one contract
instrument.buy(1)
# record values for later analysis
self.record(ma_cross=1)
# trading logic - exit signal
elif bars['short_ma'].crossed_below(bars['long_ma'])[-1]:
if positions["position"] != 0:
# exit / flatten position
instrument.exit()
# record values for later analysis
self.record(ma_cross=-1)
if __name__ == "__main__":
strategy = CrossOver(
instruments = [ ("ES", "FUT", "GLOBEX", "USD", 201609, 0.0, "") ], # ib tuples
resolution = "1T", # Pandas resolution (use "K" for tick bars)
tick_window = 20, # no. of ticks to keep
bar_window = 5, # no. of bars to keep
preload = "1D", # preload 1 day history when starting
timezone = "US/Central" # convert all ticks/bars to this timezone
)
strategy.run()To run your algo in a live enviroment, from the command line, type:
$ python strategy.py --logpath ~/qtpy/The resulting trades be saved in ~/qtpy/STRATEGY_YYYYMMDD.csv for later analysis.
While the Blotter running in the background, write the dashboard:
# dashboard.py
from qtpylib.reports import Reports
class Dashboard(Reports):
pass # we just need the name
if __name__ == "__main__":
dashboard = Dashboard(port = 5000)
dashboard.run()To run your dashboard, run it from the command line:
$ python dashboard.py
>>> Dashboard password is: a0f36d95a9
>>> Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)Now, point your browser to http://localhost:5000 and use the password generated to access your dashboard.
Note
You can find other examples in the qtpylib/examples directory. Please refer to the Full Documentation to learn how to enable SMS notifications, use the bundled Indicators, and more.
Install using pip:
$ pip install qtpylib --upgrade --no-cache-dirQTPyLib is licensed under the Apache License, Version 2.0. A copy of which is included in LICENSE.txt.
QTPyLib is not a product of Interactive Brokers, nor is it affiliated with Interactive Brokers.
I'm very interested in your experience with QTPyLib. Please drop me a note with any feedback you have.
Ran
| Back | FazBrowse Home | New Git URL |