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 / 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 / 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 / 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 / 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 / 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,