<jupyter_start><jupyter_text>Bitcoin and stocks data analysis  Task 1: Bitcoin vs USD analysis   Importing relevant libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
data = pd.read_csv('btc_data.csv')
data.head()
data.tail()<jupyter_output><empty_output><jupyter_text>We have got the data from 31 may 2019 to 31 may 2021 For this analysis i dont want to see how much v0lume is being traded and also the 'close' column and the 'adj close' columns look the same. Dropping these columns.<jupyter_code>data.drop(['Adj Close','Volume'],axis = 1,inplace = True)
data.head()<jupyter_output><empty_output><jupyter_text>Looks good. Now lets check out the dimensions of our data<jupyter_code>data.shape<jupyter_output><empty_output><jupyter_text>Looks like our dataset contains 732 rows and 5 columns Let us now look for some statistics of our data<jupyter_code>data.describe()<jupyter_output><empty_output><jupyter_text>Checking for null values<jupyter_code>data.isnull().sum()<jupyter_output><empty_output><jupyter_text>Looks like our data has some missing values. The reason behind it could be that at that day the market was not open. Anyways it won't affect our analysis but still dropping null values.<jupyter_code>data.dropna(inplace = True)
#Checking to see if the null values are dropped
data.isna().sum()<jupyter_output><empty_output><jupyter_text>There you go. Now we can continue with our analysisAt first i am intersted in the closing trend of bitcoin  Exploratory data analysis<jupyter_code>#Plotting the closing trend of bitcoin
plt.figure(figsize = (14,8))
plt.plot(data['Close'])
plt.title('Closing trend of bitcoin for 2 years', fontsize = 15)
plt.xlabel('Days')
plt.ylabel('Price is USD')
plt.show()<jupyter_output><empty_output><jupyter_text>As we can see that there are a lot of spikes in our graph and bitcoin reached its peak at somewhere around day 670 and then started falling after that Now let's have a look at the open price vs the close price<jupyter_code>df_oc = data[['Open','Close']]
df_oc<jupyter_output><empty_output><jupyter_text>Now that we have got our separate dataframe for opening and closing trends let us plot them<jupyter_code>plt.figure(figsize = (14,8))
for column in df_oc.columns.values:
    plt.plot(df_oc[column], label = column)

plt.title("Open vs Close")
plt.xlabel("Days")
plt.legend(df_oc.columns.values)
plt.show()<jupyter_output><empty_output><jupyter_text>As we can see from above graph that there's not much of a difference in the open and close prices of bitcoin for that same day Let us now get the daily returns of bitcoin<jupyter_code>daily_return = data['Close'].pct_change(1)
daily_return<jupyter_output><empty_output><jupyter_text>This series object does not give us any information. So let's visualise the daily returns<jupyter_code>plt.figure(figsize = (12,8))
plt.plot(daily_return)
plt.title('Daily returns',fontsize = 15)
plt.ylabel('Percentage change')
plt.xlabel('Days')
plt.show()<jupyter_output><empty_output><jupyter_text>As we can see there are quite a few spikes in our graph but the biggest spike was around day 290 and also its a negative spike Getting the daily cumulative returns in order to get more insights from the data<jupyter_code>daily_cum_return = (daily_return+1).cumprod()
daily_cum_return<jupyter_output><empty_output><jupyter_text>Visualising the cumulative returns<jupyter_code>plt.figure(figsize = (14,8))
plt.plot(daily_cum_return)
plt.title("Daily returns")
plt.xlabel('Days')
plt.ylabel('Growth')
plt.show()<jupyter_output><empty_output><jupyter_text>**Inferences**:- As we can see from the above graph if we would have invested a 100 dollars two years ago in bitcoin we would have got back around 400 dollars today.Bitcoin recently saw its peak but has fallen significantly in the last couple of weeks. **Future scope** - In future we could analyse bitcoin against other cryptocurrencies like etherium and dogecoin and figure out other factors which drive the prices of these cryptos.  Stocks analysis  In this project i am interested in looking at the stocks of the five of the most famous companies i.e. Facebook, Netflix, Tesla, Microsoft and Amazon<jupyter_code>#importing IexFinance library
from iexfinance.stocks import get_historical_data
import datetime
token = '<KEY>'
start = datetime.date(2019,5,31)
end = datetime.date(2021,6,1)<jupyter_output><empty_output><jupyter_text>Let us clean our dataframe in order to get what we want<jupyter_code>df_fb = FB[['close','high','low','open']]
df_fb.head()<jupyter_output><empty_output><jupyter_text>Now lets do this for all the stocks we are interested in<jupyter_code>NFLX = get_historical_data('NFLX', output_format = 'pandas', token = token, start = start, end = end)
MSFT = get_historical_data('MSFT', output_format = 'pandas', token = token, start = start, end = end)<jupyter_output><empty_output><jupyter_text>At this point i had run out of all the credits so let's just look at the stocks from facebook, netflix and microsoft<jupyter_code>MSFT.head()
df_msft = MSFT[['close', 'high', 'low', 'open']]
df_msft.head()
df_nflx = NFLX[['close', 'high', 'low', 'open']]
df_nflx.head()
#checking for null values in our dataframes
df_fb.isna().sum()
df_msft.isna().sum()
df_nflx.isna().sum()<jupyter_output><empty_output><jupyter_text>Looks like our dataframes are already clean  i am intersted in doing the analysis on only the closing trend of these stocks so let's first extract the closing prices and store it in a separate dataframe<jupyter_code>closing_data = pd.DataFrame({'FB': df_fb['close'],
                              'MSFT': df_msft['close'],
                               'NFLX': df_nflx['close']})
closing_data.head()<jupyter_output><empty_output><jupyter_text>The values in the above column represents the closing price of the companies<jupyter_code>#checking the dimensions of the data
closing_data.shape<jupyter_output><empty_output><jupyter_text>So our dataset contains 505 rows and 3 columns  Exploratory Data Analysis<jupyter_code>#Visualising the stocks closing prices
plt.figure(figsize = (14,8))
for column in closing_data.columns.values:
    plt.plot(closing_data[column], label = column)

plt.title('Stocks Graph',fontsize = 15)
plt.xlabel('Date')
plt.ylabel('Stock price in USD')
plt.legend(closing_data.columns.values, loc = 'upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>From the above graph we get to know that out nof the three Netflix has the highest stock price followed by facebook and microsoft As netflix stocks ar way higher than other two's let us try scaling the data and then visualise it<jupyter_code>from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range = (0,80))
scaled_data = scaler.fit_transform(closing_data)
scaled_data
type(scaled_data)<jupyter_output><empty_output><jupyter_text>As this a numpy array let us convert it to a dataframe<jupyter_code>scaled_closing_data = pd.DataFrame(scaled_data,columns = closing_data.columns)
scaled_closing_data.head()<jupyter_output><empty_output><jupyter_text>Perfect!! Now since all the features are on the same scale let us plot them and see what we get<jupyter_code>plt.figure(figsize = (14,8))
for column in scaled_closing_data.columns.values:
    plt.plot(scaled_closing_data[column], label = column)

plt.title('Scaled Graph', fontsize = 15)
plt.xlabel('Date')
plt.ylabel('Scaled price in USD')
plt.legend(scaled_closing_data.columns.values)
plt.show()<jupyter_output><empty_output><jupyter_text>From above graph looks like there is some corelation between the prices of the three companies Let us look at the daily return of these stocks<jupyter_code>daily_return_stocks = closing_data.pct_change()
daily_return_stocks
#Visualising the daily returns of the stocks
plt.figure(figsize = (14,8))
for column in daily_return_stocks.columns.values:
    plt.plot(daily_return_stocks[column], label = column)
    
plt.title('Daily returns Graph', fontsize = 15)
plt.xlabel('Date')
plt.ylabel('Percentage channge')
plt.legend(daily_return_stocks.columns.values)
plt.show()<jupyter_output><empty_output><jupyter_text>We can see from above graph that microsoft and netflix have some high and low spikes at march of 2020 and february of 2021 Let us now look how volatile are these stocks<jupyter_code>daily_return_stocks.std()<jupyter_output><empty_output><jupyter_text>looks like all these stocks have the same kind of volatility<jupyter_code>#Corelation between the three stocks
plt.figure(figsize = (10,10))
sns.heatmap(daily_return_stocks.corr(), annot = True, cmap = 'coolwarm')
plt.show()<jupyter_output><empty_output><jupyter_text>Looks like the facebook and microsoft stocks are highly corelated with each otherNow let us look at what we would have got if we had invested 100 dollars in these stocks two years<jupyter_code>#Daily returns cumulative
DRC = (daily_return_stocks+1).cumprod()
DRC
#visualising the cumulative daily returns
plt.figure(figsize = (14,8))
for column in DRC.columns.values:
     plt.plot(DRC[column], label = column)
    
plt.title('Daily cumulative returns', fontsize = 15)
plt.xlabel('Date')
plt.ylabel('Investment Growth')
plt.legend(DRC.columns.values)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09344686354817848, 'fraction_numerical': 0.011424876050873034, 'mean_word_length': 6.459003215434084, 'pattern_counts': {'":': 0, '<': 110, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '27', 'dir': 'jupyter-structured-clean-dedup', 'id': '281341', 'license': 'MIT', 'n_tokens_mistral': 3142, 'n_tokens_neox': 2911, 'n_words': 954, 'path': 'intabs_stock_data_analysis.ipynb', 'repo_name': 'MohitAbhyankar/Stock_data_analysis', 'size': '9308'}