Last active
March 17, 2016 03:31
-
-
Save lockefox/46efaaf77bb4eb253132 to your computer and use it in GitHub Desktop.
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
library(RODBC) | |
library(ggplot2) | |
library(grid) | |
library(reshape) | |
library(scales) | |
library(data.table) | |
library(quantmod) | |
library(jsonlite) | |
library(cowplot) | |
locationID <- 30000142 #JITA=30000142 | |
region_id = "10000002" | |
cutoff_date <- Sys.Date() | |
plot.width <- 1600 | |
#plot.height <- 900 | |
plot.height <- 900 | |
date_range <- 45 | |
title_date <- Sys.Date() | |
#chart_repo <- "F:/Prosper/Plots/" | |
chart_repo <- "C:/Users/Lockefox/Plots/" | |
chart_repo <- paste0(chart_repo,Sys.Date(),"_zoomin-",date_range,"/") | |
dir.create(chart_repo, showWarnings=FALSE) | |
title_date <- Sys.Date() | |
title_head <- "Buy/Sell Prices" | |
EC_ODBC <- "evemarketdata" | |
#EC_ODBC <- "randomboy_new" | |
patch_list.full <- as.POSIXlt( c("2015-09-29 13:00", #Vanguard | |
"2015-09-15 13:00", #Galatea | |
"2015-11-03 13:00", #Parallax | |
"2015-11-24 16:00", #Black Friday PLEX sale | |
"2015-10-20 05:00", #Bloody Omir Starts | |
"2015-12-08 13:00", #Frostline | |
"2016-01-12 13:00", #YC118.1 | |
"2016-01-18 15:10", #brainmeat | |
#"2016-02-05 19:45", | |
"2016-02-09 13:00", #YC118.2 | |
"2016-03-11 13:00" #YC118.3 | |
)) | |
patch_list.names <- c("Vanguard", | |
"Galatea", | |
"Parallax", | |
"Black Friday Sale", | |
"Crimson Harvest", | |
"Frostline", | |
"YC118.1", | |
"Skill Trading Devblog", | |
#"AUR Prices Released", | |
"YC118.2", | |
"YC118.3") | |
patch_list <- c() | |
patch_title <- c() | |
date.min <- Sys.Date() - date_range | |
for (row in 1:NROW(patch_list.full)){ #Find the patches in the date range | |
tmpdate <- as.Date(patch_list.full[row]) | |
if (tmpdate > date.min){ | |
patch_list <- c(patch_list, as.character(patch_list.full[row])) | |
patch_title <- c(patch_title, as.character(patch_list.names[row])) | |
} | |
} | |
x_intercepts <- as.POSIXlt(patch_list) | |
### FETCH DB STUFF ### | |
EC <- odbcConnect(EC_ODBC) | |
ec_q <- paste0("SELECT price_date AS `date`, price_time AS `hour`, locationid,", | |
"SUM(IF(buy_sell=1, price_best,0)) AS 'SellOrder', ", | |
"SUM(IF(buy_sell=1, order_volume,0)) AS 'SellVolume', ", | |
"SUM(IF(buy_sell=0, price_best,0)) AS 'BuyOrder', ", | |
"SUM(IF(buy_sell=0, order_volume,0)) AS 'BuyVolume', typeid ", | |
"FROM snapshot_evecentral ", | |
"WHERE typeID in (", | |
"34,35,36,37,38,39,40,11399,", #Minerals | |
#"25588,25589,25590,25591,25592,25593,25594,25595,25596,25597,25598,25599,25600,25601,25602,25603,25604,25605,25606,25607,25608,25609,25610,25611,25612,25613,25614,25615,25616,25617,25618,25619,25620,25621,25622,25623,25624,25625,", #Salvage | |
"16274,17887,17888,17889,4051,4246,4247,4312,16273,16272,16275,",#ice products | |
"16647,16648,16646,16649,16650,16651,16652,16653,",#R32/R64 | |
"2867,2868,2869,2870,2871,2872,2875,2876,", #PI4 | |
"16670,16671,16672,16673,16678,16679,16680,16681,16682,16683,17317,33359,33360,33361,33362,", #ADV mats | |
#"17476,17478,17480,22544,22546,22548,33697,37135,",#mining ships | |
"13267,3810,", #janitors and marines | |
"28756,28758,30486,30488,",#sisters loot | |
"28844,28846,28848,28850,",#JF | |
"20183,20185,20187,20189,", #freighters | |
"10222,10209,10213,10217,10226,",#+5 | |
"10221,10208,10212,10216,10225,",#+4 | |
"29668,34133,34132,33681,17715,3898,17718,17922,28606,40519,40520)", #RMT+gecko | |
"AND price_date > (SELECT MAX(price_date) FROM snapshot_evecentral) - INTERVAL ",date_range," DAY ", | |
"GROUP BY price_date, price_time, typeid,locationid") | |
print("fetching EC Data") | |
ec_data <- sqlQuery(EC,ec_q) | |
print("Bunging Data") | |
market_data2 <- ec_data | |
market_data2$date <- as.Date(market_data2$date) | |
market_data2$typeid <- as.factor(market_data2$typeid) | |
market_data2 <- subset(market_data2, SellOrder > 0) | |
market_data2$datetime <- paste(market_data2$date,market_data2$hour, sep=" ") | |
market_data2$datetime <- as.POSIXlt(market_data2$datetime, tz="GMT") | |
print("Looking up SolarSystemNames") | |
system_list <- unique(market_data2$locationid) | |
market_data2$SolarSystemName <- NA | |
for(sysIndex in 1:length(system_list)) | |
{ | |
systemName <- "" | |
systemID <- system_list[sysIndex] | |
systemQuery.addr <- paste0("https://public-crest.eveonline.com/solarsystems/",systemID,"/") | |
systemQuery.json <- fromJSON(readLines(systemQuery.addr)) | |
systemName <- systemQuery.json$name | |
market_data2$SolarSystemName[market_data2$locationid==systemID] <- systemName | |
} | |
theme_dark <- function( ... ) { | |
theme( | |
text = element_text(color="gray90"), | |
title = element_text(size=rel(2.5),hjust=0.05,vjust=3.5), | |
axis.title.x = element_text(size=rel(0.75),hjust=0.5, vjust=0), | |
axis.title.y = element_text(size=rel(0.75),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="#252525"), | |
strip.text = element_text(size=rel(1.2)) | |
) + theme(...) | |
} | |
typeids <- unique(market_data2$typeid) | |
line_titles <- data.frame(x=patch_list, | |
y=NA, | |
label=patch_title) | |
line_titles$x <- as.POSIXlt(line_titles$x) | |
market_data2$typeName <- NA | |
for(index in 1:length(typeids)) | |
{ | |
#print(typeids[index]) | |
typ_id <- typeids[index] | |
typ_id.addr <- paste0("https://public-crest.eveonline.com/types/",typ_id,"/") | |
typ_id.json <- fromJSON(readLines(typ_id.addr)) | |
typ_name <- typ_id.json$name | |
print(paste0("--",typ_name)) | |
market_data2$typeName[market_data2$typeid==typ_id] <- typ_name | |
### EC PRICE HISTORIES - MAJOR HUBS ### | |
graphname <- paste0(typ_name," - Major Market SellOrder Prices - ", title_date) | |
filename <- paste0(chart_repo,typ_name,"_",title_date,"_SPECIAL-HUBS.png") | |
filename2 <- paste0(chart_repo,typ_name,"2_",title_date,".png") | |
market_short <- subset(market_data2, typeid==typ_id & !(SolarSystemName %in% c("Thera","Hek"))) | |
market_short$SellOrder[market_short$SellOrder <= 0] <- NA | |
market_short$size <- 0.5 | |
market_short$size[market_short$SolarSystemName == "Jita"] <- 1.0 | |
market_short$size <- as.factor(market_short$size) | |
market_short$datenumeric <- as.numeric(market_short$datetime) | |
market_short[order(market_short$datetime),] | |
max_scale <- max(market_short$SellOrder, na.rm=TRUE) | |
min_scale <- min(market_short$SellOrder, na.rm=TRUE) | |
line_titles$y <- max_scale | |
png(filename, width=plot.width, height=plot.height) | |
GG <- ggplot(market_short, aes(x=datetime, y=SellOrder, color=SolarSystemName)) | |
GG <- GG + geom_line(aes(size=size)) + scale_size_manual(values=c(0.25,1),guide="none") | |
GG <- GG + scale_color_manual(values=c("Dodixie"="#097686","Rens"="#B7090D","Jita"="#2169E0","Amarr"="#EA8B25")) | |
GG <- GG + geom_vline(xintercept=as.numeric(x_intercepts), linetype=2, color="white") | |
GG <- GG + geom_text(aes(x=x, | |
y=y, | |
label=label), | |
color="white", | |
angle=-90, | |
vjust=-0.9, | |
hjust=0, | |
#text=element_text(size=15), | |
data=line_titles) | |
GG <- GG + theme_dark() | |
if(max_scale > 1e9){ | |
GG <- GG + scale_y_continuous(limits=c(min(min_scale),NA),labels=function(x)sprintf("%.2fB",x/1e9)) | |
}else if(max_scale > 1e6){ | |
GG <- GG + scale_y_continuous(limits=c(min(min_scale),NA),labels=function(x)sprintf("%.2fM",x/1e6)) | |
}else if(max_scale > 1e3){ | |
GG <- GG + scale_y_continuous(limits=c(min(min_scale),NA),labels=function(x)sprintf("%.2fK",x/1e3)) | |
} | |
GG <- GG + labs(title=graphname, color="Solar System", x="date", y="Sell Price") | |
print(GG) | |
dev.off() | |
### EC PRICE/VOLUME HISTORIES - JITA ### | |
# typ_id=34 | |
# typ_name="DEBUG" | |
market_buysell <- subset(market_data2, typeid==typ_id & locationid==30000142) | |
market_buysell$SellOrder[market_buysell$SellOrder <= 0] <- NA | |
market_buysell.melt <- melt.data.frame(market_buysell, id.vars=c("datetime","typeName","typeid"),measure.vars=c("SellOrder","BuyOrder","BuyVolume","SellVolume")) | |
market_buysell.melt$facet[market_buysell.melt$variable %in% c("SellOrder", "BuyOrder")] <- "price" | |
market_buysell.melt$facet[market_buysell.melt$variable %in% c("SellVolume","BuyVolume")] <- "volume" | |
market_buysell.price <- subset(market_buysell.melt,facet=="price") | |
market_buysell.volume <- subset(market_buysell.melt,facet=="volume") | |
price.max_scale = max(market_buysell.price$value, na.rm=TRUE) | |
price.min_scale = min(market_buysell.price$value, na.rm=TRUE) | |
volume.max_scale = max(market_buysell.volume$value, na.rm=TRUE) | |
volume.min_scale = min(market_buysell.volume$value, na.rm=TRUE) | |
graphname <- paste0(typ_name," - Jita Buy/Sell - ", title_date) | |
filename <- paste0(chart_repo,typ_name,"_",title_date,"_SPECIAL-JITA.png") | |
market_buysell.price[order(market_buysell.price$datetime),] | |
price.plot <- ggplot(market_buysell.price, aes(x=datetime, y=value, color=variable)) | |
price.plot <- price.plot + geom_line(size=rel(1.2)) | |
price.plot <- price.plot + labs(title=graphname, x="date", y="price", color="key") | |
price.plot <- price.plot + geom_vline(xintercept=as.numeric(x_intercepts), linetype=2, color="white") | |
price.plot <- price.plot + geom_text(aes(x=x, | |
y=Inf, | |
label=label), | |
color="white", | |
angle=-90, | |
vjust=1.2, | |
hjust=0, | |
#text=element_text(size=17), | |
data=line_titles) | |
if(price.max_scale > 1e9){ | |
price.plot <- price.plot + scale_y_continuous(limits=c(min(price.min_scale),NA),labels=function(x)sprintf("%.2fB",x/1e9)) | |
}else if(price.max_scale > 1e6){ | |
price.plot <- price.plot + scale_y_continuous(limits=c(min(price.min_scale),NA),labels=function(x)sprintf("%.2fM",x/1e6)) | |
}else if(price.max_scale > 1e3){ | |
price.plot <- price.plot + scale_y_continuous(limits=c(min(price.min_scale),NA),labels=function(x)sprintf("%.2fK",x/1e3)) | |
} | |
price.plot <- price.plot + theme_dark() | |
price.plot <- price.plot + scale_color_manual(values=c("BuyOrder"="#B7090D","SellOrder"="#2169E0")) | |
market_buysell.volume[order(market_buysell.volume$datetime),] | |
volume.plot <- ggplot(market_buysell.volume, aes(x=datetime, y=value, color=variable)) | |
volume.plot <- volume.plot + geom_line(size=rel(1.2)) | |
volume.plot <- volume.plot + labs(x="date", y="volume", color="key") | |
volume.plot <- volume.plot + geom_vline(xintercept=as.numeric(x_intercepts), linetype=2, color="white") | |
if(volume.max_scale > 1e9){ | |
volume.plot <- volume.plot + scale_y_continuous(limits=c(min(volume.min_scale),NA),labels=function(x)sprintf("%.2fB",x/1e9)) | |
}else if(volume.max_scale > 1e6){ | |
volume.plot <- volume.plot + scale_y_continuous(limits=c(min(volume.min_scale),NA),labels=function(x)sprintf("%.2fM",x/1e6)) | |
}else if(volume.max_scale > 1e3){ | |
volume.plot <- volume.plot + scale_y_continuous(limits=c(min(volume.min_scale),NA),labels=function(x)sprintf("%.2fK",x/1e3)) | |
} | |
volume.plot <- volume.plot + theme_dark() | |
volume.plot <- volume.plot + scale_color_manual(values=c("BuyVolume"="#B7090D","SellVolume"="#2169E0")) | |
C_plot <- plot_grid(price.plot, volume.plot, ncol = 1, align = 'v') | |
png(filename, width=plot.width, height=plot.height) | |
print(C_plot) | |
dev.off() | |
## CREST HISTORY DATA + QUANTMOD ### | |
query_addr = paste0("https://public-crest.eveonline.com/market/",region_id,"/types/",typ_id,"/history/") | |
market.json <- fromJSON(readLines(query_addr)) | |
market.data.json <- data.table(market.json$items) | |
market.data <- market.data.json[,list(Date = as.Date(date), | |
Volume= volume, | |
High = highPrice, | |
Low = lowPrice, | |
Close =avgPrice[-1], | |
Open = avgPrice)] | |
n <- nrow(market.data) | |
market.data <- market.data[1:n-1,] | |
low_flag = quantile(market.data$Low,.25)/5 | |
high_flag = quantile(market.data$High,.75)*5 | |
market.data$Low[market.data$Low<=low_flag] <-min(market.data$Open,market.data$Close) | |
market.data$High[market.data$High>=high_flag] <-max(market.data$Open,market.data$Close) | |
market.data.ts <- xts(market.data[,-1,with=F], order.by=market.data[,Date], period=7) | |
graphname <- paste0(typ_name," - CREST History - ", title_date) | |
filename <- paste0(chart_repo,typ_name,"_",title_date,"_SPECIAL-CREST.png") | |
png(filename, width=plot.width, height=plot.height) | |
chartSeries(market.data.ts, | |
name = graphname, | |
TA = "addBBands(15,2);addVo();addMACD(5,15,5);addRSI();addLines(h=30, on=4);addLines(h=70, on=4)" | |
,subset = paste0("last ", date_range, " days")) | |
dev.off() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample Graphs:
Major Hubs
Jita Zoomin
CREST Quantmod