/// <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));
            });
        }
Example #2
0
        public static void PlaceOrder(Stock stock, TimeInForce tif, TradeStep tradeStep, Form parentForm)
        {
            string     errString      = string.Empty;
            PricePoint pricePoint     = null;
            var        newOrderSingle = new NewOrderSingle();

            try
            {
                Account account = rh.DownloadAllAccounts().Result.First();
                //if (pricePoint.PendingOrders == null)
                //  pricePoint.PendingOrders = new Dictionary<decimal, String>();
                if (tradeStep == TradeStep.Entry)
                {
                    pricePoint = stock.Entry;
                }
                else if (tradeStep == TradeStep.ProfitTarget)
                {
                    pricePoint = stock.PriceTarget;
                    if (pricePoint.NoOfShares > 0)
                    {
                        pricePoint.NoOfShares *= -1;
                    }
                }
                else if (tradeStep == TradeStep.StopLoss)
                {
                    if (stock.PendingOrders != null && stock.PendingOrders.Any(o => o.Side == Side.Sell && o.Trigger == "stop"))
                    {
                        ThreadPool.QueueUserWorkItem(Robinhood.CancelOrder,
                                                     new KeyValuePair <Form, ThreadedBindingList <OrderSnapshot> >(parentForm, stock.PendingOrders));
                    }

                    while (stock.PendingOrders != null && stock.PendingOrders.Count > 0)
                    {
                        Thread.Sleep(1000);
                    }

                    pricePoint = stock.StopLoss;
                    if (pricePoint.NoOfShares > 0)
                    {
                        pricePoint.NoOfShares *= -1;
                    }
                }
                else
                {
                    pricePoint = null;
                }

                Instrument instrument = null;
                while (instrument == null)
                {
                    try
                    {
                        instrument = rh.FindInstrument(stock.Ticker.ToUpperInvariant()).Result.First(i => i.Symbol == stock.Ticker);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Problem. Try again. " + e.Message);
                    }
                }

                lastOrderSuccess = false;
                if (pricePoint.Execution == CustomControls.PricePointControl.Execution.Limit ||
                    pricePoint.Execution == CustomControls.PricePointControl.Execution.Trailing)
                {
                    newOrderSingle             = new NewOrderSingle(instrument);
                    newOrderSingle.AccountUrl  = account.AccountUrl;
                    newOrderSingle.TimeInForce = tif;
                    newOrderSingle.Side        = pricePoint.NoOfShares > 0 ? Side.Buy : Side.Sell;
                    newOrderSingle.Quantity    = Math.Abs(pricePoint.NoOfShares);
                    newOrderSingle.OrderType   = (OrderType)Enum.Parse(typeof(OrderType), pricePoint.Type.ToString());
                    newOrderSingle.Trigger     = pricePoint.Trigger;
                    //if (pricePoint.Value == 0)
                    //{
                    //  //newOrderSingle.OrderType = OrderType.Market;
                    //}
                    //else
                    //{
                    //newOrderSingle.OrderType = OrderType.Limit;
                    if (pricePoint.Trigger == TriggerType.Stop)
                    {
                        //newOrderSingle.OrderType = OrderType.Market;
                        //newOrderSingle.Trigger = "stop";
                        if (newOrderSingle.OrderType == OrderType.Limit)
                        {
                            newOrderSingle.Price = pricePoint.Price;
                        }
                        else if (newOrderSingle.OrderType == OrderType.Market)
                        {
                            newOrderSingle.Price = stock.LastTradePrice;
                        }

                        newOrderSingle.StopPrice = pricePoint.Price + pricePoint.StopOffset;
                    }
                    else if (pricePoint.Trigger == TriggerType.Immediate)
                    {
                        newOrderSingle.Price = pricePoint.Price;
                    }
                    //}

                    var order = rh.PlaceOrder(newOrderSingle).Result;
                    PlacingOrder.Set();

                    if (order.State == "queued")
                    {
                        lastOrderSuccess = true;
                    }
                    // var test = rh.DownloadAllOrders().Result;
                    //test.Start();
                    //test.Wait();
                    //pricePoint.PendingOrders.Add(pricePoint.Value, order.CancelUrl.Uri.AbsoluteUri.First());
                }
                else if (pricePoint.Execution == CustomControls.PricePointControl.Execution.Spread)
                {
                    int noOfShare = pricePoint.NoOfShares > 0 ? 1 : -1;
                    foreach (decimal orderValue in pricePoint.ExecutionSpread)
                    {
                        newOrderSingle             = new NewOrderSingle(instrument);
                        newOrderSingle.AccountUrl  = account.AccountUrl;
                        newOrderSingle.TimeInForce = tif;
                        newOrderSingle.Side        = pricePoint.NoOfShares > 0 ? Side.Buy : Side.Sell;
                        newOrderSingle.OrderType   = (OrderType)Enum.Parse(typeof(OrderType), pricePoint.Type.ToString());
                        newOrderSingle.Trigger     = pricePoint.Trigger;
                        newOrderSingle.Quantity    = Math.Abs(noOfShare);
                        if (pricePoint.Trigger == TriggerType.Stop)
                        {
                            if (newOrderSingle.OrderType == OrderType.Limit)
                            {
                                newOrderSingle.Price = orderValue;
                            }
                            newOrderSingle.StopPrice = orderValue + 0.02m;
                        }
                        else if (pricePoint.Trigger == TriggerType.Immediate)
                        {
                            newOrderSingle.Price = orderValue;
                        }

                        var order = rh.PlaceOrder(newOrderSingle).Result;
                        //pricePoint.PendingOrders.Add(pricePoint.Value, order.CancelUrl);
                    }
                }

                Notification notifForm = new Notification();
                parentForm.Invoke((MethodInvoker) delegate()
                {
                    notifForm.label1.Text = string.Format("{0} {1} {2} Order Sent for {3} shares at {4}",
                                                          stock.Ticker, newOrderSingle.Side, newOrderSingle.OrderType,
                                                          newOrderSingle.Quantity, newOrderSingle.Price);
                    notifForm.Show();
                });
            }
            catch (WebException e)
            {
                if (pricePoint.Execution != CustomControls.PricePointControl.Execution.Trailing &&
                    !stock.ManageTrade)
                {
                    if (pricePoint.NoOfShares > 0)
                    {
                        errString = String.Format("Error Placing Buy Order for {0}, Check network connection", stock.Ticker);
                    }
                    else
                    {
                        errString = String.Format("Error Placing Sell Order for {0}, Check network connection", stock.Ticker);
                    }

                    Notification notifForm = new Notification();
                    parentForm.Invoke((MethodInvoker) delegate()
                    {
                        notifForm.label1.Text = string.Format(errString);
                        notifForm.Show();
                    });
                }
            }
            catch (HttpException e)
            {
                if (pricePoint.Execution != CustomControls.PricePointControl.Execution.Trailing &&
                    !(stock.ManageTrade && tradeStep == TradeStep.StopLoss))
                {
                    if (pricePoint.NoOfShares > 0)
                    {
                        errString = String.Format("Error Placing Buy Order for {0}, Check network connection", stock.Ticker);
                    }
                    else
                    {
                        errString = String.Format("Error Placing Sell Order for {0}, Check network connection", stock.Ticker);
                    }

                    Notification notifForm = new Notification();
                    parentForm.Invoke((MethodInvoker) delegate()
                    {
                        notifForm.label1.Text = string.Format(errString);
                        notifForm.Show();
                    });
                }
            }
            catch
            {
                if (pricePoint.Execution != CustomControls.PricePointControl.Execution.Trailing &&
                    !(stock.ManageTrade && tradeStep == TradeStep.StopLoss))
                {
                    if (pricePoint.NoOfShares > 0)
                    {
                        errString = String.Format("Error Placing Buy Order for {0}, Check buying power", stock.Ticker);
                    }
                    else
                    {
                        errString = String.Format("Error Placing Sell Order for {0}, Make sure you have enough shares available", stock.Ticker);
                    }

                    Notification notifForm = new Notification();
                    parentForm.Invoke((MethodInvoker) delegate()
                    {
                        notifForm.label1.Text = string.Format(errString);
                        notifForm.Show();
                    });
                }
            }
        }
Example #3
0
        public static void Main(string [] args)
        {
            var rh = new RobinhoodClient();

            authenticate(rh).Wait();

            Account account = rh.DownloadAllAccounts().Result.First();

            Instrument instrument = null;

            while (instrument == null)
            {
                try
                {
                    Console.Write("Symbol: ");
                    var symbol = Console.ReadLine().ToUpperInvariant();
                    instrument = rh.FindInstrument(symbol).Result.First(i => i.Symbol == symbol);
                    Console.WriteLine(instrument.Name);
                }
                catch
                {
                    Console.WriteLine("Problem. Try again.");
                }
            }

            int qty = 0;

            while (true)
            {
                Console.Write("Quantity (positive for buy, negative for sell): ");
                string q = Console.ReadLine();
                if (Int32.TryParse(q, out qty))
                {
                    break;
                }
            }

            decimal price = 0m;

            while (true)
            {
                Console.Write("Limit price (or 0 for Market order): ");
                string p = Console.ReadLine();
                if (Decimal.TryParse(p, out price))
                {
                    break;
                }
            }

            TimeInForce tif = TimeInForce.Unknown;

            while (true)
            {
                Console.Write("Time in Force (GFD or GTC): ");
                string t = Console.ReadLine();
                if (t.Equals("GFD", StringComparison.InvariantCultureIgnoreCase))
                {
                    tif = TimeInForce.GoodForDay;
                    break;
                }
                else if (t.Equals("GTC", StringComparison.InvariantCultureIgnoreCase))
                {
                    tif = TimeInForce.GoodTillCancel;
                    break;
                }
            }


            var newOrderSingle = new NewOrderSingle(instrument);

            newOrderSingle.AccountUrl = account.AccountUrl;
            newOrderSingle.Quantity   = Math.Abs(qty);
            newOrderSingle.Side       = qty > 0 ? Side.Buy : Side.Sell;

            newOrderSingle.TimeInForce = tif;
            if (price == 0)
            {
                newOrderSingle.OrderType = OrderType.Market;
            }
            else
            {
                newOrderSingle.OrderType = OrderType.Limit;
                newOrderSingle.Price     = price;
            }


            var order = rh.PlaceOrder(newOrderSingle).Result;

            Console.WriteLine("{0}\t{1}\t{2} x {3}\t{4}",
                              order.Side,
                              instrument.Symbol,
                              order.Quantity,
                              order.Price.HasValue ? order.Price.ToString() : "mkt",
                              order.State);
            if (!String.IsNullOrEmpty(order.RejectReason))
            {
                Console.WriteLine(order.RejectReason);
            }
            else
            {
                Console.WriteLine("Press C to cancel this order, or anything else to quit");
                var x = Console.ReadKey();
                if (x.KeyChar == 'c' || x.KeyChar == 'C')
                {
                    rh.CancelOrder(order.CancelUrl).Wait();
                    Console.WriteLine("Cancelled");
                }
            }
        }
Example #4
0
        public static void Main (string [] args)
        {
            var rh = new RobinhoodClient();

            authenticate(rh).Wait();

            Account account = rh.DownloadAllAccounts().Result.First();

            Instrument instrument = null;
            while (instrument == null)
            {
                try
                {
                    Console.Write("Symbol: ");
                    var symbol = Console.ReadLine().ToUpperInvariant();
                    instrument = rh.FindInstrument(symbol).Result.First(i => i.Symbol == symbol);
                    Console.WriteLine(instrument.Name);
                }
                catch
                {
                    Console.WriteLine("Problem. Try again.");
                }
            }

            int qty = 0;
            while (true)
            {
                
                Console.Write("Quantity (positive for buy, negative for sell): ");
                string q = Console.ReadLine();
                if (Int32.TryParse(q, out qty))
                {
                    break;
                }
            }

            decimal price = 0m;
            while (true)
            {
                Console.Write("Limit price (or 0 for Market order): ");
                string p = Console.ReadLine();
                if (Decimal.TryParse(p, out price))
                {
                    break;
                }
            }

            TimeInForce tif = TimeInForce.Unknown;
            while (true)
            {
                Console.Write("Time in Force (GFD or GTC): ");
                string t = Console.ReadLine();
                if (t.Equals("GFD", StringComparison.InvariantCultureIgnoreCase))
                {
                    tif = TimeInForce.GoodForDay;
                    break;
                }
                else if (t.Equals("GTC", StringComparison.InvariantCultureIgnoreCase))
                {
                    tif = TimeInForce.GoodTillCancel;
                    break;
                }
            }


            var newOrderSingle = new NewOrderSingle(instrument);
            newOrderSingle.AccountUrl = account.AccountUrl;
            newOrderSingle.Quantity   = Math.Abs(qty);
            newOrderSingle.Side       = qty > 0 ? Side.Buy : Side.Sell;

            newOrderSingle.TimeInForce = tif;
            if (price == 0)
            {
                newOrderSingle.OrderType = OrderType.Market;
            }
            else
            {
                newOrderSingle.OrderType = OrderType.Limit;
                newOrderSingle.Price = price;
            }


            var order = rh.PlaceOrder(newOrderSingle).Result;
            Console.WriteLine("{0}\t{1}\t{2} x {3}\t{4}",
                                order.Side,
                                instrument.Symbol,
                                order.Quantity,
                                order.Price.HasValue ? order.Price.ToString() : "mkt",
                                order.State);
            if (!String.IsNullOrEmpty(order.RejectReason))
            {
                Console.WriteLine(order.RejectReason);
            }
            else
            {
                Console.WriteLine("Press C to cancel this order, or anything else to quit");
                var x = Console.ReadKey();
                if (x.KeyChar == 'c' || x.KeyChar == 'C')
                {
                    rh.CancelOrder(order.CancelUrl).Wait();
                    Console.WriteLine("Cancelled");
                }
            }
        }