Python: Sharpe Ratio of Top-performing ETFs

Jatin
2 min readSep 22, 2020

The Sharpe ratio is widely used to measure the return on investment compared to risk. The ratio is defined as the difference between the returns of the investment and the risk-free return, divided by the volatility. Volatility is measured by the standard deviation of the asset. In general, the higher the Sharpe ratio, the better the risk-adjusted return.

source: https://www.investopedia.com/

In 3 simple steps, I am going to calculate the Sharpe Ratio for top-performing ETFs of the year using Python.

Step1: Load ETFs with highest YTD return from etfdb.com

Step2: Calculate Sharpe Ratio

Step3: Scatter Plot of YTD return vs Sharpe Ratio

Colab Notebook with code

Final Result
Step1: Load ETFs with highest YTD return from etfdb.comurl = 'https://etfdb.com/compare/highest-ytd-returns/no-leveraged/'html = requests.get(url).content#get all tables in a listdf_list = pd.read_html(html)#get first tableetf_list = df_list[0]etf_list['Sharpe Ratio']=''etf_list.head()Step2: Calculate Sharpe Ratiostart = datetime.datetime(2020, 1, 1)end = datetime.datetime(2020, 8, 26)
for sym in etf_list['Symbol']:df = web.DataReader(sym,'yahoo',start,end)df['Daily Return'] = df['Adj Close'].pct_change(1)sharpe_ratio = df['Daily Return'].mean() / df['Daily Return'].std()annual_sharpe_ratio = (252**0.5) * sharpe_ratio#sharpe_ratioetf_list.loc[etf_list['Symbol'] == sym, 'Sharpe Ratio'] = annual_sharpe_ratioStep3: Scatter Plot of YTD return vs Sharpe Ratiofig = px.scatter(etf_list,x='Sharpe Ratio',y='YTD Return',color='Symbol',hover_data=['Name'],title="Sharpe Ratio VS YTD Return")fig.show()

Plotly also had a feature to reveal more information through hover. Here I’ve set the ETF’s Name as the hover attribute of the scatter plot.Plotly Also allows an user to zoom into the plot.

--

--