Created
August 9, 2018 15:46
-
-
Save subpath/cbb6146a1edadf35ca22a76bed30de98 to your computer and use it in GitHub Desktop.
Custom backtest for Time Series model validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import requests | |
from sklearn.linear_model import BayesianRidge | |
#load market data | |
market_data = get_market_data('BTC/USD') | |
#shift target variable one day back | |
#in order to train model to predict one day into future | |
market_data['low_shifted'] = market_data['low'].shift(-1) | |
market_data = market_data.dropna() | |
#make numpy arrays | |
x = np.array(market_data[['close', 'high', 'low', 'open']]) | |
y = np.array(market_data['low_shifted']).reshape(-1,1) | |
#custom backtest function | |
def backtest(n_days): | |
""" | |
n_days - amount of the last n_days that we whant to get prediction and calculate metrics | |
""" | |
predictions = [] | |
true_values = [] | |
for i in reversed(range(1, n_days)): | |
x_train = x[:len(x)-i] | |
y_train = y[:len(y)-i] | |
x_test = x[len(x)-i] | |
y_test = y[len(y)-i] | |
model = BayesianRidge() | |
model.fit(x_train, y_train) | |
predictions.append(model.predict([x_test])[0]) | |
true_values.append(y_test[0]) | |
return true_values, predictions | |
true_values, predictions = backtest(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment