stbt package¶
Subpackages¶
Submodules¶
stbt.helpers module¶
Module with Strategy class to all backtest related manipulations
-
stbt.helpers.plot_charts(df_ohlc)[source]¶ Function to plot simple Close/Volume graph
- Parameters
df_ohlc (DataFrame) – Close and Volume columns are necessary
- Returns
Matplotlib graph
- Return type
figure (figure)
-
stbt.helpers.resample(df_ohlc, frequency='H')[source]¶ Function to change frequency and fill the gaps
-
stbt.helpers.initialize_universe(ohlc_df)[source]¶ func to create technical data dfs and add them to dict
stbt.simulator module¶
Module with Strategy class to all backtest related manipulations
-
class
stbt.simulator.Strategy(data_df, weights_df, pool_file='strategy_pool.pickle', cash=1.0)[source]¶ Bases:
objectClass to do all backtesting, visualization and statistics calculation
Note
data_df and weights_df should have same columns(name and len)
- Parameters
data_df (DataFrame) – Close prices of instruments should be columns.
weights_df (DataFrame) – Money distribution for every day, same form with data_df
pool_file (str) – File to save pnl of created strategy
cash (float64) – Starting capital and returns multiplier
-
data¶ Close prices of instruments should be columns.
- Type
DataFrame
-
weights¶ Money distribution for every day, same form with data
- Type
DataFrame
-
cash¶ Starting capital and returns multiplier
- Type
float64
-
pnl¶ Accumelated profit and loss of strategy, index is time
- Type
DataFrame
-
stats_figure¶ Statistics visualized
- Type
figure
-
strategy_figure¶ Strategy visualized
- Type
figure
-
tests_figure¶ Tests visualized
- Type
figure
-
verify_data_integrity(frequency=None)[source]¶ Method to check data passed in constructor for mistakes
- Parameters
frequency (str) – Timeframe for data resampling ‘D’, ‘W’, ‘M’, if None - do not resample data
-
backtest(delay=1, instruments_drop=None, commissions_const=0.0, capitalization=False, start_date=None, end_date=None)[source]¶ Method to calculate returns and pnl
- Parameters
delay (int) – Time delay in applying weights to data
instruments_drop (list) – Columns with such names will be droped from data and weights
commissions_const (float64) – Fee paid for every transaction: 0.01 is 1% fee for every trade
capitalization (Boolean) – If money should be reinvested every time
start_date (datetime) – Date to start trading
end_date (datetime) – Date to end trading
- Returns
dict with pnl, returns, commissions dataframes
-
calculate_sim_stats(pnl, returns)[source]¶ Method to calculate vatious statistics of simulation
- Parameters
pnl (DataFrame) – Accumelated profit and loss of strategy, index is time
returns (DataFrame) – Strategy returns for every day, index is time
- Returns
dict sim_stats_dict with great deal of stats
Note
Also creates stats_figure attribute
-
plot_sim_results(pnl)[source]¶ Method to visualize previous backtest
- Parameters
pnl (DataFrame) – Accumelated profit and loss of strategy, index is time
Note
Creates strategy_figure attribute
-
run_tests()[source]¶ Method to check strategy robusness against time and comissions
Note
Creates tests_figure attribute
-
run_all(delay=1, verify_data_integrity=True, instruments_drop=None, commissions_const=0, capitalization=False, start_date=None, end_date=None)[source]¶ Method to get all info about strategy(run all methods)
- Parameters
delay (int) – Time delay in applying weights to data
instruments_drop (list) – Columns with such names will be droped from data and weights
commissions_const (float64) – Fee paid for every transaction: 0.01 is 1% fee for every trade
capitalization (Boolean) – If money should be reinvested every time
start_date (datetime) – Date to start trading
end_date (datetime) – Date to end trading
Module contents¶
Module to make package from folder