|
1 | 1 | # Numerical and logical expression wrapper
|
| 2 | + |
| 3 | +Wrapper for logical expressions that can be debugged, printed, enabled and disabled. |
| 4 | + |
| 5 | +Here is a practical example: |
| 6 | +```python |
| 7 | +from Trading.utils.criterion.expression import Numerical, and_ |
| 8 | +import operator |
| 9 | + |
| 10 | +payout_criterion = Numerical( |
| 11 | + "Payout ratio", operator.lt, payout_ratio, max_payout_ratio |
| 12 | +) |
| 13 | +yield_criterion = Numerical( |
| 14 | + "Dividend yield", operator.ge, dividend_yield, min_yield |
| 15 | +) |
| 16 | +debt_criterion = Numerical( |
| 17 | + "Debt-to-equity ratio", operator.le, debt_equity, max_debt_equity |
| 18 | +) |
| 19 | +roe_criterion = Numerical("Return on equity", operator.ge, roe, min_roe) |
| 20 | +free_cashflow_criterion = Numerical( |
| 21 | + "Free cash flow", operator.ge, free_cashflow, min_free_cashflow |
| 22 | +) |
| 23 | +criteria = and_( |
| 24 | + payout_criterion, |
| 25 | + yield_criterion, |
| 26 | + debt_criterion, |
| 27 | + roe_criterion, |
| 28 | + free_cashflow_criterion, |
| 29 | +) |
| 30 | +print(criteria.formatted()) |
| 31 | +``` |
| 32 | + |
| 33 | +*Console output:* |
| 34 | +```txt |
| 35 | +( (Payout ratio 250.0 < 80 False) & |
| 36 | +( (Dividend yield 12.93 >= 2.0 True) & |
| 37 | +( (Debt-to-equity ratio 1.16 <= 0.5 False) & |
| 38 | +( (Return on equity 4.45 >= 10.0 False) & |
| 39 | +(Free cash flow 200260256 >= 10000000.0 True) False) False) False) False) |
| 40 | +``` |
| 41 | +Whenever you have complex criteria in your program, you should really use this framework. |
| 42 | + |
| 43 | +Calling `.debug` on a criterion will list the conditions that make it evaluate to `False`. |
| 44 | + |
| 45 | +By inspecting this output, you can painlessly debug why your criteria doesn't evaluate to `True` when expected. |
| 46 | + |
2 | 47 | ```python
|
3 | 48 | import operator
|
4 | 49 | roe = Threshold("Return on Equity: ", operator.ge, 10.0)
|
|
0 commit comments