This example predicts future ETH price, using simple input data (just historical ETH price) and a simple model (linear dynamical model).
Predictions are 5mins, 10mins, ..., 60mins into the future.
We assume you've already done main5.md "Setup".
In the Python console:
import ccxt
cex_x = ccxt.binance().fetch_ohlcv('ETH/USDT', '5m')
allcex_uts = [xi[0]/1000 for xi in cex_x] # timestamps
allcex_vals = [xi[4] for xi in cex_x] # ETH prices
# # Extracts dates and ether price values
print_datetime_info("CEX data info", allcex_uts)
# Transform timestamps to dates
dts = to_datetimes(allcex_uts)
# create a Data Frame with two columns [date,eth-prices] with dates given in intervals of 5-minutes
import pandas as pd
data = pd.DataFrame({"ds": dts, "y": allcex_vals})
In the same Python console:
# use the last 12 intervals of 5mins each of testing set, all the previous data is used as training
train_data = data.iloc[0:-12,:]
test_data = data.iloc[-12:,:]
# fit a linear model (Open sourced Facebook's Prophet model: https://facebook.github.io/prophet/)
# As the data is subdaily, the model will fit daily seasonality
from prophet import Prophet
model = Prophet()
model.fit(train_data)
In the same Python console:
#Predict ETH values over the range of the test set
forecast = model.predict(pd.DataFrame({"ds":test_data.ds}))
pred_vals_test = forecast.set_index('ds')['yhat'][-12:].to_numpy()
In the same Python console:
# now, we have predicted and actual values. Let's find error, and plot!
cex_vals = test_data.y
nmse = calc_nmse(cex_vals, pred_vals_test)
print(f"NMSE = {nmse}")
plot_prices(cex_vals, pred_vals_test)
Keep iterating in step 3 until you're satisfied with accuracy. Then...
# fit model with all the available data
model = Prophet()
model.fit(data)
# generate dates for prediction (12 time perods ahead of the latest datapoint in the data time)
future_dates = model.make_future_dataframe(periods=12, freq="5min", include_history=False)
# predcit eth values on future_dates
forecast = model.predict(future_dates)
pred_vals = forecast.set_index('ds')['yhat'].to_numpy()
From Challenge 5, do:
- Publish & share predictions