Skip to content

Commit

Permalink
big update
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhomeexperiments committed Mar 31, 2019
1 parent c0a05f4 commit 941ad04
Show file tree
Hide file tree
Showing 104 changed files with 197,034 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
Binary file added RESEARCH/2019-03-24-Result-100-60.rds
Binary file not shown.
Binary file added RESEARCH/2019-03-24-Result-125-1.rds
Binary file not shown.
Binary file added RESEARCH/2019-03-24-Result-125-15.rds
Binary file not shown.
Binary file added RESEARCH/2019-03-24-Result-125-60.rds
Binary file not shown.
Binary file added RESEARCH/2019-03-24-Result-75-1.rds
Binary file not shown.
Binary file added RESEARCH/2019-03-24-Result-75-15.rds
Binary file not shown.
Binary file added RESEARCH/2019-03-24-Result-75-60.rds
Binary file not shown.
5 changes: 5 additions & 0 deletions RESEARCH/SelfLearn-Research.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# https://www.udemy.com/self-learning-trading-robot/?couponCode=LAZYTRADE7-10
# Script to gather financial data, transform it and to perform
# Supervised Deep Learning Classification Modelling
#
# Purpose of this script is to simulate results in the several possible variants
# NOTE: delete all previously create models from the folder R_selflearning/model after updating h2o engine
#
# load libraries to use and custom functions
library(tidyverse)
Expand All @@ -21,6 +24,7 @@ source("C:/Users/fxtrams/Documents/000_TradingRepo/R_selflearning/self_learn_ai_
source("C:/Users/fxtrams/Documents/000_TradingRepo/R_selflearning/test_model.R")
#absolute path to store model objects (useful when scheduling tasks)
path_model <- "C:/Users/fxtrams/Documents/000_TradingRepo/R_selflearning/model"

h2o.init()

### Create For loop to test possible outcomes and test those strategies
Expand Down Expand Up @@ -75,6 +79,7 @@ files_to_analyse <-list.files(file.path(getwd(),"RESEARCH/"), pattern="*.rds", f
# e.g.: str_extract(files_to_analyse[2], "(?<=Result-)(.*)(?=.rds)")
#
for (FILE in files_to_analyse) {
#FILE <- files_to_analyse[3]
#extract nbars from the file name, note we use only those of the latest date
num_bars <- FILE %>% str_extract(paste0("(?<=",Sys.Date(),"-Result-)(.*)(?=.rds)"))
if(!exists("summary_file")){
Expand Down
79 changes: 79 additions & 0 deletions _FUN/create_labelled_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
## FUNCTION create_labelled_data
## PURPOSE: function gets price data in each column
## it is splitting this data by periods and transposes the data.
## additionally it is label the data based on the simple logic assigning it to 2 categories based on the difference
## between beginning and end of the vector
## finally it is stacking all data and joining everything into the table

## TEST:
# library(tidyverse)
# library(lubridate)
# pathT2 <- "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/"
# prices <- read_csv(file.path(pathT2, "AI_CP1.csv"), col_names = F)
# prices$X1 <- ymd_hms(prices$X1)
# write_rds(prices, "test_data/prices.rds")

#' Create labelled data
#' https://www.udemy.com/self-learning-trading-robot/?couponCode=LAZYTRADE7-10
#'
#' @param x - data set containing a table where 1st column is a Time index and other columns containing financial asset price values
#' @param n - number of rows we intend to split and transpose the data
#' @param type - type of the label required. Can be either "classification" or "regression". "classification" will return either "BU" or "BE",
#' "regression" will return the difference between first value and the last value in each row
#'
#' @return function returns transposed data. One column called 'LABEL' indicate achieved value of the label.
#' Transposed values from every column are stacked one to each other
#'
#' @export
#'
#' @examples
create_labelled_data <- function(x, n = 50, type = "regression"){
require(tidyverse)
#source("C:/Users/fxtrams/Documents/000_TradingRepo/R_selflearning/_FUN/load_data.R")
#x <- load_data(path_terminal = "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/", trade_log_file = "AI_CP", time_period = 1, data_deepth = "50000")
#x <- read_rds("_TEST_DATA/price_dataset.rds")
#n <- 100
#type <- "classification"
#type <- "regression"
#
nr <- nrow(x)
dat11 <- x %>%
# remove column 1 with data and time information
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
# remove last element of the list
dat11[length(dat11)] <- NULL

# operations within the list
for (i in 1:length(dat11)) {
#i <- 2
if(type == "classification"){

# classify by 2 classes 'BU', 'BE'
if(!exists("dfr12")){
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal") %>%
mutate(LABEL = ifelse(.[[1]]>.[[n]], "BU", "BE"))} else {
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal") %>%
mutate(LABEL = ifelse(.[[1]]>.[[n]], "BU", "BE")) %>%
bind_rows(dfr12)
}
} else if(type == "regression"){
# add label with numeric difference {in pips}
# i <- 1
if(!exists("dfr12")){
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal", verbose =F) %>%
mutate(LABEL = 10000*(.[[1]]-.[[n]]))} else {
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal", verbose =F) %>%
mutate(LABEL = 10000*(.[[1]]-.[[n]])) %>%
#oldest data will be on top of the dataframe!
bind_rows(dfr12)
}


}
}

return(dfr12)

}
51 changes: 51 additions & 0 deletions _FUN/create_transposed_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## FUNCTION create_transposed_data
## PURPOSE: function gets indicator data in each column
## it is splitting this data by periods and transpose the data.
## additionally it is label the data based on the simple logic assigning it to 2 categories based on the difference
## between beginning and end of the vector
## finally it is stacking all data and joining everything into the table

## TEST:
# library(tidyverse)
# library(lubridate)
# pathT2 <- "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/"
# macd <- read_csv(file.path(pathT2, "AI_Macd1.csv"), col_names = F)
# macd$X1 <- ymd_hms(macd$X1)
# write_rds(macd, "test_data/macd.rds")

#' Create Transposed Data
#' https://www.udemy.com/self-learning-trading-robot/?couponCode=LAZYTRADE7-10
#'
#' @param x - data set containing a table where 1st column is a Time index and other columns containing financial asset indicator values
#' @param n - number of rows we intend to split and transpose the data
#'
#' @return function returns transposed data. Transposed values from every column are stacked one to each other
#' @export
#'
#' @examples
#'
create_transposed_data <- function(x, n = 50){
require(tidyverse)
#source("C:/Users/fxtrams/Documents/000_TradingRepo/R_selflearning/_FUN/load_data.R")
#n <- 100
#x <- load_data(path_terminal = "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/", trade_log_file = "AI_Macd", time_period = 1, data_deepth = "50000")
#x <- read_rds("_TEST_DATA/indicator_dataset.rds")
nr <- nrow(x)
dat11 <- x %>% select(-1) %>% split(rep(1:ceiling(nr/n), each=n, length.out=nr)) #list
dat11[length(dat11)] <- NULL

# operations within the list
for (i in 1:length(dat11)) {
#i <- 1

if(!exists("dfr12")){
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal", verbose =F) } else {
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal", verbose =F) %>% bind_rows(dfr12)
}

}

return(dfr12)

}

79 changes: 79 additions & 0 deletions _FUN/load_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# import data function
# (C) 2018 Vladimir Zhbanko
# -------------------------
# Import Data to R
# -------------------------
#' Load Data Function
#' https://www.udemy.com/self-learning-trading-robot/?couponCode=LAZYTRADE7-10
#'
#' Function imports file and change date column type. Function return the dataframe with trade data.
#' -> update: data_deepth argument was added to optimize code and separate data needed to train and score the model
#'
#' @param path_terminal - path to the MT4 terminal, string
#' @param trade_log_file - csv file name where the data is stored, without ".csv"
#' @param time_period - data periodicity in minutes, can be 1, 15, 60
#' @param data_deepth - collected data deepth in rows. describe how many rows in original file to read
#'
#' @return - dataframe with asset data in columns where X1 column is in a POSIXct format
#' @export
#'
#' @examples
#' prices <- load_data(path_terminal = "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/",
#' trade_log_file = "AI_CP",
#' time_period = 1,
#' data_deepth = "14200")
#'
load_data <- function(path_terminal, trade_log_file, time_period = 1, data_deepth = 14200){
# path_terminal <- "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/"
# path_terminal <- file.path(getwd(), "test_data")
# trade_log_file <- "AI_CP"
# trade_log_file <- "AI_Macd"
# time_period <- 1
# data_deepth <- "100000"
require(tidyverse)
require(lubridate)
DFT1 <- try(read_csv(file = file.path(path_terminal, paste0(trade_log_file, time_period, "-", data_deepth, ".csv")),
col_names = F),
silent = TRUE)
if(class(DFT1)[1] == "try-error") {stop("Error reading file. File with trades may not exist yet!",
call. = FALSE)}
if(!nrow(DFT1)==0){
# data frame preparation
DFT1$X1 <- ymd_hms(DFT1$X1)
if(trade_log_file == "AI_CP"){
## divide JPY pairs by 100
DFT2 <- DFT1[ , c(8,10,18,22,24,25,26)]/100
DFT3 <- DFT1[, -c(8,10,18,22,24,25,26)] %>% bind_cols(DFT2) %>% select(X1,X2,X3,X4,X5,X6,X7,X8,
X9,X10,X11,X12,X13,X14,X15,
X16,X17,X18,X19,X20,X21,X22,
X23,X24,X25,X26,X27,X28,X29)
return(DFT3)
}

return(DFT1)
} else {
stop("Data log is empty!", call. = FALSE)
}

}





















Loading

0 comments on commit 941ad04

Please sign in to comment.