Skip to content

Commit 81ca8c0

Browse files
committed
Merge pull request #121 from quantopian/turnover-tests
TST: Added tests for get_turnover. Closes #104.
2 parents 5164331 + bad94dd commit 81ca8c0

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

pyfolio/tests/test_pos.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
date_range,
88
Timestamp
99
)
10-
from pandas.util.testing import assert_frame_equal
10+
from pandas.util.testing import (assert_frame_equal,
11+
assert_series_equal)
1112
from numpy import (
1213
absolute,
1314
arange,
1415
zeros_like,
1516
)
1617

17-
from pyfolio.pos import get_portfolio_alloc, extract_pos
18+
from pyfolio.pos import (get_portfolio_alloc,
19+
extract_pos,
20+
get_turnover)
1821

1922

2023
class PositionsTestCase(TestCase):
@@ -71,3 +74,50 @@ def test_extract_pos(self):
7174
expected.columns.name = 'sid'
7275

7376
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

Comments
 (0)