Skip to content

Commit

Permalink
Qs649 update backtest target allocations (#397)
Browse files Browse the repository at this point in the history
* Changed datetime to date in get_target_allocations method

* Updated pypi package to qstrader 0.2.8
  • Loading branch information
juliettejames committed Jun 12, 2024
1 parent eb507fa commit 8d9664c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.2.8

* Updates BacktestTradingSession.get_target_allocations() to use burn_in_dt.date() instead of burn_in_dt Timestamp. Previous method compared a Timestamp to a datetime.date.
* Adds an integration test to check that target allocations match the expected output, including a date index.

# 0.2.7

* Updates the execution handler to update final orders ensuring an execution order is created in the event of a single submission without a further rebalance.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "qstrader"
version = "0.2.7"
version = "0.2.8"
dependencies = [
"click>=8.1",
"matplotlib>=3.8",
Expand Down
2 changes: 1 addition & 1 deletion qstrader/trading/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def get_target_allocations(self):
alloc_df.index = alloc_df.index.date
alloc_df = alloc_df.reindex(index=equity_curve.index, method='ffill')
if self.burn_in_dt is not None:
alloc_df = alloc_df[self.burn_in_dt:]
alloc_df = alloc_df[self.burn_in_dt.date():]
return alloc_df

def run(self, results=False):
Expand Down
37 changes: 36 additions & 1 deletion tests/integration/trading/test_backtest_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,39 @@ def test_backtest_buy_and_hold(etf_filepath, capsys):

expected_execution_text = "(2015-11-09 14:30:00+00:00) - executed order:"
captured = capsys.readouterr()
assert expected_execution_text in captured.out
assert expected_execution_text in captured.out


def test_backtest_target_allocations(etf_filepath,):
"""
"""
settings.print_events=True
os.environ['QSTRADER_CSV_DATA_DIR'] = etf_filepath

assets = ['EQ:ABC', 'EQ:DEF']
universe = StaticUniverse(assets)
signal_weights = {'EQ:ABC': 0.6, 'EQ:DEF': 0.4}
alpha_model = FixedSignalsAlphaModel(signal_weights)

start_dt = pd.Timestamp('2019-01-01 00:00:00', tz=pytz.UTC)
end_dt = pd.Timestamp('2019-01-31 23:59:00', tz=pytz.UTC)
burn_in_dt = pd.Timestamp('2019-01-07 14:30:00', tz=pytz.UTC)

backtest = BacktestTradingSession(
start_dt,
end_dt,
universe,
alpha_model,
portfolio_id='000001',
rebalance='weekly',
rebalance_weekday='WED',
long_only=True,
cash_buffer_percentage=0.05,
burn_in_dt = burn_in_dt
)
backtest.run(results=False)

target_allocations = backtest.get_target_allocations()
expected_ta = pd.DataFrame(data={'EQ:ABC': 0.6, 'EQ:DEF': 0.4}, index=pd.date_range("20190125", periods=5, freq='B'))
actual_ta = target_allocations.tail()
assert expected_ta.equals(actual_ta)

0 comments on commit 8d9664c

Please sign in to comment.