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.
65 lines
1.7 KiB
65 lines
1.7 KiB
using Binance.TradeRobot.Model.Base;
|
|
using System;
|
|
|
|
namespace SDKAdapter.WebSockets.Market
|
|
{
|
|
public class SpotMarketWebSocketClient
|
|
{
|
|
/// <summary>
|
|
/// 更新间隔(ms)
|
|
/// </summary>
|
|
protected int updateInterval = 3000;
|
|
|
|
/// <summary>
|
|
/// 交易对
|
|
/// </summary>
|
|
public string Symbol { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 最新成交价
|
|
/// </summary>
|
|
public decimal NewestPrice { get; protected set; }
|
|
|
|
/// <summary>r
|
|
/// 上一次价格更新时间
|
|
/// </summary>
|
|
public DateTime? LastUpdateTime { get; private set; }
|
|
|
|
public NLog.ILogger logger { get; private set; }
|
|
|
|
protected bool IsConnected { get; set; }
|
|
|
|
public static SpotMarketWebSocketClient Create(Enums.Exchange exchange, string symbol, NLog.ILogger logger)
|
|
{
|
|
if (exchange == Enums.Exchange.Binance)
|
|
return new BinanceSpotMarketWebSocketClient(symbol, logger);
|
|
return null;
|
|
}
|
|
|
|
public SpotMarketWebSocketClient(string symbol, NLog.ILogger logger)
|
|
{
|
|
this.Symbol = symbol;
|
|
this.logger = logger;
|
|
}
|
|
|
|
public virtual void Start()
|
|
{
|
|
IsConnected = true;
|
|
}
|
|
|
|
public virtual void Stop()
|
|
{
|
|
IsConnected = false;
|
|
}
|
|
|
|
protected virtual void OnReceived(decimal newestPrice)
|
|
{
|
|
NewestPrice = newestPrice;
|
|
if (LastUpdateTime == null || (DateTime.Now - LastUpdateTime.Value).TotalMilliseconds >= updateInterval)
|
|
{
|
|
logger.Info($"NewestPrice:{newestPrice}");
|
|
LastUpdateTime = DateTime.Now;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|