Example #1
0
        public AlgorithmScreen(AlgorithmScreenConfig config = null)
        {
            this.Cfg = ((config != null) ? config : new AlgorithmScreenConfig());
            GuiPanel = new Panel();

            GuiBox           = new Panel();
            GuiBox.AutoSize  = true;
            GuiBox.BackColor = GuiStyle.BACKGROUND_COLOR;

            // Add the GUI elements for configurating the script
            DataFileButton          = new GuiButton("Open...");
            DataFileButton.Location = new Point(5, 30);
            DataFileButton.MouseUp += (sender, e) =>
            {
                System.Windows.Forms.OpenFileDialog diag = new System.Windows.Forms.OpenFileDialog();
                diag.Multiselect      = true;
                diag.Title            = "Open Stock Data File...";
                diag.InitialDirectory = Cfg.SourceFilePath;
                if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if ((DataFileTextbox.Text.Length > 0) && (!DataFileTextbox.Text.EndsWith("\n")))
                    {
                        DataFileTextbox.Text += "\r\n";
                    }
                    foreach (string dataPath in diag.FileNames)
                    {
                        DataFileTextbox.Text += dataPath + "\r\n";
                        Cfg.SourceFilePath    = Path.GetDirectoryName(dataPath);
                    }
                    DataFileTextbox.Text           = DataFileTextbox.Text.Remove(DataFileTextbox.Text.Length - 2, 2);
                    DataFileTextbox.SelectionStart = DataFileTextbox.Text.Length;
                    DataFileTextbox.ScrollToCaret();
                    DataFileButton.SetImage(GuiButton.ButtonImage.GREEN_WHITE);
                    DataLiveButton.SetImage(GuiButton.ButtonImage.GREEN_TRANSPARENT);
                }
            };
            GuiBox.Controls.Add(DataFileButton);
            GuiButton clearButton = new GuiButton("Clear");

            clearButton.Location = new Point(DataFileButton.Bounds.Right + 5, DataFileButton.Location.Y);
            clearButton.SetImage(GuiButton.ButtonImage.GREEN_TRANSPARENT);
            clearButton.MouseUp += (sender, e) =>
            {
                DataFileTextbox.Text = "";
            };
            GuiBox.Controls.Add(clearButton);
            DataLiveButton          = new GuiButton("Live");
            DataLiveButton.Location = new Point(clearButton.Bounds.Right + 5, clearButton.Location.Y);
            DataLiveButton.SetImage(GuiButton.ButtonImage.GREEN_WHITE);
            DataLiveButton.MouseUp += (sender, e) =>
            {
                DataFileTextbox.Text = "";
                DataLiveButton.SetImage(GuiButton.ButtonImage.GREEN_WHITE);
                DataFileButton.SetImage(GuiButton.ButtonImage.GREEN_TRANSPARENT);
            };
            GuiBox.Controls.Add(DataLiveButton);
            Label sourceDataLabel = new Label();

            sourceDataLabel.Text      = "Source Data";
            sourceDataLabel.ForeColor = GuiStyle.PRICE_COLOR_POSITIVE;
            sourceDataLabel.Font      = GuiStyle.Font;
            sourceDataLabel.Location  = new Point(DataFileButton.Bounds.Left, (DataFileButton.Bounds.Top - sourceDataLabel.Height) + 5);
            GuiBox.Controls.Add(sourceDataLabel);
            DataFileTextbox           = new TextBox();
            DataFileTextbox.Location  = new Point(DataFileButton.Bounds.Left, DataFileButton.Bounds.Bottom + 5);
            DataFileTextbox.Size      = new Size(300, 300);
            DataFileTextbox.Multiline = true;
            DataFileTextbox.WordWrap  = false;
            DataFileTextbox.BackColor = GuiStyle.DARK_GREY;
            DataFileTextbox.ForeColor = GuiStyle.TEXT_COLOR;
            DataFileTextbox.Font      = GuiStyle.Font;
            DataFileTextbox.Text      = Cfg.SourceFiles.Replace("\n", "\r\n");
            if (!string.IsNullOrEmpty(DataFileTextbox.Text))
            {
                DataFileButton.SetImage(GuiButton.ButtonImage.GREEN_WHITE);
                DataLiveButton.SetImage(GuiButton.ButtonImage.GREEN_TRANSPARENT);
            }
            DataFileTextbox.TextChanged += (sender, e) =>
            {
                Cfg.SourceFiles = DataFileTextbox.Text;
            };
            GuiBox.Controls.Add(DataFileTextbox);

            // Add the GUI for selecting the data scripts
            DataScriptListPanel             = new Panel();
            DataScriptListPanel.Location    = new Point(DataFileTextbox.Bounds.Right + 25, DataFileTextbox.Location.Y);
            DataScriptListPanel.Size        = new Size(150, DataFileTextbox.Height);
            DataScriptListPanel.BorderStyle = BorderStyle.FixedSingle;
            DataScriptListPanel.ForeColor   = GuiStyle.PRICE_COLOR_POSITIVE;
            GuiBox.Controls.Add(DataScriptListPanel);
            Label dataScriptsLabel = new Label();

            dataScriptsLabel.Text      = "Data Analysis Scripts";
            dataScriptsLabel.ForeColor = GuiStyle.PRICE_COLOR_POSITIVE;
            dataScriptsLabel.Font      = GuiStyle.Font;
            dataScriptsLabel.Location  = new Point(DataScriptListPanel.Bounds.Left, (DataScriptListPanel.Bounds.Top - dataScriptsLabel.Height) + 5);
            GuiBox.Controls.Add(dataScriptsLabel);
            DataScriptListScrollbar             = new CustomControls.CustomScrollbar();
            DataScriptListScrollbar.Minimum     = 0;
            DataScriptListScrollbar.Maximum     = DataScriptListPanel.Height;
            DataScriptListScrollbar.LargeChange = DataScriptListScrollbar.Maximum / DataScriptListScrollbar.Height;
            DataScriptListScrollbar.SmallChange = 15;
            DataScriptListScrollbar.Value       = 0;
            DataScriptListScrollbar.Attach(DataScriptListPanel);
            GuiBox.Controls.Add(DataScriptListScrollbar);
            DataScriptAddButton          = new GuiButton("Add...");
            DataScriptAddButton.Location = new Point(DataScriptListPanel.Location.X + 5, DataScriptListPanel.Bounds.Bottom + 5);
            DataScriptAddButton.MouseUp += (sender, e) =>
            {
                System.Windows.Forms.OpenFileDialog diag = new System.Windows.Forms.OpenFileDialog();
                diag.Multiselect = true;
                diag.Filter      = "C# Files|*.cs";
                diag.Title       = "Open stock data script(s)...";
                string prevPath = diag.InitialDirectory;
                diag.InitialDirectory = Cfg.DataScriptPath;
                diag.RestoreDirectory = false;
                if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (string scriptPath in diag.FileNames)
                    {
                        Cfg.DataScriptPath = Path.GetDirectoryName(scriptPath);

                        // Skip this item if it has already been loaded
                        var script = scriptPath;
                        if (Cfg.DataScripts.Contains(script))
                        {
                            continue;
                        }

                        AddDataScript(script);
                        Cfg.DataScripts.Add(script);
                    }
                }
            };
            foreach (var script in Cfg.DataScripts)
            {
                AddDataScript(script);
            }
            GuiBox.Controls.Add(DataScriptAddButton);
            DataScriptReloadButton          = new GuiButton("Reload");
            DataScriptReloadButton.Location = new Point(DataScriptAddButton.Right + 5, DataScriptAddButton.Top);
            DataScriptAddButton.MouseUp    += (sender, e) =>
            {
                if (StockSession.Instance != null)
                {
                    StockSession.Instance.Reload();
                }
            };
            GuiBox.Controls.Add(DataScriptReloadButton);

            // Add the GUI for selecting the data scripts
            DecisionScriptListPanel             = new Panel();
            DecisionScriptListPanel.Location    = new Point(DataScriptListPanel.Bounds.Right + 25, DataScriptListPanel.Location.Y);
            DecisionScriptListPanel.Size        = new Size(450, DataFileTextbox.Height);
            DecisionScriptListPanel.BorderStyle = BorderStyle.FixedSingle;
            DecisionScriptListPanel.ForeColor   = GuiStyle.PRICE_COLOR_POSITIVE;
            GuiBox.Controls.Add(DecisionScriptListPanel);
            Label decisionScriptsLabel = new Label();

            decisionScriptsLabel.Text      = "Action Scripts";
            decisionScriptsLabel.ForeColor = GuiStyle.PRICE_COLOR_POSITIVE;
            decisionScriptsLabel.Font      = GuiStyle.Font;
            decisionScriptsLabel.Location  = new Point(DecisionScriptListPanel.Bounds.Left, (DecisionScriptListPanel.Bounds.Top - decisionScriptsLabel.Height) + 5);
            GuiBox.Controls.Add(decisionScriptsLabel);
            DecisionScriptListScrollbar             = new CustomControls.CustomScrollbar();
            DecisionScriptListScrollbar.Minimum     = 0;
            DecisionScriptListScrollbar.Maximum     = DecisionScriptListPanel.Height;
            DecisionScriptListScrollbar.LargeChange = DecisionScriptListScrollbar.Maximum / DecisionScriptListScrollbar.Height;
            DecisionScriptListScrollbar.SmallChange = 15;
            DecisionScriptListScrollbar.Value       = 0;
            DecisionScriptListScrollbar.Attach(DecisionScriptListPanel);
            GuiBox.Controls.Add(DecisionScriptListScrollbar);
            DecisionScriptAddButton          = new GuiButton("Add...");
            DecisionScriptAddButton.Location = new Point(DecisionScriptListPanel.Location.X + 5, DecisionScriptListPanel.Bounds.Bottom + 5);
            DecisionScriptAddButton.MouseUp += (sender, e) =>
            {
                DecisionScriptListPanel.Controls.Add(new ScriptTextBox(this, ""));
            };
            GuiBox.Controls.Add(DecisionScriptAddButton);
            DecisionScriptBrowseButton          = new GuiButton("Browse...");
            DecisionScriptBrowseButton.Location = new Point(DecisionScriptAddButton.Right + 5, DecisionScriptAddButton.Bounds.Top);
            DecisionScriptBrowseButton.MouseUp += (sender, e) =>
            {
                System.Windows.Forms.OpenFileDialog diag = new System.Windows.Forms.OpenFileDialog();
                diag.Multiselect = true;
                diag.Filter      = "C# Files|*.cs";
                diag.Title       = "Open stock data script(s)...";
                string prevPath = diag.InitialDirectory;
                diag.InitialDirectory = Cfg.DataScriptPath;
                diag.RestoreDirectory = false;
                if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (string scriptPath in diag.FileNames)
                    {
                        Cfg.DataScriptPath = Path.GetDirectoryName(scriptPath);

                        // Skip this item if it has already been loaded
                        var script = scriptPath;
                        if (Cfg.DataScripts.Contains(script))
                        {
                            continue;
                        }

                        Cfg.ActionScripts.Add(scriptPath);
                        DecisionScriptListPanel.Controls.Add(new ScriptTextBox(this, scriptPath));
                    }
                }
            };
            GuiBox.Controls.Add(DecisionScriptBrowseButton);

            foreach (var script in Cfg.ActionScripts)
            {
                DecisionScriptListPanel.Controls.Add(new ScriptTextBox(this, script));
            }


            // Create the start button
            var RunAllButton = new GuiButton("Start");

            RunAllButton.Location = new Point(DecisionScriptListPanel.Bounds.Right - RunAllButton.Width - 5, DecisionScriptListPanel.Bounds.Top - RunAllButton.Height - 5);
            RunAllButton.MouseUp += (sender, e) =>
            {
                foreach (var s in Scripts)
                {
                    s.Run();
                }
            };
            GuiPanel.Controls.Add(RunAllButton);


            // Create the back button to leave the screen
            BackButton          = new PictureBox();
            BackButton.Image    = Bitmap.FromFile("Content/GUI/Back.png");
            BackButton.Size     = BackButton.Image.Size;
            BackButton.Location = new Point(5, 0);
            GuiPanel.Controls.Add(BackButton);

            // Define the overall panel that everything else is contained inside
            GuiPanel.BackColor = GuiStyle.BACKGROUND_COLOR;
            GuiPanel.Controls.Add(GuiBox);
            GuiPanel.Resize += (sender, e) =>
            {
                GuiBox.Size     = new Size(GuiPanel.Width - BackButton.Width, 300);
                GuiBox.Location = new System.Drawing.Point(BackButton.Width + 10, 10);
            };

            // Specify the interface that should be used to add a chart to the screen
            Script.StockSession.AddToGui += (control) =>
            {
                if (control != null)
                {
                    control.Location = new Point(ChartButton.Location.X, ChartButton.Bottom + 5);
                    GuiPanel.Controls.Add(control);
                }
            };
            Script.StockSession.GuiContainer = (System.Windows.Forms.Control)GuiPanel;

            // Create a button which adds a chart to the screen
            ChartButton          = new GuiButton("Add Chart");
            ChartButton.Location = new Point(5, DataFileTextbox.Bottom + 25);
            ChartButton.MouseUp += (sender, e) =>
            {
                Script.StockSession.AddChart(Cfg.SourceFiles.Replace("\r", "").Split('\n').ToList(), Cfg.DataScripts);
            };
            GuiPanel.Controls.Add(ChartButton);
        }
Example #2
0
        public HomePageForm()
        {
            InitializeComponent();
            this.FormClosing += HomePageForm_FormClosing;

            // Load the configuration
            this.Config = UserConfig.Load(UserConfig.CONFIG_FILE);

            // Create the interface to the stock data
            Robinhood = new RobinhoodInterface();
            DataAccessor.SetAccessor(new DataTableCache(Robinhood));
            Broker.SetBroker(Robinhood);

            if (String.IsNullOrEmpty(Config.DeviceToken))
            {
                Config.DeviceToken = Broker.Instance.GenerateDeviceToken();
                Config.Save(UserConfig.CONFIG_FILE);
            }

            UIList = new Panel();
            UIList.HorizontalScroll.Maximum = 0;
            UIList.AutoScroll             = false;
            UIList.VerticalScroll.Enabled = false;
            UIList.Resize += (sender, e) =>
            {
                foreach (Control c in UIList.Controls)
                {
                    c.Size = new Size(UIList.Width - 10, c.Height);
                }
            };
            UIList.ControlAdded   += UIList_Pack;
            UIList.ControlRemoved += UIList_Pack;
            UIList.Location        = new Point(340, 20);
            this.Controls.Add(UIList);

            UiScrollBar             = new CustomControls.CustomScrollbar();
            UiScrollBar.Minimum     = 0;
            UiScrollBar.Maximum     = UIList.Height;
            UiScrollBar.LargeChange = UiScrollBar.Maximum / UiScrollBar.Height;
            UiScrollBar.SmallChange = 15;
            UiScrollBar.Value       = 0;
            UiScrollBar.Scroll     += (sender, e) =>
            {
                //UIList.AutoScrollPosition = new Point(0, UiScrollBar.Value);
                UiScrollBar.Invalidate();
                //Application.DoEvents();
                UIList_Pack(sender, e);
            };
            UIList.Resize += (sender, e) =>
            {
                UiScrollBar.Bounds = new Rectangle(UIList.Right + 5, UIList.Top, 10, UIList.Height);
            };
            this.Controls.Add(UiScrollBar);

            // Create the account summary panel
            AccountSummaryPanel          = new Panel();
            AccountSummaryPanel.Location = new Point(50, 20);
            AccountSummaryPanel.Size     = new Size(270, 60);
            AccountSummaryPanel.Visible  = true;
            Controls.Add(AccountSummaryPanel);
            AccountSummaryPanel.Controls.Add(SummaryTotalLabel       = new Label());
            AccountSummaryPanel.Controls.Add(SummaryBuyingPowerLabel = new Label());
            int idx = 0;

            foreach (Label l in AccountSummaryPanel.Controls)
            {
                l.Size      = new Size(AccountSummaryPanel.Width / AccountSummaryPanel.Controls.Count, AccountSummaryPanel.Height);
                l.Location  = new Point(l.Width * idx++, 0);
                l.BackColor = GuiStyle.BACKGROUND_COLOR;
                l.ForeColor = GuiStyle.TEXT_COLOR;
                l.Font      = new Font(GuiStyle.Font.Name, 12, FontStyle.Bold);
                l.TextAlign = ContentAlignment.TopCenter;
            }
            UiUpdateTick       = new Timer();
            UiUpdateTick.Tick += (sender, e) =>
            {
                AccountSummaryPanel.Visible = AccountSummaryPanel.Bounds.Contains(PointToClient(MousePosition));
            };
            UiUpdateTick.Interval = 50;
            UiUpdateTick.Start();

            // Create the labels to show the account value and daily change
            CashLabel           = new Label();
            CashLabel.Location  = new Point(50, 20);
            CashLabel.Size      = new Size(270, 40);
            CashLabel.BackColor = GuiStyle.BACKGROUND_COLOR;
            CashLabel.ForeColor = GuiStyle.TEXT_COLOR;
            CashLabel.Font      = new Font(GuiStyle.Font.Name, 18, FontStyle.Bold);
            CashLabel.TextAlign = ContentAlignment.MiddleCenter;
            Controls.Add(CashLabel);
            ChangeLabel           = new Label();
            ChangeLabel.Location  = new Point(CashLabel.Location.X, CashLabel.Location.Y + CashLabel.Height);
            ChangeLabel.Size      = new Size(CashLabel.Size.Width, 20);
            ChangeLabel.BackColor = GuiStyle.BACKGROUND_COLOR;
            ChangeLabel.ForeColor = GuiStyle.TEXT_COLOR;
            ChangeLabel.Font      = new Font(GuiStyle.Font.Name, 8, FontStyle.Bold);
            ChangeLabel.TextAlign = ContentAlignment.MiddleCenter;
            Controls.Add(ChangeLabel);


            // Create the buy/sell menu
            BuySell          = new BuySellPanel();
            BuySell.Location = new Point(0, AccountSummaryPanel.Location.Y + AccountSummaryPanel.Height);
            BuySell.Size     = new Size(AccountSummaryPanel.Location.X + AccountSummaryPanel.Width, this.Height - BuySell.Location.Y);
            BuySell.Visible  = false;
            //BuySell.SubmitOrderButton.MouseClick += (sender, e) => {
            System.Threading.Tasks.Task orderMonitor = new Task((Action)(() =>
            {
                List <Broker.Order> orders;
                while ((orders = Broker.Instance.GetOrders()).Count > 0)
                {
                    // Check if the order gui matches the current state
                    List <StockList.StockLine> guiOrders = StockListHome.Stocks[StockList.ORDERS];
                    foreach (Broker.Order o in orders)
                    {
                        if (guiOrders.Find((a) => { return(a.Symbol.Equals(o.Symbol)); }) == null)
                        {
                            // Add the new order
                            StockListHome.Add(StockList.ORDERS, o.Symbol, new StockList.OrderSummary(o));
                        }
                    }

                    // Remove inactive orders from the GUI if needed
                    if (orders.Count < guiOrders.Count)
                    {
                        for (int i = 0; i < guiOrders.Count; i++)
                        {
                            if (orders.Find((a) => { return(a.Symbol.Equals(guiOrders[i].Symbol)); }) == null)
                            {
                                // Remove the order
                                guiOrders.RemoveAt(i--);
                            }
                        }
                    }

                    System.Threading.Thread.Sleep(250);
                }
            }));
            orderMonitor.Start();
            //};
            Controls.Add(BuySell);


            // Create the search box
            SearchHome                 = new SearchList();
            SearchHome.Size            = new Size(CashLabel.Width, 50);
            SearchHome.Location        = new Point(CashLabel.Location.X, ChangeLabel.Location.Y + ChangeLabel.Height);
            SearchHome.AutoSize        = true;
            SearchHome.AddToWatchlist += (string symbol) => { StockListHome.Add("Watchlist", symbol); };
            SearchHome.AddStockUi     += CreateStockChart;
            Controls.Add(SearchHome);

            // Create the list of stocks being examined
            StockListHome             = new StockList();
            StockListHome.Location    = new Point(SearchHome.Location.X, SearchHome.Location.Y + 100);
            StockListHome.Size        = new Size(300, 300);
            StockListHome.AddStockUi += CreateStockChart;
            StockListHome.GroupOrder  = new List <string>()
            {
                StockList.NOTIFICATIONS,
                StockList.ORDERS,
                StockList.POSITIONS,
                StockList.WATCHLIST
            };
            Controls.Add(StockListHome);

            // Create the menu
            Menu = new MenuBar(Config);
            Menu.ToggleButton.Location       = new Point(20, 20);
            Menu.LogIn.RememberLogIn.Checked = Config.RememberLogin;
            Menu.LogIn.LogInButton.MouseUp  += (sender, e) => {
                HomePageForm_AccountUpdate();
                if (Broker.Instance.IsSignedIn())
                {
                    SaveConfig();
                }
            };
            Controls.Add(Menu.ToggleButton);

            // Set up the resize handler
            this.ResizeEnd += HomePageForm_ResizeEnd;
            HomePageForm_ResizeEnd(this, EventArgs.Empty);

            // Sign in if authentification is available
            if (Config.RememberLogin && !string.IsNullOrEmpty(Config.AuthenticationToken))
            {
                Broker.Instance.SignIn(Config.AuthenticationToken);
            }

            // Wait to request stock data until after the window has been created
            this.HandleCreated += (sender, e) =>
            {
                foreach (var configObj in Config.StockCharts)
                {
                    CreateStockChart(StockChartPanel.LoadConfig(configObj));
                }
                foreach (string symbol in Config.LocalWatchlist)
                {
                    StockListHome.Add(StockList.WATCHLIST, symbol);
                }
                HomePageForm_AccountUpdate();
            };
        }