Issue with Backtesting.py - Why Sharpe, Sortino and Calmar Ratio all show 0?

154 views Asked by At

I am using the backtesting.py to carry out a simple backtest strategy about the simple moving average crossing. But Sharpe, Sortino and Calmar Ratio all show 0. What's wrong in my code? Kindly ask for your help. The 3 ratios show proper values when I used the test GOOG dataset from the library, which is in daily interval.

It appears that the issue is related to the timestamp column of my dataset which is a csv. On the other hand, I tried to use a dataset from yfinance python library for AAPL which loads the index automatically and the analysis runs properly with the sharpe ratio shown.

However, in my original dataset, I have to use below to set the timestamp column and index.

a subset of the csv data is here.

btc['timestamp'] = pd.to_datetime(btc['timestamp'], format="%d/%m/%Y %H:%M")
btc.set_index('timestamp', inplace=True)
def SMA(values, n):
   return pd.Series(values).rolling(n).mean()

class SmaCross(Strategy):
  n1 = 5
  n2 = 10

  def init(self):
    self.sma1 = self.I(SMA, self.data.Close, self.n1)
    self.sma2 = self.I(SMA, self.data.Close, self.n2)


  def next(self):
    if crossover(self.sma1, self.sma2):
      self.position.close()
      self.buy(size=1)
    elif crossover(self.sma2, self.sma1):
      self.position.close()
      self.sell(size=1)

bt = Backtest(btc, SmaCross, cash = 1000000)
stats = bt.run()
print(stats)

backtest results

enter image description here

Data head

enter image description here

Data info

enter image description here

0

There are 0 answers