Skip to content

Commit 8d9664c

Browse files
Qs649 update backtest target allocations (#397)
* Changed datetime to date in get_target_allocations method * Updated pypi package to qstrader 0.2.8
1 parent eb507fa commit 8d9664c

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.2.8
2+
3+
* 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.
4+
* Adds an integration test to check that target allocations match the expected output, including a date index.
5+
16
# 0.2.7
27

38
* 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.

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "qstrader"
7-
version = "0.2.7"
7+
version = "0.2.8"
88
dependencies = [
99
"click>=8.1",
1010
"matplotlib>=3.8",

Diff for: qstrader/trading/backtest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def get_target_allocations(self):
362362
alloc_df.index = alloc_df.index.date
363363
alloc_df = alloc_df.reindex(index=equity_curve.index, method='ffill')
364364
if self.burn_in_dt is not None:
365-
alloc_df = alloc_df[self.burn_in_dt:]
365+
alloc_df = alloc_df[self.burn_in_dt.date():]
366366
return alloc_df
367367

368368
def run(self, results=False):

Diff for: tests/integration/trading/test_backtest_e2e.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,39 @@ def test_backtest_buy_and_hold(etf_filepath, capsys):
159159

160160
expected_execution_text = "(2015-11-09 14:30:00+00:00) - executed order:"
161161
captured = capsys.readouterr()
162-
assert expected_execution_text in captured.out
162+
assert expected_execution_text in captured.out
163+
164+
165+
def test_backtest_target_allocations(etf_filepath,):
166+
"""
167+
"""
168+
settings.print_events=True
169+
os.environ['QSTRADER_CSV_DATA_DIR'] = etf_filepath
170+
171+
assets = ['EQ:ABC', 'EQ:DEF']
172+
universe = StaticUniverse(assets)
173+
signal_weights = {'EQ:ABC': 0.6, 'EQ:DEF': 0.4}
174+
alpha_model = FixedSignalsAlphaModel(signal_weights)
175+
176+
start_dt = pd.Timestamp('2019-01-01 00:00:00', tz=pytz.UTC)
177+
end_dt = pd.Timestamp('2019-01-31 23:59:00', tz=pytz.UTC)
178+
burn_in_dt = pd.Timestamp('2019-01-07 14:30:00', tz=pytz.UTC)
179+
180+
backtest = BacktestTradingSession(
181+
start_dt,
182+
end_dt,
183+
universe,
184+
alpha_model,
185+
portfolio_id='000001',
186+
rebalance='weekly',
187+
rebalance_weekday='WED',
188+
long_only=True,
189+
cash_buffer_percentage=0.05,
190+
burn_in_dt = burn_in_dt
191+
)
192+
backtest.run(results=False)
193+
194+
target_allocations = backtest.get_target_allocations()
195+
expected_ta = pd.DataFrame(data={'EQ:ABC': 0.6, 'EQ:DEF': 0.4}, index=pd.date_range("20190125", periods=5, freq='B'))
196+
actual_ta = target_allocations.tail()
197+
assert expected_ta.equals(actual_ta)

0 commit comments

Comments
 (0)