Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Last active May 5, 2024 20:10
Show Gist options
  • Save quantra-go-algo/f5003ed83cc9294b9c622f735e27e49d to your computer and use it in GitHub Desktop.
Save quantra-go-algo/f5003ed83cc9294b9c622f735e27e49d to your computer and use it in GitHub Desktop.
import cudf
import cuml
import pyfolio as pf
import numpy as np
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
from hmmlearn import hmm
from sklearn.utils import check_random_state
import warnings
warnings.filterwarnings("ignore")
# Obtaining the EURUSD data
data = yf.download('EURUSD=X', start='2000-01-01', end='2024-01-13', auto_adjust=True)
# Initial index number to start forecasting and trading
initial_iloc = data.index.get_loc(data.loc['2019-01'].index[0])
# Drop the volume column
data.drop('Volume', axis=1, inplace=True)
# Set the columns names
columns_names = data.columns.tolist()
# Create the prediction feature
data['y'] = np.where(data['Close'].pct_change().shift(-1) > 0,1, np.where(data['Close'].pct_change().shift(-1) < 0, -1, np.nan))
# Create the input features
for j in range(1,5):
data[[f'{column}_{j}' for column in columns_names]] = data[columns_names].pct_change().shift(j)
# Compute the Buy-and-Hold returns
data['returns'] = data['Close'].pct_change()
# Drop the NaN values
data.dropna(inplace=True)
# Function to create signal based on next-day return
def create_signal(data, window_size=initial_iloc):
# Copy the dataframe
data = data.copy()
# Create the signal column
data['signal'] = 0.0
# Create the train signal column
data['train_signal'] = 0.0
# Create the leverage column
data['leverage'] = 0.0
# Loop to create the signal for each day
for i in range(len(data) - window_size):
# Get the input features
data_sample = data.iloc[i:(i+window_size)]
# Get the input features to train the model
X_train = data_sample[[f'{column}_{j}' for column in columns_names for j in range(1,5)]].iloc[:-2]
# Get the prediction feature
y = data_sample['y'].iloc[:-2]
# Create the random forest classifier object
rf_classifier = cuml.ensemble.RandomForestClassifier(n_estimators=10, n_streams=1, random_state=100)
# Fit the model
rf_classifier.fit(X_train, y)
# Get the input features to get the signal prediction
X_test = data_sample[[f'{column}_{j}' for column in columns_names for j in range(1,5)]].iloc[-1]
# Get the signal prediction
data['signal'].iloc[(i+window_size)] = rf_classifier.predict(X_test).values[0]
return data
# Creating the signal using the historical stock data
data = create_signal(data)
print(data['signal'].value_counts())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment