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.54k stars 1.07k forks source link

Backtester is not calculating Sharpe/Sortino/Calmar Ratio #793

Closed samueltg92 closed 1 year ago

samueltg92 commented 2 years ago

Expected Behavior

When I run the backtester I expect to have all the metrics calculated, included the Sharpe R, Sortino R and Calmar R, instead, it is not calculating anyone of those metrics. Is it probably a bug?

Actual Behavior

image

Steps to Reproduce

1. 2. 3.


class MyStrat(Strategy):

    riskpct = 0.01
    rsi_window = 2
    os = 5
    ob = 95
    sl_buy = 0.985
    tp_buy = 1.03
    sl_sell = 1.015
    tp_sell = 0.97

    def init(self):
        super().init()

        self.vwap = self.I(lambda: ta.vwap(self.data.High.s, self.data.Low.s, self.data.Close.s, self.data.Volume.s, anchor='W'))
        self.rsi = self.I(lambda: ta.rsi(self.data.Close.s, length=self.rsi_window))
        self._trade_max_cash = self.equity * self.riskpct

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

        price = self.data.Close[-1]

        # if self.position:
        #     self.position.close()

        if self.rsi <= self.os and price > self.vwap:
            self.position.close()       

            self.buy(sl = self.sl_buy*price, tp = self.tp_buy*price, size = int(self._trade_max_cash//price)) 

        elif self.rsi >= self.ob and price < self.vwap:  
            self.position.close()

            self.sell(sl = self.sl_sell*price, tp = self.tp_sell*price, size = int(self._trade_max_cash//price)) 

bt = Backtest(df1, MyStrat, cash=1000000, margin=1/1, commission = 0.0006, trade_on_close=True, hedging=True, exclusive_orders=True)
stat = bt.run()
print(stat)

Additional info

kernc commented 1 year ago

Duplicate of https://github.com/kernc/backtesting.py/issues/715.