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

Backtest is closing trades at last candle #535

Closed
replacementAI opened this issue Nov 17, 2021 · 1 comment
Closed

Backtest is closing trades at last candle #535

replacementAI opened this issue Nov 17, 2021 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@replacementAI
Copy link

replacementAI commented Nov 17, 2021

Expected Behavior

Number of trades shouldn't account for open trade at last candle.
._trades shouldnt return an ExitBar, ExitPrice, PnL, ReturnPct, ExitTime, and a Duration for open trade.

Actual Behavior

Number of trades accounts for open trade at last candle.
._trades returns an ExitBar, ExitPrice, PnL, ReturnPct, ExitTime, and a Duration for an open trade.

Steps to Reproduce

prediction (2).csv
prediction (1).csv

  1. Set prediction (1).csv as Data
  2. Run it through bt.run
  3. Last trade should be open since forecastedClose > Open
  4. Set prediction (2).csv as Data
  5. Run it through bt.run
  6. Last trade is closed as it should be since forecastedClose < Open

ALTERNATIVELY, use any strategy that should have an open trade at the last candle.

from backtesting import Strategy, Backtest
class MyStrategy(Strategy):
    Data = prediction
    
    def init(self):
        super().init()
    
    def next(self):
        if  self.data.forecastedClose>self.data.Open:
            self.buy()
        elif self.data.forecastedClose<self.data.Open:
            self.position.close()

bt = Backtest(prediction, MyStrategy, cash=1000, trade_on_close=False)
stats=bt.run()
stats
stats._trades

Additional info

image
._trades output for prediction 1

image
._trades output for prediction 2

  • Backtesting version: 0.3.2
@kernc
Copy link
Owner

kernc commented Feb 21, 2022

Duplicate of #343.

@kernc kernc closed this as completed Feb 21, 2022
@kernc kernc added the duplicate This issue or pull request already exists label Feb 21, 2022
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