Package: fastbt
Introductionfastbtis a simple and dirty way to do backtests based on end of day data, especially for day trading.
The main purpose is to provide a simple framework to weed out bad strategies so that you could test and improve your better strategies further.It is based on the assumption that you enter into a position based on some pre-defined rules for a defined period and exit either at the end of the period or when stop loss is triggered. See therationalefor this approach and the built-in assumptions.fastbt is rule-based and not event-based.If your strategy gets you good results, then check them with a full featured backtesting framework such asziplineorbacktraderto verify your results.
If your strategy fails, then it would most probably fail in other environments.This isalphaMost of the modules are stand alone and you could use them as a single file. See embedding for more detailsFeaturesCreate your strategies in Microsoft ExcelBacktest as functions so you can parallelizeTry different simulationsRun from your own datasource or a database connection.Run backtest based on rulesAdd any column you want to your datasource as formulasInstallationfastbt requires python>=3.6and can be installed via pippip install fastbtQuickstartFastbt assumes your data have the following columns (rename them in case of other names)timestampsymbolopenhighlowclosevolumefromfastbt.rapidimport*backtest(data=data)would return a dataframe with all the trades.And if you want to see some metricsmetrics(backtest(data=data))You now ran a backtest without a strategy! By default, the strategy buys the top 5 stocks with the lowest price at open price on each period and sells them at the close price at the end of the period.You can either specify the strategy by way of rules (the recommended way) or create your strategy as a function in python and pass it as a parameterbacktest(data=data,strategy=strategy)If you want to connect to a database, thenfromsqlalchemyimportcreate_engineengine=create_engine('sqlite:///data.db')backtest(connection=engine,tablename='data')And to SELL instead of BUYbacktest(data=data,order='S')Let's implement a simple strategy.BUYthe top 5 stocks with highest last week returnsAssuming we have aweeklyretcolumn,backtest(data=data,order='B',sort_by='weeklyret',sort_mode=False)We used sort_mode=False to sort them in descending order.If you want to test this strategy on a weekly basis, just pass a dataframe with weekly frequency.See the Introduction notebook in the examples directory for an in depth introduction.EmbeddingSince fastbt is a thin wrapper around existing packages, the following files can be used as standalone without installing the fastbt packagedatasourceutilsloadersCopy these files and just use them in your own modules.=========
Historyv0.6.0New methods added toTradeBookobjectmtm - to calculate mtm for open positionsclear - to clear the existing entrieshelper attributes for positionsorder_fill_pricemethod added to utils to simulate order quantityv0.5.1Simple bug fixes addedv0.5.0OptionExpiryclass added to calculate option payoffs based on expiryv0.4.0Brokers module deprecation warning addedOptions module revampedv0.3.0 (2019-03-15)More helper functions added to utilsTradebook class enhancedA Meta class added for event based simulationv0.2.0 (2018-12-26)Backtest from different formats addedRolling function addedv0.1.0. (2018-10-13)First release on PyPI