backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

commission in dollar value

Open gypsyzz opened this issue 2 years ago • 1 comments

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

gypsyzz avatar Apr 04 '22 03:04 gypsyzz

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/shortprice, 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)))

gypsyzz avatar Apr 05 '22 01:04 gypsyzz

Duplicate of https://github.com/kernc/backtesting.py/issues/113.

kernc avatar Dec 06 '22 01:12 kernc