コード例 #1
0
        public BitmexSubscribeRequest AddSubscription(BitmexSubscribtions table, string symbol = null)
        {
            string endpoint = JsonConvert.SerializeObject(table, new BitmexWebsocketTableConverter(false));

            if (!String.IsNullOrEmpty(symbol))
            {
                endpoint += $":{symbol}";
            }
            if (!Args.Contains(endpoint))
            {
                Args.Add(endpoint);
            }
            return(this);
        }
コード例 #2
0
        public async Task <CallResult <UpdateSubscription> > SubscribeAsync(BitmexSubscribeRequest bitmexSubscribeRequest)
        {
            CheckDoubleSendingRequest(bitmexSubscribeRequest);
            if (!bitmexSubscribeRequest.Args.Any())
            {
                log.Write(LogVerbosity.Warning, $"Not sending empty request {JsonConvert.SerializeObject(bitmexSubscribeRequest)}");
                return(new CallResult <UpdateSubscription>(null, new ServerError("Not sending empty request ", bitmexSubscribeRequest)));
            }
            var handler = new Action <string>(data =>
            {
                var token = JToken.Parse(data);
                var table = (string)token["table"];

                if (String.IsNullOrEmpty(table) || !Map.Mappings.ContainsKey(table))
                {
                    var subscriptionResponse = (string)token["subscribe"];
                    if (!String.IsNullOrEmpty(subscriptionResponse))
                    {
                        var response = Deserialize <BitmexSubscriptionResponse>(data);
                        if (response)
                        {
                            _sendedSubscriptions.TryAdd(response.Data.Subscribe, response.Data.Request);
                        }
                        return;
                    }
                    else if (!String.IsNullOrEmpty((string)token["unsubscribe"]))
                    {
                        var response = Deserialize <BitmexSubscriptionResponse>(data);
                        if (response)
                        {
                            OnUnsubscribe(response.Data);
                        }
                        return;
                    }
                    else
                    {
                        log.Write(LogVerbosity.Warning, $"Unknown table [{table}] update catched at data {data}");
                        return;
                    }
                }
                BitmexSubscribtions updatedTable = Map.Mappings[table];

                switch (updatedTable)
                {
                case BitmexSubscribtions.Announcements:
                    {
                        var result = Deserialize <BitmexSocketEvent <Announcement> >(token, false);
                        if (result.Success)
                        {
                            OnAnnouncementUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Chat:
                    {
                        var result = Deserialize <BitmexSocketEvent <Chat> >(token, false);
                        if (result.Success)
                        {
                            OnChatMessageUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Connected:
                    {
                        var result = Deserialize <BitmexSocketEvent <ConnectedUsers> >(token, false);
                        if (result.Success)
                        {
                            OnChatConnectionUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Funding:
                    {
                        var result = Deserialize <BitmexSocketEvent <Funding> >(token, false);
                        if (result.Success)
                        {
                            OnFundingUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Instrument:
                    {
                        var result = Deserialize <BitmexSocketEvent <Instrument> >(token, false);
                        if (result.Success)
                        {
                            OnInstrimentUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Insurance:
                    {
                        var result = Deserialize <BitmexSocketEvent <Insurance> >(token, false);
                        if (result.Success)
                        {
                            OnInsuranceUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Liquidation:
                    {
                        var result = Deserialize <BitmexSocketEvent <Liquidation> >(token, false);
                        if (result.Success)
                        {
                            OnLiquidationUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.OrderBookL2_25:
                    {
                        var result = Deserialize <BitmexSocketEvent <BitmexOrderBookEntry> >(token, false);
                        if (result.Success)
                        {
                            if (InstrumentsIndexesAndTicks.Any())
                            {
                                foreach (var level in result.Data.Data)
                                {
                                    var symbolTickInfo = InstrumentsIndexesAndTicks[level.Symbol];
                                    level.SetPrice(symbolTickInfo.Index, symbolTickInfo.TickSize);
                                }
                            }
                            OnOrderBookL2_25Update?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.OrderBookL2:
                    {
                        var result = Deserialize <BitmexSocketEvent <BitmexOrderBookEntry> >(token, false);
                        if (result.Success)
                        {
                            if (InstrumentsIndexesAndTicks.Any())
                            {
                                foreach (var level in result.Data.Data)
                                {
                                    var symbolTickInfo = InstrumentsIndexesAndTicks[level.Symbol];
                                    level.SetPrice(symbolTickInfo.Index, symbolTickInfo.TickSize);
                                }
                            }
                            OnorderBookL2Update?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.OrderBook10:
                    {
                        var result = Deserialize <BitmexSocketEvent <BitmexOrderBookL10> >(token, false);
                        if (result.Success)
                        {
                            OnOrderBook10Update?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.PublicNotifications:
                    {
                        var result = Deserialize <BitmexSocketEvent <GlobalNotification> >(token, false);
                        if (result.Success)
                        {
                            OnGlobalNotificationUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Quote:
                    {
                        var result = Deserialize <BitmexSocketEvent <Quote> >(token, false);
                        if (result.Success)
                        {
                            OnQuotesUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.QuoteBin1m:
                    {
                        var result = Deserialize <BitmexSocketEvent <Quote> >(token, false);
                        if (result.Success)
                        {
                            OnOneMinuteQuoteBinUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.QuoteBin5m:
                    {
                        var result = Deserialize <BitmexSocketEvent <Quote> >(token, false);
                        if (result.Success)
                        {
                            OnFiveMinuteQuoteBinUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.QuoteBin1h:
                    {
                        var result = Deserialize <BitmexSocketEvent <Quote> >(token, false);
                        if (result.Success)
                        {
                            OnOneHourQuoteBinUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.QuoteBin1d:
                    {
                        var result = Deserialize <BitmexSocketEvent <Quote> >(token, false);
                        if (result.Success)
                        {
                            OnDailyQuoteBinUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Settlement:
                    {
                        var result = Deserialize <BitmexSocketEvent <Settlement> >(token, false);
                        if (result.Success)
                        {
                            OnSettlementUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Trade:
                    {
                        var result = Deserialize <BitmexSocketEvent <Trade> >(token, false);
                        if (result.Success)
                        {
                            OnTradeUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.TradeBin1m:
                    {
                        var result = Deserialize <BitmexSocketEvent <TradeBin> >(token, false);
                        if (result.Success)
                        {
                            OnOneMinuteTradeBinUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.TradeBin5m:
                    {
                        var result = Deserialize <BitmexSocketEvent <TradeBin> >(token, false);
                        if (result.Success)
                        {
                            OnFiveMinuteTradeBinUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.TradeBin1h:
                    {
                        var result = Deserialize <BitmexSocketEvent <TradeBin> >(token, false);
                        if (result.Success)
                        {
                            OnOneHourTradeBinUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.TradeBin1d:
                    {
                        var result = Deserialize <BitmexSocketEvent <TradeBin> >(token, false);
                        if (result.Success)
                        {
                            OnDailyTradeBinUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Affiliate:
                    {
                        var result = Deserialize <BitmexSocketEvent <Affiliate> >(token, false);
                        if (result.Success)
                        {
                            OnUserAffiliatesUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Execution:
                    {
                        var result = Deserialize <BitmexSocketEvent <Execution> >(token, false);
                        if (result.Success)
                        {
                            OnUserExecutionsUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Order:
                    {
                        var result = Deserialize <BitmexSocketEvent <Order> >(token, false);
                        if (result.Success)
                        {
                            OnUserOrdersUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Margin:
                    {
                        var result = Deserialize <BitmexSocketEvent <Margin> >(token, false);
                        if (result.Success)
                        {
                            OnUserMarginUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Position:
                    {
                        var result = Deserialize <BitmexSocketEvent <Position> >(token, false);
                        if (result.Success)
                        {
                            OnUserPositionsUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Transact:
                    {
                        var result = Deserialize <BitmexSocketEvent <Transaction> >(token, false);
                        if (result.Success)
                        {
                            OnUserTransactionsUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                case BitmexSubscribtions.Wallet:
                    {
                        var result = Deserialize <BitmexSocketEvent <Wallet> >(token, false);
                        if (result.Success)
                        {
                            OnUserWalletUpdate?.Invoke(result.Data);
                        }
                        else
                        {
                            log.Write(LogVerbosity.Warning, "Couldn't deserialize data received from  stream: " + result.Error);
                        }
                        break;
                    }

                default:
                    {
                        log.Write(LogVerbosity.Warning, $"Catched inknown table update: {data}");
                        break;
                    }
                }
            });

            return(await Subscribe(bitmexSubscribeRequest, handler));
        }
コード例 #3
0
        public async Task <CallResult <UpdateSubscription> > SubscribeToOrderBookUpdatesAsync(Action <BitmexSocketEvent <BitmexOrderBookEntry> > onData, string symbol = "", bool full = false)
        {
            BitmexSubscribtions orderbookType = full ? BitmexSubscribtions.OrderBookL2 : BitmexSubscribtions.OrderBookL2_25;

            return(await Subscribe(new BitmexSubscribeRequest().AddSubscription(orderbookType, symbol), onData));
        }