Created
April 12, 2017 07:14
-
-
Save berkorbay/35e79da3a29e305d336334abf241a414 to your computer and use it in GitHub Desktop.
This is an example code for BOUN58D project.
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
#Get the required libraries, install first if not installed. | |
options(dplyr.width=Inf) | |
library(boun58d) #Copy paste the necessary function if you cannot install this from github | |
library(tidyverse) | |
library(tidyquant) | |
library(rpart) | |
library(rpart.plot) | |
library(rattle) | |
set.seed(58) #Don't forget to set the seed for reproducibility | |
#Add some commments to your code so others would understand | |
start_time = as.numeric(as.POSIXct("2015-08-01 00:00:00",tz="UTC")) | |
end_time = as.numeric(as.POSIXct("2017-04-01 00:00:00",tz="UTC")) | |
period_interval <- 86400 #Daily | |
lookback_period <- 365 | |
the_currency <- "BTC_ETH" | |
#Get the Data | |
chartData <- get_from_poloniex("returnChartData",parameters=list(currencyPair = the_currency,start=start_time,end=end_time,period=period_interval)) | |
#Convert chartData into a data.table data frame with data.table::rbindlist | |
chartData <- data.table::rbindlist(chartData) | |
analysis_df <- | |
chartData %>% | |
transmute(numdate = date,date=as.POSIXct(date,origin="1970-01-01",tz="UTC"),close=close) %>% | |
mutate(logreturn=log(close/lag(close,1)) #calculate log-returns | |
) %>% | |
filter(!is.na(logreturn)) %>% #remove the NA generated by first calculation | |
mutate( | |
ma5=rollmean(logreturn,5,align="right",fill=NA), #calculate MA5 | |
ma20=rollmean(logreturn,20,align="right",fill=NA) #calculate MA20 | |
) %>% | |
tbl_df | |
#Arrange the data frame to suit with forecasting needs | |
#Move predictors to the to be predicted values so we can add the predictions right next to actual values | |
forecast_df <- | |
analysis_df %>% | |
mutate(s0=lag(close,1),ma5=lag(ma5,1),ma20=lag(ma20,1)) %>% #move predictors one day ahead | |
filter(complete.cases(.)) %>% #remove NA | |
arrange(desc(numdate)) #Rearrange by descending date | |
#Determine the dates to forecast and select predictors | |
oos_forecast_df <- forecast_df %>% slice(1:30) %>% select(date,ma5,ma20) | |
#Initiate the predicted log-return variable | |
pred_logreturn <- 0 | |
for(i in 1:30){ | |
#Run the model with the available information for each day | |
rpart_model <- rpart(logreturn ~ ma5 + ma20,data= forecast_df %>% filter(date < oos_forecast_df$date[i])) | |
#Get the logreturn prediction as an output of the model | |
pred_logreturn[i] <- predict(rpart_model,newdata=oos_forecast_df %>% slice(i) %>% select(ma5,ma20)) | |
} | |
#Merge the findings with our forecast_df | |
results_df<- | |
oos_forecast_df %>% | |
transmute(date,pred_logreturn=pred_logreturn) %>% | |
left_join(forecast_df,.,by="date") %>% | |
mutate( | |
pred_close = s0*exp(pred_logreturn), | |
position=ifelse(pred_close>=s0,"long","short"), | |
rpe=round((pred_close-close)/close,4), | |
ape=abs(pred_close-close), | |
pnl=exp(logreturn)^ifelse(pred_close >= s0,1,-1) | |
) %>% | |
tbl_df | |
#This graph is prediction vs realization | |
ggplot(data=results_df %>% slice(1:50), aes(x=date)) + | |
geom_line(aes(y=close)) + | |
geom_line(aes(y=pred_close),color="red") + | |
labs(title="Prediction vs Realization") | |
#This graph is ape | |
ggplot(data = results_df %>% filter(!is.na(ape)),aes(x=date,y=ape)) + geom_line() + | |
labs(title="Absolute Pricing Error") | |
#This graph is rpe | |
ggplot(data = results_df %>% filter(!is.na(rpe)),aes(x=date,y=rpe)) + geom_line() + | |
labs(title="Relative Pricing Error") | |
#This graph is pnl | |
ggplot(data = results_df %>% filter(!is.na(pnl)),aes(x=date,y=pnl)) + geom_line() + | |
labs(title="Daily Profit and Loss") | |
cumulative_pnl_data <- | |
results_df %>% | |
filter(!is.na(pnl)) %>% | |
transmute(date,pnl) %>% | |
add_row(date=min(.$date - 86400),pnl=1) %>% | |
arrange(date) %>% | |
mutate(cum_pnl = cumprod(pnl)*100) | |
ggplot(data=cumulative_pnl_data,aes(x=date,y=cum_pnl)) + geom_line() + | |
labs(title="Cumulative PnL if started with 100 BTC/ETH") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment