-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for EndOfMonthRebalance.
- Loading branch information
1 parent
1ce13c6
commit fe4cd00
Showing
2 changed files
with
48 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/unit/system/rebalance/test_end_of_month_rebalance.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import pandas as pd | ||
import pytest | ||
import pytz | ||
|
||
from qstrader.system.rebalance.end_of_month import EndOfMonthRebalance | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"start_date,end_date,pre_market,expected_dates,expected_time", | ||
[ | ||
( | ||
'2020-03-11', '2020-12-31', False, [ | ||
'2020-03-31', '2020-04-30', '2020-05-29', '2020-06-30', | ||
'2020-07-31', '2020-08-31', '2020-09-30', '2020-10-30', | ||
'2020-11-30', '2020-12-31' | ||
], '21:00:00' | ||
), | ||
( | ||
'2019-12-26', '2020-09-01', True, [ | ||
'2019-12-31', '2020-01-31', '2020-02-28', '2020-03-31', | ||
'2020-04-30', '2020-05-29', '2020-06-30', '2020-07-31', | ||
'2020-08-31' | ||
], '14:30:00' | ||
) | ||
] | ||
) | ||
def test_monthly_rebalance( | ||
start_date, end_date, pre_market, expected_dates, expected_time | ||
): | ||
""" | ||
Checks that the end of month (business day) rebalance provides | ||
the correct datetimes for the provided range. | ||
""" | ||
sd = pd.Timestamp(start_date, tz=pytz.UTC) | ||
ed = pd.Timestamp(end_date, tz=pytz.UTC) | ||
|
||
reb = EndOfMonthRebalance( | ||
start_dt=sd, end_dt=ed, pre_market=pre_market | ||
) | ||
|
||
actual_datetimes = reb._generate_rebalances() | ||
|
||
expected_datetimes = [ | ||
pd.Timestamp('%s %s' % (expected_date, expected_time), tz=pytz.UTC) | ||
for expected_date in expected_dates | ||
] | ||
|
||
assert actual_datetimes == expected_datetimes |