/// <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); } }
void Work() { State nextState = State.Initial; State actState = State.Initial; State prevState = State.Initial; while (true) { Events events = (Events)notify.WaitOne(); switch (actState) { case State.Initial: if (socket.IsConnected) { nextState = State.ConnectedNoLease; } else if (socket.IsConnecting) { } else { socket.ConnectAsync(Settings.ConnectionServer); } break; case State.ConnectedNoLease: if (events.HasFlag(Events.Disconnected)) { nextState = State.Disconnected; } else if (client.HasLease) { nextState = State.ConnectedWithLease; } break; case State.ConnectedWithLease: if (events.HasFlag(Events.Disconnected)) { nextState = State.Disconnected; } else if (!client.HasLease) { nextState = State.ConnectedNoLease; } else { //We are connected and have a lease. foreach (PlayerInfo tx in tileClients) { foreach (PlayerInfo rx in tileClients.Where(p => p != tx)) { client.SendMessage(rx.ID, serializer.Serialize(new TileCMD_UpdatePlayerInfo() { Playerinfo = tx })); } } TileBaseCmd cmd; if (commands.TryDequeue(out cmd)) { ProcessCommand(cmd); if (commands.Any()) { notify.SetBits((UInt32)Events.CommandRecieved); } } } break; } if (actState != nextState) { notify.SetBits((UInt32)Events.StateChanged); prevState = actState; actState = nextState; Console.WriteLine($"{this.GetType().Name}: State change '{prevState}' => {actState}"); } } }
void UpdateGrids() { try { int scrollPosition = 0; int rowPositionsGrid = 0, rowWatchList = 0; //Getting sort order var propertyInfo = typeof(Stock).GetProperty(stocks.sortProperty); List <Stock> sortedPositions = stocks.Where(s => s.NoOfShares > 0).OrderBy(x => x.NoOfShares).ToList(); List <Stock> sortedWatch = stocks.Where(s => s.NoOfShares == 0).OrderBy(x => x.NoOfShares).ToList(); //Saving selected row index if (PositionsGrid.Rows.Count > 0 && PositionsGrid.SelectedRows != null && PositionsGrid.SelectedRows.Count > 0) { rowPositionsGrid = PositionsGrid.SelectedRows[0].Index; } if (WatchListGrid.Rows.Count > 0 && WatchListGrid.SelectedRows != null && WatchListGrid.SelectedRows.Count > 0) { rowWatchList = WatchListGrid.SelectedRows[0].Index; } //if list is sorted ascending if (stocks.sortDirection == ListSortDirection.Ascending) { scrollPosition = PositionsGrid.FirstDisplayedScrollingRowIndex; PositionsGrid.DataSource = new ThreadedBindingList <Stock>(sortedPositions .OrderBy(x => propertyInfo.GetValue(x, null)) .ToList()); if (scrollPosition > 0) { PositionsGrid.FirstDisplayedScrollingRowIndex = scrollPosition; } scrollPosition = WatchListGrid.FirstDisplayedScrollingRowIndex; WatchListGrid.DataSource = new ThreadedBindingList <Stock>(sortedWatch .OrderBy(x => propertyInfo.GetValue(x, null)) .ToList()); if (scrollPosition > 0) { WatchListGrid.FirstDisplayedScrollingRowIndex = scrollPosition; } } else //if list is sorted desending { scrollPosition = PositionsGrid.FirstDisplayedScrollingRowIndex; PositionsGrid.DataSource = new ThreadedBindingList <Stock>(sortedPositions .OrderByDescending(x => propertyInfo.GetValue(x, null)) .ToList()); if (scrollPosition > 0) { PositionsGrid.FirstDisplayedScrollingRowIndex = scrollPosition; } scrollPosition = WatchListGrid.FirstDisplayedScrollingRowIndex; WatchListGrid.DataSource = new ThreadedBindingList <Stock>(sortedWatch .OrderByDescending(x => propertyInfo.GetValue(x, null)) .ToList()); WatchListGrid.DataSource = new ThreadedBindingList <Stock>(sortedWatch .OrderBy(x => propertyInfo.GetValue(x, null)) .ToList()); if (scrollPosition > 0) { WatchListGrid.FirstDisplayedScrollingRowIndex = scrollPosition; } } if (rowPositionsGrid != 0 && rowPositionsGrid < PositionsGrid.Rows.Count) { PositionsGrid.Rows[rowPositionsGrid].Selected = true; } if (rowWatchList != 0 && rowWatchList < WatchListGrid.Rows.Count) { WatchListGrid.Rows[rowWatchList].Selected = true; } } catch { } }