Skip to content

Commit

Permalink
TST Moved transaction focused tests to test_txn.py. Added slippage ad…
Browse files Browse the repository at this point in the history
…justement test.
  • Loading branch information
a-campbell committed Oct 16, 2015
1 parent 84277ff commit b66c257
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 48 deletions.
48 changes: 0 additions & 48 deletions pyfolio/tests/test_pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from pyfolio.pos import (get_portfolio_alloc,
extract_pos)
from pyfolio.txn import (get_turnover)


class PositionsTestCase(TestCase):
Expand Down Expand Up @@ -74,50 +73,3 @@ def test_extract_pos(self):
expected.columns.name = 'sid'

assert_frame_equal(result, expected)

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)
81 changes: 81 additions & 0 deletions pyfolio/tests/test_txn.py
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)

0 comments on commit b66c257

Please sign in to comment.