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