Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative commission isnt properly calculated? #958

Open
masafumimori opened this issue Apr 6, 2023 · 1 comment
Open

Negative commission isnt properly calculated? #958

masafumimori opened this issue Apr 6, 2023 · 1 comment

Comments

@masafumimori
Copy link

masafumimori commented Apr 6, 2023

Expected Behavior

Increased entry price when selling while it decreases when buying.

Actual Behavior

No matter if it is a positive or negative commission, it is counted as a positive value.
For example, when 0 commission is set, the entry price is 5,429,749 for buying. (calculated with atr values)
When setting 0.04% commission, it becomes 5,431,921, which is 1.0004x than the 0 commission.
When setting -0.04%, the entry price stays the same as 5,431,921. This should be 5,427,577.1004(5,429,749*0.9996)?

For selling, 5,458,457 for both -0.0004 and 0.0004% commission, which is 1.0004x less than commission 0 for 5,460,642.

Steps to Reproduce

  1. Create any strategy
  2. place order with limit price.
  3. run backtest with negative commision like -0.0001
  4. run backtest with positive commision like 0.0001
  5. compare the result and entry prices in _trade

Additional info

I see the code accepting negative commision til -10% but negative commision doent seem to be working.

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, \
            ("commission should be between -10% "
             f"(e.g. market-maker's rebates) and 10% (fees), is {commission}")

Backtesting version: 0.3.3

I'm sorry if i am missing something for negative commission settings.

@mehrnoosh-hk
Copy link

mehrnoosh-hk commented Apr 13, 2023

@masafumimori
The problem is copysign function behavior, copysing(x,y) returns a float with the magnitude (absolute value) of x but the sign of y. The _adjusted_price method, which adjust the price, uses copysign and therefore always use the absolute value of commission

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.
        """
        return (price or self.last_price) * (1 + copysign(self._commission, size))

@kernc can I work on it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants