Skip to content

Instantly share code, notes, and snippets.

@c3h3
Created October 24, 2016 10:37
Show Gist options
  • Save c3h3/357d43b102addea4e07b50df0076da2f to your computer and use it in GitHub Desktop.
Save c3h3/357d43b102addea4e07b50df0076da2f to your computer and use it in GitHub Desktop.
library(quantstrat)
library(magrittr)
Xt = getSymbols('2498.TW', auto.assign = F)
# 1st Step: Attach MA indicators onto symbol
Xt$MA5 = Xt %>% Cl %>% SMA(5)
Xt$MA60 = Xt %>% Cl %>% SMA(60)
# 2nd Step: Compute crossover events
goldenCross = sigCrossover(label="goldenCross",
data=Xt,
columns=c("MA5","MA60"),
relationship="gt")
deathCross = sigCrossover(label="deathCross",
data=Xt,
columns=c("MA5","MA60"),
relationship="lt")
# 3rd Step: Plot Events on candlestick line
liftRatio = 0.02
Xt %>% tail(600) %>% chartSeries(TA = NULL)
# Xt %>% chartSeries(TA = NULL)
addSMA(5,col = 4)
addSMA(60,col = 6)
addMACD(fast = 5,slow = 60)
# addTA(Lo(Xt)[!is.na(goldenCross)]*(1-liftRatio),on=1,type="p",col=2,pch=24,bg="red")
# addTA(Hi(Xt)[!is.na(deathCross)]*(1+liftRatio),on=1,type="p",col=3,pch=25,bg="green")
goldenCross %>% is.na %>% not %>% `[`(Lo(Xt),.) %>% `*`(1-liftRatio) %>% addTA(on=1,type="p",col=2,pch=24,bg="red")
deathCross %>% is.na %>% not %>% `[`(Hi(Xt),.) %>% `*`(1+liftRatio) %>% addTA(on=1,type="p",col=3,pch=25,bg="green")
#############################################
## Forward Sampling Window
#############################################
# library(devtools)
# install_github("c3h3/QuantitativeBacktestingTools")
library(QuantitativeBacktestingTools)
win_size = 30
goldenCrossDates = goldenCross %>% which %>% index(Xt)[.]
long_gcfswXtDf =
Xt %>% Pt2Rt %>%
ForwardSlidingWindow(win_size = win_size) %>%
FswXt2Df(filterDatetimes=goldenCrossDates,longFormat=T)
long_gcfswXtDf %>% na.omit %>% tail
#############################################
## New-School ETL + Data Viz
#############################################
library(ggplot2)
long_gcfswXtDf %>% ggplot(mapping = aes(x=t,y=Rt,group=datetime,color=datetime)) + geom_line()
long_gcfswXtDf %>% head(50)
#############################################
## EDA + Viz: How to choose stop & limit?
#############################################
max_min_long_gcfswXtDf = long_gcfswXtDf %>% group_by(datetime) %>% summarise(max=max(Rt)-1,
maxIdx=which.max(Rt),
min=1-min(Rt),
minIdx=which.min(Rt))
max_min_long_gcfswXtDf %>% ggplot()+geom_histogram(mapping = aes(min),binwidth = 0.01)
max_min_long_gcfswXtDf %>% ggplot()+geom_histogram(mapping = aes(minIdx))
max_min_long_gcfswXtDf %>% ggplot()+geom_histogram(mapping = aes(max),binwidth = 0.01)
max_min_long_gcfswXtDf %>% ggplot()+geom_histogram(mapping = aes(maxIdx))
#############################################
## Simulation Stop-Limit Prices
#############################################
expsResultsDf =
long_gcfswXtDf %>% doSeriesOfSimulateStopLimitPrices()
expsResultsDf %>%
group_by(hitType,limitRatio,stopRatio) %>%
summarise(n=n(),price=mean(hitPrice-1))
expsResultsDf %>%
group_by(hitType,limitRatio,stopRatio) %>%
summarise(n=n(),price=mean(hitPrice-1)) %>%
group_by(limitRatio,stopRatio) %>% summarise(ex=sum(n*price))
expsResultsDf %>%
group_by(hitType,limitRatio,stopRatio) %>%
summarise(n=n(),price=mean(hitPrice-1)) %>%
group_by(limitRatio,stopRatio) %>% summarise(ex=sum(n*price)) %>%
ggplot(mapping = aes(x=limitRatio,y=stopRatio,z=ex)) + geom_raster(aes(fill = ex)) + geom_contour(colour = "white")
# expsResultsDf %>%
# group_by(hitType,limitRatio,stopRatio) %>%
# summarise(n=n(),price=mean(hitPrice-1)) %>%
# group_by(limitRatio,stopRatio) %>% summarise(ex=sum(n*price)) %>%
# ggplot(mapping = aes(x=limitRatio,y=stopRatio,z=ex)) + stat_contour(geom="polygon",aes(fill=..level..))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment