/// <summary> /// Updates the status of active orders /// </summary> private void RefreshActiveOrders() { // Periodically update the entire list of orders if ((DateTime.Now - OrderLastUpdated).TotalSeconds > 60) { OrderLastUpdated = DateTime.MaxValue; System.Threading.Tasks.Task <IList <OrderSnapshot> > response; if (ActiveOrders.Count > 0) { response = Client.DownloadOrders(ActiveOrders[0].UpdatedAt.AddDays(-1)); } else { response = Client.DownloadAllOrders(); } response.ContinueWith((result) => { ActiveOrderMutex.WaitOne(); if ((result.Status != System.Threading.Tasks.TaskStatus.Canceled) && result.IsCompleted && !result.IsFaulted) { foreach (OrderSnapshot s in result.Result) { RobinhoodOrder order = ActiveOrders.Find(a => a.OrderId.Equals(s.OrderId)); if (order == null) { order = new RobinhoodOrder(Client.DownloadInstrument(s.InstrumentId).Result.Symbol, s); ActiveOrders.Add(order); } order.Update(s); } } OrderLastUpdated = DateTime.Now; ActiveOrderMutex.ReleaseMutex(); }); } else { // Update pending orders more frequently for (int orderIdx = 0; orderIdx < ActiveOrders.Count; orderIdx++) { RobinhoodOrder order = ActiveOrders[orderIdx]; if ((order.Status == Broker.Order.OrderStatus.PENDING) && ((DateTime.Now - order.RefreshedAt).TotalSeconds >= 5)) { // Update the status of this order Client.DownloadSingleOrder(order.OrderId).ContinueWith((result) => { if (result.IsCompleted && !result.IsFaulted) { order.Update(result.Result); } }); break; } } } }