币安量化交易
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.7 KiB

using Binance.TradeRobot.Common.DI;
3 years ago
using Binance.TradeRobot.Model.Dto;
using Microsoft.Extensions.DependencyInjection;
3 years ago
using SDKAdapter.WebSockets.Market;
using System.Collections.Generic;
namespace Binance.TradeRobot.Business
{
[BatchRegistration(ServiceLifetime.Singleton, RegistrationType.Self)]
public class GlobalContext
{
3 years ago
private NLogManager logManager;
private IDictionary<string, SpotMarketWebSocketClient> spotMarketWebSocketClientDictionary;
public GlobalContext(NLogManager logManager)
{
this.logManager = logManager;
spotMarketWebSocketClientDictionary = new Dictionary<string, SpotMarketWebSocketClient>();
}
/// <summary>
/// 订阅K线
/// </summary>
/// <param name="robot"></param>
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();
}
/// <summary>
/// 订阅订单推送
/// </summary>
/// <param name="robot"></param>
public void SubscribeOrderPublish(RobotResponse robot)
{
}
/// <summary>
/// 取消订阅K线
/// </summary>
/// <param name="robot"></param>
public void UnSubscribeKLine(RobotResponse robot)
{
//停止订阅k线
if (spotMarketWebSocketClientDictionary.TryGetValue(robot.KLineKey, out SpotMarketWebSocketClient spotMarketWebSocketClient))
spotMarketWebSocketClient.Stop();
}
/// <summary>
/// 取消订阅订单推送
/// </summary>
/// <param name="robot"></param>
public void UnSubscribeOrderPublish(RobotResponse robot)
{
}
/// <summary>
/// 获取指定交易对现货最新成交价
/// </summary>
/// <param name="symbol"></param>
/// <returns></returns>
public decimal? GetSpotNewestPrice(string key)
{
if (spotMarketWebSocketClientDictionary.TryGetValue(key, out SpotMarketWebSocketClient spotMarketWebSocketClient))
return spotMarketWebSocketClient.NewestPrice;
return null;
}
}
}