-
Notifications
You must be signed in to change notification settings - Fork 52
/
TradeStrategyCacheManager.cs
78 lines (65 loc) · 2.54 KB
/
TradeStrategyCacheManager.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
using DevelopmentInProgress.TradeServer.StrategyExecution.WebHost.Notification.Server;
using DevelopmentInProgress.TradeView.Core.TradeStrategy;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DevelopmentInProgress.TradeServer.StrategyExecution.WebHost.Cache.TradeStrategy
{
public class TradeStrategyCacheManager : ServerNotificationBase, ITradeStrategyCacheManager
{
private readonly ConcurrentDictionary<string, ITradeStrategy> tradeStrategies;
public TradeStrategyCacheManager()
{
tradeStrategies = new ConcurrentDictionary<string, ITradeStrategy>();
}
public List<Strategy> GetStrategies()
{
return tradeStrategies.Values.Select(s => s.Strategy).ToList();
}
public bool TryGetTradeStrategy(string strategyName, out ITradeStrategy tradeStrategy)
{
return tradeStrategies.TryGetValue(strategyName, out tradeStrategy);
}
public bool TryAddTradeStrategy(string strategyName, ITradeStrategy tradeStrategy)
{
if (tradeStrategies.TryAdd(strategyName, tradeStrategy))
{
OnServerNotification();
return true;
}
return false;
}
public bool TryRemoveTradeStrategy(string strategyName, out ITradeStrategy tradeStrategy)
{
if (tradeStrategies.TryRemove(strategyName, out tradeStrategy))
{
OnServerNotification();
return true;
}
return false;
}
public async Task StopStrategy(string strategyName, string parameters)
{
if (tradeStrategies.TryGetValue(strategyName, out ITradeStrategy tradeStrategy))
{
await tradeStrategy.TryStopStrategy(parameters).ConfigureAwait(false);
OnServerNotification();
}
}
public async Task UpdateStrategy(string strategyName, string parameters)
{
if (tradeStrategies.TryGetValue(strategyName, out ITradeStrategy tradeStrategy))
{
await tradeStrategy.TryUpdateStrategyAsync(parameters).ConfigureAwait(false);
OnServerNotification();
}
}
public void StopStrategies()
{
var strategies = tradeStrategies.Values;
var tasks = strategies.Select(s => s.TryStopStrategy(string.Empty));
Task.WhenAll(tasks);
}
}
}