Skip to content

Instantly share code, notes, and snippets.

@lockefox
Last active August 29, 2015 14:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lockefox/2a502aadbfbffd54a6c7 to your computer and use it in GitHub Desktop.
library(RODBC)
library(jsonlite)
library(data.table)
library(grid)
library(reshape)
library(ggplot2)
library(sde)
library(zoo)
library(plyr)
#### SCRIPT GLOBALS ####
locationID <- 30000142 #JITA=30000142
date_range <- 100
typeid <- 29668
#PLEX = 29668
predict.start <- 50
predict.length <- 50
predict.repeat <- 2000
rolling_window <- 10
plot.path <- getwd()
plot.width <- 1600
plot.height <- 900
#### SDE VALS ####
# Can be replaced with RODBC connector and lookups
# Manually set for wider distribution
#################
regionID <- 10000002 #THE FORGE=10000002
typeName <- "PLEX"
solarSystemName <- "Jita"
#### FETCH CREST DATA ####
CREST_addr <- paste0("https://public-crest.eveonline.com/market/",regionID,"/types/",typeid,"/history/")
print("FETCHING CREST DATA")
CREST_json <- fromJSON(readLines(CREST_addr))
CREST_data <- CREST_json$items
CREST_data$date <- as.Date(CREST_data$date)
#### SPECIAL GROUPINGS ####
patch_list <- as.POSIXlt( c("2014-12-09 13:00", "2015-01-13 13:00", "2015-02-17 13:00",
"2015-03-24 13:00", "2015-04-28 13:00", "2015-06-02 13:00",
"2015-07-07 13:00"
))
date.min <- Sys.Date() - date_range
patch_list.aux <- c()
for (row in 1:NROW(patch_list)){ #Find the patches in the date range
tmpdate <- as.Date(patch_list[row])
if (tmpdate > date.min){
patch_list.aux <- c(patch_list.aux, as.character(patch_list[row]))
}
}
patch_list.aux <- as.POSIXlt(patch_list.aux)
#### CREST PROCESSING ####
CREST_data$rollingAvg <- rollmean(CREST_data$avgPrice,k=rolling_window,na.pad=TRUE,align="right")
CREST_data$diff <- CREST_data$avgPrice - CREST_data$rollingAvg
CREST_data$perdiff <- CREST_data$diff/CREST_data$avgPrice
CREST_data$rollSD_diff <- rollapply(CREST_data$diff,width=rolling_window,FUN=sd,fill=NA,align="right")
CREST_data$rollSD_per <- rollapply(CREST_data$perdiff,width=rolling_window,FUN=sd,fill=NA,align="right")
var_cdf <- ecdf(CREST_data$rollSD_per)
CREST_data <- subset(CREST_data, date > date.min)
CREST_predict.low <- rep(NA, NROW(CREST_data)-predict.start-1)
CREST_predict.med <- rep(NA, NROW(CREST_data)-predict.start-1)
CREST_predict.hi <- rep(NA, NROW(CREST_data)-predict.start-1)
predict.window <- as.Date(patch_list.aux[-2])
minDate <- max(CREST_data$date)-predict.start
lastPrice <- CREST_data$avgPrice[CREST_data$date==minDate]
CREST_predict <- subset(CREST_data, date > minDate-30 & date < minDate)
lowVar <- quantile(CREST_predict$rollSD_per,0.25)
medVar <- quantile(CREST_predict$rollSD_per,0.50)
hiVar <- quantile(CREST_predict$rollSD_per,0.75)
lowSig <- (lowVar-mean(CREST_data$rollSD_per,na.rm=TRUE))/sd(CREST_data$rollSD_per,na.rm=TRUE)
medSig <- (medVar-mean(CREST_data$rollSD_per,na.rm=TRUE))/sd(CREST_data$rollSD_per,na.rm=TRUE)
hiSig <- (hiVar -mean(CREST_data$rollSD_per,na.rm=TRUE))/sd(CREST_data$rollSD_per,na.rm=TRUE)
predict_matrix.low <- matrix(data=NA, nrow=predict.length+1, ncol=predict.repeat)
predict_matrix.med <- matrix(data=NA, nrow=predict.length+1, ncol=predict.repeat)
predict_matrix.hi <- matrix(data=NA, nrow=predict.length+1, ncol=predict.repeat)
for(iter in 1:predict.repeat){
predict_matrix.low[,iter] <- GBM(lastPrice,0,lowSig,lowVar,predict.length)
predict_matrix.med[,iter] <- GBM(lastPrice,0,medSig,medVar,predict.length)
predict_matrix.hi [,iter] <- GBM(lastPrice,0,hiSig ,hiVar ,predict.length)
}
#TODO: summarize predictions#
#CREST_predict.low <- c(CREST_predict.low, predict_matrix.low[,1])
#CREST_predict.med <- c(CREST_predict.med, predict_matrix.low[,1])
#CREST_predict.hi <- c(CREST_predict.hi , predict_matrix.low[,1])
lowVar <- quantile(CREST_predict$perdiff,0.25) / (rolling_window/2)
medVar <- quantile(CREST_predict$perdiff,0.50) / (rolling_window/2)
hiVar <- quantile(CREST_predict$perdiff,0.75) / (rolling_window/2)
for(step in 0:predict.length){#Run Exponential steps with local variance values
exp_low <- lastPrice * (1+lowVar)^step
exp_med <- lastPrice * (1+medVar)^step
exp_hi <- lastPrice * (1+hiVar )^step
CREST_predict.low <- c(CREST_predict.low, exp_low)
CREST_predict.med <- c(CREST_predict.med, exp_med)
CREST_predict.hi <- c(CREST_predict.hi , exp_hi )
}
lenPredict <- NROW(CREST_predict.low)
lenTarget <- NROW(CREST_data$avgPrice)
if(lenPredict < lenTarget){ #pad rows
CREST_predict.low <- c(CREST_predict.low, rep(NA,lenTarget-lenPredict))
CREST_predict.med <- c(CREST_predict.med, rep(NA,lenTarget-lenPredict))
CREST_predict.hi <- c(CREST_predict.hi , rep(NA,lenTarget-lenPredict))
}
CREST_data$predict.low <- CREST_predict.low
CREST_data$predict.med <- CREST_predict.med
CREST_data$predict.hi <- CREST_predict.hi
CREST_stack <- melt.data.frame(CREST_data, id.vars=c("date"), measure.vars=c("avgPrice","predict.low","predict.med","predict.hi"))
#### CREST PLOTTING ####
plot.title <- paste0("CREST History - Predictor - ",typeName," - ",Sys.Date())
plot.name <- paste0("CREST-Predictor_",typeName,"_",Sys.Date(),".png")
theme_dark <- function( ... ) {
theme(
text = element_text(color="gray90"),
title = element_text(size=rel(2),hjust=0.05,vjust=3.5),
axis.title.x = element_text(size=rel(1),hjust=0.5, vjust=0),
axis.title.y = element_text(size=rel(1),hjust=0.5, vjust=1.5),
plot.margin = unit(c(2,1,1,1), "cm"),
plot.background=element_rect(fill="gray8",color="gray8"),
panel.background=element_rect(fill="gray10",color="gray10"),
panel.grid.major = element_line(colour="gray17"),
panel.grid.minor = element_line(colour="gray12"),
axis.line = element_line(color = "gray50"),
plot.title = element_text(color="gray80"),
axis.title = element_text(color="gray70"),
axis.text = element_text(color="gray50",size=rel(1.1)),
legend.key = element_rect(fill="gray8",color="gray8"),
legend.background = element_rect(fill="gray8"),
legend.title = element_text(size=rel(0.6)),
legend.text = element_text(size=rel(1.1)),
strip.background = element_rect(fill="gray1"),
strip.text = element_text(size=rel(1.2))
) + theme(...)
}
#png(plot.name, width=plot.width, height=plot.height)
GG <- ggplot(CREST_stack, aes(x=date,y=value,color=variable))
GG <- GG + geom_line() + theme_dark()
GG <- GG + labs(title=plot.title, color="PriceKey", x="date", y="Price")
GG <- GG + geom_vline(xintercept=as.numeric(as.Date(patch_list.aux)),color="white")
print(GG)
#dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment