backtesting.py
backtesting.py copied to clipboard
commission in dollar value
Expected Behavior
able to set commission in dollars, disregard percentage. Some instruments I know (futures for example) has a fixed commission per trade, instead of percentage, there should be a switch to set % or $.
Actual Behavior
only percentage is available
Additional info
- Backtesting version: 0.3.3
current hack-ish fix
class _Broker: def __init__(self, *, data, cash, commission, margin, trade_on_close, hedging, exclusive_orders, index): assert 0 < cash, f"cash should be >0, is {cash}" assert -.1 <= commission < .1 or commission >=.5, \
to assert that we want between +-0.1 for percentage, or above 0.5 for fixed amount. I am aware of some brokers offering 0.3 per trade so that may require tweaking.
Accordingly, change the adjuststed price
def _adjusted_price(self, size=None, price=None) -> float: """ Long/short
price, adjusted for commisions. In long positions, the adjusted price is a fraction higher, and vice versa. """ if self._commission >=0.5: return ((price or self.last_price) + copysign(self._commission, size)) else: return ((price or self.last_price) * (1 + copysign(self._commission, size)))
Duplicate of https://github.com/kernc/backtesting.py/issues/113.