stbt package

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.get_sharpe(df_ohlc)[source]

Function to calculate sharpe ratio

stbt.helpers.get_max_drawdown(returns)[source]

Assumes returns is a pandas Series

stbt.helpers.get_label_from_dict(settings_dict)[source]

Function to get name from dict

stbt.helpers.initialize_universe(ohlc_df)[source]

func to create technical data dfs and add them to dict

stbt.helpers.prolong(series)[source]

func to prolong weight weights_df = events.rolling(prolong_timeframe).apply(prolong)

stbt.helpers.unpack_ohlc(ohlc_df)[source]

Just helper to create dict with all tech vars

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: object

Class 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

pool_file

File to save pnl of created strategy

Type

str

pnl

Accumelated profit and loss of strategy, index is time

Type

DataFrame

stats_dict

All the math statistics of strategy backtest

Type

dict

data_mistakes_dict

All the data inconsistencies

Type

dict

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

get_pnls_pool()[source]

Method to read all pnls from self.pool_file

add_to_pnls_pool(pnl_df=None, name='3014_pnl')[source]

Method to add pnls to self.pool_file

get_pool_heatmap()[source]

Method to visualize self.pool_file

get_max_corr(pnl)[source]

Method to get highest correlation with pnl from self.pool_file

stbt.simulator.get_correlation(list_of_pnls, plot=True)[source]

Function to get correlation heatmap

Parameters

list_of_pnls (list) – List with dataframes of pnls

Returns

Matrix of correalations figure (figure):

Seaborn heatmap

Return type

corr (DataFrame)

Module contents

Module to make package from folder