Market Forecast: StatsModels VARMAX Method
This is a small contribution to the vast expanse of price action forecast modeling that has existed, thankfully, long enough for it to retain such great value. I hope this prevails a couple revelations, if not just one, at least in the realm of VARMAX, forecasting outputs, or just revel at my chicken-scratch coding. Regardless your reason to continue reading, you will at bare minimum see my take on applying a look-ahead function in order to model a predicted vs observed price action of one cryptocurrency sample.
You can see the code here: [https://www.kaggle.com/code/dascient/crypto-forecast-using-statsmodels-varmax]
I will begin the rest with less to zero pleasantries:
# packages
import numpy as np # linear algebra
import pandas as pd # data processing & cdv
Let’s look at the most common currency, Bitcoin.
# bring in data
df = pd.read_csv('/kaggle/input/all-crypto-currencies/crypto-markets.csv')
# isolate BTC
df = df.loc[df.symbol == 'BTC']
df.head()
# plot
df.set_index('date')['close'].tail(500).plot(figsize=(15,4), title='BTC - close')
Scale data using sklearn MinMaxScaler and/or MaxAbsScaler. The cell below also creates the “train” and “test” dataframes. “Train” contains observed data for the model to interpret. “Test” will be observed data with which a model will be forecasted.
# Use Scikit-learn to transform with maximum absolute scaling
from sklearn.preprocessing import MinMaxScaler,MaxAbsScaler
scaled_df = df.set_index('date')[['open','high','low','close']]
scaler = MaxAbsScaler()
scaler.fit(scaled_df)
scaled = scaler.transform(scaled_df)
scaled_df = pd.DataFrame(scaled_df, columns=scaled_df.columns)
# use first 1000 days as train
train = scaled_df[:1000]
# simulation window
window = 100 # days look ahead
test = scaled_df[1000:1000+window]
Initiate VARMAX Modeling
As well as, output model summary for each variable [o,h,l,c].
# VARMA example
from statsmodels.tsa.statespace.varmax import VARMAX
# contrived dataset
data = train[['open','high','low','close']]
# fit model
model = VARMAX(data, freq = 'D', order=(1,1), seasonal_order=(0, 0, 0, 0),
mle_regression = True,
filter_concentrated = True)
model_fit = model.fit(disp=True)
# make prediction
yhat = model_fit.predict(len(data), len(data))
# simulate prediction
model_fit.plot_diagnostics(3,figsize=(20,5))
# plot prediction vs observed
pred = pd.DataFrame()
pred['prediction'] = model_fit.simulate(window).reset_index(drop=True).close
pred['observed'] = test.reset_index(drop=True).close
pred.plot(figsize=(20,5),title='prediction vs observed',color=['blue','black'],style=['--','-'])
# plot prediction - observed
pred['prediction - observed'] = pred['prediction'] - pred['observed']
pred.plot(figsize=(20,5),title='forecast error',color=['blue','black','green'],style=['--','-',':'])
Looking at the outcome above, I found that running the VARMAX predictor will generate random forecast regardless of the train data. This is due in part of a randomly generated factor within the model parameters that could still be explored. Future work includes the VARMAX model to make trade signals against live price action data.
Please feel free to fork, star, watch, & even scrutinize/criticize my projects/repositories:
My credentials & other contributions are verifiable on my LinkedIn profile here:
https://www.linkedin.com/in/dontadaya
Thank you!
Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing
Also, Read
- 3Commas Review | Pionex Review | Coinrule review
- Ledger vs Ngrave | Ledger nano s vs x | Binance Review
- Bybit Exchange Review | Bityard Review | Jet-Bot Review
- 3Commas vs Cryptohopper | Earn crypto interest
- The Best Bitcoin Hardware wallet | BitBox02 Review
- BlockFi vs Celsius | Hodlnaut Review | KuCoin Review