public static string RScriptIntraday(string ticker, CacheRepositoryManager cacheMgr, int days = 15) { string script = string.Format("##### {0}: Auto R Script Generated #####", ticker) + NEWLINE; script += string.Format("ticker = \"{0}\"", ticker) + NEWLINE; // Initial R variables script += "date = c()" + NEWLINE; foreach (string key in R_metaVars.Keys) { script += key + " = c()" + NEWLINE; } int i = 0; foreach (string path in cacheMgr.GetCacheIntradayPathList(ticker, days)) { string date = cacheMgr.GetCacheFileDate(path, ticker); if (long.Parse(date) < 20150917) //converted time column was add in the csv later { script += string.Format( "qIntraday{0} = read.table(\"{1}\", header=F, sep=\",\", skip=17, col.names=c(\"TS\",\"C\",\"H\",\"L\",\"O\",\"Volume\"))", i, path.Replace("\\", "/")) + NEWLINE; } else { script += string.Format( "qIntraday{0} = read.table(\"{1}\", header=F, sep=\",\", skip=17, col.names=c(\"TS\",\"C\",\"H\",\"L\",\"O\",\"Volume\",\"T\"))", i, path.Replace("\\", "/")) + NEWLINE; } //meta info script += string.Format("date = c(\"{0}\", date)", date) + NEWLINE; foreach (string key in R_metaVars.Keys) { script += string.Format(R_metaVars[key], i, i, i, i, i, i) + NEWLINE; } i++; } script += "jb_meta = data.frame(date=date"; foreach (string key in R_metaVars.Keys) { script += string.Format(",{0} = {1}", key, key); } script += ")" + NEWLINE; //script += "options(\"width\"=300)" + NEWLINE; // set output width in Rprofile.site //script += "sortByC = qIntraday0[order(qIntraday0$C),c(\"C\", \"Volume\")]"; //script += "sortByC = qIntraday0[order(qIntraday0$C),]" + NEWLINE; //script += "groupByC = aggregate(Volume ~ C, qIntraday0, sum)" + NEWLINE; //script += "min_date = jb_meta[jb_meta$low == min(jb_meta$low), ]" + Environment.NewLine; //script += string.Format("message(\"***** {0} *****\")", Ticker) + NEWLINE; //script += "jb_meta[jb_meta$low == min(jb_meta$low), ]" + NEWLINE; //Console.Write(script); return script; }
static void CMD_Mining(CacheRepositoryManager cacheMgr, ListManager listMgr, int days=30) { foreach (string ticker in listMgr.TickList) { foreach(string path in cacheMgr.GetCacheIntradayPathList(ticker, days)) { Console.WriteLine(path); //YahooIntraday.CSV csvIntraday = YahooIntraday.CSV.openCache(path); } } }
static void CMD_Download(ListManager listMgr, CacheRepositoryManager cacheManager, string tickers, int[] days = null) { if (days == null) days = new int[] { 1 }; List<string> tickerList = (tickers == null) ? listMgr.TickList : tickers.Split(',').ToList(); foreach (string ticker in tickerList) { foreach (int n in days) { YahooIntraday.CSV csvIntraday = YahooIntraday.CSV.download(ticker, n); string cacheFileName = cacheManager.getTickYahooIntradayCacheFileName(ticker, csvIntraday.RangeStart, n); csvIntraday.cacheSave(cacheFileName); //Console.WriteLine(cacheFileName); } } }
public static string RGraph_Intraday(string ticker, CacheRepositoryManager cacheMgr, int startDay=0) { string[] colors = { "black", "purple", "blue", "green", "red" }; List<string> cacheList = cacheMgr.GetCacheIntradayPathList(ticker, colors.Length); string script = string.Format("##### {0} #####", ticker) + NEWLINE; script += "library(\"ggplot2\")" + NEWLINE; script += string.Format("graph <- ggplot()") + NEWLINE; int i = 0; foreach (string path in cacheList) { if (i++ < startDay) continue; string date = cacheMgr.GetCacheFileDate(path, ticker); if (long.Parse(date) < 20150917) //converted time column was added in the csv after this date { script += string.Format( "qIntraday{0} = read.table(\"{1}\", header=F, sep=\",\", skip=17, col.names=c(\"TS\",\"C\",\"H\",\"L\",\"O\",\"Volume\"))", i - startDay - 1, path.Replace("\\", "/")) + NEWLINE; } else { script += string.Format( "qIntraday{0} = read.table(\"{1}\", header=F, sep=\",\", skip=17, col.names=c(\"TS\",\"C\",\"H\",\"L\",\"O\",\"Volume\",\"T\"))", i - startDay - 1, path.Replace("\\", "/")) + NEWLINE; } if ((i-startDay) == colors.Length) break; } // ggplot2 script for(i=0; i<colors.Length; i++) { script += string.Format( "graph <- graph + geom_point(data=qIntraday{0}, aes(seq(qIntraday{1}$C),qIntraday{2}$C,size=qIntraday{3}$Volume,alpha=.7), colour=\"{4}\")", i, i, i, i, colors[i]) + NEWLINE; } script += string.Format("graph <- graph + labs(title=\"{0} Intraday\") + ylab(\"Close\")", ticker) + NEWLINE; script += string.Format("graph") + NEWLINE; return script; }
static void CMD_RGraphScript(ListManager listMgr, CacheRepositoryManager cacheMgr, string rScriptFolder, int days =5) { const string fileNamePattern = "RGraph_{0}.R"; foreach (string ticker in listMgr.TickList) { string path = Path.Combine(rScriptFolder, string.Format(fileNamePattern, ticker)); Utility.PutFileContent(path, RScript.RGraph_Intraday(ticker, cacheMgr, 0)); //Console.WriteLine(path); } }
static void Main(string[] args) { //MethodInfo m = new JB007Main().GetType().GetMethod("CMD_Mining"); //m.Invoke() if (args.Length > 0 && args[0].StartsWith("-")) { ListManager listManager = new ListManager(AppConf_ReadSetting(CONF_LISTDIR)); CacheRepositoryManager cacheManager = new CacheRepositoryManager(AppConf_ReadSetting(CONF_WORKDIR)); string rDir = AppConf_ReadSetting(CONF_RSCRIPTDIR); Console.WriteLine("***** DownloadDir: {0}", AppConf_ReadSetting(CONF_WORKDIR)); Console.WriteLine("***** ListFile: {0}", listManager.ListFile); Console.WriteLine("***** R Dir: {0}", rDir); for(int i=0; i<args.Length; i++) { string tickerList = null; string cmd = args[i].Substring(1); Console.WriteLine("************************************************"); Console.WriteLine("* {0} {1}ing ...", DateTime.Now, cmd); switch (cmd) { case "Mining": CMD_Mining(cacheManager, listManager); break; case "Download": if (args.Length > (i+1) && !args[i+1].StartsWith("-")) { tickerList = args[++i]; } CMD_Download(listManager, cacheManager, tickerList); break; case "RScriptIntraday": if (args.Length > (i + 1) && !args[i + 1].StartsWith("-")) { tickerList = args[++i]; } CMD_RScriptIntraday(listManager, cacheManager, rDir, tickerList); break; case "RunRScriptIntraday": CMD_RunRScriptIntraday(listManager, rDir); break; case "MailRScriptIntraday": CMD_MailRScriptIntraday(rDir); break; case "RGraphScript": CMD_RGraphScript(listManager, cacheManager, rDir); break; default: Console.WriteLine("- Bad command. Try again."); Usage(); break; } Console.WriteLine("* {0} {1} Done", DateTime.Now, cmd); } } else { Usage(); } }
// days=15: default number of days of intraday csv data files static void CMD_RScriptIntraday(ListManager listMgr, CacheRepositoryManager cacheMgr, string rScriptFolder, string tickers, int days = 15) { string Rfile_Append = Path.Combine(rScriptFolder, "RScriptIntraday_Append.R"); List<string> tickerList = (tickers == null) ? listMgr.TickList : tickers.Split(',').ToList(); foreach (string ticker in tickerList) { string path = Path.Combine(rScriptFolder, string.Format(RScriptFilePattern, ticker)); string script = RScript.RScriptIntraday(ticker, cacheMgr, days); script += Utility.GetFileContent(Rfile_Append); Utility.PutFileContent(path, script); //Console.WriteLine(path); } }