Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update backtest.py #435

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions bt/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def run(*backtests):
object containing the results of the backtests.

Args:
* backtest (*list): List of backtests.
* backtests (*list): List of backtests.

Returns:
Result
Expand Down Expand Up @@ -62,8 +62,7 @@ def benchmark_random(backtest, random_strategy, nsim=100):
if not backtest.has_run:
backtest.run()

bts = []
bts.append(backtest)
bts = [backtest]
data = backtest.data.dropna()

# create and run random backtests
Expand Down Expand Up @@ -135,6 +134,8 @@ class Backtest(object):
* security_weights (DataFrame): Weights of each security as a
percentage of the whole portfolio over time
* additional_data (dict): Additional data passed at construction
* risk_free_rate (float): Risk free rate for performance stats
* annualization_factor (int): Annualization factor for performance stats

"""

Expand All @@ -148,6 +149,8 @@ def __init__(
integer_positions=True,
progress_bar=False,
additional_data=None,
risk_free_rate=0.0,
annualization_factor=252,
):
if data.columns.duplicated().any():
cols = data.columns[data.columns.duplicated().tolist()].tolist()
Expand All @@ -173,6 +176,9 @@ def __init__(
self._sweights = None
self.has_run = False

self.risk_free_rate = risk_free_rate
self.annualization_factor = annualization_factor

def _process_data(self, data, additional_data):
# add virtual row at t0-1day with NaNs
# this is so that any trading action at t0 can be evaluated relative to
Expand Down Expand Up @@ -256,7 +262,10 @@ def run(self):
if self.progress_bar:
bar.stop()

self.stats = self.strategy.prices.calc_perf_stats()
self.stats = self.strategy.prices.calc_perf_stats(
risk_free_rate=self.risk_free_rate,
annualization=self.annualization_factor
)
self._original_prices = self.strategy.prices

@property
Expand Down
Loading