Skip to content

Backtest is closing trades at last candle #535

Closed
@replacementAI

Description

@replacementAI

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions