|
| 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) |
0 commit comments