using Binance.TradeRobot.Common.DI; using Binance.TradeRobot.Model.Dto; using Microsoft.Extensions.DependencyInjection; using SDKAdapter.WebSockets.Market; using System.Collections.Generic; namespace Binance.TradeRobot.Business { [BatchRegistration(ServiceLifetime.Singleton, RegistrationType.Self)] public class GlobalContext { private NLogManager logManager; private IDictionary spotMarketWebSocketClientDictionary; public GlobalContext(NLogManager logManager) { this.logManager = logManager; spotMarketWebSocketClientDictionary = new Dictionary(); } /// /// 订阅K线 /// /// public void SubscribeKLine(RobotResponse robot) { if (!spotMarketWebSocketClientDictionary.TryGetValue(robot.KLineKey, out SpotMarketWebSocketClient spotMarketWebSocketClient)) { var loggerName = $"SpotKLine-{robot.ExchangeId}-{robot.Symbol}"; spotMarketWebSocketClient = SpotMarketWebSocketClient.Create(robot.ExchangeId, robot.Symbol, logManager.GetLogger(loggerName)); spotMarketWebSocketClientDictionary.TryAdd(robot.KLineKey, spotMarketWebSocketClient); } if (!spotMarketWebSocketClient.IsConnected) spotMarketWebSocketClient.Start(); } /// /// 订阅订单推送 /// /// public void SubscribeOrderPublish(RobotResponse robot) { } /// /// 取消订阅K线 /// /// public void UnSubscribeKLine(RobotResponse robot) { //停止订阅k线 if (spotMarketWebSocketClientDictionary.TryGetValue(robot.KLineKey, out SpotMarketWebSocketClient spotMarketWebSocketClient)) spotMarketWebSocketClient.Stop(); } /// /// 取消订阅订单推送 /// /// public void UnSubscribeOrderPublish(RobotResponse robot) { } /// /// 获取指定交易对现货最新成交价 /// /// /// public decimal? GetSpotNewestPrice(string key) { if (spotMarketWebSocketClientDictionary.TryGetValue(key, out SpotMarketWebSocketClient spotMarketWebSocketClient)) return spotMarketWebSocketClient.NewestPrice; return null; } } }