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>
|
/// <returns></returns>
|
||||||
string FormatSymbol(string baseAsset, string quoteAsset, TradingMode tradingMode, DateTime? deliverDate = null);
|
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>
|
/// <summary>
|
||||||
/// Set a default exchange parameter. This can be used instead of passing in an ExchangeParameters object which each request.
|
/// Set a default exchange parameter. This can be used instead of passing in an ExchangeParameters object which each request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Book ticker
|
/// Book ticker
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SharedBookTicker
|
public record SharedBookTicker : SharedSymbolModel
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Price of the best ask
|
/// Price of the best ask
|
||||||
@ -25,7 +25,8 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </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;
|
BestAskPrice = bestAskPrice;
|
||||||
BestAskQuantity = bestAskQuantity;
|
BestAskQuantity = bestAskQuantity;
|
||||||
|
@ -5,12 +5,8 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Futures order info
|
/// Futures order info
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SharedFuturesOrder
|
public record SharedFuturesOrder : SharedSymbolModel
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Symbol
|
|
||||||
/// </summary>
|
|
||||||
public string Symbol { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Id of the order
|
/// Id of the order
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -97,14 +93,15 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SharedFuturesOrder(
|
public SharedFuturesOrder(
|
||||||
|
SharedSymbol? sharedSymbol,
|
||||||
string symbol,
|
string symbol,
|
||||||
string orderId,
|
string orderId,
|
||||||
SharedOrderType orderType,
|
SharedOrderType orderType,
|
||||||
SharedOrderSide orderSide,
|
SharedOrderSide orderSide,
|
||||||
SharedOrderStatus orderStatus,
|
SharedOrderStatus orderStatus,
|
||||||
DateTime? createTime)
|
DateTime? createTime)
|
||||||
|
: base(sharedSymbol, symbol)
|
||||||
{
|
{
|
||||||
Symbol = symbol;
|
|
||||||
OrderId = orderId;
|
OrderId = orderId;
|
||||||
OrderType = orderType;
|
OrderType = orderType;
|
||||||
Side = orderSide;
|
Side = orderSide;
|
||||||
|
@ -7,10 +7,6 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public record SharedFuturesSymbol : SharedSpotSymbol
|
public record SharedFuturesSymbol : SharedSpotSymbol
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Symbol type
|
|
||||||
/// </summary>
|
|
||||||
public SharedSymbolType SymbolType { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The size of a single contract
|
/// The size of a single contract
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -27,9 +23,8 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// Futures ticker info
|
/// Futures ticker info
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SharedFuturesTicker
|
public record SharedFuturesTicker: SharedSymbolModel
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The symbol
|
|
||||||
/// </summary>
|
|
||||||
public string Symbol { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Last trade price
|
/// Last trade price
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -51,9 +47,9 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </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;
|
LastPrice = lastPrice;
|
||||||
HighPrice = highPrice;
|
HighPrice = highPrice;
|
||||||
LowPrice = lowPrice;
|
LowPrice = lowPrice;
|
||||||
|
@ -5,12 +5,8 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Position info
|
/// Position info
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SharedPosition
|
public record SharedPosition : SharedSymbolModel
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Symbol
|
|
||||||
/// </summary>
|
|
||||||
public string Symbol { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Current size of the position
|
/// Current size of the position
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -43,9 +39,9 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </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;
|
PositionSize = positionSize;
|
||||||
UpdateTime = updateTime;
|
UpdateTime = updateTime;
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,8 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Position history
|
/// Position history
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SharedPositionHistory
|
public record SharedPositionHistory : SharedSymbolModel
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Symbol of the position
|
|
||||||
/// </summary>
|
|
||||||
public string Symbol { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The side of the position
|
/// The side of the position
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -52,6 +48,7 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SharedPositionHistory(
|
public SharedPositionHistory(
|
||||||
|
SharedSymbol? sharedSymbol,
|
||||||
string symbol,
|
string symbol,
|
||||||
SharedPositionSide side,
|
SharedPositionSide side,
|
||||||
decimal openPrice,
|
decimal openPrice,
|
||||||
@ -59,8 +56,8 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
decimal quantity,
|
decimal quantity,
|
||||||
decimal realizedPnl,
|
decimal realizedPnl,
|
||||||
DateTime timestamp)
|
DateTime timestamp)
|
||||||
|
: base(sharedSymbol, symbol)
|
||||||
{
|
{
|
||||||
Symbol = symbol;
|
|
||||||
PositionSide = side;
|
PositionSide = side;
|
||||||
AverageOpenPrice = openPrice;
|
AverageOpenPrice = openPrice;
|
||||||
AverageClosePrice = closePrice;
|
AverageClosePrice = closePrice;
|
||||||
|
@ -5,12 +5,8 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Spot order info
|
/// Spot order info
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SharedSpotOrder
|
public record SharedSpotOrder : SharedSymbolModel
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The symbol the order is on
|
|
||||||
/// </summary>
|
|
||||||
public string Symbol { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The id of the order
|
/// The id of the order
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -84,14 +80,15 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SharedSpotOrder(
|
public SharedSpotOrder(
|
||||||
|
SharedSymbol? sharedSymbol,
|
||||||
string symbol,
|
string symbol,
|
||||||
string orderId,
|
string orderId,
|
||||||
SharedOrderType orderType,
|
SharedOrderType orderType,
|
||||||
SharedOrderSide orderSide,
|
SharedOrderSide orderSide,
|
||||||
SharedOrderStatus orderStatus,
|
SharedOrderStatus orderStatus,
|
||||||
DateTime? createTime)
|
DateTime? createTime)
|
||||||
|
: base(sharedSymbol, symbol)
|
||||||
{
|
{
|
||||||
Symbol = symbol;
|
|
||||||
OrderId = orderId;
|
OrderId = orderId;
|
||||||
OrderType = orderType;
|
OrderType = orderType;
|
||||||
Side = orderSide;
|
Side = orderSide;
|
||||||
|
@ -5,6 +5,10 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public record SharedSpotSymbol
|
public record SharedSpotSymbol
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The trading mode of the symbol
|
||||||
|
/// </summary>
|
||||||
|
public TradingMode TradingMode { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Base asset of the symbol
|
/// Base asset of the symbol
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -57,8 +61,9 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </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;
|
BaseAsset = baseAsset;
|
||||||
QuoteAsset = quoteAsset;
|
QuoteAsset = quoteAsset;
|
||||||
Name = symbol;
|
Name = symbol;
|
||||||
|
@ -3,12 +3,8 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ticker info
|
/// Ticker info
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SharedSpotTicker
|
public record SharedSpotTicker: SharedSymbolModel
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Symbol name
|
|
||||||
/// </summary>
|
|
||||||
public string Symbol { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Last trade price
|
/// Last trade price
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -33,9 +29,9 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </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;
|
LastPrice = lastPrice;
|
||||||
HighPrice = highPrice;
|
HighPrice = highPrice;
|
||||||
LowPrice = lowPrice;
|
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>
|
/// <summary>
|
||||||
/// A user trade
|
/// A user trade
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SharedUserTrade
|
public record SharedUserTrade : SharedSymbolModel
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Symbol the trade was on
|
|
||||||
/// </summary>
|
|
||||||
public string Symbol { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The trade id
|
/// The trade id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -51,7 +47,8 @@ namespace CryptoExchange.Net.SharedApis
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </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;
|
Symbol = symbol;
|
||||||
OrderId = orderId;
|
OrderId = orderId;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user