-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TST Moved transaction focused tests to test_txn.py. Added slippage ad…
…justement test.
- Loading branch information
1 parent
84277ff
commit b66c257
Showing
2 changed files
with
81 additions
and
48 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
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,81 @@ | ||
from unittest import TestCase | ||
|
||
from pandas import ( | ||
Series, | ||
DataFrame, | ||
date_range | ||
) | ||
from pandas.util.testing import (assert_series_equal) | ||
|
||
from pyfolio.txn import (get_turnover, | ||
adjust_returns_for_slippage) | ||
|
||
|
||
class TransactionsTestCase(TestCase): | ||
|
||
def test_get_turnover(self): | ||
""" | ||
Tests turnover using a 20 day period. | ||
With no transactions the turnover should be 0. | ||
with 100% of the porfolio value traded each day | ||
the daily turnover rate should be 0.5. | ||
For monthly turnover it should be the sum | ||
of the daily turnovers because 20 days < 1 month. | ||
e.g (20 days) * (0.5 daily turn) = 10x monthly turnover rate. | ||
""" | ||
dates = date_range(start='2015-01-01', freq='D', periods=20) | ||
|
||
positions = DataFrame([[0.0, 10.0]]*len(dates), | ||
columns=[0, 'cash'], index=dates) | ||
transactions = DataFrame([[0, 0]]*len(dates), | ||
columns=['txn_volume', 'txn_shares'], | ||
index=dates) | ||
|
||
# Test with no transactions | ||
expected = Series([0.0]*len(dates), index=dates) | ||
result = get_turnover(transactions, positions) | ||
assert_series_equal(result, expected) | ||
|
||
# Monthly freq | ||
index = date_range('01-01-2015', freq='M', periods=1) | ||
expected = Series([0.0], index=index) | ||
result = get_turnover(transactions, positions, period='M') | ||
assert_series_equal(result, expected) | ||
|
||
# Test with 0.5 daily turnover | ||
transactions = DataFrame([[10.0, 0]]*len(dates), | ||
columns=['txn_volume', 'txn_shares'], | ||
index=dates) | ||
|
||
expected = Series([0.5]*len(dates), index=dates) | ||
result = get_turnover(transactions, positions) | ||
assert_series_equal(result, expected) | ||
|
||
# Monthly freq: should be the sum of the daily freq | ||
result = get_turnover(transactions, positions, period='M') | ||
expected = Series([10.0], index=index) | ||
assert_series_equal(result, expected) | ||
|
||
def test_adjust_returns_for_slippage(self): | ||
dates = date_range(start='2015-01-01', freq='D', periods=20) | ||
|
||
positions = DataFrame([[0.0, 10.0]]*len(dates), | ||
columns=[0, 'cash'], index=dates) | ||
|
||
# 100% total, 50% average daily turnover | ||
transactions = DataFrame([[10.0, 0]]*len(dates), | ||
columns=['txn_volume', 'txn_shares'], | ||
index=dates) | ||
returns = Series([0.05]*len(dates), index=dates) | ||
# 0.001% slippage per dollar traded | ||
slippage_bps = 10 | ||
expected = Series([0.049]*len(dates), index=dates) | ||
|
||
turnover = get_turnover(transactions, positions, average=False) | ||
result = adjust_returns_for_slippage(returns, turnover, slippage_bps) | ||
|
||
assert_series_equal(result, expected) |