Learn R Programming

lazytrade

The goal of lazytrade is to keep all functions and scripts of the lazytrade educational project on UDEMY. Functions are providing an opportunity to learn Computer and Data Science using example of Algorithmic Trading. Please kindly not that this project was created for Educational Purposes only!

Installation

You can install the released version of lazytrade from CRAN with:

install.packages("lazytrade")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("vzhomeexperiments/lazytrade")

Several ideas explored in this package

  • Data manipulation and analysis of performed trades results
  • Reinforcement Learning for Automated Trading Risk Management
  • Data manipulation and preparation for Machine Learning (transposing, aggregation, lagging, etc)
  • Using Deep Learning for prediction of Market Types (Classification)
  • Using Deep Learning for prediction of future price change (Regression)
  • Strategy Tests simulations
  • Utility functions to generate passwords, initialization files, encryption of passwords, etc
  • Explored idea of building a model using random structures combined with an automated functional (strategy) test to improve model performance
  • Overall, all functions have working examples with relevant documented sample data included in the package

Example - prepare data for machine learning

This is a basic example which shows you how to solve a common problem:

library(lazytrade)
library(magrittr, warn.conflicts = FALSE)
## basic example code
# Convert a time series vector to matrix with 64 columns
macd_m <- seq(1:1000) %>% as.data.frame() %>% to_m(20)

head(macd_m, 2)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]    1    2    3    4    5    6    7    8    9    10    11    12    13    14
#> [2,]   21   22   23   24   25   26   27   28   29    30    31    32    33    34
#>      [,15] [,16] [,17] [,18] [,19] [,20]
#> [1,]    15    16    17    18    19    20
#> [2,]    35    36    37    38    39    40

Why is it useful? It is possible to convert time-series data into matrix data to do make modeling

Example - aggregate multiple log files and visualize results

Multiple log files could be joined into one data object

library(lazytrade)
library(readr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(magrittr)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

# files are located in the sample folders
DFOLDER <- system.file("extdata/RES", package = "lazytrade")

DFR <- opt_aggregate_results(path_data = DFOLDER)

This data object can be visualized

library(ggplot2)
opt_create_graphs(x = DFR, outp_path = dir,graph_type = 'bars')

Or just visualize results with time-series plot

opt_create_graphs(x = DFR, outp_path = dir,graph_type = 'ts')

Example - leverage Reinforcement Learning for Risk Management

Example below would generate RL policy based on the trade results achieved so far

library(dplyr)
library(ReinforcementLearning)
library(magrittr)

data(data_trades)
states <- c("tradewin", "tradeloss")
actions <- c("ON", "OFF")
control <- list(alpha = 0.7, gamma = 0.3, epsilon = 0.1)
rl_generate_policy(data_trades, states, actions, control)
#>           TradeState Policy
#> tradeloss  tradeloss     ON
#> tradewin    tradewin    OFF

Example - generating passwords for trading platforms login

Multiple trading accounts require passwords, package contains function that may easily generate random passwords:

library(lazytrade)
library(stringr)
library(magrittr)
library(openssl)
library(readr)

#generate 8digit password for trading platform
util_generate_password(salt = 'random text')
#>          .
#> 1 ac5cE049

Example - generate initialization files for MT4 platform

Facilitate generation of initialisation files:

library(lazytrade)

dir <- normalizePath(tempdir(),winslash = "/")

# test file to launch MT4 terminal with parameters
write_ini_file(mt4_Profile = "Default",
               mt4_Login = "12345678",
               mt4_Password = "password",
               mt4_Server = "BrokerServerName",
               dss_inifilepath = dir,
               dss_inifilename = "prod_T1.ini",
               dss_mode = "prod")

Notes to remind myself how to create R package

This readme file

What is special about using README.Rmd instead of just README.md? You can include R chunks like so:

summary(cars)
#>      speed           dist       
#>  Min.   : 4.0   Min.   :  2.00  
#>  1st Qu.:12.0   1st Qu.: 26.00  
#>  Median :15.0   Median : 36.00  
#>  Mean   :15.4   Mean   : 42.98  
#>  3rd Qu.:19.0   3rd Qu.: 56.00  
#>  Max.   :25.0   Max.   :120.00

You’ll still need to render README.Rmd regularly, to keep README.md up-to-date.

taken from https://r-pkgs.org/

Communicate about lifecycle changes

taken from https://lifecycle.r-lib.org/articles/communicate.html

Run Once:

usethis::use_lifecycle()

To insert badge:

Add badges in documentation topics by inserting one of:

#’ #’ #’

Generating Documentation

Title of the package

Create right title case for the title of the package By running this command… tools::toTitleCase("Learn computer and data science using algorithmic trading") the Title will become: “Learn Computer and Data Science using Algorithmic Trading”

Re-generating documentation

Run this code to re-generate documentation devtools::document()

Fixing License

Run this code to fix license: usethis::use_mit_license(name = "Vladimir Zhbanko")

Adding data to the package for internal tests

Run this code to add data to the folder data/ x <- sample(1000) usethis::use_data(x)

To update this data: x <- sample(2000) usethis::use_data(x, overwrite = T)

To convert character into time: mutate(across('X1', ~ as.POSIXct(.x, format = "%Y.%m.%d %H:%M:%S")))

Note: use option ’LazyLoad` to make data available only when user wants it always include LazyData: true in your DESCRIPTION. Note: to document dataset see https://stackoverflow.com/questions/2310409/how-can-i-document-data-sets-with-roxygen

Document dataset using the R script R/datasets.R

Use data in the function with data(x)

Adding files to the package

Place data like small files to the folder: inst/extdata

Adding examples to test package function

Tests setup first time

Run this command to setup tests ‘usethis::use_testthat()’

This will create a folder with the name tests

Inside this folder there will be another folder testthat.

Examples in Roxygen code

@examples …

code to execute during package checks

@examples

/donttest{

code to NOT execute during package checks

}

Testing a package

Create a test script

Run this command to create a new script with the test skeleton:

usethis::use_test("profit_factor.R")

Enrich the test script

Details:

  1. add libraries used for test
  2. add function context("profit_factor")
  3. add function test_that(“test description”, {test process})
  4. load data using function data(named_data_object)

Example:

library(testthat)
#> 
#> Attaching package: 'testthat'
#> The following object is masked from 'package:dplyr':
#> 
#>     matches
#> The following objects are masked from 'package:readr':
#> 
#>     edition_get, local_edition
#> The following objects are masked from 'package:magrittr':
#> 
#>     equals, is_less_than, not
library(dplyr)
library(magrittr)

context("profit_factor")

test_that("test value of the calculation", {

  data(profit_factor_data)

  DF_Stats <- profit_factor_data %>%
    group_by(X1) %>%
    summarise(PnL = sum(X5),
              NumTrades = n(),
              PrFact = util_profit_factor(X5)) %>%
    select(PrFact) %>%
    head(1) %>%
    pull(PrFact) %>%
    round(3)

  expect_equal(DF_Stats, 0.68)

})
#> Test passed 

Copy Link

Version

Install

install.packages('lazytrade')

Monthly Downloads

363

Version

0.5.4

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Vladimir Zhbanko

Last Published

July 17th, 2024

Functions in lazytrade (0.5.4)

evaluate_macroeconomic_event

Function used to evaluate market type situation by reading the file with Macroeconomic Events and writing a trigger to the trading robot
get_profit_factorDF

Function that returns the profit factors of the systems in a form of a DataFrame
create_transposed_data

Create Transposed Data
create_labelled_data

Create labelled data
mt_import_data

Import Market Type related Data to R from the Sandbox
mt_make_model

Function to train Deep Learning Classification model for Market Type recognition
indicator_dataset

Table with indicator dataset
rl_generate_policy

Function performs Reinforcement Learning using the past data to generate model policy
mt_evaluate

Function to score data and predict current market type using pre-trained classification model
macd_df

Table with one column indicator dataset
rl_generate_policy_mt

Function performs RL and generates model policy for each Market Type
test_data_pattern

Table with several columns containing indicator values and Label values
decrypt_mykeys

Function that decrypt encrypted content
macd_ML60M

Table with indicator and market type category used to train model
result_prev

Table with one column as result from the model prediction
macd_100

Table with indicator only used to train model, 128 col 1646 rows
result_R1

Table with aggregated trade results
mt_stat_evaluate

Function to prepare and score data, finally predict current market type using pre-trained classification model
mt_stat_transf

Perform Statistical transformation and clustering of Market Types on the price data
profit_factorDF

Table with Trade results samples
price_dataset_big

Table with price dataset, 30000 rows
policy_tr_systDF

Table with Market Types and sample of actual policy for those states
dlog

Create log difference distribution
to_m

Convert time series data to matrix with defined number of columns
price_dataset

Table with price dataset
trading_systemDF

Table with trade data and joined market type info
write_ini_file

Create initialization files to launch MT4 platform with specific configuration
x_test_model

Table with a dataset to test the Model
rl_write_control_parameters

Function to find and write the best control parameters.
rl_write_control_parameters_mt

Function to find and write the best control parameters.
rl_log_progress

Function to retrieve and help to log Q values during RL progress.
rl_log_progress_mt

Function to retrieve and help to log Q values during RL progress. This function is dedicated to the situations when Market Types are used as a 'states' for the Environment.
y

Table with indicators and price change which is used to train model
opt_create_graphs

Function to create summary graphs of the trading results
result_R

Table with predicted price change
rl_record_policy

Record Reinforcement Learning Policy.
util_find_file_with_code

R function to find file with specific code within it's content
opt_aggregate_results

Function to aggregate trading results from multiple folders and files
encrypt_api_key

Encrypt api keys
rl_record_policy_mt

Record Reinforcement Learning Policy for Market Types
write_command_via_csv

Write csv files with indicated commands to the external system
util_profit_factor

Calculate Profit Factor
util_generate_password

R function to generate random passwords for MT4 platform or other needs
profit_factor_data

Table with Trade results samples
util_find_pid

R function to find PID of active applications
aml_consolidate_results

Function to consolidate model test results
aml_test_model

Function to test the model and conditionally decide to update existing model for a single currency pair
aml_score_data

Function to score new data and predict change for each single currency pair
TradeStatePolicy

Table with Trade States and sample of actual policy for those states
aml_simulation

Function to simulate multiple input structures
aml_make_model

Function to train Deep Learning regression model for a single asset
aml_collect_data

Function to read, transform, aggregate and save data for further retraining of regression model for a single asset
import_data

Import Data file with Trade Logs to R.
DFR

Table with predicted price change
EURUSDM15X75

Table with indicator and price change dataset
check_if_optimize

Function check_if_optimize.
data_trades

Table with Trade results samples