public static StockSession Start(List <string> sources, List <string> sinkScripts, string executeScript) { StockSession session = new StockSession(); List <string> script = new List <string>(); Directory.CreateDirectory("tmp"); // Convert any legacy files before further processing var legacyFiles = sources.Where((s) => { return(s.EndsWith(".csv")); }).ToList(); if (legacyFiles.Count() > 0) { System.Windows.Forms.SaveFileDialog saveDiag = new System.Windows.Forms.SaveFileDialog(); saveDiag.Title = "Save converted data file as..."; if (saveDiag.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var convertedFile = StockDataFile.Convert(legacyFiles, new FileStream(saveDiag.FileName, FileMode.Create)); convertedFile.Close(); sources.Add(saveDiag.FileName); } else { // Cancel running the script return(null); } foreach (var l in legacyFiles) { sources.Remove(l); } } session.SourceFile = StockDataFile.Open(sources.ConvertAll <Stream>((s) => { return(new FileStream(s, FileMode.Open)); })); script.Add("tmp/" + SOURCE_CLASS + ".cs"); using (var file = new StreamWriter(new FileStream(script.Last(), FileMode.Create))) file.Write(session.SourceFile.GetSourceCode(SOURCE_CLASS)); session.SinkFile = new StockDataFile(sinkScripts.ConvertAll <string>((f) => { return(Path.GetFileNameWithoutExtension(f)); }), sinkScripts.ConvertAll <string>((f) => { return(File.ReadAllText(f)); })); session.SinkFile.Interval = session.SourceFile.Interval; script.Add("tmp/" + SINK_CLASS + ".cs"); using (var file = new StreamWriter(new FileStream(script.Last(), FileMode.Create))) file.Write(session.SinkFile.GetSourceCode(SINK_CLASS)); // Create the evaluator file (needs to be compiled in the script since it references StockDataSource) string[] embeddedFiles = new string[] { "RobinhoodDesktop.Script.StockEvaluator.cs", "RobinhoodDesktop.Script.StockProcessor.cs" }; foreach (var f in embeddedFiles) { script.Add(string.Format("tmp/{0}.cs", f.Substring(24, f.Length - 27))); StringBuilder analyzerCode = new StringBuilder(); analyzerCode.Append(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(f)).ReadToEnd()); using (var file = new StreamWriter(new FileStream(script.Last(), FileMode.Create))) file.Write(StockDataFile.FormatSource(analyzerCode.ToString())); } // Add the user defined analyzers foreach (string path in Directory.GetFiles(@"Script/Decision", "*.cs", SearchOption.AllDirectories)) { script.Add(path); } foreach (string path in Directory.GetFiles(@"Script/Action", "*.cs", SearchOption.AllDirectories)) { script.Add(path); } // Get the code that will actually run the session script.Add(executeScript); // Build and run the session #if DEBUG var isDebug = true; #else var isDebug = false; #endif try { if (!string.IsNullOrEmpty(executeScript)) { session.ScriptInstance = CSScript.LoadFiles(script.ToArray(), null, isDebug, "TensorFlow.NET.dll", "Google.Protobuf.dll", "NumSharp.Lite", "netstandard", "System.Memory", "System.Numerics"); var run = session.ScriptInstance.GetStaticMethod("*.Run", session); run(session); } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString()); session.SourceFile.Close(); session.SinkFile.Close(); } return(session); }
public void Run() { System.Windows.Forms.OpenFileDialog diag = new System.Windows.Forms.OpenFileDialog(); diag.Multiselect = true; diag.Title = "Open Stock Data File..."; if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK) { List <string> script = new List <string>(); Directory.CreateDirectory("tmp"); // Get the source file if (diag.FileName.EndsWith(".csv")) { System.Windows.Forms.SaveFileDialog saveDiag = new System.Windows.Forms.SaveFileDialog(); if (saveDiag.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } SourceFile = StockDataFile.Convert(diag.FileNames.ToList(), new FileStream(saveDiag.FileName, FileMode.Create)); } else { SourceFile = StockDataFile.Open(new FileStream(diag.FileName, FileMode.Open)); } script.Add("tmp/" + SOURCE_CLASS + ".cs"); using (var file = new StreamWriter(new FileStream(script.Last(), FileMode.Create))) file.Write(SourceFile.GetSourceCode(SOURCE_CLASS)); // Create the sink file //SinkFile = new StockDataFile(new List<string>() { }, new List<string>() { }); SinkFile = new StockDataFile(new List <string>() { "MovingAverage" }, new List <string>() { File.ReadAllText(@"Script/Data/MovingAverage.cs") }); script.Add("tmp/" + SINK_CLASS + ".cs"); using (var file = new StreamWriter(new FileStream(script.Last(), FileMode.Create))) file.Write(SinkFile.GetSourceCode(SINK_CLASS)); // Create the analyzer file (needs to be compiled in the script since it references StockDataSource) var analyzerFilename = "RobinhoodDesktop.Script.StockAnalyzer.cs"; script.Add("tmp/StockAnalyzer.cs"); StringBuilder analyzerCode = new StringBuilder(); analyzerCode.Append(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(analyzerFilename)).ReadToEnd()); using (var file = new StreamWriter(new FileStream(script.Last(), FileMode.Create))) file.Write(StockDataFile.FormatSource(analyzerCode.ToString())); // Add the user defined analyzers string[] analyzerPaths = Directory.GetFiles(@"Script/Decision", "*.cs", SearchOption.AllDirectories); foreach (string path in analyzerPaths) { script.Add(path); } // Get the code that will actually run the session script.Add("tmp/StockSessionScript.cs"); var sessionFilename = "RobinhoodDesktop.Script.StockSessionScript.cs"; using (var file = new StreamWriter(new FileStream(script.Last(), FileMode.Create))) file.Write(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(sessionFilename)).ReadToEnd()); // Build and run the session #if DEBUG var isDebug = true; #else var isDebug = false; #endif #if true try { var scriptInstance = CSScript.LoadFiles(script.ToArray(), null, isDebug); var run = scriptInstance.GetStaticMethod("RobinhoodDesktop.Script.StockSessionScript.Run", this); run(this); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString()); } #else // Set up the derived data sink var sourceData = SourceFile.GetSegments <StockDataBase>(); var sinkData = StockDataSetDerived <StockDataBase, StockDataBase> .Derive(sourceData, SinkFile, (data, idx) => { var point = new StockDataBase(); point.Update(data, idx); return(point); }); SinkFile.SetSegments(sinkData); // Load the first set of data foreach (var pair in sinkData) { foreach (var set in pair.Value) { set.Load(); } } #endif // Cleanup SourceFile.Close(); } }