mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-08 00:16:27 +00:00
Added ExchangeSymbolCache, added SharedSymbol property on all relevant response models
This commit is contained in:
parent
ec1f469848
commit
bf103ce9d1
67
CryptoExchange.Net/ExchangeSymbolCache.cs
Normal file
67
CryptoExchange.Net/ExchangeSymbolCache.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using CryptoExchange.Net.SharedApis;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace CryptoExchange.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Cache for symbol parsing
|
||||
/// </summary>
|
||||
public static class ExchangeSymbolCache
|
||||
{
|
||||
private static ConcurrentDictionary<string, ExchangeInfo> _symbolInfos = new ConcurrentDictionary<string, ExchangeInfo>();
|
||||
|
||||
/// <summary>
|
||||
/// Update the cached symbol data for an exchange
|
||||
/// </summary>
|
||||
/// <param name="topicId">Id for the provided data</param>
|
||||
/// <param name="updateData">Symbol data</param>
|
||||
public static void UpdateSymbolInfo(string topicId, SharedSpotSymbol[] updateData)
|
||||
{
|
||||
if(!_symbolInfos.TryGetValue(topicId, out var exchangeInfo))
|
||||
{
|
||||
exchangeInfo = new ExchangeInfo(DateTime.UtcNow, updateData.ToDictionary(x => x.Name, x => new SharedSymbol(x.TradingMode, x.BaseAsset, x.QuoteAsset, (x as SharedFuturesSymbol)?.DeliveryTime) { SymbolName = x.Name }));
|
||||
_symbolInfos.TryAdd(topicId, exchangeInfo);
|
||||
}
|
||||
|
||||
if (DateTime.UtcNow - exchangeInfo.UpdateTime < TimeSpan.FromMinutes(60))
|
||||
return;
|
||||
|
||||
_symbolInfos[topicId] = new ExchangeInfo(DateTime.UtcNow, updateData.ToDictionary(x => x.Name, x => new SharedSymbol(x.TradingMode, x.BaseAsset, x.QuoteAsset, (x as SharedFuturesSymbol)?.DeliveryTime) { SymbolName = x.Name }));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a symbol name to a SharedSymbol
|
||||
/// </summary>
|
||||
/// <param name="topicId">Id for the provided data</param>
|
||||
/// <param name="symbolName">Symbol name</param>
|
||||
public static SharedSymbol? ParseSymbol(string topicId, string symbolName)
|
||||
{
|
||||
if (!_symbolInfos.TryGetValue(topicId, out var exchangeInfo))
|
||||
return null;
|
||||
|
||||
if (!exchangeInfo.Symbols.TryGetValue(symbolName, out var symbolInfo))
|
||||
return null;
|
||||
|
||||
return new SharedSymbol(symbolInfo.TradingMode, symbolInfo.BaseAsset, symbolInfo.QuoteAsset, symbolName)
|
||||
{
|
||||
DeliverTime = symbolInfo.DeliverTime
|
||||
};
|
||||
}
|
||||
|
||||
class ExchangeInfo
|
||||
{
|
||||
public DateTime UpdateTime { get; set; }
|
||||
public Dictionary<string, SharedSymbol> Symbols { get; set; }
|
||||
|
||||
public ExchangeInfo(DateTime updateTime, Dictionary<string, SharedSymbol> symbols)
|
||||
{
|
||||
UpdateTime = updateTime;
|
||||
Symbols = symbols;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -33,19 +33,6 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <returns></returns>
|
||||
string FormatSymbol(string baseAsset, string quoteAsset, TradingMode tradingMode, DateTime? deliverDate = null);
|
||||
|
||||
/// <summary>
|
||||
/// Parse a string to a shared symbol
|
||||
/// </summary>
|
||||
/// <param name="symbol">Symbol as returned by the server</param>
|
||||
/// <returns>Shared symbol</returns>
|
||||
SharedSymbol ParseSymbol(string symbol);
|
||||
|
||||
/// <summary>
|
||||
/// Generate a new random client order id
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
string GenerateClientOrderId();
|
||||
|
||||
/// <summary>
|
||||
/// Set a default exchange parameter. This can be used instead of passing in an ExchangeParameters object which each request.
|
||||
/// </summary>
|
||||
|
@ -3,7 +3,7 @@
|
||||
/// <summary>
|
||||
/// Book ticker
|
||||
/// </summary>
|
||||
public record SharedBookTicker
|
||||
public record SharedBookTicker : SharedSymbolModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Price of the best ask
|
||||
@ -25,7 +25,8 @@
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedBookTicker(decimal bestAskPrice, decimal bestAskQuantity, decimal bestBidPrice, decimal bestBidQuantity)
|
||||
public SharedBookTicker(SharedSymbol? sharedSymbol, string symbol, decimal bestAskPrice, decimal bestAskQuantity, decimal bestBidPrice, decimal bestBidQuantity)
|
||||
: base(sharedSymbol, symbol)
|
||||
{
|
||||
BestAskPrice = bestAskPrice;
|
||||
BestAskQuantity = bestAskQuantity;
|
||||
|
@ -5,12 +5,8 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <summary>
|
||||
/// Futures order info
|
||||
/// </summary>
|
||||
public record SharedFuturesOrder
|
||||
public record SharedFuturesOrder : SharedSymbolModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Symbol
|
||||
/// </summary>
|
||||
public string Symbol { get; set; }
|
||||
/// <summary>
|
||||
/// Id of the order
|
||||
/// </summary>
|
||||
@ -97,14 +93,15 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedFuturesOrder(
|
||||
SharedSymbol? sharedSymbol,
|
||||
string symbol,
|
||||
string orderId,
|
||||
SharedOrderType orderType,
|
||||
SharedOrderSide orderSide,
|
||||
SharedOrderStatus orderStatus,
|
||||
DateTime? createTime)
|
||||
: base(sharedSymbol, symbol)
|
||||
{
|
||||
Symbol = symbol;
|
||||
OrderId = orderId;
|
||||
OrderType = orderType;
|
||||
Side = orderSide;
|
||||
|
@ -7,10 +7,6 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// </summary>
|
||||
public record SharedFuturesSymbol : SharedSpotSymbol
|
||||
{
|
||||
/// <summary>
|
||||
/// Symbol type
|
||||
/// </summary>
|
||||
public SharedSymbolType SymbolType { get; set; }
|
||||
/// <summary>
|
||||
/// The size of a single contract
|
||||
/// </summary>
|
||||
@ -27,9 +23,8 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedFuturesSymbol(SharedSymbolType symbolType, string baseAsset, string quoteAsset, string symbol, bool trading) : base(baseAsset, quoteAsset, symbol, trading)
|
||||
public SharedFuturesSymbol(TradingMode symbolType, string baseAsset, string quoteAsset, string symbol, bool trading) : base(baseAsset, quoteAsset, symbol, trading, symbolType)
|
||||
{
|
||||
SymbolType = symbolType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,12 +5,8 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <summary>
|
||||
/// Futures ticker info
|
||||
/// </summary>
|
||||
public record SharedFuturesTicker
|
||||
public record SharedFuturesTicker: SharedSymbolModel
|
||||
{
|
||||
/// <summary>
|
||||
/// The symbol
|
||||
/// </summary>
|
||||
public string Symbol { get; set; }
|
||||
/// <summary>
|
||||
/// Last trade price
|
||||
/// </summary>
|
||||
@ -51,9 +47,9 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedFuturesTicker(string symbol, decimal? lastPrice, decimal? highPrice, decimal? lowPrice, decimal volume, decimal? changePercentage)
|
||||
public SharedFuturesTicker(SharedSymbol? sharedSymbol, string symbol, decimal? lastPrice, decimal? highPrice, decimal? lowPrice, decimal volume, decimal? changePercentage)
|
||||
:base(sharedSymbol, symbol)
|
||||
{
|
||||
Symbol = symbol;
|
||||
LastPrice = lastPrice;
|
||||
HighPrice = highPrice;
|
||||
LowPrice = lowPrice;
|
||||
|
@ -5,12 +5,8 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <summary>
|
||||
/// Position info
|
||||
/// </summary>
|
||||
public record SharedPosition
|
||||
public record SharedPosition : SharedSymbolModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Symbol
|
||||
/// </summary>
|
||||
public string Symbol { get; set; }
|
||||
/// <summary>
|
||||
/// Current size of the position
|
||||
/// </summary>
|
||||
@ -43,9 +39,9 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedPosition(string symbol, decimal positionSize, DateTime? updateTime)
|
||||
public SharedPosition(SharedSymbol? sharedSymbol, string symbol, decimal positionSize, DateTime? updateTime)
|
||||
: base(sharedSymbol, symbol)
|
||||
{
|
||||
Symbol = symbol;
|
||||
PositionSize = positionSize;
|
||||
UpdateTime = updateTime;
|
||||
}
|
||||
|
@ -5,12 +5,8 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <summary>
|
||||
/// Position history
|
||||
/// </summary>
|
||||
public record SharedPositionHistory
|
||||
public record SharedPositionHistory : SharedSymbolModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Symbol of the position
|
||||
/// </summary>
|
||||
public string Symbol { get; set; }
|
||||
/// <summary>
|
||||
/// The side of the position
|
||||
/// </summary>
|
||||
@ -52,6 +48,7 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedPositionHistory(
|
||||
SharedSymbol? sharedSymbol,
|
||||
string symbol,
|
||||
SharedPositionSide side,
|
||||
decimal openPrice,
|
||||
@ -59,8 +56,8 @@ namespace CryptoExchange.Net.SharedApis
|
||||
decimal quantity,
|
||||
decimal realizedPnl,
|
||||
DateTime timestamp)
|
||||
: base(sharedSymbol, symbol)
|
||||
{
|
||||
Symbol = symbol;
|
||||
PositionSide = side;
|
||||
AverageOpenPrice = openPrice;
|
||||
AverageClosePrice = closePrice;
|
||||
|
@ -5,12 +5,8 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <summary>
|
||||
/// Spot order info
|
||||
/// </summary>
|
||||
public record SharedSpotOrder
|
||||
public record SharedSpotOrder : SharedSymbolModel
|
||||
{
|
||||
/// <summary>
|
||||
/// The symbol the order is on
|
||||
/// </summary>
|
||||
public string Symbol { get; set; }
|
||||
/// <summary>
|
||||
/// The id of the order
|
||||
/// </summary>
|
||||
@ -84,14 +80,15 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedSpotOrder(
|
||||
SharedSymbol? sharedSymbol,
|
||||
string symbol,
|
||||
string orderId,
|
||||
SharedOrderType orderType,
|
||||
SharedOrderSide orderSide,
|
||||
SharedOrderStatus orderStatus,
|
||||
DateTime? createTime)
|
||||
: base(sharedSymbol, symbol)
|
||||
{
|
||||
Symbol = symbol;
|
||||
OrderId = orderId;
|
||||
OrderType = orderType;
|
||||
Side = orderSide;
|
||||
|
@ -5,6 +5,10 @@
|
||||
/// </summary>
|
||||
public record SharedSpotSymbol
|
||||
{
|
||||
/// <summary>
|
||||
/// The trading mode of the symbol
|
||||
/// </summary>
|
||||
public TradingMode TradingMode { get; set; }
|
||||
/// <summary>
|
||||
/// Base asset of the symbol
|
||||
/// </summary>
|
||||
@ -57,8 +61,9 @@
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedSpotSymbol(string baseAsset, string quoteAsset, string symbol, bool trading)
|
||||
public SharedSpotSymbol(string baseAsset, string quoteAsset, string symbol, bool trading, TradingMode? tradingMode = null)
|
||||
{
|
||||
TradingMode = tradingMode ?? TradingMode.Spot;
|
||||
BaseAsset = baseAsset;
|
||||
QuoteAsset = quoteAsset;
|
||||
Name = symbol;
|
||||
|
@ -3,12 +3,8 @@
|
||||
/// <summary>
|
||||
/// Ticker info
|
||||
/// </summary>
|
||||
public record SharedSpotTicker
|
||||
public record SharedSpotTicker: SharedSymbolModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Symbol name
|
||||
/// </summary>
|
||||
public string Symbol { get; set; }
|
||||
/// <summary>
|
||||
/// Last trade price
|
||||
/// </summary>
|
||||
@ -33,9 +29,9 @@
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedSpotTicker(string symbol, decimal? lastPrice, decimal? highPrice, decimal? lowPrice, decimal volume, decimal? changePercentage)
|
||||
public SharedSpotTicker(SharedSymbol? sharedSymbol, string symbol, decimal? lastPrice, decimal? highPrice, decimal? lowPrice, decimal volume, decimal? changePercentage)
|
||||
: base(sharedSymbol, symbol)
|
||||
{
|
||||
Symbol = symbol;
|
||||
LastPrice = lastPrice;
|
||||
HighPrice = highPrice;
|
||||
LowPrice = lowPrice;
|
||||
|
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CryptoExchange.Net.SharedApis
|
||||
{
|
||||
/// <summary>
|
||||
/// Symbol model
|
||||
/// </summary>
|
||||
public record SharedSymbolModel
|
||||
{
|
||||
/// <summary>
|
||||
/// SharedSymbol, only filled when the related GetSpotSymbolsAsync or GetFuturesSymbolsAsync method has been called previously
|
||||
/// </summary>
|
||||
public SharedSymbol? SharedSymbol { get; set; }
|
||||
/// <summary>
|
||||
/// Symbol name
|
||||
/// </summary>
|
||||
public string Symbol { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedSymbolModel(SharedSymbol? sharedSymbol, string symbol)
|
||||
{
|
||||
Symbol = symbol;
|
||||
SharedSymbol = sharedSymbol;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,12 +5,8 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <summary>
|
||||
/// A user trade
|
||||
/// </summary>
|
||||
public record SharedUserTrade
|
||||
public record SharedUserTrade : SharedSymbolModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Symbol the trade was on
|
||||
/// </summary>
|
||||
public string Symbol { get; set; }
|
||||
/// <summary>
|
||||
/// The trade id
|
||||
/// </summary>
|
||||
@ -51,7 +47,8 @@ namespace CryptoExchange.Net.SharedApis
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public SharedUserTrade(string symbol, string orderId, string id, SharedOrderSide? side, decimal quantity, decimal price, DateTime timestamp)
|
||||
public SharedUserTrade(SharedSymbol? sharedSymbol, string symbol, string orderId, string id, SharedOrderSide? side, decimal quantity, decimal price, DateTime timestamp)
|
||||
: base(sharedSymbol, symbol)
|
||||
{
|
||||
Symbol = symbol;
|
||||
OrderId = orderId;
|
||||
|
Loading…
x
Reference in New Issue
Block a user