scripts/4_run_trials.R

# NOTE: You can run this script on multiple threads simultaneously. 
# Simply fire up lots of R shells and run it on each.
# Alternatively, wrap into doparallel, but I've found that quite tricky.
# Define paths and load parameters
rm(list=ls())
source("R/set_paths.R")
source("scripts/parameters.R")

# Load trading functions
###############################################################
source("R/trading_functions.R")
# Some functions depend on run mode
if (run_mode == "BACKTEST") {
source("R/backtest_trading_functions.R") 
  } else {
source("R/live_trading_functions.R")}

# Run trials
###############################################################
all_trials_begin <- Sys.time()
# Create temp directory
temp_path <- file.path(working_directory, "temp")
# Get a list of trials
trials <- list.files(trial_directory)
# length_of_trials
for (trial in trials) {
  print(paste("INFO: Running trial:", trial))
  trial_path <- file.path(trial_directory, trial)
  file.rename(from=trial_path, to=file.path(temp_path, trial))
  trial_path <- file.path(temp_path, trial)
  # clearing out results folder if it has data
  print("INFO: Deleting results directory if it exists already.")
  results_path <- file.path(results_directory, 
                            paste(data_source, constituent_index, tools::file_path_sans_ext(trial), sep="__"))
  unlink(results_path, recursive=T )
  print("INFO: Creating new results directory.")
  dir.create(results_path, showWarnings = FALSE)
  # trade for the length of the backtest
  print(paste("INFO: Running backtest for", trial))
  source("scripts/trading/trade.R")
  print("Moving the trial script to the results directory")
  file.rename(from=trial_path, to=file.path(results_path, trial))
  gc(verbose=T)
}

all_trials_end <- Sys.time()

print(all_trials_end - all_trials_begin)
riazarbi/equity_analysis documentation built on June 8, 2019, 1:36 p.m.