This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(IBrokers) | |
tws <- twsConnect(verbose = FALSE) | |
symbol <- 'ABC' | |
quantity <- 100 | |
lmtPrice <- 10 | |
placeOrder(twsconn=tws, | |
Contract=twsSTK(symbol), | |
Order=twsOrder(reqIds(tws), | |
"BUY", | |
quantity, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mail_query <- paste0('from:"@xxx.com" ', | |
'{subject:trade subject:sell} ', | |
'after:', Sys.Date()) | |
list <- gmailr::threads( | |
search = mail_query, | |
include_spam_trash = TRUE, num_results = 99999) | |
ids <- sapply(list, function(x) sapply(x$threads, function(y) y$id)) %>% | |
unlist() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while(hour(Sys.time()) < 16) { | |
# check email | |
try(get_alert_email(), silent = TRUE) | |
# Place order if needed | |
tws_order() | |
Sys.sleep(5) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 9 * * 1-5 /usr/lib/R/bin/R -f '/home/ubuntu/TraderR/script/trading_robot.R' > '/home/ubuntu/TraderR/script/trading_robot.log' 2>&1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lw <- function(v) { | |
if (sum(v, na.rm = TRUE) == 0) { | |
1 / length(v) | |
} else { | |
v / sum(v, na.rm = TRUE) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
quick_date <- function(x, …) { | |
if (anyDuplicated(x)) { | |
ux <- unique(x) | |
idx <- match(x, ux) | |
y <- as.Date.character(ux, …) | |
return(y[idx]) | |
} | |
as.Date.character(x, …) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cc <- function(df) { | |
print(df %>% is.na() %>% colSums()) | |
} | |
cd <- function(df) { | |
print(df %>% is.na() %>% colMeans()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
h2o_start <- function() { | |
h2o::h2o.init(nthreads = -1, enable_assertions = FALSE, min_mem_size = '8g', | |
strict_version_check = FALSE, port = 54321) | |
} | |
h2o_stop <- function() { | |
h2o::h2o.shutdown(prompt = FALSE) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(survival) | |
library(data.table) | |
library(ggplot2) | |
data <- fread('https://raw.githubusercontent.com/IBM/invoke-wml-using-cognos-custom-control/master/data/Telco-Customer-Churn.csv') | |
churn_data <- data[, .(churn_flag = ifelse(Churn == 'Yes', 1, 0), tenure)] | |
km_curve <- survfit(Surv(tenure, churn_flag) ~ 1, data=churn_data) | |
# Calculate Survival and Churn Rate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(survival) | |
library(data.table) | |
library(ggplot2) | |
data = fread('https://raw.githubusercontent.com/IBM/invoke-wml-using-cognos-custom-control/master/data/Telco-Customer-Churn.csv') | |
churn_data <- data[, .(churn_flag = ifelse(Churn == 'Yes', 1, 0), tenure)] | |
km_curve <- survfit(Surv(tenure, churn_flag) ~ 1, data=churn_data) | |
# Calculate Survival and Churn Rate |
OlderNewer