Skip to content

Commit 0fb91af

Browse files
committed
Merge pull request #247 from quantopian/test_tears
Test tearsheets
2 parents df495ca + 61d8f9e commit 0fb91af

File tree

6 files changed

+145
-3
lines changed

6 files changed

+145
-3
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ before_install:
2020
- conda update -q conda
2121
# Useful for debugging any issues with conda
2222
- conda info -a
23+
- cp pyfolio/tests/matplotlibrc .
2324

2425
install:
25-
- conda create -q -n testenv --yes python=$TRAVIS_PYTHON_VERSION ipython pyzmq numpy scipy nose matplotlib pandas Cython patsy statsmodels flake8 scikit-learn seaborn runipy pytables networkx pandas-datareader
26+
- conda create -q -n testenv --yes python=$TRAVIS_PYTHON_VERSION ipython pyzmq numpy scipy nose matplotlib=1.4.1 pandas Cython patsy statsmodels flake8 scikit-learn seaborn runipy pytables networkx pandas-datareader
2627
- source activate testenv
2728
- pip install nose_parameterized contextlib2 logbook==0.10.1
2829
- pip install --no-deps git+https://github.com/quantopian/zipline

pyfolio/tears.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ def create_txn_tear_sheet(returns, positions, transactions,
453453
transactions : pd.DataFrame
454454
Prices and amounts of executed trades. One row per trade.
455455
- See full explanation in create_full_tear_sheet.
456+
unadjusted_returns : pd.Series, optional
457+
Daily unadjusted returns of the strategy, noncumulative.
458+
Will plot additional swippage sweep analysis.
459+
- See pyfolio.plotting.plot_swippage_sleep and
460+
pyfolio.plotting.plot_slippage_sensitivity
456461
return_fig : boolean, optional
457462
If True, returns the figure that was plotted on.
458463
"""

pyfolio/tests/matplotlibrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
backend : Agg

pyfolio/tests/test_tears.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
from matplotlib.testing.decorators import cleanup
2+
3+
from unittest import TestCase
4+
from nose_parameterized import parameterized
5+
6+
import os
7+
import gzip
8+
9+
from pandas import read_csv
10+
11+
from pyfolio.tears import (create_full_tear_sheet,
12+
create_returns_tear_sheet,
13+
create_position_tear_sheet,
14+
create_txn_tear_sheet,
15+
create_round_trip_tear_sheet,
16+
create_interesting_times_tear_sheet,
17+
create_bayesian_tear_sheet)
18+
19+
20+
def to_utc(df):
21+
try:
22+
df.index = df.index.tz_localize('UTC')
23+
except TypeError:
24+
df.index = df.index.tz_convert('UTC')
25+
26+
return df
27+
28+
29+
def to_series(df):
30+
return df[df.columns[0]]
31+
32+
33+
class PositionsTestCase(TestCase):
34+
__location__ = os.path.realpath(
35+
os.path.join(os.getcwd(), os.path.dirname(__file__)))
36+
37+
test_returns = read_csv(
38+
gzip.open(
39+
__location__ + '/test_data/test_returns.csv.gz'),
40+
index_col=0, parse_dates=0)
41+
test_returns = to_series(to_utc(test_returns))
42+
test_gross_lev = read_csv(
43+
gzip.open(
44+
__location__ + '/test_data/test_gross_lev.csv.gz'),
45+
index_col=0, parse_dates=0)
46+
test_gross_lev = to_series(to_utc(test_gross_lev))
47+
test_txn = to_utc(read_csv(
48+
gzip.open(
49+
__location__ + '/test_data/test_txn.csv.gz'),
50+
index_col=0, parse_dates=0))
51+
test_pos = to_utc(read_csv(
52+
gzip.open(__location__ + '/test_data/test_pos.csv.gz'),
53+
index_col=0, parse_dates=0))
54+
55+
@parameterized.expand([({},),
56+
({'slippage': 1},),
57+
({'live_start_date':
58+
test_returns.index[-20]},),
59+
({'round_trips': True},),
60+
({'hide_positions': True},),
61+
({'cone_std': 1},),
62+
])
63+
@cleanup
64+
def test_create_full_tear_sheet_breakdown(self, kwargs):
65+
create_full_tear_sheet(self.test_returns,
66+
positions=self.test_pos,
67+
transactions=self.test_txn,
68+
gross_lev=self.test_gross_lev,
69+
**kwargs
70+
)
71+
72+
@parameterized.expand([({},),
73+
({'live_start_date':
74+
test_returns.index[-20]},),
75+
({'cone_std': 1},),
76+
])
77+
@cleanup
78+
def test_create_returns_tear_sheet_breakdown(self, kwargs):
79+
create_returns_tear_sheet(self.test_returns,
80+
**kwargs
81+
)
82+
83+
@parameterized.expand([({},),
84+
({'hide_positions': True},),
85+
({'show_and_plot_top_pos': 0},),
86+
({'show_and_plot_top_pos': 1},),
87+
])
88+
@cleanup
89+
def test_create_position_tear_sheet_breakdown(self, kwargs):
90+
create_position_tear_sheet(self.test_returns,
91+
self.test_pos,
92+
gross_lev=self.test_gross_lev,
93+
**kwargs
94+
)
95+
96+
@parameterized.expand([({},),
97+
({'unadjusted_returns': test_returns},),
98+
])
99+
@cleanup
100+
def test_create_txn_tear_sheet_breakdown(self, kwargs):
101+
create_txn_tear_sheet(self.test_returns,
102+
self.test_pos,
103+
self.test_txn,
104+
**kwargs
105+
)
106+
107+
@parameterized.expand([({},),
108+
({'sector_mappings': {}},),
109+
])
110+
@cleanup
111+
def test_create_round_trip_tear_sheet_breakdown(self, kwargs):
112+
create_round_trip_tear_sheet(self.test_pos,
113+
self.test_txn,
114+
**kwargs
115+
)
116+
117+
@parameterized.expand([({},),
118+
({'legend_loc': 1},),
119+
])
120+
@cleanup
121+
def test_create_interesting_times_tear_sheet_breakdown(self,
122+
kwargs):
123+
create_interesting_times_tear_sheet(self.test_returns,
124+
**kwargs
125+
)
126+
127+
@parameterized.expand([({},),
128+
({'stoch_vol': True},),
129+
])
130+
@cleanup
131+
def test_create_bayesian_tear_sheet_breakdown(self, kwargs):
132+
create_bayesian_tear_sheet(
133+
self.test_returns,
134+
live_start_date=self.test_returns.index[-20],
135+
**kwargs)

pyfolio/tests/test_timeseries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def test_bootstrap_cone_against_linear_cone_normal_returns(self):
369369
random_seed = 100
370370
np.random.seed(random_seed)
371371
days_forward = 200
372-
cone_stdevs = [1, 1.5, 2]
372+
cone_stdevs = (1., 1.5, 2.)
373373
mu = .005
374374
sigma = .002
375375
rets = pd.Series(np.random.normal(mu, sigma, 10000))

pyfolio/timeseries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ def rolling_sharpe(returns, rolling_sharpe_window):
964964
* np.sqrt(APPROX_BDAYS_PER_YEAR)
965965

966966

967-
def forecast_cone_bootstrap(is_returns, num_days, cone_std=[1, 1.5, 2],
967+
def forecast_cone_bootstrap(is_returns, num_days, cone_std=(1., 1.5, 2.),
968968
starting_value=1, num_samples=1000,
969969
random_seed=None):
970970
"""

0 commit comments

Comments
 (0)