Commit fefa5d9
committed
fix(backtest): guard zero cost_ratio in _get_buy_amount_by_cash_limit
`Exchange._get_buy_amount_by_cash_limit` computes the cash level at which the
proportional fee overtakes `min_cost`:
critical_price = self.min_cost / cost_ratio + self.min_cost
`cost_ratio` is `open_cost + impact_cost`, and `impact_cost` defaults to 0.0, so
any backtest configured with `open_cost=0` raises ZeroDivisionError as soon as a
buy order exceeds available cash — the branch that calls this helper. Setting
`min_cost=0` as well does not help, since 0.0 / 0.0 raises too.
A zero-cost run is a routine baseline for isolating how much of a strategy's
result is being consumed by frictions, and it currently crashes.
When there is no proportional fee the service fee is always `min_cost`, so no
critical price exists and the min_cost branch is the correct one. Returning it
directly keeps every non-zero `cost_ratio` path byte-identical.
Adds unit tests over the default fee schedule, the above/below critical-price
branches, zero cost_ratio with and without min_cost, and cash below min_cost.1 parent 79633dd commit fefa5d9
2 files changed
Lines changed: 65 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
846 | 846 | | |
847 | 847 | | |
848 | 848 | | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
849 | 854 | | |
850 | 855 | | |
851 | 856 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
0 commit comments