library(ggplot2) library(directlabels) library(optparse) option_list <- list( make_option(c("--date"), type="character", default=NULL, help="date in yyyy-mm-dd format", metavar="character"), make_option(c("--skip_latest"), action="store_true", default=FALSE, help="set to skip copying the latest file") ) opt_parser <- OptionParser(option_list=option_list) opt <- parse_args(opt_parser) #library(ggrepel) #holly <- read.csv("symbol_trades.main.2018-08-27.csv") plotf <- function(max_date=max(holly$dt), segment="Holly", risk_type="Risk Off") { profit_data <- holly[holly$dt<= max_date&!is.na(holly$Profit),] #max_date <- max(profit_data$dt) min_date <- min(holly$dt) #print(paste("Max: ", max_date, " Min: ", min_date, sep="")) seconds_width <- difftime(max_date, min_date, units="secs") ymd <- format(min_date, "%Y-%m-%d") title <- paste(segment, " Strategy Performance ", ymd, sep="") date_breaks <- "30 min" if (format(max_date, "%H") > 12) { date_breaks <- "1 hour" } ggplot(data=profit_data, aes(x=dt, y=Profit, group=Strategy, color=Strategy)) + geom_line() + scale_colour_discrete(guide = 'none') + #theme(plot.margin = unit(c(1,3,1,1), "lines")) + geom_dl(aes(label = Strategy), method = list(dl.combine("last.points"))) + #geom_text_repel(aes(label = Strategy) #arrow = arrow(length = unit(0.03, "npc"), type = "closed", ends = "first"), ##force = 10, #hjust = 0 #) + #coord_cartesian(xlim = c(0, x_max)) + geom_hline(yintercept=0, linetype="dashed", color="red") + scale_x_datetime(date_breaks = date_breaks, date_labels="%H:%M", limits=c(min_date, max_date + (seconds_width * 0.23)) ) + #labs(x="",y="",title=title, subtitle=paste("Compares intraday performance of ", segment, " strategies", sep=""), caption="@davemabe Source: Trade-Ideas http://www.trade-ideas.com") + #theme(plot.title=element_text(face="bold",size=16), plot.subtitle=element_text(face="italic",size=12), plot.caption=element_text(hjust=0,size=8)) labs(x="",y="",title=title, caption="@davemabe Source: Trade-Ideas http://www.trade-ideas.com") + theme(plot.title=element_text(face="bold",size=18), plot.caption=element_text(hjust=0,size=12), axis.text.x=element_text(hjust=0, size=12)) } segment_names <- c("Holly Grail", "Holly 2.0", "Holly Neo") names(segment_names) <- c("main", "test", "aws1") publish_dir <- "/var/www/alldocs/main/holly_gifs" latest_dir <- "/var/www/alldocs/main/holly_gifs/latest" copy_latest <- !opt$skip_latest date_part <- "" if (!is.null(opt$date)) { date_part <- paste(".", opt$date, sep="") } for (segment in c("main", "test", "aws1")) { for (risk_type in c("risk_off", "risk_on")) { my_dir <- "image.temp" unlink(paste(my_dir, "/*.png", sep="")) file_path <- paste("strategy_trades.", segment, ".", risk_type, date_part, ".csv", sep="") holly <- read.csv(file_path) holly$dt <- as.POSIXct(holly$Time, "%Y-%m-%d %H:%M:%S", tz="America/Los_Angeles") attr(holly$dt, "tzone") <- "America/New_York" dlist <- unique(holly$dt) earliest_dt <- min(holly$dt) earliest_trade_dt <- min(holly[!is.na(holly$Profit),]$dt) minutes_from_open <- difftime(earliest_trade_dt, earliest_dt, units="mins") + 1 this_year <- format(earliest_dt, "%Y") this_month <- format(earliest_dt, "%m") this_day <- format(earliest_dt, "%d") destination_path <- paste(publish_dir, "/", this_year, "/", this_month, "/", this_day, "/", sep="") segment_name <- segment_names[segment] for (i in c(seq(from=minutes_from_open, to=390, by=30), 390, 391, 392)) { file_path <- paste0(my_dir, "/plot-", segment, 4000+i, ".png") g <- plotf(dlist[min(i, length(dlist))], segment_name, risk_type) ggsave(file_path, g, width=10, height=8, units="cm", scale=2) #print(paste(i, " out of ", length(dlist))) } dir.create(destination_path, showWarnings=FALSE, recursive=TRUE) final_gif_name <- paste("ai-", segment, "-", risk_type, ".gif", sep="") system_command <- paste("convert -resize 24% -delay 75 -loop 0 image.temp/plot-", segment, "*.png image.temp/", final_gif_name, " && cp image.temp/", final_gif_name, " ", destination_path, sep="") #print(paste("Running command: ", system_command, sep="")) system(system_command) if (copy_latest) { dir.create(latest_dir, showWarnings=FALSE, recursive=TRUE) copy_command <- paste("cp image.temp/", final_gif_name, " ", latest_dir, sep="") system(copy_command) } } }