backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

How to add a dollar amount for take profit and stop loss

Open azimgiant opened this issue 1 year ago • 1 comments

I am having trouble adding a dollar amount to take profit and stop loss. I've seen examples of having percentages, but can I add a dollar amount to these two variables? For example, if my take profit was $1,000 and stop loss was $350 how can I add that to my code? Would it go in self.buy() or self.close_position(). Also, how can I add it to a short position? Would the values be negative?

azimgiant avatar Aug 31 '23 02:08 azimgiant

its easier to set a percentage based stop/target:

def next(self): super().next()

    price = self.data.Close[-1]
    
    if self.signal == 1: 
        if not self.position:
            self.buy(size=0.99, tp=1.06*price, sl=0.98*price)

maybe divide your capital by the dollar profit target (x100) to get a percentage, then add in as above?

BL0987 avatar Jun 08 '24 21:06 BL0987