-
Notifications
You must be signed in to change notification settings - Fork 51
/
ExchangeSubscriptionsCache.cs
160 lines (134 loc) · 6.04 KB
/
ExchangeSubscriptionsCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using DevelopmentInProgress.TradeView.Core.Enums;
using DevelopmentInProgress.TradeView.Core.Events;
using DevelopmentInProgress.TradeView.Core.Interfaces;
using DevelopmentInProgress.TradeView.Core.Model;
using DevelopmentInProgress.TradeView.Core.TradeStrategy;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;
using System.Threading;
namespace DevelopmentInProgress.TradeServer.StrategyExecution.WebHost.Cache.Subscriptions
{
public class ExchangeSubscriptionsCache : IExchangeSubscriptionsCache
{
private readonly IExchangeService exchangeService;
private readonly Exchange exchange;
private bool disposed;
public ExchangeSubscriptionsCache(Exchange exchange, IExchangeService exchangeService)
{
this.exchange = exchange;
this.exchangeService = exchangeService;
Caches = new ConcurrentDictionary<string, ISubscriptionCache>();
}
public ConcurrentDictionary<string, ISubscriptionCache> Caches { get; private set; }
public bool HasSubscriptions
{
get
{
return (from c in Caches.Values where c.HasSubscriptions select c).Any();
}
}
public async Task Subscribe(string strategyName, List<StrategySubscription> strategySubscriptions, ITradeStrategy tradeStrategy)
{
if (strategySubscriptions == null)
{
throw new ArgumentNullException(nameof(strategySubscriptions));
}
if (tradeStrategy == null)
{
throw new ArgumentNullException(nameof(tradeStrategy));
}
await tradeStrategy.AddExchangeService(strategySubscriptions, exchange, exchangeService).ConfigureAwait(false);
var exchangeApi = exchangeService.GetExchangeApi(exchange);
foreach (var strategySubscription in strategySubscriptions)
{
if (strategySubscription.Subscribes.HasFlag(Subscribes.Trades)
|| strategySubscription.Subscribes.HasFlag(Subscribes.OrderBook)
|| strategySubscription.Subscribes.HasFlag(Subscribes.Candlesticks))
{
if (!Caches.TryGetValue(strategySubscription.Symbol, out ISubscriptionCache symbolCache))
{
symbolCache = new SymbolSubscriptionCache(strategySubscription.Symbol, strategySubscription.Limit, strategySubscription.CandlestickInterval, exchangeApi);
Caches.TryAdd(strategySubscription.Symbol, symbolCache);
}
symbolCache.Subscribe(strategyName, strategySubscription, tradeStrategy);
}
if (strategySubscription.Subscribes.HasFlag(Subscribes.AccountInfo))
{
var user = new User
{
ApiKey = strategySubscription.ApiKey,
ApiSecret = strategySubscription.SecretKey,
ApiPassPhrase = strategySubscription.ApiPassPhrase,
Exchange = strategySubscription.Exchange
};
var accountInfo = await exchangeService.GetAccountInfoAsync(exchange, user, new CancellationToken()).ConfigureAwait(false);
tradeStrategy.SubscribeAccountInfo(new AccountInfoEventArgs { AccountInfo = accountInfo });
if (!Caches.TryGetValue(strategySubscription.ApiKey, out ISubscriptionCache accountInfoCache))
{
accountInfoCache = new AccountInfoSubscriptionCache(exchangeApi);
Caches.TryAdd(strategySubscription.ApiKey, accountInfoCache);
}
accountInfoCache.Subscribe(strategyName, strategySubscription, tradeStrategy);
}
}
}
public void Unsubscribe(string strategyName, List<StrategySubscription> strategySubscriptions, ITradeStrategy tradeStrategy)
{
if (strategySubscriptions == null)
{
throw new ArgumentNullException(nameof(strategySubscriptions));
}
foreach (var strategySubscription in strategySubscriptions)
{
if (strategySubscription.Subscribes.HasFlag(Subscribes.Trades)
|| strategySubscription.Subscribes.HasFlag(Subscribes.OrderBook)
|| strategySubscription.Subscribes.HasFlag(Subscribes.Candlesticks))
{
Unsubscribe(strategyName, strategySubscription, strategySubscription.Symbol, tradeStrategy);
}
if (strategySubscription.Subscribes.HasFlag(Subscribes.AccountInfo))
{
Unsubscribe(strategyName, strategySubscription, strategySubscription.ApiKey, tradeStrategy);
}
}
}
private void Unsubscribe(string strategyName, StrategySubscription symbol, string cacheKey, ITradeStrategy tradeStrategy)
{
if (Caches.TryGetValue(cacheKey, out ISubscriptionCache cache))
{
cache.Unsubscribe(strategyName, symbol, tradeStrategy);
if (!cache.HasSubscriptions)
{
if (Caches.TryRemove(cacheKey, out ISubscriptionCache cacheDispose))
{
cacheDispose.Dispose();
}
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
{
return;
}
if (disposing)
{
foreach (var cache in Caches)
{
cache.Value.Dispose();
}
Caches.Clear();
}
disposed = true;
}
}
}