Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
5.4 kB
12
Indexable
Never
using Deneme.Net;
using Deneme.Net.Enums;

namespace ExcelTakip;
public class DenemeCoinsOnly
{
    private Dictionary<string, Dictionary<decimal, decimal>> _bidsOriginal;
    private Dictionary<string, Dictionary<decimal, decimal>> _asksOriginal;
    private Dictionary<string, Dictionary<DateTime, List<Tuple<OrderSide, decimal, decimal, DateTime>>>> _tradeDataOriginal;
    private Dictionary<decimal, decimal> _bids = new();
    private Dictionary<decimal, decimal> _asks = new();
    private Dictionary<DateTime, List<Tuple<OrderSide, decimal, decimal, DateTime>>> _tradeData = new();
    
    public Dictionary<decimal, decimal> GetAsks(string sembol)
    {
        lock (_asksOriginal[sembol]) lock (_bidsOriginal[sembol])
        {
            _asks = new Dictionary<decimal, decimal>(_asksOriginal[sembol]);
        }
        return new Dictionary<decimal, decimal>(_asks.OrderBy(x => x.Key));
    }

    public Dictionary<decimal, decimal> GetBids(string sembol)
    {
        lock (_bidsOriginal[sembol]) lock (_asksOriginal[sembol])
        {
            _bids = new Dictionary<decimal, decimal>(_bidsOriginal[sembol]);
        }
        return new Dictionary<decimal, decimal>(_bids.OrderByDescending(x => x.Key));
    }
    public Dictionary<DateTime, List<Tuple<OrderSide, decimal, decimal, DateTime>>> GetTradeData(string sembol)
    {
        lock (_tradeDataOriginal[sembol])
        {
            _tradeData = new Dictionary<DateTime, List<Tuple<OrderSide, decimal, decimal, DateTime>>>(_tradeDataOriginal[sembol]);
        }

        var list = new List<DateTime>(_tradeData.Keys.Where(x => (DateTime.UtcNow - x).TotalSeconds >= 120));
        list.ForEach(x => _tradeData.Remove(x));
        
        lock (_tradeDataOriginal[sembol])
        {
            _tradeDataOriginal[sembol] = new Dictionary<DateTime, List<Tuple<OrderSide, decimal, decimal, DateTime>>>(_tradeData);
        }
        return new Dictionary<DateTime, List<Tuple<OrderSide, decimal, decimal, DateTime>>>(_tradeData);
    }
    private DenemeClient _client;
    private DenemeSocketClient _socketClient;
    public DenemeCoinsOnly()
    {
        _client = new DenemeClient();
        _socketClient = new DenemeSocketClient();
        _bidsOriginal = new Dictionary<string, Dictionary<decimal,decimal>>();
        _asksOriginal = new Dictionary<string, Dictionary<decimal,decimal>>();
        _tradeDataOriginal = new Dictionary<string, Dictionary<DateTime, List<Tuple<OrderSide, decimal, decimal, DateTime>>>>();
    }
    public void CreateReqVar(string sembol)
    {
        _bidsOriginal.Add(sembol, new Dictionary<decimal, decimal>());
        _asksOriginal.Add(sembol, new Dictionary<decimal, decimal>());
        _tradeDataOriginal.Add(sembol, new Dictionary<DateTime, List<Tuple<OrderSide, decimal, decimal, DateTime>>>());
    }

    public async Task StartRestControl(string sembol)
    {
        var marketData = await _client.GetMarketDataAsync(sembol);
        _bidsOriginal[sembol] = new Dictionary<decimal, decimal>(marketData.Data.OrderBook.Bids.ToDictionary(x => x.Price, x => x.Amount));
        _asksOriginal[sembol] = new Dictionary<decimal, decimal>(marketData.Data.OrderBook.Asks.ToDictionary(x => x.Price, x => x.Amount));
    }
    public async Task StartSocket(string symbol)
    {
        await _socketClient.SubscribeToMarketDataAsync(symbol, (d) =>
        {
            Task.Run(() =>
            {
                lock (_asksOriginal[symbol])
                {
                    d.AsksToAdd.ForEach(x => 
                    {
                        if (!_asksOriginal[symbol].ContainsKey(x.Price)) _asksOriginal[symbol].TryAdd(x.Price, x.Amount);
                        _asksOriginal[symbol][x.Price] = x.Amount;
                    });
                    d.AsksToRemove.ForEach(x =>
                    {
                        if (!_asksOriginal.ContainsKey(symbol)) return;
                        if (_asksOriginal[symbol].ContainsKey(x.Price)) _asksOriginal[symbol].Remove(x.Price);
                    });
                }
            });
            Task.Run(() =>
            {
                lock (_bidsOriginal[symbol])
                {
                    d.BidsToAdd.ForEach(x =>
                    {
                        if (!_bidsOriginal[symbol].ContainsKey(x.Price)) _bidsOriginal[symbol].TryAdd(x.Price, x.Amount);
                        _bidsOriginal[symbol][x.Price] = x.Amount;
                    });
                    d.BidsToRemove.ForEach(x =>
                    {
                        if (!_bidsOriginal.ContainsKey(symbol)) return; 
                        if (_bidsOriginal[symbol].ContainsKey(x.Price)) _bidsOriginal[symbol].Remove(x.Price);
                    });
                }
            });
        }, (d) =>
        {
            Task.Run(() => 
            {
                lock (_tradeDataOriginal[symbol])
                {
                    if (!_tradeDataOriginal[symbol].ContainsKey(d.Timestamp))
                    {
                        _tradeDataOriginal[symbol].TryAdd(d.Timestamp, new List<Tuple<OrderSide, decimal, decimal, DateTime>>());
                    }
                    _tradeDataOriginal[symbol][d.Timestamp].Add(new Tuple<OrderSide, decimal, decimal, DateTime>(d.Side, d.Amount, d.Price, DateTime.Now));
                }
            });
        });
    }
}