Skip to content

Commit

Permalink
Merge pull request #23 from vzhomeexperiments/dev_0.4.1
Browse files Browse the repository at this point in the history
Dev 0.4.1
  • Loading branch information
vzhomeexperiments authored Oct 15, 2020
2 parents 9fbd80e + 6404ffc commit 92e14af
Show file tree
Hide file tree
Showing 79 changed files with 1,409 additions and 756 deletions.
5 changes: 2 additions & 3 deletions 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.0
Version: 0.4.1
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 All @@ -26,8 +26,7 @@ Imports:
grDevices,
h2o,
ReinforcementLearning,
openssl,
tidyr
openssl
Suggests:
testthat (>= 2.1.0),
covr,
Expand Down
22 changes: 11 additions & 11 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ export(create_transposed_data)
export(decrypt_mykeys)
export(encrypt_api_key)
export(evaluate_macroeconomic_event)
export(evaluate_market_type)
export(generate_RL_policy)
export(generate_RL_policy_mt)
export(get_profit_factorDF)
export(import_data)
export(import_data_mt)
export(load_asset_data)
export(log_RL_progress)
export(log_RL_progress_mt)
export(mt_evaluate)
export(mt_import_data)
export(mt_make_model)
export(opt_aggregate_results)
export(opt_create_graphs)
export(profit_factor)
export(record_policy)
export(record_policy_mt)
export(rl_generate_policy)
export(rl_generate_policy_mt)
export(rl_log_progress)
export(rl_log_progress_mt)
export(rl_record_policy)
export(rl_record_policy_mt)
export(rl_write_control_parameters)
export(rl_write_control_parameters_mt)
export(self_learn_ai_R)
export(test_model)
export(to_m)
export(util_generate_password)
export(writeCommandViaCSV)
export(util_profit_factor)
export(write_command_via_csv)
export(write_control_parameters)
export(write_control_parameters_mt)
export(write_ini_file)
18 changes: 15 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
## Planned Changes

* Add folder generation function
* Remove warnings:

1: `as_data_frame()` is deprecated as of tibble 2.0.0. Please use `as_tibble()` instead.
2: `data_frame()` is deprecated as of tibble 1.1.0. Please use `tibble()` instead.
# lazytrade 0.4.1

# Version 0.4.1

## Changes

### Implemented

* added option to balance classes in mt_make_model function
* add and delete depricated functions
* change names of functions to reflect the purpose: mt - market type, rl- reinforcement learning, util - utility
* change example methods to unify them across functions
* align names of the variables across several functions
* remove warnings caused by new dplyr functionalities .groups
* remove tidyr from imports to solve Note

# lazytrade 0.4.0

Expand Down
4 changes: 2 additions & 2 deletions R/aml_make_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
#'
#' # dataset will be written to the temp directory
#'
#' # start h2o engine (using all CPU's by default)
#' h2o.init()
#' # start h2o engine
#' h2o.init(nthreads = 2)
#'
#'
#' # performing Deep Learning Regression using the custom function
Expand Down
2 changes: 1 addition & 1 deletion R/aml_score_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#' path_data = path_data)
#' # start h2o engine (using all CPU's by default)
#'
#' h2o.init()
#' h2o.init(nthreads = 2)
#'
#'
#' # performing Deep Learning Regression using the custom function
Expand Down
20 changes: 10 additions & 10 deletions R/aml_test_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
#' path_data = path_data)
#'
#'
#' # start h2o engine (using all CPU's by default)
#' h2o.init()
#' # start h2o engine
#' h2o.init(nthreads = 2)
#'
#'
#' # performing Deep Learning Regression using the custom function
Expand Down Expand Up @@ -116,14 +116,14 @@ aml_test_model <- function(symbol, num_bars, timeframe, path_model, path_data,
#dataset with date column X1
y <- readr::read_rds(full_path) %>%
# remove empty rows
na.omit() %>% filter_all(any_vars(. != 0))
na.omit() %>% dplyr::filter_all(any_vars(. != 0))


#dataset without X1 column (for predictions)
x <- y %>%
select(-X1, -X2, -X3, -LABEL) %>%
dplyr::select(-X1, -X2, -X3, -LABEL) %>%
# only keep last month for simulation
head(num_bars)
utils::head(num_bars)

# generate a file name for model
m_name <- paste0("DL_Regression", "-", symbol,"-", timeframe)
Expand Down Expand Up @@ -161,7 +161,7 @@ aml_test_model <- function(symbol, num_bars, timeframe, path_model, path_data,

dat31 <- y %>%
# using last 600 observations
head(num_bars) %>%
utils::head(num_bars) %>%
## select columns:
# X1 time index
# X2 price at the time index
Expand Down Expand Up @@ -227,10 +227,10 @@ aml_test_model <- function(symbol, num_bars, timeframe, path_model, path_data,


df_tr <- df_res %>%
group_by(TR_Level, NB_hold, Symbol) %>%
summarise(MaxPerf = max(PnL_NB)) %>%
arrange(desc(MaxPerf)) %>%
head(1)
dplyr::group_by(TR_Level, NB_hold, Symbol) %>%
dplyr::summarise(MaxPerf = max(PnL_NB)) %>%
dplyr::arrange(desc(MaxPerf)) %>%
utils::head(1)



Expand Down
73 changes: 28 additions & 45 deletions R/check_if_optimize.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,58 @@
#' @details Whenever there will be not enough trades then empty file will be written to the destination
#'
#' @param x - dataframe containing trading results
#' @param path_trading_robot - path of trading robot repository. must contain folder TEST with file Setup.csv.
#' File Setup.csv contains a table with magic numbers under test
#'
#' @param system_list - dataframe containing a table with magic numbers used by robots. Stored in file Setup.csv
#' @param path_data - string, path to the folder where optimization file should be written
#' @param num_trades_to_consider - Number of trades to calculate profit factor
#' @param profit_factor_limit - Limit below which trading robot considered not working properly
#' @param demo_mode - When true function uses package test dataset
#' @param profit_factor_limit - Limit below which trading robot is considered not working properly
#' @param write_mode - When true function will write result to the file located in the temporary directory
#'
#' @return function returns a dataframe with systems that should be optimized
#'
#' @author (C) 2019 Vladimir Zhbanko
#' @author (C) 2019,2020 Vladimir Zhbanko
#'
#' @export
#'
#' @examples
#'
#' library(lazytrade)
#' library(magrittr)
#' library(dplyr)
#' library(readr)
#' library(lubridate)
#' DFT1 <- import_data(trade_log_file = system.file("extdata",
#' "OrdersResultsT1.csv",
#' package = "lazytrade"),
#' demo_mode = TRUE)
#'
#' path_data <- normalizePath(tempdir(),winslash = "/")
#'
#' # without writing to the file
#' DFT1 %>% check_if_optimize(num_trades_to_consider = 3,
#' profit_factor_limit = 1.2,
#' demo_mode = TRUE,
#' write_mode = FALSE)
#' file.copy(from = system.file("extdata", "Setup.csv", package = "lazytrade"),
#' to = file.path(path_data, "Setup.csv"), overwrite = TRUE)
#'
#' system_list <- read_csv(file.path(path_data, "Setup.csv"))
#'
#' # function will write to the temporary file
#' DFT1 %>% check_if_optimize(num_trades_to_consider = 3,
#' profit_factor_limit = 1.2,
#' demo_mode = TRUE,
#' write_mode = TRUE)
#' data(profit_factorDF)
#'
#'
check_if_optimize <- function(x, path_trading_robot = "",
#' # without writing to the file
#' check_if_optimize(x = profit_factorDF,
#' system_list = system_list,
#' path_data,
#' num_trades_to_consider = 3,
#' profit_factor_limit = 0.8,
#' write_mode = TRUE)
#'
#'
check_if_optimize <- function(x,
system_list,
path_data,
num_trades_to_consider = 3,
profit_factor_limit = 0.7,
demo_mode = FALSE,
write_mode = FALSE){

requireNamespace("dplyr", quietly = TRUE)
requireNamespace("readr", quietly = TRUE)


if(!demo_mode){

y <- x %>% # filtered to contain last 20 orders for each system
dplyr::ungroup() %>%
dplyr::group_by(MagicNumber) %>%
dplyr::arrange(MagicNumber, desc(OrderCloseTime)) %>%
dplyr::filter(row_number() <= num_trades_to_consider+1) %>%
Expand All @@ -69,30 +69,13 @@ check_if_optimize <- function(x, path_trading_robot = "",
dplyr::filter(PrFact < profit_factor_limit) %>%
dplyr::select(MagicNumber, PrFact) %>%
dplyr::mutate(ToOptimize = 1) %>%
dplyr::inner_join(y = readr::read_csv(file = file.path(path_trading_robot,"TEST", "Setup.csv"),
col_types = "cci"),
by = c("MagicNumber" = "Magic"))
if(write_mode){
readr::write_csv(y, path = file.path(paste0(path_trading_robot, "TEST/", Sys.Date(), "-Re-Train", ".csv")))
} else {return(y)}
dplyr::inner_join(y = system_list, by = c("MagicNumber" = "Magic"))

} else {
if(write_mode){
readr::write_csv(y, path = file.path(path_data, paste0(Sys.Date(), "-Re-Train", ".csv")))
} else {return(y)}

y <- x %>% # filtered to contain last 20 orders for each system
dplyr::group_by(MagicNumber) %>%
dplyr::arrange(MagicNumber, desc(OrderCloseTime)) %>%
dplyr::filter(row_number() <= num_trades_to_consider+1) %>%
lazytrade::get_profit_factorDF(num_trades_to_consider) %>%
dplyr::ungroup() %>%
dplyr::filter(PrFact < profit_factor_limit) %>%
dplyr::select(MagicNumber, PrFact) %>%
dplyr::mutate(ToOptimize = 1) %>%
dplyr::inner_join(y = readr::read_csv(file = system.file("extdata", "Setup.csv", package = "lazytrade"),
col_types = "cci"),
by = c("MagicNumber" = "Magic"))
if(write_mode){readr::write_csv(y, tempfile("Re-Train", fileext = ".csv"))} else {return(y)}

}


}
3 changes: 2 additions & 1 deletion R/create_labelled_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#'
#'
#' library(dplyr)
#' library(magrittr)
#' library(readr)
#' library(lazytrade)
#'
Expand All @@ -51,7 +52,7 @@ create_labelled_data <- function(x, n = 50, type = "regression"){
# remove column 1 with data and time information
dplyr::select(-1) %>%
# split dataset into several objects each containing n rows (it will be a list)
split(rep(1:ceiling(nr/n), each=n, length.out=nr)) #list
base::split(rep(1:ceiling(nr/n), each=n, length.out=nr)) #list
# remove last element of the list
dat11[length(dat11)] <- NULL

Expand Down
2 changes: 1 addition & 1 deletion R/datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
#' \item{M_T}{Category of Market Type}
#' }
#'
"macd_ML2"
"macd_ML60M"

#' Table with indicator only used to train model, 128 col 1646 rows
#'
Expand Down
3 changes: 2 additions & 1 deletion R/evaluate_macroeconomic_event.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#' # evaluate data on macroeconomic event (required to start trading)
#' library(dplyr)
#' library(readr)
#' library(lazytrade)
#'
#' dir <- normalizePath(tempdir(),winslash = "/")
#'
Expand Down Expand Up @@ -79,7 +80,7 @@ evaluate_macroeconomic_event <- function(setup_file_path,
#read the file containing a flag (1 will mean that event is present hence no new opened orders)
DF_NT <- readr::read_csv(file= macro_complete_path, col_types = "i")
#read the table of trading robots in operation
DF_Setup <- readr::read_csv(setup_complete_path)
DF_Setup <- readr::read_csv(setup_complete_path, col_types = "ffi")

## condition to disable systems
if(DF_NT[1,1] == 1) {
Expand Down
44 changes: 23 additions & 21 deletions R/get_profit_factorDF.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#' library(dplyr)
#' library(magrittr)
#' data(profit_factorDF)
#' get_profit_factorDF(profit_factorDF, 10)
#' get_profit_factorDF(x = profit_factorDF,
#' num_orders = 10)
#'
#'
#'
Expand All @@ -25,35 +26,36 @@ get_profit_factorDF <- function(x, num_orders){
x <- x %>% ungroup() #ungroup to remove warning

DF_L <- x %>%
group_by(MagicNumber) %>%
summarise(nOrders = n())%>%
filter(nOrders > num_orders)%>%
select(MagicNumber)%>%
dplyr::group_by(MagicNumber) %>%
dplyr::summarise(nOrders = n(), .groups = "drop_last") %>%

dplyr::filter(nOrders > num_orders) %>%
dplyr::select(MagicNumber) %>%
# subset only rows that contans magic numbers from x
inner_join(x, by = "MagicNumber")%>%
group_by(MagicNumber)%>%
filter(Profit < 0) %>%
summarise(Loss = abs(sum(Profit)))
dplyr::inner_join(x, by = "MagicNumber") %>%
dplyr::group_by(MagicNumber) %>%
dplyr::filter(Profit < 0) %>%
dplyr::summarise(Loss = abs(sum(Profit)), .groups = "drop_last")
# generate DF with only MagicNumbers when > 10 trades and all trades are profits
DF_P <- x %>%
group_by(MagicNumber) %>%
summarise(nOrders = n())%>%
filter(nOrders > num_orders)%>%
select(MagicNumber)%>%
dplyr::group_by(MagicNumber) %>%
dplyr::summarise(nOrders = n(), .groups = "drop_last") %>%
dplyr::filter(nOrders > num_orders)%>%
dplyr::select(MagicNumber)%>%
# subset only rows that contans magic numbers from x
inner_join(x, by = "MagicNumber")%>%
group_by(MagicNumber)%>%
filter(Profit > 0) %>%
summarise(Gain = abs(sum(Profit)))
dplyr::inner_join(x, by = "MagicNumber")%>%
dplyr::group_by(MagicNumber)%>%
dplyr::filter(Profit > 0) %>%
dplyr::summarise(Gain = abs(sum(Profit)), .groups = "drop_last")
# generate DF containing profit factor of all systems
DF <- DF_P %>%
full_join(DF_L, by = "MagicNumber")
dplyr::full_join(DF_L, by = "MagicNumber")
# replace any NA with 1!
DF[is.na(DF)] <- 1
# calculate profit factors of the each system!
DF_PF <- DF%>%
group_by(MagicNumber)%>%
mutate(PrFact = Gain/(0.001+Loss))%>%
select(MagicNumber, PrFact)
dplyr::group_by(MagicNumber)%>%
dplyr::mutate(PrFact = Gain/(0.001+Loss))%>%
dplyr::select(MagicNumber, PrFact)
return(DF_PF)
}
Loading

0 comments on commit 92e14af

Please sign in to comment.