10 changed files with 225 additions and 16 deletions
@ -1,14 +1,66 @@ |
|||||
using Binance.TradeRobot.Common.DI; |
using Binance.TradeRobot.Common.DI; |
||||
|
using Binance.TradeRobot.Model.Dto; |
||||
using Microsoft.Extensions.DependencyInjection; |
using Microsoft.Extensions.DependencyInjection; |
||||
|
using SDKAdapter.WebSockets.Market; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
namespace Binance.TradeRobot.Business |
namespace Binance.TradeRobot.Business |
||||
{ |
{ |
||||
[BatchRegistration(ServiceLifetime.Singleton, RegistrationType.Self)] |
[BatchRegistration(ServiceLifetime.Singleton, RegistrationType.Self)] |
||||
public class GlobalContext |
public class GlobalContext |
||||
{ |
{ |
||||
public GlobalContext() |
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) |
||||
|
{ |
||||
|
|
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
@ -0,0 +1,36 @@ |
|||||
|
using Binance.Net.Clients; |
||||
|
using Binance.Net.Enums; |
||||
|
using System.Threading; |
||||
|
|
||||
|
namespace SDKAdapter.WebSockets.Market |
||||
|
{ |
||||
|
public class BinanceSpotMarketWebSocketClient : SpotMarketWebSocketClient |
||||
|
{ |
||||
|
private BinanceSocketClient client; |
||||
|
private CancellationTokenSource cancellationTokenSource; |
||||
|
|
||||
|
public BinanceSpotMarketWebSocketClient(string symbol, NLog.ILogger logger) : base(symbol, logger) |
||||
|
{ |
||||
|
client = new BinanceSocketClient(); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public override void Start() |
||||
|
{ |
||||
|
cancellationTokenSource = new CancellationTokenSource(); |
||||
|
client.SpotStreams.SubscribeToKlineUpdatesAsync(Symbol, KlineInterval.OneMinute, (e) => |
||||
|
{ |
||||
|
base.OnReceived(e.Data.Data.ClosePrice); |
||||
|
}, cancellationTokenSource.Token); |
||||
|
base.Start(); |
||||
|
} |
||||
|
|
||||
|
public override void Stop() |
||||
|
{ |
||||
|
cancellationTokenSource.Cancel(); |
||||
|
client.SpotStreams.Dispose(); |
||||
|
base.Stop(); |
||||
|
cancellationTokenSource = null; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace SDKAdapter.WebSockets.Market |
||||
|
{ |
||||
|
public class GateIOSpotMarketWebSocketClient : SpotMarketWebSocketClient |
||||
|
{ |
||||
|
public GateIOSpotMarketWebSocketClient(string symbol, NLog.ILogger logger) : base(symbol, logger) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue