Skip to content

Commit

Permalink
Merge pull request #28 from vzhomeexperiments/dev_0.4.3
Browse files Browse the repository at this point in the history
Dev 0.4.3
  • Loading branch information
vzhomeexperiments authored Dec 16, 2020
2 parents 69a1bc0 + abe7b3d commit eae65fa
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 66 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: lazytrade
Type: Package
Title: Learn Computer and Data Science using Algorithmic Trading
Version: 0.4.2
Version: 0.4.3
Author: Vladimir Zhbanko
Maintainer: Vladimir Zhbanko <[email protected]>
Description: Provide sets of functions and methods to learn and practice data science using idea of algorithmic trading.
Expand Down
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
## Planned Changes

* Add folder generation function
* Put option predict ahead as a parameter (now 34 bars)
* aml_make_model use parameter to split data with specific n of rows e.g. n_rows

# lazytrade 0.4.3

# Version 0.4.3

## Changes

* solving warnings by changing example in *mt_make_model* function
* change function *mt_make_model* to read manually checked dataset
* update documentation

# lazytrade 0.4.2

Expand Down
6 changes: 3 additions & 3 deletions R/aml_make_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ aml_make_model <- function(symbol, timeframe, path_model, path_data,
model_status <- readr::read_csv(dec_file_path) %>% select(MaxPerf) %$% MaxPerf
} else if(force_update == TRUE) {
# delete the model and previous test results
remove(dec_file_path)
remove(m_path)
#try(remove(dec_file_path), silent = TRUE)
#try(remove(m_path),silent = TRUE)
model_status <- -1
} else { model_status <- 0 }

Expand All @@ -129,7 +129,7 @@ aml_make_model <- function(symbol, timeframe, path_model, path_data,


# split data to train and test blocks
# note: model will be tested on the PAST data and trained on the NEWEST data
# note: model will be trained on the OLDEST data
test_ind <- 1:round(0.3*(nrow(dat12))) #train indices 1:xxx
dat21 <- dat12[test_ind, ] #dataset to test the model using 30% of data
dat22 <- dat12[-test_ind, ] #dataset to train the model
Expand Down
23 changes: 9 additions & 14 deletions R/aml_test_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
#' @author (C) 2020 Vladimir Zhbanko
#'
#' @param symbol Character symbol of the asset for which to train the model
#' @param num_bars Number of bars used to test the model
#' @param timeframe Data timeframe e.g. 60 min
#' @param path_model Path where the models are be stored
#' @param path_data Path where the aggregated historical data is stored, if exists in rds format
#' @param path_sbxm Path to the sandbox where file with strategy test results should be written (master terminal)
#' @param path_sbxs Path to the sandbox where file with strategy test results should be written (slave terminal)
#' @param num_bars Integer, Number of (rows) bars used to test the model
#' @param timeframe Integer, Data timeframe e.g. 60 min. This will be equal to 1 bar
#' @param path_model String, User path where the models are be stored
#' @param path_data String, User path where the aggregated historical data is stored, if exists in rds format
#' @param path_sbxm String, User path to the sandbox where file with strategy test results should be written (master terminal)
#' @param path_sbxs String, User path to the sandbox where file with strategy test results should be written (slave terminal)
#'
#' @return Function is writing file into Decision Support System folder
#' @return Function is writing file into Decision Support System folders
#' @export
#'
#' @examples
Expand Down Expand Up @@ -116,10 +116,6 @@ aml_test_model <- function(symbol, num_bars, timeframe, path_model, path_data,
# file name with the tick data
path_tick <- file.path(path_data, "TickSize_AI_RSIADX.csv")

## !!!!!! TDL
## only select the latest 30% of data... or only last 2 month


#dataset with date column X1
y <- readr::read_rds(full_path) %>%
# remove empty rows
Expand All @@ -139,6 +135,8 @@ aml_test_model <- function(symbol, num_bars, timeframe, path_model, path_data,
#value z will contain tick value for this symbol
X2

#TDL add fail safe mechanism for 'z'

# generate a file name for model
m_name <- paste0("DL_Regression", "-", symbol,"-", timeframe)
m_path <- file.path(path_model, m_name)
Expand All @@ -158,9 +156,6 @@ aml_test_model <- function(symbol, num_bars, timeframe, path_model, path_data,
#NB <- 3





# To DO: Add ATR multiplier to simulate TP/SL
# add for loop for ATR Multiplier

Expand Down
32 changes: 5 additions & 27 deletions R/mt_evaluate.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@
#' data(macd_ML60M)
#'
#' # start h2o engine (using all CPU's by default)
#' h2o.init()
#' h2o.init(nthreads = 2)
#'
#' # performing Deep Learning Regression using the custom function
#' # this function stores model to the temp location
#' mt_make_model(indicator_dataset = macd_ML60M,
#' num_bars = 64,
#' timeframe = 60,
#' path_model = path_model,
#' path_data = path_data)
#' path_data = path_data,
#' activate_balance = TRUE,
#' num_nn_options = 3)
#'
#'
#' # Use sample data
Expand Down Expand Up @@ -77,30 +79,6 @@ mt_evaluate <- function(x, path_model, num_cols, timeframe){
# load models
m1 <- h2o::h2o.loadModel(m_path)


if(class(m1)[1] == "H2ORegressionModel") {
# Convert to matrix
X_m <- lazytrade::to_m(x, num_cols) %>% as.data.frame()
colnames(X_m) <- c(paste("X",1:num_cols,sep=""))
# load the dataset to h2o
test <- h2o::as.h2o(x = X_m, destination_frame = "test")

# retrieve the predicted market type value
e1 <- h2o::h2o.predict(m1, test)

# round the number to achieve class
result <- round(e1) %>% as.vector()

# manage negatives and/or bizzare numbers
if(result <= 0 || result > 6) {element <- -1} else {element <- result}

# output result of prediction from the function
return(element)

}

if(class(m1)[1] == "H2OMultinomialModel") {

# Convert to matrix
X_m <- lazytrade::to_m(x, num_cols) %>% as.data.frame()
colnames(X_m) <- c(paste("X",1:num_cols,sep=""))
Expand All @@ -117,6 +95,6 @@ mt_evaluate <- function(x, path_model, num_cols, timeframe){
# output result of prediction from the function
return(e1)

}


}
18 changes: 10 additions & 8 deletions R/mt_make_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
#' @param path_model String, Path where the models are be stored
#' @param path_data String, Path where the aggregated historical data is stored, if exists in rds format
#' @param activate_balance Boolean, option to choose if to balance market type classes or not, default TRUE
#' @param num_nn_options Integer, value from 1 to 20 or more. Used to change number of variants
#' of the random neural network structures
#' @param num_nn_options Integer, value from 3 to 24 or more. Used to change number of variants
#' of the random neural network structures. Value 3 will mean that only one
#' random structure will be used. To avoid warnings make sure to set this value
#' multiple of 3. Higher values will increase computation time.
#'
#' @return Function is writing file object with the model
#' @export
Expand Down Expand Up @@ -59,7 +61,7 @@
#' path_model = path_model,
#' path_data = path_data,
#' activate_balance = TRUE,
#' num_nn_options = 2)
#' num_nn_options = 3)
#'
#' # stop h2o engine
#' h2o.shutdown(prompt = FALSE)
Expand All @@ -86,18 +88,18 @@ mt_make_model <- function(indicator_dataset,

## check if the latest data is available
# construct path to the new data
path_file_name <- paste0("macd_ai_classified_", timeframe, "M.rds")
path_file_name <- paste0("macd_checked_", timeframe, "M.rds")
path_newdata <- file.path(path_data, path_file_name)
if(file.exists(path_newdata)){
# use new data...
macd_ML2 <- read_rds(path_newdata) %>%
macd_ML2 <- readr::read_rds(path_newdata) %>%
# and add new data
bind_rows(indicator_dataset) %>%
dplyr::bind_rows(indicator_dataset) %>%
# convert one column to factor
mutate_at("M_T", as.factor)
dplyr::mutate_at("M_T", as.factor)
} else {
# use input data and transform dataset column to factor
macd_ML2 <- indicator_dataset %>% mutate_at("M_T", as.factor)
macd_ML2 <- indicator_dataset %>% dplyr::mutate_at("M_T", as.factor)
}

# check if we don't have too much data
Expand Down
2 changes: 1 addition & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* windows, R 4.0.3
* R-hub builder
* R version 4.0.3 (2020-10-10)
* R Under development (unstable) (2020-10-11 r79327)
* R Under development (unstable) (2020-12-13 r79623)

## R CMD check results
Status: OK
Expand Down
14 changes: 7 additions & 7 deletions man/aml_test_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/mt_evaluate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions man/mt_make_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eae65fa

Please sign in to comment.