Skip to content
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

Why Sharpe, Sortino and Calmar Ratio all show 0? #1082

Open
jefferror404 opened this issue Nov 1, 2023 · 1 comment
Open

Why Sharpe, Sortino and Calmar Ratio all show 0? #1082

jefferror404 opened this issue Nov 1, 2023 · 1 comment

Comments

@jefferror404
Copy link

jefferror404 commented Nov 1, 2023

Expected Behavior

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 with my code or data? 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. My data is in 5minute interval.

Not sure if 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 data and datetimeindex automatically and the analysis runs properly with the all the 3 ratios shown. However, in my original dataset, I have to use below to set the timestamp column and index.

A subset of the csv data from binance is here: https://drive.google.com/file/d/1y3cfelTStSOjjPxhM7ZnXwnoV7fYvRbC/view?usp=drive_link

Steps to Reproduce

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)

Additional info

results
data info
data head

@jefferror404
Copy link
Author

I am also uncertain if it is just because a negative sharpe ratio is not allowed. However, I do have a friend who runs another analysis which shows a negative sharpe ratio.

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

1 participant