Skip to content

weird behavior of take profit when trade_on_close = True #582

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
hpicsk opened this issue Jan 27, 2022 · 1 comment
Closed

weird behavior of take profit when trade_on_close = True #582

hpicsk opened this issue Jan 27, 2022 · 1 comment

Comments

@hpicsk
Copy link

hpicsk commented Jan 27, 2022


import pandas as pd
import numpy as np
from backtesting.test import GOOG

tg = GOOG.head(30).copy()
nums = list(range(1,7)) + list(range(6,2,-1)) + list(range(6,20)) + list(range(14,17)) + list(range(15,12,-1))
tg["Open"] = tg["Close"] = tg["High"] = tg["Low"] = nums

from backtesting import Strategy
from backtesting.lib import crossover

class PosChange(Strategy):
    """ 5 days rise buy, with tp = 1.5, sl = 0.8 """
    def init(self):
        pass
    
    def next(self):      
        if len(self.data) >= 5:
            if self.data["Close"][-1] > self.data["Close"][-2] > self.data["Close"][-3] > self.data["Close"][-4] > self.data["Close"]
                print("buy singal: " + str(self.data["Open"].s.index[-1]))
                self.buy(tp=1.5*self.data["Close"][-1], sl=0.8*self.data["Close"][-1])              

from backtesting import Backtest

bt = Backtest(tg, PosChange, cash=100, commission=.000, trade_on_close=True)
stats = bt.run()

bt.plot()
stats._trades

It buys again although it shouldn't have money until the next day.

Although I understand there is no reason to use trade_on_close, are there any plans to fix this behavior?

Additional info

image

@kernc
Copy link
Owner

kernc commented Feb 4, 2025

Fixed in 4aee89a.

@kernc kernc closed this as completed Feb 4, 2025
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