I am backtesting using vectorBT, a python backtest library, to get the backtest result with Sharpe Ratio.
Here is the sample code
import vectorbt as vbt
import yfinance as yf
symbol = 'AAPL'
ohlcv = yf.download(symbol, start='2022-01-01', end='2023-01-01')
entries = ohlcv['Close'] > ohlcv['Close'].rolling(50).mean()
exits = ohlcv['Close'] < ohlcv['Close'].rolling(50).mean()
pf = vbt.Portfolio.from_signals(
close=ohlcv['Close'],
entries=entries,
exits=exits,
init_cash=10000
)
stats = pf.stats()
print(stats)
Here is the result
- Start 2022-01-03 00:00:00
- End 2022-12-30 00:00:00 Period 251
- Start Value 10000.0
- End Value 7982.422082
- Total Return [%] -20.175779
- Benchmark Return [%] -28.613814
- Max Gross Exposure [%] 100.0
- Total Fees Paid 0.0
- Max Drawdown [%] 25.578501
- Max Drawdown Duration 94.0
- Total Trades 9
- Total Closed Trades 9
- Total Open Trades 0
- Open Trade PnL 0.0
- Win Rate [%] 11.111111
- Best Trade [%] 6.46395
- Worst Trade [%] -5.867954
- Avg Winning Trade [%] 6.46395
- Avg Losing Trade [%] -3.524771
- Avg Winning Trade Duration 41.0
- Avg Losing Trade Duration 4.25
- Profit Factor 0.223676
- Expectancy -224.175324
- Name: Close, dtype: object
How do I have the Sharpe ratio included in the result? Thanks
Found the answer. freq="1d" is a necessary parameter to get the shape ratio etc.,.