using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Text.RegularExpressions; using TradeIdeas.TIProGUI; using TradeIdeas.TIProData; using System.IO; /* I call this window the "optimizer" because I assume it's the start of something * bigger and better. Dan and Brad want something that will help them call R somewhat * automatically and do other things for them. If our only goal was to load the * file, we wouldn't even need a window. Presumably the URL will be automatically * downloaded in future versions of the software. */ namespace TradeIdeas.TIProGUI { public partial class OddsMakerOptimizer : Form { public string Config { get; set; } public OddsMakerResults OddsMakerResults { get; set; } public string CSV { get; set; } private string _windowName = ""; public string WindowName { get { return _windowName; } set { _windowName = value; Text = "OddsMakerOptimizer: " + _windowName; } } public OddsMakerOptimizer() { InitializeComponent(); } private string _localFile = ""; private void buttonRpart_Click(object sender, EventArgs e) { // download URL _rpartVersion = RpartVersion.normal; _rpartBatchFile = "runrpart2.bat"; StartRPartProcess(buttonRpart); } private string MakeFileNameSafe(string value) { foreach (var c in Path.GetInvalidFileNameChars()) { value.Replace(c, '-'); } value = value.Replace(" ", ""); value = value.Replace("/", "-"); value = value.Replace(":", "-"); if (value.StartsWith("-")) value = value.Substring(1); return value; } private void DownloadFile() { if (CSV != "") { _localFile = GetLocalDirectory() + "\\" + MakeFileNameSafe(_windowName) + "." + DateTime.Now.ToString("yyyy-MM-ddhhmmss") + ".csv"; using (StreamWriter writer = new StreamWriter(_localFile)) { writer.Write(CSV); LocalFileAvailable(); EnableButtons(); } } } void webClient_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e) { string debugView = e.ToString(); } void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { LocalFileAvailable(); } private void LocalFileAvailable() { this.InvokeIfRequired(delegate { EnableButtons(); }); if (_startOmOnDownload) StartOddsMakerResultsWindow(); else DoRpart(); } private void EnableButtons() { foreach (Button button in _buttonsToEnable) { button.Enabled = true; } _buttonsToEnable.Clear(); } private void DoRpart() { // start rpart. try { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; startInfo.EnvironmentVariables.Add("csvfile", _localFile); GuiEnvironment.LogMessage("set csvfile=" + _localFile); startInfo.EnvironmentVariables.Add("rscriptpath", GetRscriptPath()); GuiEnvironment.LogMessage("set rscriptpath=" + GetRscriptPath()); startInfo.EnvironmentVariables.Add("alertconfig", "http://www.trade-ideas.com/View.php?" + Config); GuiEnvironment.LogMessage("set alertconfig=" + "http://www.trade-ideas.com/View.php?" + Config); if (_rpartVersion == RpartVersion.alternateone) { startInfo.EnvironmentVariables.Add("alternatesettings", "alternatesettings_one"); GuiEnvironment.LogMessage("set alternatesettings=" + "alternatesettings_one"); } else if (_rpartVersion == RpartVersion.alternatetwo) { startInfo.EnvironmentVariables.Add("alternatesettings", "alternatesettings_two"); GuiEnvironment.LogMessage("set alternatesettings=" + "alternatesettings_two"); } string rpartFilename = _rpartBatchFile; string fullRpartPath = GetPath(rpartFilename); if (fullRpartPath == "") MessageBox.Show("Couldn't find " + rpartFilename + " in your machine's path."); else { startInfo.WorkingDirectory = Path.GetDirectoryName(fullRpartPath); startInfo.FileName = fullRpartPath; GuiEnvironment.LogMessage("Working Directory: " + startInfo.WorkingDirectory); GuiEnvironment.LogMessage("Full Command: " + fullRpartPath); Process process = new Process(); process.StartInfo = startInfo; process.Start(); } } catch (Exception exc) { string debugView = exc.ToString(); } } private string GetPath(string filename) { string fullPath = FindExePath(filename); if (fullPath == "") { // not in path, check app_name\oddsmakeroptimize string tiProPath = GetLocalDirectory() + "\\..\\" + filename; if (File.Exists(tiProPath)) fullPath = tiProPath; } return fullPath; } // got this from: http://csharptest.net/526/how-to-search-the-environments-path-for-an-exe-or-dll/ /// /// Expands environment variables and, if unqualified, locates the exe in the working directory /// or the environment's path. /// /// The name of the executable file /// The fully-qualified path to the file /// Raised when the exe was not found private string FindExePath(string exe) { exe = Environment.ExpandEnvironmentVariables(exe); if (!File.Exists(exe)) { if (Path.GetDirectoryName(exe) == String.Empty) { foreach (string test in (Environment.GetEnvironmentVariable("PATH") ?? "").Split(';')) { string path = test.Trim(); if (!String.IsNullOrEmpty(path) && File.Exists(path = Path.Combine(path, exe))) return Path.GetFullPath(path); } } return ""; } return Path.GetFullPath(exe); } private string GetLocalFile(string url) { string localFile = ""; Match match = Regex.Match(url, @"OddsMakerResults/(.*\.csv)", RegexOptions.IgnoreCase); if (match.Success) localFile = GetLocalDirectory() + "\\" + match.Groups[1].Value; return localFile; } private string GetLocalDirectory() { string directory = GuiEnvironment.DataDirectory + "\\oddsmakeroptimization\\reports"; if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); return directory; } private string GetRscriptPath() { string[] pathsToTry = new string[] { "c:\\program files\\r\\r-2.15.1\\bin\\rscript.exe", "c:\\program files (x86)\\r\\r-2.15.1\\bin\\rscript.exe", "d:\\program files (x86)\\r\\r-2.15.1\\bin\\rscript.exe", "d:\\program files\\r\\r-2.15.1\\bin\\rscript.exe", "c:\\program files\\r\\r-2.15.2\\bin\\rscript.exe", "c:\\program files (x86)\\r\\r-2.15.2\\bin\\rscript.exe", "d:\\program files (x86)\\r\\r-2.15.2\\bin\\rscript.exe", "d:\\program files\\r\\r-2.15.2\\bin\\rscript.exe", "c:\\program files\\r\\r-3.5.1\\bin\\rscript.exe", "c:\\program files (x86)\\r\\r-3.5.1\\bin\\rscript.exe", "d:\\program files\\r\\r-3.5.1\\bin\\rscript.exe", "d:\\program files (x86)\\r\\r-3.5.1\\bin\\rscript.exe", "f:\\program files\\r\\r-3.5.1\\bin\\rscript.exe", "f:\\program files (x86)\\r\\r-3.5.1\\bin\\rscript.exe", }; string x86path = ProgramFilesx86() + "\\R\\R-2.15.1\\bin\\RScript.exe"; if (File.Exists(x86path)) return x86path; string standardPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\R\\R-2.15.1\\bin\\RScript.exe"; if (File.Exists(standardPath)) return standardPath; foreach (string path in pathsToTry) { if (File.Exists(path)) return path; } return "rscript.exe"; } private string ProgramFilesx86() { if (8 == IntPtr.Size || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432")))) { return Environment.GetEnvironmentVariable("ProgramFiles(x86)"); } return Environment.GetEnvironmentVariable("ProgramFiles"); } private bool _startOmOnDownload = false; private void buttonShowOMReport_Click(object sender, EventArgs e) { if (!File.Exists(_localFile)) { _startOmOnDownload = true; DownloadFile(); } else StartOddsMakerResultsWindow(); } private void StartOddsMakerResultsWindow() { this.InvokeIfRequired(delegate { if (null != OddsMakerResults) { if (File.Exists(_localFile)) { string csv = File.ReadAllText(_localFile); OddsMakerResults.SetCSV(csv); OddsMakerResults.Show(); } } }); } private HashSet