|
7 | 7 | date_range,
|
8 | 8 | Timestamp
|
9 | 9 | )
|
10 |
| -from pandas.util.testing import assert_frame_equal |
| 10 | +from pandas.util.testing import (assert_frame_equal, |
| 11 | + assert_series_equal) |
11 | 12 | from numpy import (
|
12 | 13 | absolute,
|
13 | 14 | arange,
|
14 | 15 | zeros_like,
|
15 | 16 | )
|
16 | 17 |
|
17 |
| -from pyfolio.pos import get_portfolio_alloc, extract_pos |
| 18 | +from pyfolio.pos import (get_portfolio_alloc, |
| 19 | + extract_pos, |
| 20 | + get_turnover) |
18 | 21 |
|
19 | 22 |
|
20 | 23 | class PositionsTestCase(TestCase):
|
@@ -71,3 +74,50 @@ def test_extract_pos(self):
|
71 | 74 | expected.columns.name = 'sid'
|
72 | 75 |
|
73 | 76 | assert_frame_equal(result, expected)
|
| 77 | + |
| 78 | + def test_get_turnover(self): |
| 79 | + """ |
| 80 | + Tests turnover using a 20 day period. |
| 81 | +
|
| 82 | + With no transactions the turnover should be 0. |
| 83 | +
|
| 84 | + with 100% of the porfolio value traded each day |
| 85 | + the daily turnover rate should be 0.5. |
| 86 | +
|
| 87 | + For monthly turnover it should be the sum |
| 88 | + of the daily turnovers because 20 days < 1 month. |
| 89 | +
|
| 90 | + e.g (20 days) * (0.5 daily turn) = 10x monthly turnover rate. |
| 91 | + """ |
| 92 | + dates = date_range(start='2015-01-01', freq='D', periods=20) |
| 93 | + |
| 94 | + positions = DataFrame([[0.0, 10.0]]*len(dates), |
| 95 | + columns=[0, 'cash'], index=dates) |
| 96 | + transactions = DataFrame([[0, 0]]*len(dates), |
| 97 | + columns=['txn_volume', 'txn_shares'], |
| 98 | + index=dates) |
| 99 | + |
| 100 | + # Test with no transactions |
| 101 | + expected = Series([0.0]*len(dates), index=dates) |
| 102 | + result = get_turnover(transactions, positions) |
| 103 | + assert_series_equal(result, expected) |
| 104 | + |
| 105 | + # Monthly freq |
| 106 | + index = date_range('01-01-2015', freq='M', periods=1) |
| 107 | + expected = Series([0.0], index=index) |
| 108 | + result = get_turnover(transactions, positions, period='M') |
| 109 | + assert_series_equal(result, expected) |
| 110 | + |
| 111 | + # Test with 0.5 daily turnover |
| 112 | + transactions = DataFrame([[10.0, 0]]*len(dates), |
| 113 | + columns=['txn_volume', 'txn_shares'], |
| 114 | + index=dates) |
| 115 | + |
| 116 | + expected = Series([0.5]*len(dates), index=dates) |
| 117 | + result = get_turnover(transactions, positions) |
| 118 | + assert_series_equal(result, expected) |
| 119 | + |
| 120 | + # Monthly freq: should be the sum of the daily freq |
| 121 | + result = get_turnover(transactions, positions, period='M') |
| 122 | + expected = Series([10.0], index=index) |
| 123 | + assert_series_equal(result, expected) |
0 commit comments