-
pandas
-
yfinance (This is the Yahoo Finance API, it will collect the latest Bitcoin prices and serve as our dataset)
-
datetime (as well as
from datetime import date, timedelta
) -
plotly
-
autots
Create the 'today' variable with the current date as it's value.
today = date.today()
Then covert the current date and store it as a string in the variable d1
d1 = today.strftime("%Y-%m-%d")
Create an end_date variable and set it's value equal to d1(current date)
end_date = d1
Calculate 2 years before d1 (current date) and convert it to a string using strftime method
d2 = date.today() - timedelta(days=730)
d2 = d2.strftime("%Y-%m-%d")
Then create the start_date variable with a value equal to d2
start_date = d2
Retreave historical data for 'BTC-USD' between the start_date(d2) and the end_date(current date) using the yfinance library, and store it in the 'data' variable.
type(data) == pandas.core.frame.DataFrame
Add a new column called "Date" to the data frame, and set its values equal to the index of the data frame.
data['Date'] = data.index
Reorder the columns of the data frame to be ["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"].
data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]]
Reset the index of the data frame, dropping the old index and replacing it with a new sequential index starting at 0.
data.reset_index(drop=True, inplace=True)
Print the first few rows of the data frame using the head method.
print(data.head())
We can check the 'shape' of the Dataset to verify if we are working with 730 rows or not using the shape method:
In: print(data.shape)
Out: (730, 7)
---## Step 3: Visualize the change in Bitcoin Prices from 730 days(2 years) to today using a candlestick chart:
Import the plotly graph object
import plotly.graph_objects as go
Create a Candlestick trace and specify which columns in the 'data' DataFrame should be used in the chart.
figure = go.Figure(data=[go.Candlestick(x=data["Date"], open=data["Open"], high=data["High"],low=data["Low"], close=data["Close"])])
Update the layout of the chart and set chart Title to; "Bitcoin Price Analysis", and hiding the x-axis range slider
figure.update_layout(title = "Bitcoin Price Analysis", xaxis_rangeslider_visible=False)
Display the Chart
figure.show()
Example:
correlation = data.corr()
print(correlation["Close"].sort_values(ascending=False))
Sample Output:
Predicting the price of Crypto depends on Time Series Analysis This is a statistical technique for analyzing and modeling time-dependent data.
I will use the AutoTS library to predict the Crypto Prices for the next 30 days:
from autots import AutoTs
Create an instance of the 'AutoTS' class and store it in the "model" variable
model = AutoTS(forecast_length=30, frequency='infer', ensemble='simple',)
Fit the model to the 'data' Dataset
model = model.fit(data, date_col='Date', value_col='Close', id_col=None)
Predict and print out the forecast to the console
prediction = model.predict()
forecast = prediction.forecast
print(forecast)
*Buying and selling result in a change in the price of any cryptocurrency, but buying and selling trends depend on many factors. Using machine learning for cryptocurrency price prediction can only work in situations where prices change due to historical prices that people see before buying and selling their cryptocurrency.