Skip to content

Commit c00a628

Browse files
eigenfootwiecki
authored andcommitted
BLD build input checker
1 parent a3cecc3 commit c00a628

File tree

2 files changed

+120
-2
lines changed

2 files changed

+120
-2
lines changed

pyfolio/check_inputs.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#
2+
# Copyright 2017 Quantopian, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
from __future__ import division
16+
17+
import engarde.checks as ed
18+
19+
20+
class TearSheetInputError(Exception):
21+
"""Custom Exception for pyfolio input errors"""
22+
pass
23+
24+
25+
def check_inputs(returns,
26+
positions,
27+
txns):
28+
"""
29+
Check inputs to create_foo_tear_sheet functions
30+
31+
- Raises a TearSheetInputError if inputs violate specification
32+
"""
33+
check_returns(returns)
34+
check_positions(positions)
35+
check_txns(txns)
36+
37+
38+
def check_returns(returns):
39+
"""
40+
Check returns for specification
41+
42+
- There must not be NaNs
43+
- Index must be timezone-localized datetimes
44+
- Returns must be floats
45+
"""
46+
try:
47+
ed.none_missing(returns)
48+
except AssertionError as e:
49+
raise TearSheetInputError('''returns has NaNs. engarde raises the
50+
following: ''' + e)
51+
52+
if returns.index.tz is None:
53+
raise TearSheetInputError('returns index is not timezone-localized')
54+
55+
if returns.dtype != float:
56+
raise TearSheetInputError('returns data types are not all float')
57+
58+
def check_positions(positions):
59+
"""
60+
Check positions for specification
61+
62+
- There must not be any NaNs
63+
- Index must be timezone-localized datetimes
64+
- There must a column called 'cash'
65+
- Positions must be floats
66+
"""
67+
try:
68+
ed.none_missing(positions)
69+
except AssertionError as e:
70+
raise TearSheetInputError('''positions has NaNs. engarde raises the
71+
following: ''' + e)
72+
73+
if positions.index.tz is None:
74+
raise TearSheetInputError('positions index is not timezone-localized')
75+
76+
if 'cash' not in positions.columns:
77+
raise TearSheetInputError('positions does not have a cash column')
78+
79+
if not all(positions.apply(lambda col: col.dtype, axis='columns') == float):
80+
raise TearSheetInputError('''positions data types are not all float''')
81+
82+
83+
def check_txns(txns):
84+
"""
85+
Check transactions for specification
86+
87+
- There must not be any NaNs
88+
- Index must be timezone-localized datetimes
89+
- There must be columns called 'amount', 'price', and 'symbol'
90+
- Amounts must be ints
91+
- Prices must be floats
92+
- Symbols must be strings
93+
"""
94+
try:
95+
ed.none_missing(txns)
96+
except AssertionError as e:
97+
raise TearSheetInputError('''transactions has NaNs. engarde raises the
98+
following: ''' + e)
99+
100+
if txns.index.tz is None:
101+
raise TearSheetInputError('''transactions index is not
102+
timezone-localized''')
103+
104+
if all([col in txns.columns for col in ['amount',
105+
'price',
106+
'symbol']]) == False:
107+
raise TearSheetInputError('''transactions does not have correct columns.
108+
Columns must be "amount", "price" and
109+
"symbol"''')
110+
111+
try:
112+
ed.has_dtypes(txns, {'amount': int;
113+
'price': float;
114+
'symbol': str})
115+
except AssertionError as e:
116+
raise TearSheetInputError('''transactions columns do not have correct
117+
data types. engarde raises the following: '''
118+
+ e)

pyfolio/tears.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def create_full_tear_sheet(returns,
9898
positions : pd.DataFrame, optional
9999
Daily net position values.
100100
- Time series of dollar amount invested in each position and cash.
101-
- Days where stocks are not held can be represented by 0 or NaN.
101+
- Days where stocks are not held must be represented by 0.
102102
- Non-working capital is labelled 'cash'
103103
- Example:
104104
index 'AAPL' 'MSFT' cash
@@ -109,7 +109,7 @@ def create_full_tear_sheet(returns,
109109
Executed trade volumes and fill prices.
110110
- One row per trade.
111111
- Trades on different names that occur at the
112-
same time will have identical indicies.
112+
same time will have identical indices.
113113
- Example:
114114
index amount price symbol
115115
2004-01-09 12:18:01 483 324.12 'AAPL'

0 commit comments

Comments
 (0)