private void MainWindowViewModel_OnCandleStickDataEventHandler(object sender, CandleStickDataEventArgs e) { //CandleStickCollection List <string> xLabels = new List <string>(e.Data.Count); ChartValues <OhlcPoint> candles = new ChartValues <OhlcPoint>(); e.Data.Reverse(); foreach (var cs in e.Data) { candles.Add(new OhlcPoint(cs.Open, cs.Highest, cs.Lowest, cs.Close)); xLabels.Add(cs.SampleFor.ToShortDateString() + " " + cs.SampleFor.ToShortTimeString()); } App.Current.Dispatcher.Invoke((Action) delegate { CandleStickCollection = new SeriesCollection { new CandleSeries { Values = candles } }; CandleStickXLabels = xLabels.ToArray(); }); }
void qResponseMsgConsumer_Received(object sender, BasicDeliverEventArgs e) { try { var body = e.Body; IBasicProperties props = e.BasicProperties; _channel.BasicAck(e.DeliveryTag, false); Task.Factory.StartNew(() => { if (props.Type != null && props.Type.Equals("orderUpdate", StringComparison.OrdinalIgnoreCase)) { if (null != OnOrderUpdateReceived) { GenericRequestResponseDictionary update = JsonConvert.DeserializeObject <GenericRequestResponseDictionary>(ASCIIEncoding.UTF8.GetString(body)); OnOrderUpdateReceived(this, new GenericResponseEventArgs() { GenericResponse = update }); } } else if (props.Type != null && props.Type.Equals("update", StringComparison.OrdinalIgnoreCase)) { if (null != OnUpdateReceived) { ClientUpdateEventArgs update = JsonConvert.DeserializeObject <ClientUpdateEventArgs>(ASCIIEncoding.UTF8.GetString(body)); if (null == update.Positions) { update.Positions = new Dictionary <Guid, TradePosition>(); } //for net calculation Dictionary <string, List <TradePosition> > netPosition = new Dictionary <string, List <TradePosition> >(update.Positions.Count); //fill in the current price against each of the positions foreach (var p in update.Positions) { if (p.Value.OrderType.Equals(TradePosition.ORDER_TYPE_BUY)) { p.Value.CurrentPrice = (update.ClientQuotes.ContainsKey(p.Value.Commodity)) ? update.ClientQuotes[p.Value.Commodity].Bid : decimal.Zero; } else { p.Value.CurrentPrice = (update.ClientQuotes.ContainsKey(p.Value.Commodity)) ? update.ClientQuotes[p.Value.Commodity].Ask : decimal.Zero; } string key = p.Value.Commodity + "_" + p.Value.OrderType; if (!netPosition.ContainsKey(key)) { netPosition[key] = new List <TradePosition>(); } netPosition[key].Add(p.Value); } //determine net position update.NetPosition = new Dictionary <Guid, TradePosition>(); foreach (var row in netPosition) { string commodity = row.Key.Split(new char[] { '_' })[0]; string type = row.Key.Split(new char[] { '_' })[1]; decimal sumAmt = 0; decimal sumPL = 0; decimal price = 0; foreach (var p in row.Value) { sumAmt += p.Amount; sumPL += p.CurrentPl; price += p.OpenPrice; } price /= (decimal)row.Value.Count; TradePosition position = new TradePosition(); position.OrderId = Guid.NewGuid(); position.Commodity = commodity; position.OrderType = type; position.OpenPrice = price; position.Amount = sumAmt; position.CurrentPl = sumPL; if (type.Equals(TradePosition.ORDER_TYPE_BUY)) { position.CurrentPrice = update.ClientQuotes[commodity].Bid; } else { position.CurrentPrice = update.ClientQuotes[commodity].Ask; } update.NetPosition.Add(position.OrderId, position); } //update net prices on the net positions OnUpdateReceived(this, update); } } else if (props.Type != null && props.Type.Equals("accountStatus", StringComparison.OrdinalIgnoreCase)) { if (null != OnAccountStatusEventReceived) { GenericRequestResponseDictionary update = JsonConvert.DeserializeObject <GenericRequestResponseDictionary>(ASCIIEncoding.UTF8.GetString(body)); OnAccountStatusEventReceived(this, new GenericResponseEventArgs() { GenericResponse = update }); } } else if (props.Type != null && props.Type.Equals("candlestick", StringComparison.OrdinalIgnoreCase)) { if (null != OnCandleStickDataEventHandler) { List <CandleStick> data = JsonConvert.DeserializeObject <List <CandleStick> >(ASCIIEncoding.UTF8.GetString(body)); CandleStickDataEventArgs args = new CandleStickDataEventArgs() { Data = data }; OnCandleStickDataEventHandler(this, args); } } else if (props.Type != null && props.Type.Equals("notification")) { string payload = ASCIIEncoding.UTF8.GetString(body); if (null != OnNotificationReceived) { OnNotificationReceived(payload); } } }); } catch (Exception ex) { log.Error(ex); } }