Skip to main content

package for backtesting

Project description

nav_calc

It is a lib for backtesting.

multi_assets

This function is used to test strategies involving mutliple assets, like multi-factors model.

params:

  • prz: dataframe, index = dates, columns = stocks

  • w_tar: dataframe, target weight, index = dates, columns = stocks

It is worthwhile to note that the dates in w_tar does not need to be the same as the dates in prz, it can be a subset of the dates in prz.

  • dates_reb: array, rebalance dates

If you want to balance at every w_tar's dates, set dates_reb to w_tar.index

  • fee: fee rate, default = 0

return a dataframe

It has the following columns:

  • reb: 1 indicate it is a rebalance date, 0 for not a rebalance date

  • ret_gross: return before fee

  • fee: fees to substract from ret_gross

  • ret_net: return after fee

  • nav: net asset value of the strategy

time_series

This function is used to test timing strategies, like CTA strategies.

params:

  • prz: array

  • pos: array, indicate the strategy's position

It must be the same size with prz

  • fee: fee rate, default 0

nav_analysis

It is a lib for analysis nav (which maybe from nav_calc).

calc_mdd

Function to calculate max drawdown

params

  • arr: an array that represents nav or price

return a dict

The dict has the following keys:

  • mdd_value: the max drawdown in value

  • mdd_pct: the max drawdown in percentage

calc_ann_ret

Function to calculate the annualized return, assuming daily data.

parmas

  • arr: an array that represents nav or price

return a value

  • annualized return

stats

Function to return both ret and mdd

params

  • arr: an array that represents nav or price

return a dict which has the following keys

  • ret: the total return (not annualized return)

  • mdd: the max drawdown in percentage

stats_yearly

params

  • s: a series that represents nav or price

The index of s should be date and must be in the form like "20181010".

return a dataframe which has the following columns:

  • ret: the total return (not annualized return)

  • mdd: the max drawdown in percentage

The index of the dataframe is 'year'.

stats_yearly_multi_assets

params

  • df: a dataframe whose each column represents a nav of an asset or strategy.

return a dataframe which has two-level columns

Level I: the asset or strategy

Level II: ret and mdd, the same meaning as in stats_yearly

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
pybt-0.0.12.tar.gz (4.6 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page