Esempio n. 1
0
        /// <summary>
        /// Refreshes Order history/Queued orders
        /// </summary>
        /// <param name="param"></param>
        void PendingOrdersUpdater(object param)
        {
            while (true)
            {
                try
                {
                    IList <OrderSnapshot> queuedOrders    = new List <OrderSnapshot>();
                    IList <OrderSnapshot> completedOrders = new List <OrderSnapshot>();
                    IList <OrderSnapshot> cancelledOrders = new List <OrderSnapshot>();
                    //get the most recent 1k orders
                    var result = Robinhood.rh.DownloadOrders().Result;
                    int i      = 1;
                    do
                    {
                        ThreadedBindingList <OrderSnapshot> oList = new ThreadedBindingList <OrderSnapshot>(result.Items.ToList());

                        if (oList != null && oList.Any(o => o.State == "queued") || oList.Any(o => o.State == "confirmed"))
                        {
                            //adding confirmed orders to pending order
                            ((List <OrderSnapshot>)queuedOrders).AddRange(oList.Where(o => o.State == "confirmed"));
                            //adding queueed order to pending orders
                            ((List <OrderSnapshot>)queuedOrders).AddRange(oList.Where(o => o.State == "queued" && !queuedOrders.Any(or => or.OrderId == o.OrderId)).ToList());
                        }
                        //TODO: add support for "partially_filled" orders
                        if (oList != null && oList.Any(o => o.State == "filled"))
                        {
                            //adding filled orders
                            ((List <OrderSnapshot>)completedOrders).AddRange(oList.Where(o => o.State == "filled").ToList());
                        }
                        if (oList != null && oList.Any(o => o.State == "cancelled"))
                        {
                            //adding canceled orders
                            ((List <OrderSnapshot>)cancelledOrders).AddRange(oList.Where(o => o.State == "cancelled").ToList());
                        }

                        //removing filled and cancelled orders from pending orders
                        lock (AllPendingOrders)
                        {
                            for (int j = 0; j < AllPendingOrders.Count(); j++)
                            {
                                if (cancelledOrders.Any(or => or.OrderId == AllPendingOrders[j].OrderId) ||
                                    completedOrders.Any(or => or.OrderId == AllPendingOrders[j].OrderId))
                                {
                                    AllPendingOrders.RemoveAt(j);
                                }
                            }
                        }

                        //From the most recent 1k orders adding opened & filled order to recent orders ui grid
                        if (i == 1)
                        {
                            RecentOrders = new ThreadedBindingList <OrderSnapshot>(oList.Where(o => o.State != "cancelled").ToList());
                            this.dataGridViewRecentOrders.BeginInvoke((Action) delegate
                            {
                                this.UpdateRecentOrders();
                            });
                        }

                        //after downloading recent  3 order break loop and start from most recent order again
                        if (i >= 3)
                        {
                            break;
                        }
                        i++; // going back 1k orders
                        result = Robinhood.rh.DownloadOrders(result.Next).Result;
                    } while (result.Items != null);

                    //from the most recent 3k order adding open order to a list
                    foreach (OrderSnapshot order in queuedOrders)
                    {
                        if (!AllPendingOrders.Any(o => o.OrderId == order.OrderId))
                        {
                            lock (AllPendingOrders)
                            {
                                AllPendingOrders.Add(order);
                            }
                        }
                    }

                    foreach (Stock s in stocks)
                    {
                        s.PendingOrders = new ThreadedBindingList <OrderSnapshot>(AllPendingOrders.Where(o => o.InstrumentId == s.InstrumentURL).ToList());
                    }
                }
                catch (Exception e)
                {
                }

                //sleep for 1 second before fetching recent 3k orders again
                Thread.Sleep(1000);
            }
        }