Skip to content

Commit 941ad04

Browse files
big update
1 parent c0a05f4 commit 941ad04

File tree

104 files changed

+197034
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+197034
-0
lines changed

RESEARCH/2019-03-24-Result-100-60.rds

383 Bytes
Binary file not shown.

RESEARCH/2019-03-24-Result-125-1.rds

383 Bytes
Binary file not shown.

RESEARCH/2019-03-24-Result-125-15.rds

383 Bytes
Binary file not shown.

RESEARCH/2019-03-24-Result-125-60.rds

383 Bytes
Binary file not shown.

RESEARCH/2019-03-24-Result-75-1.rds

383 Bytes
Binary file not shown.

RESEARCH/2019-03-24-Result-75-15.rds

383 Bytes
Binary file not shown.

RESEARCH/2019-03-24-Result-75-60.rds

383 Bytes
Binary file not shown.

RESEARCH/SelfLearn-Research.R

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# https://www.udemy.com/self-learning-trading-robot/?couponCode=LAZYTRADE7-10
66
# Script to gather financial data, transform it and to perform
77
# Supervised Deep Learning Classification Modelling
8+
#
9+
# Purpose of this script is to simulate results in the several possible variants
10+
# NOTE: delete all previously create models from the folder R_selflearning/model after updating h2o engine
811
#
912
# load libraries to use and custom functions
1013
library(tidyverse)
@@ -21,6 +24,7 @@ source("C:/Users/fxtrams/Documents/000_TradingRepo/R_selflearning/self_learn_ai_
2124
source("C:/Users/fxtrams/Documents/000_TradingRepo/R_selflearning/test_model.R")
2225
#absolute path to store model objects (useful when scheduling tasks)
2326
path_model <- "C:/Users/fxtrams/Documents/000_TradingRepo/R_selflearning/model"
27+
2428
h2o.init()
2529

2630
### Create For loop to test possible outcomes and test those strategies
@@ -75,6 +79,7 @@ files_to_analyse <-list.files(file.path(getwd(),"RESEARCH/"), pattern="*.rds", f
7579
# e.g.: str_extract(files_to_analyse[2], "(?<=Result-)(.*)(?=.rds)")
7680
#
7781
for (FILE in files_to_analyse) {
82+
#FILE <- files_to_analyse[3]
7883
#extract nbars from the file name, note we use only those of the latest date
7984
num_bars <- FILE %>% str_extract(paste0("(?<=",Sys.Date(),"-Result-)(.*)(?=.rds)"))
8085
if(!exists("summary_file")){

_FUN/create_labelled_data.R

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## FUNCTION create_labelled_data
2+
## PURPOSE: function gets price data in each column
3+
## it is splitting this data by periods and transposes the data.
4+
## additionally it is label the data based on the simple logic assigning it to 2 categories based on the difference
5+
## between beginning and end of the vector
6+
## finally it is stacking all data and joining everything into the table
7+
8+
## TEST:
9+
# library(tidyverse)
10+
# library(lubridate)
11+
# pathT2 <- "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/"
12+
# prices <- read_csv(file.path(pathT2, "AI_CP1.csv"), col_names = F)
13+
# prices$X1 <- ymd_hms(prices$X1)
14+
# write_rds(prices, "test_data/prices.rds")
15+
16+
#' Create labelled data
17+
#' https://www.udemy.com/self-learning-trading-robot/?couponCode=LAZYTRADE7-10
18+
#'
19+
#' @param x - data set containing a table where 1st column is a Time index and other columns containing financial asset price values
20+
#' @param n - number of rows we intend to split and transpose the data
21+
#' @param type - type of the label required. Can be either "classification" or "regression". "classification" will return either "BU" or "BE",
22+
#' "regression" will return the difference between first value and the last value in each row
23+
#'
24+
#' @return function returns transposed data. One column called 'LABEL' indicate achieved value of the label.
25+
#' Transposed values from every column are stacked one to each other
26+
#'
27+
#' @export
28+
#'
29+
#' @examples
30+
create_labelled_data <- function(x, n = 50, type = "regression"){
31+
require(tidyverse)
32+
#source("C:/Users/fxtrams/Documents/000_TradingRepo/R_selflearning/_FUN/load_data.R")
33+
#x <- load_data(path_terminal = "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/", trade_log_file = "AI_CP", time_period = 1, data_deepth = "50000")
34+
#x <- read_rds("_TEST_DATA/price_dataset.rds")
35+
#n <- 100
36+
#type <- "classification"
37+
#type <- "regression"
38+
#
39+
nr <- nrow(x)
40+
dat11 <- x %>%
41+
# remove column 1 with data and time information
42+
select(-1) %>%
43+
# split dataset into several objects each containing n rows (it will be a list)
44+
split(rep(1:ceiling(nr/n), each=n, length.out=nr)) #list
45+
# remove last element of the list
46+
dat11[length(dat11)] <- NULL
47+
48+
# operations within the list
49+
for (i in 1:length(dat11)) {
50+
#i <- 2
51+
if(type == "classification"){
52+
53+
# classify by 2 classes 'BU', 'BE'
54+
if(!exists("dfr12")){
55+
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal") %>%
56+
mutate(LABEL = ifelse(.[[1]]>.[[n]], "BU", "BE"))} else {
57+
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal") %>%
58+
mutate(LABEL = ifelse(.[[1]]>.[[n]], "BU", "BE")) %>%
59+
bind_rows(dfr12)
60+
}
61+
} else if(type == "regression"){
62+
# add label with numeric difference {in pips}
63+
# i <- 1
64+
if(!exists("dfr12")){
65+
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal", verbose =F) %>%
66+
mutate(LABEL = 10000*(.[[1]]-.[[n]]))} else {
67+
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal", verbose =F) %>%
68+
mutate(LABEL = 10000*(.[[1]]-.[[n]])) %>%
69+
#oldest data will be on top of the dataframe!
70+
bind_rows(dfr12)
71+
}
72+
73+
74+
}
75+
}
76+
77+
return(dfr12)
78+
79+
}

_FUN/create_transposed_data.R

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## FUNCTION create_transposed_data
2+
## PURPOSE: function gets indicator data in each column
3+
## it is splitting this data by periods and transpose the data.
4+
## additionally it is label the data based on the simple logic assigning it to 2 categories based on the difference
5+
## between beginning and end of the vector
6+
## finally it is stacking all data and joining everything into the table
7+
8+
## TEST:
9+
# library(tidyverse)
10+
# library(lubridate)
11+
# pathT2 <- "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/"
12+
# macd <- read_csv(file.path(pathT2, "AI_Macd1.csv"), col_names = F)
13+
# macd$X1 <- ymd_hms(macd$X1)
14+
# write_rds(macd, "test_data/macd.rds")
15+
16+
#' Create Transposed Data
17+
#' https://www.udemy.com/self-learning-trading-robot/?couponCode=LAZYTRADE7-10
18+
#'
19+
#' @param x - data set containing a table where 1st column is a Time index and other columns containing financial asset indicator values
20+
#' @param n - number of rows we intend to split and transpose the data
21+
#'
22+
#' @return function returns transposed data. Transposed values from every column are stacked one to each other
23+
#' @export
24+
#'
25+
#' @examples
26+
#'
27+
create_transposed_data <- function(x, n = 50){
28+
require(tidyverse)
29+
#source("C:/Users/fxtrams/Documents/000_TradingRepo/R_selflearning/_FUN/load_data.R")
30+
#n <- 100
31+
#x <- load_data(path_terminal = "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/", trade_log_file = "AI_Macd", time_period = 1, data_deepth = "50000")
32+
#x <- read_rds("_TEST_DATA/indicator_dataset.rds")
33+
nr <- nrow(x)
34+
dat11 <- x %>% select(-1) %>% split(rep(1:ceiling(nr/n), each=n, length.out=nr)) #list
35+
dat11[length(dat11)] <- NULL
36+
37+
# operations within the list
38+
for (i in 1:length(dat11)) {
39+
#i <- 1
40+
41+
if(!exists("dfr12")){
42+
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal", verbose =F) } else {
43+
dfr12 <- dat11[i] %>% as.data.frame() %>% t() %>% as_tibble(.name_repair = "universal", verbose =F) %>% bind_rows(dfr12)
44+
}
45+
46+
}
47+
48+
return(dfr12)
49+
50+
}
51+

_FUN/load_data.R

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# import data function
2+
# (C) 2018 Vladimir Zhbanko
3+
# -------------------------
4+
# Import Data to R
5+
# -------------------------
6+
#' Load Data Function
7+
#' https://www.udemy.com/self-learning-trading-robot/?couponCode=LAZYTRADE7-10
8+
#'
9+
#' Function imports file and change date column type. Function return the dataframe with trade data.
10+
#' -> update: data_deepth argument was added to optimize code and separate data needed to train and score the model
11+
#'
12+
#' @param path_terminal - path to the MT4 terminal, string
13+
#' @param trade_log_file - csv file name where the data is stored, without ".csv"
14+
#' @param time_period - data periodicity in minutes, can be 1, 15, 60
15+
#' @param data_deepth - collected data deepth in rows. describe how many rows in original file to read
16+
#'
17+
#' @return - dataframe with asset data in columns where X1 column is in a POSIXct format
18+
#' @export
19+
#'
20+
#' @examples
21+
#' prices <- load_data(path_terminal = "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/",
22+
#' trade_log_file = "AI_CP",
23+
#' time_period = 1,
24+
#' data_deepth = "14200")
25+
#'
26+
load_data <- function(path_terminal, trade_log_file, time_period = 1, data_deepth = 14200){
27+
# path_terminal <- "C:/Program Files (x86)/FxPro - Terminal2/MQL4/Files/"
28+
# path_terminal <- file.path(getwd(), "test_data")
29+
# trade_log_file <- "AI_CP"
30+
# trade_log_file <- "AI_Macd"
31+
# time_period <- 1
32+
# data_deepth <- "100000"
33+
require(tidyverse)
34+
require(lubridate)
35+
DFT1 <- try(read_csv(file = file.path(path_terminal, paste0(trade_log_file, time_period, "-", data_deepth, ".csv")),
36+
col_names = F),
37+
silent = TRUE)
38+
if(class(DFT1)[1] == "try-error") {stop("Error reading file. File with trades may not exist yet!",
39+
call. = FALSE)}
40+
if(!nrow(DFT1)==0){
41+
# data frame preparation
42+
DFT1$X1 <- ymd_hms(DFT1$X1)
43+
if(trade_log_file == "AI_CP"){
44+
## divide JPY pairs by 100
45+
DFT2 <- DFT1[ , c(8,10,18,22,24,25,26)]/100
46+
DFT3 <- DFT1[, -c(8,10,18,22,24,25,26)] %>% bind_cols(DFT2) %>% select(X1,X2,X3,X4,X5,X6,X7,X8,
47+
X9,X10,X11,X12,X13,X14,X15,
48+
X16,X17,X18,X19,X20,X21,X22,
49+
X23,X24,X25,X26,X27,X28,X29)
50+
return(DFT3)
51+
}
52+
53+
return(DFT1)
54+
} else {
55+
stop("Data log is empty!", call. = FALSE)
56+
}
57+
58+
}
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+

0 commit comments

Comments
 (0)