Skip to content

Instantly share code, notes, and snippets.

View wlinInspire's full-sized avatar
🏠
Working from home

Wei Lin wlinInspire

🏠
Working from home
View GitHub Profile
@wlinInspire
wlinInspire / place_order.R
Last active August 4, 2019 04:52
How to Build an Automated Trading System using R
library(IBrokers)
tws <- twsConnect(verbose = FALSE)
symbol <- 'ABC'
quantity <- 100
lmtPrice <- 10
placeOrder(twsconn=tws,
Contract=twsSTK(symbol),
Order=twsOrder(reqIds(tws),
"BUY",
quantity,
@wlinInspire
wlinInspire / fetch_email_id.R
Last active August 4, 2019 04:56
How to Build an Automated Trading System using R
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()
@wlinInspire
wlinInspire / trading_loop.R
Created August 4, 2019 05:08
How to Build an Automated Trading System using R
while(hour(Sys.time()) < 16) {
# check email
try(get_alert_email(), silent = TRUE)
# Place order if needed
tws_order()
Sys.sleep(5)
}
@wlinInspire
wlinInspire / cron.sh
Created August 4, 2019 05:43
How to Build an Automated Trading System using R
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
@wlinInspire
wlinInspire / lw.R
Last active August 25, 2019 02:30
10 R Functions to Boost Your Productivity
lw <- function(v) {
if (sum(v, na.rm = TRUE) == 0) {
1 / length(v)
} else {
v / sum(v, na.rm = TRUE)
}
}
@wlinInspire
wlinInspire / quick_date.R
Created August 11, 2019 03:08
10 R Tricks to Boost Your Productivity
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, …)
}
@wlinInspire
wlinInspire / cc.R
Last active August 11, 2019 03:28
10 R Tricks to Boost Your Productivity
cc <- function(df) {
print(df %>% is.na() %>% colSums())
}
cd <- function(df) {
print(df %>% is.na() %>% colMeans())
}
@wlinInspire
wlinInspire / h2o.R
Created August 11, 2019 03:58
10 R Tricks to Boost Your Productivity
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)
}
@wlinInspire
wlinInspire / km_curve.R
Last active August 19, 2019 03:28
Calculating Life Time Value for Subscription Products
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
@wlinInspire
wlinInspire / survival_rurve_fit.R
Last active August 19, 2019 03:32
Calculating Life Time Value for Subscription Products
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