Skip to content

Strategy not selling when it should #348

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

Closed
gaborvecsei opened this issue May 8, 2021 · 3 comments
Closed

Strategy not selling when it should #348

gaborvecsei opened this issue May 8, 2021 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@gaborvecsei
Copy link

gaborvecsei commented May 8, 2021

Expected Behavior

The dummy stratey should sell every long position (only has long positions) at day 12.

Breakdown:

  • BUY at 10 will all the money we have
  • Don't BUY at 11 because we don't have money
  • SELL all the positions at 12
  • BUY at 13 will all the money we have
  • SELL every position at 14
  • Dont't SELL at 15 because there are no positions to sell

(data does not matter as long as it has more days than 15, so the dummy strategy can be tested)

Actual Behavior

There are only 2 trades (BUYs) at step 10 and 11, which are closed at 1 day before the end of the simulation (This is also strange).

image

Steps to Reproduce

Try out this dummy strategy with any data which has more days than 15.

class ErrorStrategy(Strategy):
    INDICES_buy = [10, 11, 13]
    INDICES_sell = [12, 14, 15]

    def __init__(self, broker, data, params):
        self.sell_size: float = None
        self.buy_size: float = None
        super().__init__(broker, data, params)
        self.elapsed_days: int = None

    def init(self):
        super().init()

    def next(self):
        super().next()
        self.elapsed_days = len(self.data)

        if self.elapsed_days in self.INDICES_buy:
            self.buy(size=self.buy_size)
            print(f"I bought at {self.elapsed_days}")
        elif self.elapsed_days in self.INDICES_sell:
            if self.position.size > 0:
                self.sell(size=self.sell_size)
                print(f"I sold at {self.elapsed_days}")
bt = Backtest(df, ErrorStrategy, cash=10000, commission=0, trade_on_close=True)
model_results = bt.run(buy_size=0.999, sell_size=0.999)
bt.plot(open_browser=True)

Additional info

Backtesting==0.3.1

@kernc
Copy link
Owner

kernc commented May 8, 2021

Thanks for the neat repro example.

The reason the strategy doesn't sell in bar 12 is duplicate of issue #328. We check for enough cash (as if opening a brand new trade) even though we could just reduce one of the current trades.

Strange "closed at 1 day before the end of the simulation" is duplicate of bug #343.

Please subscribe there.

@kernc kernc closed this as completed May 8, 2021
@kernc kernc added the duplicate This issue or pull request already exists label May 8, 2021
@kernc
Copy link
Owner

kernc commented May 8, 2021

Instead of:

            if self.position.size > 0:
                self.sell(size=self.sell_size)

you could use:

            if self.position.size > 0:
                self.position.close()

This should always work.

@gaborvecsei
Copy link
Author

Instead of:

            if self.position.size > 0:
                self.sell(size=self.sell_size)

you could use:

            if self.position.size > 0:
                self.position.close()

This should always work.

Thank you for the quick response!

I was not aware of this function, my bad and indeed it works with this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants