Ejemplo n.º 1
0
        public async Task Subscribe(string strategyName, List <StrategySubscription> strategySubscriptions, ITradeStrategy tradeStrategy)
        {
            if (strategySubscriptions == null)
            {
                throw new ArgumentNullException(nameof(strategySubscriptions));
            }

            if (tradeStrategy == null)
            {
                throw new ArgumentNullException(nameof(tradeStrategy));
            }

            await tradeStrategy.AddExchangeService(strategySubscriptions, exchange, exchangeService).ConfigureAwait(false);

            var exchangeApi = exchangeService.GetExchangeApi(exchange);

            foreach (var strategySubscription in strategySubscriptions)
            {
                if (strategySubscription.Subscribes.HasFlag(Subscribes.Trades) ||
                    strategySubscription.Subscribes.HasFlag(Subscribes.OrderBook) ||
                    strategySubscription.Subscribes.HasFlag(Subscribes.Candlesticks))
                {
                    if (!Caches.TryGetValue(strategySubscription.Symbol, out ISubscriptionCache symbolCache))
                    {
                        symbolCache = new SymbolSubscriptionCache(strategySubscription.Symbol, strategySubscription.Limit, strategySubscription.CandlestickInterval, exchangeApi);
                        Caches.TryAdd(strategySubscription.Symbol, symbolCache);
                    }

                    symbolCache.Subscribe(strategyName, strategySubscription, tradeStrategy);
                }

                if (strategySubscription.Subscribes.HasFlag(Subscribes.AccountInfo))
                {
                    var user = new User
                    {
                        ApiKey        = strategySubscription.ApiKey,
                        ApiSecret     = strategySubscription.SecretKey,
                        ApiPassPhrase = strategySubscription.ApiPassPhrase,
                        Exchange      = strategySubscription.Exchange
                    };

                    var accountInfo = await exchangeService.GetAccountInfoAsync(exchange, user, new CancellationToken()).ConfigureAwait(false);

                    tradeStrategy.SubscribeAccountInfo(new AccountInfoEventArgs {
                        AccountInfo = accountInfo
                    });

                    if (!Caches.TryGetValue(strategySubscription.ApiKey, out ISubscriptionCache accountInfoCache))
                    {
                        accountInfoCache = new AccountInfoSubscriptionCache(exchangeApi);
                        Caches.TryAdd(strategySubscription.ApiKey, accountInfoCache);
                    }

                    accountInfoCache.Subscribe(strategyName, strategySubscription, tradeStrategy);
                }
            }
        }