# Start writing code here...#Standard Imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import yfinance as yf

#getting Bitcoin data
btc = yf.download("BTC-USD","2015-01-01","2021-11-29")

```
[*********************100%***********************] 1 of 1 completed
```

btc.head()

btc["Close"].plot()
plt.xlabel("Date")
plt.ylabel("Close Price")
plt.title("Bitcoin price since 1st Jan 2015")
plt.grid(which="major", color='k', linestyle='-.', linewidth=0.8)
plt.legend()

Looking at the picture we can see we have suffered major drawdowns

#So first we need to calculate the cumilative max for the time series.
btc["Cum_Max"] = btc["Close"].cummax()
#cumulative peaks
btc_cum_peaks = btc["Cum_Max"].unique()
#calculating drawdowns
btc["drawdown"] = (btc["Cum_Max"]-btc["Close"])/btc["Cum_Max"]*100

btc.head()

So the plan is to calculate all unique highs(local highs) then the time, measure when the price crosses it after drawdown, recovered level and the diffrence in days to find recovery time. Recovery time can be calculated as difference in time between first highs and recovered level

#we need to save drawdown data like the highs, recovery time, depth of drawdown etc so I will make another data frame to store that
btc_drawdown_data = pd.DataFrame(np.nan, columns = ["local_highs","local_highs_time","recovered_price","recovered_price_timestamp","recovery_time","max_depth_drawdown"],index=[])

for i in btc_cum_peaks:
local_highs = i
local_highs_time = btc[btc["Close"]==i].index.min()
recovered_price = btc[btc["Close"]>i]["Close"].min()
recovered_price_timestamp = btc[btc["Close"]>i].index.min()
recovery_time = (recovered_price_timestamp - local_highs_time).days
max_depth_drawdown = btc[btc["Cum_Max"]==i].drawdown.max()
#need new row to add data
new_row = {"local_highs":local_highs,"local_highs_time":local_highs_time,"recovered_price":recovered_price,"recovered_price_timestamp":recovered_price_timestamp,
"recovery_time":recovery_time,"max_depth_drawdown":max_depth_drawdown}
btc_drawdown_data= btc_drawdown_data.append(new_row,ignore_index=True)

#So far we calculated the important information like highs, recovery price, time and number of days between the drawdown and recovery

btc

btc_drawdown_data = btc_drawdown_data.rename(columns={"local_highs":"Cum_Max"})
btc = btc.merge(btc_drawdown_data[["Cum_Max","max_depth_drawdown"]],on="Cum_Max")

btc_drawdown_data

print("Number of drawdowns during this period :",btc_drawdown_data.shape[0]-1) # I have to subtract as bitcoin is still dropping right now

```
Number of drawdowns during this period : 149
```

fig,ax = plt.subplots(2,1,figsize=(15,7.5))
ax[0].plot(btc.index,btc["Close"],color="Green")
ax[0].set_xlabel("Date")
ax[0].set_ylabel("Close Price")
ax[0].set_title("Bitcoin Drawdowns")
ax[0].grid()
ax[1].plot(btc.index,btc["drawdown"]*-1,color="Red",label="Drawdown")
ax[1].plot(btc.index,btc["max_depth_drawdown"]*-1,color="blue",linestyle="dotted",label="Maximum Drawdown",alpha=1)
ax[1].set_xlabel("Date")
ax[1].set_ylabel("Drawdown (%)")
ax[1].legend()
ax[1].grid()