币安量化交易
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.

109 lines
4.9 KiB

using Binance.TradeRobot.Common.DI;
3 years ago
using Binance.TradeRobot.Model.Dto;
using Microsoft.Extensions.DependencyInjection;
3 years ago
using SDKAdapter.Model;
3 years ago
using SDKAdapter.WebSockets.Market;
3 years ago
using SDKAdapter.WebSockets.Order.Spot;
using System;
3 years ago
using System.Collections.Generic;
3 years ago
using System.Linq;
using System.Threading;
namespace Binance.TradeRobot.Business
{
[BatchRegistration(ServiceLifetime.Singleton, RegistrationType.Self)]
public class GlobalContext
{
3 years ago
private NLogManager logManager;
private IDictionary<string, SpotMarketWebSocketClient> spotMarketWebSocketClientDictionary;
3 years ago
private IDictionary<string, SpotOrderWebSocketClient> spotOrderWebSocketClientDictionary;
3 years ago
private Lazy<IEnumerable<ISpotOrderPublishBusiness>> spotOrderPublishBusinessListLazy;
private IEnumerable<ISpotOrderPublishBusiness> spotOrderPublishBusinessList => spotOrderPublishBusinessListLazy.Value;
public GlobalContext(NLogManager logManager, IServiceProvider serviceProvider)
3 years ago
{
this.logManager = logManager;
spotMarketWebSocketClientDictionary = new Dictionary<string, SpotMarketWebSocketClient>();
3 years ago
spotOrderWebSocketClientDictionary = new Dictionary<string, SpotOrderWebSocketClient>();
//this.spotOrderPublishBusinessList = spotOrderPublishBusinessList;
this.spotOrderPublishBusinessListLazy = new Lazy<IEnumerable<ISpotOrderPublishBusiness>>(() => serviceProvider.GetServices<ISpotOrderPublishBusiness>());
3 years ago
}
/// <summary>
/// 订阅K线
/// </summary>
/// <param name="robot"></param>
public void SubscribeKLine(RobotResponse robot)
{
if (!spotMarketWebSocketClientDictionary.TryGetValue(robot.KLineKey, out SpotMarketWebSocketClient spotMarketWebSocketClient))
{
3 years ago
spotMarketWebSocketClient = SpotMarketWebSocketClient.Create(robot.ExchangeId, robot.Symbol, logManager.GetLogger(robot.KLineKey));
3 years ago
spotMarketWebSocketClientDictionary.TryAdd(robot.KLineKey, spotMarketWebSocketClient);
}
3 years ago
spotMarketWebSocketClient.Start();
3 years ago
}
/// <summary>
3 years ago
/// 取消订阅K线
3 years ago
/// </summary>
/// <param name="robot"></param>
3 years ago
public void UnSubscribeKLine(RobotResponse robot)
3 years ago
{
3 years ago
//停止订阅k线
if (spotMarketWebSocketClientDictionary.TryGetValue(robot.KLineKey, out SpotMarketWebSocketClient spotMarketWebSocketClient))
spotMarketWebSocketClient.Stop();
3 years ago
}
/// <summary>
3 years ago
/// 订阅订单推送
3 years ago
/// </summary>
/// <param name="robot"></param>
3 years ago
public void SubscribeOrderPublish(RobotResponse robot)
3 years ago
{
3 years ago
if (!spotOrderWebSocketClientDictionary.TryGetValue(robot.OrderPublishKey, out SpotOrderWebSocketClient spotOrderWebSocketClient))
3 years ago
{
spotOrderWebSocketClient = SpotOrderWebSocketClient.Create(robot.BusinessType,
robot.ExchangeId,
robot.ExchangeAPIKey.AccountId,
robot.ExchangeAPIKey.APIKey,
robot.ExchangeAPIKey.SecretKey,
3 years ago
logManager.GetLogger(robot.OrderPublishKey),
OnSpotOrderPublish);
3 years ago
}
3 years ago
spotOrderWebSocketClient.Start(robot.Symbol);
3 years ago
}
3 years ago
3 years ago
/// <summary>
/// 取消订阅订单推送
/// </summary>
/// <param name="robot"></param>
public void UnSubscribeOrderPublish(RobotResponse robot)
{
3 years ago
if (spotOrderWebSocketClientDictionary.TryGetValue(robot.OrderPublishKey, out SpotOrderWebSocketClient spotOrderWebSocketClient))
spotOrderWebSocketClient.Stop(robot.Symbol);
}
/// <summary>
/// 获取指定交易对现货最新成交价
/// </summary>
3 years ago
/// <param name="kLineKey"></param>
/// <returns></returns>
3 years ago
public decimal? GetSpotNewestPrice(string kLineKey)
{
3 years ago
if (spotMarketWebSocketClientDictionary.TryGetValue(kLineKey, out SpotMarketWebSocketClient spotMarketWebSocketClient))
return spotMarketWebSocketClient.NewestPrice;
return null;
}
3 years ago
3 years ago
public void OnSpotOrderPublish(SpotOrderPublishInfo spotOrderPublishInfo)
3 years ago
{
3 years ago
var orderPublishBusiness = spotOrderPublishBusinessList.FirstOrDefault(p => p.TradePolicy == spotOrderPublishInfo.TradePolicy);
Thread.Sleep(1000);
3 years ago
orderPublishBusiness.OnSpotOrderPublish(spotOrderPublishInfo);
3 years ago
}
}
}