/// <summary>
        /// Submits an order to be executed by the broker
        /// </summary>
        /// <param name="order">The order to submit</param>
        public void SubmitOrder(Broker.Order order)
        {
            Dictionary <Broker.Order.OrderType, BasicallyMe.RobinhoodNet.OrderType> orderTypeLookup = new Dictionary <Broker.Order.OrderType, OrderType>()
            {
                { Broker.Order.OrderType.MARKET, BasicallyMe.RobinhoodNet.OrderType.Market },
                { Broker.Order.OrderType.LIMIT, BasicallyMe.RobinhoodNet.OrderType.Limit },
                { Broker.Order.OrderType.STOP, BasicallyMe.RobinhoodNet.OrderType.StopLoss },
                { Broker.Order.OrderType.STOP_LIMIT, BasicallyMe.RobinhoodNet.OrderType.StopLoss },
            };
            bool isStopOrder = ((order.Type == Broker.Order.OrderType.STOP) || (order.Type == Broker.Order.OrderType.STOP_LIMIT));

            NewOrderSingle newOrder = new NewOrderSingle()
            {
                AccountUrl    = getAccount().AccountUrl,
                InstrumentUrl = Client.FindInstrument(order.Symbol).Result.First().InstrumentUrl,
                OrderType     = orderTypeLookup[order.Type],
                Price         = order.LimitPrice,
                Quantity      = (int)order.Quantity,
                Side          = ((order.BuySell == Broker.Order.BuySellType.BUY) ? Side.Buy : Side.Sell),
                StopPrice     = (isStopOrder ? order.StopPrice : (decimal?)null),
                Symbol        = order.Symbol,
                TimeInForce   = TimeInForce.GoodForDay,
                Trigger       = isStopOrder ? TriggerType.Stop : TriggerType.Immediate
            };

            Client.PlaceOrder(newOrder).ContinueWith((result) =>
            {
                OrderSnapshot orderResult = result.Result;
                ActiveOrders.Add(new RobinhoodOrder(order.Symbol, orderResult));
            });
        }
Пример #2
0
            public OrderSummary(Broker.Order orderInfo) : base(DataAccessor.Subscribe(orderInfo.Symbol, DataAccessor.SUBSCRIBE_FIVE_SEC))
            {
                this.OrderInfo = orderInfo;

                OrderLabel = new Label();
                //OrderLabel.Font = GuiStyle.Font;
                //OrderLabel.ForeColor = GuiStyle.DARK_GREY;
                OrderLabel.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
                OrderLabel.Text      = string.Format("{0} {1}", orderInfo.BuySell == Broker.Order.BuySellType.BUY ? "Buy" : "Sell", orderInfo.Quantity);
                OrderLabel.Size      = new System.Drawing.Size(60, 15);
                OrderLabel.Location  = new System.Drawing.Point(5, 5);
                Controls.Add(OrderLabel);

                CancelOrderButton             = new GuiButton("Cancel");
                CancelOrderButton.Location    = new System.Drawing.Point(OrderLabel.Location.X, OrderLabel.Location.Y + OrderLabel.Height + 5);
                CancelOrderButton.MouseClick += (sender, e) =>
                {
                    Broker.Instance.CancelOrder(OrderInfo.Symbol);
                };
                Controls.Add(CancelOrderButton);
            }
Пример #3
0
        public BuySellPanel()
        {
            Instance       = this;
            this.BackColor = GuiStyle.BACKGROUND_COLOR;

            // Create the order type selection button
            BuySellLabel           = new Label();
            BuySellLabel.Size      = new System.Drawing.Size(50, 25);
            BuySellLabel.Location  = new System.Drawing.Point(50, 50);
            BuySellLabel.ForeColor = GuiStyle.TEXT_COLOR;
            BuySellLabel.Font      = new System.Drawing.Font(GuiStyle.Font.Name, 10, System.Drawing.FontStyle.Bold);
            BuySellLabel.TextAlign = System.Drawing.ContentAlignment.TopRight;
            this.Controls.Add(BuySellLabel);
            OrderTypeButton             = new GuiButton(OrderNames[(int)SelectedOrderType]);
            OrderTypeButton.Location    = new System.Drawing.Point(BuySellLabel.Bounds.Right + 5, 50);
            OrderTypeButton.MouseClick += (sender, e) => {
                OrderTypeSelectionPanel.Visible = !OrderTypeSelectionPanel.Visible;
                if (!OrderTypeSelectionPanel.Visible)
                {
                    RefreshOrderOptionList();
                }
            };
            this.Controls.Add(OrderTypeButton);

            // Create the order type selection list
            OrderTypeSelectionPanel          = new Panel();
            OrderTypeSelectionPanel.Location = new System.Drawing.Point(0, OrderTypeButton.Location.Y + OrderTypeButton.Height + 5);
            for (int idx = 0; idx < OrderNames.Length; idx++)
            {
                Broker.Order.OrderType ot = (Broker.Order.OrderType)idx;
                GuiButton orderButton     = new GuiButton(OrderNames[idx]);
                orderButton.Location    = new System.Drawing.Point(OrderTypeButton.Location.X + 10, (idx * (orderButton.Height + 5)) + 5);
                orderButton.MouseClick += (sender, e) =>
                {
                    SelectedOrderType = ot;
                    RefreshOrderTypeButtons();
                };
                OrderTypeSelectionPanel.Controls.Add(orderButton);
            }
            OrderTypeSelectionPanel.Visible = false;
            RefreshOrderTypeButtons();
            this.Controls.Add(OrderTypeSelectionPanel);

            this.Resize += (sender, e) =>
            {
                OrderTypeSelectionPanel.Width  = this.Width;
                OrderTypeSelectionPanel.Height = (this.Height - OrderTypeSelectionPanel.Location.Y);
            };

            // Create the back button
            BackButton             = new PictureBox();
            BackButton.Image       = System.Drawing.Bitmap.FromFile("Content/GUI/Back.png");
            BackButton.Size        = BackButton.Image.Size;
            BackButton.Location    = new System.Drawing.Point(10, 10);
            BackButton.MouseClick += (sender, e) => { this.Visible = false; };
            this.Controls.Add(BackButton);

            // Create the info label
            OrderInfoLabel                  = new Label();
            OrderInfoLabel.Size             = new System.Drawing.Size(this.Width, 60);
            OrderInfoLabel.Text             = "";
            OrderInfoLabel.ForeColor        = GuiStyle.TEXT_COLOR;
            OrderInfoLabel.Visible          = false;
            OrderInfoLabel.LocationChanged += (sender, e) =>
            {
                ReviewOrderButton.Location = new System.Drawing.Point(OrderInfoLabel.Location.X, OrderInfoLabel.Bounds.Bottom + 5);
                EditOrderButton.Location   = ReviewOrderButton.Location;
                SubmitOrderButton.Location = new System.Drawing.Point(EditOrderButton.Location.X, EditOrderButton.Bounds.Bottom + 5);
            };
            OrderInfoLabel.Location = new System.Drawing.Point(BackButton.Bounds.Right + 5, 220);
            this.Controls.Add(OrderInfoLabel);

            // Create the order review and submit buttons
            ReviewOrderButton.MouseClick += (sender, e) =>
            {
                OrderInfoLabel.Text = GetOrderSummary();
                OrderInfoLabel.Show();
                ReviewOrderButton.Hide();

                EditOrderButton.Show();
                SubmitOrderButton.Text = string.Format("Submit {0}", (BuySell == Broker.Order.BuySellType.BUY) ? "Buy" : "Sell");
                SubmitOrderButton.Show();
            };
            this.Controls.Add(ReviewOrderButton);

            EditOrderButton.MouseClick += (sender, e) =>
            {
                OrderInfoLabel.Hide();
                EditOrderButton.Hide();
                SubmitOrderButton.Hide();
                ReviewOrderButton.Show();
            };
            this.Controls.Add(EditOrderButton);

            SubmitOrderButton.MouseClick += (sender, e) =>
            {
                // Submit the order
                Broker.Order newOrder = new Broker.Order()
                {
                    BuySell    = BuySell,
                    LimitPrice = GetTradePrice(),
                    Quantity   = Shares,
                    Symbol     = Symbol,
                    Type       = SelectedOrderType,
                };
                decimal.TryParse(StopPriceTextbox.Text, out newOrder.StopPrice);
                Broker.Instance.SubmitOrder(newOrder);

                // Hide the buy/sell menu
                this.Hide();
            };
            this.Controls.Add(SubmitOrderButton);

            // Create the all and half buying power shortcut buttons
            GuiButton halfButton = new GuiButton("Half");

            halfButton.MouseClick += (sender, e) => {
                if (BuySell == Broker.Order.BuySellType.BUY)
                {
                    if (TargetTotalValue == 0)
                    {
                        TargetTotalValue = BuyingPower;
                    }
                    TargetTotalValue /= 2;
                    TargetShares      = 0;
                    UpdateTransaction();
                }
                else
                {
                    if (TargetShares == 0)
                    {
                        TargetShares = OwnedShares;
                    }
                    TargetShares    /= 2;
                    TargetTotalValue = 0;
                    UpdateTransaction();
                }
            };
            this.Controls.Add(halfButton);
            GuiButton allButton = new GuiButton("All");

            allButton.MouseClick += (sender, e) =>
            {
                if (BuySell == Broker.Order.BuySellType.BUY)
                {
                    TargetTotalValue = BuyingPower;
                    TargetShares     = 0;
                    UpdateTransaction();
                }
                else
                {
                    TargetShares     = OwnedShares;
                    TargetTotalValue = 0;
                    UpdateTransaction();
                }
            };
            this.Controls.Add(allButton);
            TotalTextbox.LocationChanged += (sender, e) =>
            {
                TextBox t = (TextBox)sender;
                halfButton.Location = new System.Drawing.Point(t.Bounds.Right - halfButton.Width, t.Bounds.Bottom + 5);
                allButton.Location  = new System.Drawing.Point(halfButton.Location.X - allButton.Width - 5, halfButton.Location.Y);
            };

            // Create the order textboxes
            Tuple <TextBox, string>[] textboxes = new Tuple <TextBox, string>[]
            {
                new Tuple <TextBox, string>(StopPriceTextbox, "Stop Price"),
                new Tuple <TextBox, string>(LimitPriceTextbox, "Limit Price"),
                new Tuple <TextBox, string>(SharesTextbox, "Shares"),
                new Tuple <TextBox, string>(PricePerShareTextbox, "Market Price"),
                new Tuple <TextBox, string>(TotalTextbox, "Total Value")
            };
            foreach (Tuple <TextBox, string> t in textboxes)
            {
                Label name = new Label();
                name.Size      = new System.Drawing.Size(150, 20);
                name.Location  = new System.Drawing.Point(50, 0);
                name.Text      = t.Item2;
                name.ForeColor = GuiStyle.TEXT_COLOR;
                name.Font      = new System.Drawing.Font(GuiStyle.FONT_NAME, 9, System.Drawing.FontStyle.Bold);
                this.Controls.Add(name);

                t.Item1.Size             = new System.Drawing.Size(100, 20);
                t.Item1.LocationChanged += (sender, e) => { name.Location = new System.Drawing.Point(name.Location.X, ((TextBox)sender).Location.Y); };
                t.Item1.VisibleChanged  += (sender, e) => { name.Visible = ((TextBox)sender).Visible; };
                t.Item1.Location         = new System.Drawing.Point(name.Bounds.Right, 0);
                t.Item1.ForeColor        = GuiStyle.TEXT_COLOR;
                t.Item1.BackColor        = GuiStyle.DARK_GREY;

                this.Controls.Add(t.Item1);
            }
            RefreshOrderOptionList();

            // Customization of textboxes
            PricePerShareTextbox.BorderStyle = BorderStyle.None;
            PricePerShareTextbox.BackColor   = GuiStyle.BACKGROUND_COLOR;
            PricePerShareTextbox.ForeColor   = GuiStyle.PRICE_COLOR_POSITIVE;
            PricePerShareTextbox.ReadOnly    = true;
            SharesTextbox.KeyDown           += (sender, e) =>
            {
                if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
                {
                    if (decimal.TryParse(((TextBox)sender).Text, out TargetShares))
                    {
                        // Remove focus from the textbox so that it will be updated
                        ((TextBox)sender).Parent.Focus();
                        UpdateTransaction();
                    }
                    TargetTotalValue = 0;
                }
            };
            TotalTextbox.KeyDown += (sender, e) =>
            {
                if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
                {
                    decimal val;
                    if (decimal.TryParse(TotalTextbox.Text.Replace("$", "").Replace(",", ""), out val))
                    {
                        ((TextBox)sender).Parent.Focus();
                        decimal price = GetTradePrice();
                        if (price != 0)
                        {
                            Shares = Math.Floor(val / price);
                        }
                        TargetTotalValue = val;
                    }
                    TargetShares = 0;
                }
            };
            LimitPriceTextbox.KeyDown += (sender, e) =>
            {
                if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
                {
                    UpdateTransaction();
                }
            };
        }