kernc / kernc/backtesting.py

Stop loss on the same day the position was opened

Open
#119 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
9k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

Expected Behavior

Strategy closes the position at 720

Actual Behavior

Strategy closes the position on next candle at open price, 705.58

Steps to Reproduce

Run the following code. You can see the wrong behaviour on 2012-10-18

from backtesting import Backtest, Strategy
from backtesting.test import GOOG


class DummyStrategy(Strategy):
    def init(self):
        pass

    def next(self):
        if f"{self.data.index[-1]}" == "2012-10-17 00:00:00":
            self.buy(sl=720)


if __name__ == "__main__":
    data = GOOG.tail(100)
    backtest = Backtest(data, DummyStrategy, cash=10000)
    result = backtest.run()
    backtest.plot()
Additional info
  • Backtesting version: 0.2.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the Backtest with the provided DummyStrategy, GOOG data, and Backtest.run() entry point to reproduce the 2012-10-18 result. Trace the Strategy.buy(sl=720) execution and verify that a position opened on 2012-10-17 closes at the stop-loss price on the same day; done means the result no longer waits for the next candle at 705.58.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
fintech-quant
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.