kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.39k stars 1.05k forks source link

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

Open azimgiant opened 1 year ago

azimgiant commented 1 year ago

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?

BL0987 commented 3 months ago

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?