public StreamingBalanceRecord GetBalance() { BalanceRecord = null; using (StreamingAPIConnector streamingApi = new StreamingAPIConnector(serverData)) using (SyncAPIConnector connector = new SyncAPIConnector(serverData)) { try { Credentials credentials = new Credentials(userId, password, appGuid, appName); LoginResponse loginResponse = APICommandFactory.ExecuteLoginCommand(connector, credentials, true); streamingApi.StreamSessionId = loginResponse.StreamSessionId; streamingApi.Connect(); streamingApi.SubscribeBalance(); streamingApi.BalanceRecordReceived += OnBalanceChanged; Stopwatch sw = new Stopwatch(); sw.Start(); var timestamp = sw.ElapsedMilliseconds; //wait for balance while (BalanceRecord == null) { Thread.Sleep(50); if (timestamp + timeout < sw.ElapsedMilliseconds) { sw.Stop(); log.Info($"Unable to fetch balance, timeout of [{timeout}s] elapsed."); break; } } } catch (Exception e) { log.Error("Unable to fetch balance, exception occured.", e); } finally { streamingApi.UnsubscribeBalance(); } return(BalanceRecord); } }
/// <summary> /// Reads stream message. /// </summary> private void ReadStreamMessage() { try { String message = ReadMessage(); if (message != null) { JSONObject responseBody = JSONObject.Parse(message); string commandName = responseBody["command"].ToString(); if (commandName == "tickPrices") { StreamingTickRecord tickRecord = new StreamingTickRecord(); tickRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (TickRecordReceived != null) { TickRecordReceived.Invoke(tickRecord); } if (sl != null) { sl.ReceiveTickRecord(tickRecord); } } else if (commandName == "trade") { StreamingTradeRecord tradeRecord = new StreamingTradeRecord(); tradeRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (TradeRecordReceived != null) { TradeRecordReceived.Invoke(tradeRecord); } if (sl != null) { sl.ReceiveTradeRecord(tradeRecord); } } else if (commandName == "balance") { StreamingBalanceRecord balanceRecord = new StreamingBalanceRecord(); balanceRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (BalanceRecordReceived != null) { BalanceRecordReceived.Invoke(balanceRecord); } if (sl != null) { sl.ReceiveBalanceRecord(balanceRecord); } } else if (commandName == "tradeStatus") { StreamingTradeStatusRecord tradeStatusRecord = new StreamingTradeStatusRecord(); tradeStatusRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (TradeStatusRecordReceived != null) { TradeStatusRecordReceived.Invoke(tradeStatusRecord); } if (sl != null) { sl.ReceiveTradeStatusRecord(tradeStatusRecord); } } else if (commandName == "profit") { StreamingProfitRecord profitRecord = new StreamingProfitRecord(); profitRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (ProfitRecordReceived != null) { ProfitRecordReceived.Invoke(profitRecord); } if (sl != null) { sl.ReceiveProfitRecord(profitRecord); } } else if (commandName == "news") { StreamingNewsRecord newsRecord = new StreamingNewsRecord(); newsRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (NewsRecordReceived != null) { NewsRecordReceived.Invoke(newsRecord); } if (sl != null) { sl.ReceiveNewsRecord(newsRecord); } } else if (commandName == "keepAlive") { StreamingKeepAliveRecord keepAliveRecord = new StreamingKeepAliveRecord(); keepAliveRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (KeepAliveRecordReceived != null) { KeepAliveRecordReceived.Invoke(keepAliveRecord); } if (sl != null) { sl.ReceiveKeepAliveRecord(keepAliveRecord); } } else if (commandName == "candle") { StreamingCandleRecord candleRecord = new StreamingCandleRecord(); candleRecord.FieldsFromJSONObject((JSONObject)responseBody["data"]); if (CandleRecordReceived != null) { CandleRecordReceived.Invoke(candleRecord); } if (sl != null) { sl.ReceiveCandleRecord(candleRecord); } } else { throw new APICommunicationException("Unknown streaming record received"); } } } catch (Exception) { } }
private void OnBalanceChanged(StreamingBalanceRecord balanceRecord) { BalanceRecord = balanceRecord; }