Skip to content

Instantly share code, notes, and snippets.

View quantra-go-algo's full-sized avatar

Algorithmic Trading quantra-go-algo

View GitHub Profile
# Function to open an XLSX file with error handling
open_xlsx <- function(file_path) {
# Output the Excel file in case it exists
result <- tryCatch({
# Read the Excel file
data <- read.xlsx(file_path)
# Set the date column as datetime type
data$date <- as.Date(data$date, origin = "1899-12-30")
# Return the data
# Set the seed to estimate the TVP-VAR-SV model
set.seed(2024)
# Estimate the TVP-VAR-SV model
bv <- bvar.sv.tvp(tvp_var_data, tau= 250, nf=1, nrep = 300, nburn=20)
# Obtain the forecasts of the model based on the mean of the posterior-distribution draws
forecast_ys <- rowMeans(bv$fc.ydraws[1:7,1,])
# Set tickers
tickers <- c('MSFT', 'AAPL', 'TSLA', 'NFLX', 'META', 'AMZN','GOOGL')
# Set start and end dates
start = "1990-01-01"
end = "2024-08-01"
df <- new.env()
# Import the data
library('TTR')
library('quantmod')
library('stats')
library('lubridate')
library('dplyr')
library('ggplot2')
library('forecast')
library('vars')
library('openxlsx')
library('bvarsv')
install.packages('TTR')
install.packages('quantmod')
install.packages('stats')
install.packages('lubridate')
install.packages('dplyr')
install.packages('ggplot2')
install.packages('forecast')
install.packages('vars')
install.packages('openxlsx')
install.packages('bvarsv')
def boruta_shap_algorithm(X, y, trials=20, workers=2, significance_level=0.05, seed=2024):
# Set the seed
np.random.seed(seed)
# Assert that the number of samples of both data match
assert X.shape[0] == y.shape[0], "X and y dimensions don't coincide"
# Set a dictionary to save the number of hits for each feature
# select features from n number of trials
def choose_features(feature_hits, TRIALS, thresh):
# Define the boundaries for the green zone
# Define the green zone threshold
green_zone_thresh = TRIALS - thresh
# Define the blue zone upper threshold
blue_zone_upper = green_zone_thresh
# Define the blue zone lower threshold
blue_zone_lower = thresh
# Set the minimum number of trials as a threshold number to accept an input feature as a selected feature
def get_tail_items(pmf, significance_level=0.05):
# Set total to zero
total = 0
# Create a loop based on the probability mass function
for i, x in enumerate(pmf):
# Increment the total variable with the probability “x” of i
total += x
# If total is higher than the significance level
if total >= significance_level:
# Import the necessary libraries
import scipy as sp
import numpy as np
import pandas as pd
import shap
from xgboost import XGBRFClassifier
from xgboost import XGBRFRegressor
from sklearn.preprocessing import LabelEncoder
from concurrent import futures