+ 3

Python Finance Analyzing Bitcoin

Hello, i'm struggling with the last Code Repo task for the Python Finance Course: Tasks 1. Calculate the volatility of Bitcoin and output the risk %. 2. Calculate and output the Sharpe ratio. My code is a mess with lot of trial and error: import numpy as np import numpy_financial as npf import matplotlib.pyplot as plt import yfinance as yf #Task1: Calculate the Volatility of Bitcoin and output the Risk % #Task2: Calculate tand output the Sharpe Ratio #Need to calculate the returns data = yf.Ticker('BTC-USD') #Daily return d = yf.download('BTC-USD', start='2020-01-01') #Calculate percentage change pc = d['Close'].pct_change() x = data.history('1y')['Close'] start = 1000 / x[0] #Volatility #To do this use the std.dv of my returns vol = (np.std(d)) print(vol) annual_std = np.std(vol) * np.sqrt(252) print(annual_std) y = np.multiply(start, x) plt.plot(y) plt.savefig('plot.png')

29th Nov 2021, 1:18 AM
Thomas Dent
Thomas Dent - avatar
6 Respostas
+ 7
Please put the code in Code Playground and then pass the link. I know a person who has already passed the course and knows a lot about the subject: Lobo Solitario
12th Dec 2021, 4:51 PM
CGO!
CGO! - avatar
+ 1
import yfinance as yf import numpy as np data = yf.Ticker('BTC-USD') x = data.history('1y')['Close'] ret = x risk = np.std(ret) * np.sqrt(252) sharpe = (np.mean(ret) / np.std(ret))*np.sqrt(252) print("Risk", risk) print("Sharpe", sharpe)
11th Dec 2022, 12:54 PM
Soroush Kachoyan
+ 1
I do not know if you are still in need of help but here is one alternative to solving the Question :) import yfinance as yf import numpy as np import matplotlib.pyplot as plt # Define the ticker symbol tickerSymbol = 'BTC-USD' # Get data on this ticker tickerData = yf.Ticker(tickerSymbol) # Get the historical prices for this ticker for the last 2 years tickerDf = tickerData.history(period='2y') # Use pct_change to calculate daily returns daily_returns = tickerDf['Close'].pct_change() # Use cumprod to calculate cumulative returns cumulative_returns = (1 + daily_returns).cumprod() # Print cumulative returns print(cumulative_returns) # Plot cumulative returns plt.figure(figsize=(14, 7)) plt.plot(cumulative_returns) plt.title(f'Cumulative Returns of {tickerSymbol} over the last 2 years') plt.xlabel('Date') plt.ylabel('Cumulative Returns') plt.show()
18th May 2023, 2:21 PM
Pontus Henriksson
Pontus Henriksson - avatar
+ 1
Pontus Henriksson I know this question is old, but the code you shared works in IDLE as 2023-07-08. However, the exercises ask us to 1. Calculate the volatility of Bitcoin and output the risk %. 2. Calculate and output the Sharpe ratio. From the slides on the course, in order to find the answer we need to get the portfolio_return first with following code data = yf.Ticker('BTC-USD') df = data.history(start='2018-01-01') # I don't know why, but I need this statement to make daily_return statement working daily_return = df['Close'].pct_change() portfolio_return = daily_return.sum(axis=1) And it raise an exception with the last statement raise ValueError(f"No axis named {axis} for object type {cls.__name__}") ValueError: No axis named 1 for object type Series If I omit axis=1, then the standard deviation of portfolio_return will become 0, and make calculating annual volatility impossible. Any idea how to accomplish the exercise as asked?
8th Jul 2023, 7:48 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Great
8th Jan 2022, 7:22 PM
Pranav Hirani
Pranav Hirani - avatar
- 1
Bitcoin Price Time to get some values! Task 1. Get and output Close prices of Tesla stock for the last 5 days. Hint Use history() function on your data object and pass the period parameter with the appropriate value for the specified period. Don't forget to add the ['Close'] filter at the end.
12th Oct 2022, 5:02 AM
Moe Sanda Htun