-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoylib.cs
More file actions
132 lines (103 loc) · 3.97 KB
/
Copy pathDoylib.cs
File metadata and controls
132 lines (103 loc) · 3.97 KB
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
using System;
using System.Net.Http;
using System.Threading.Tasks;
using doylib.Ai;
using doylib.Ai.Interfaces;
using doylib.Logging;
using doylib.Services;
using doylib.Services.Interfaces;
using doylib.Strategy;
using doylib.Strategy.Modules;
using DoyVestment.Framework.Models;
using DoyVestment.Framework.Models.Enums;
using DoyVestment.Framework.Services;
using DoyVestment.Framework.Services.Interfaces;
using Microsoft.Extensions.Logging;
using TradeAction = DoyVestment.Framework.Models.Enums.TradeAction;
namespace doylib;
public class Doylib : IStrategy, IDisposable
{
private readonly ILogger mLogger;
private readonly IDoyExceptionHandler mDoyExceptionHandler;
private readonly ICandleWindowService mCandleWindowService;
private readonly DecisionEngine mDecisionEngine;
private readonly IActiveTradeHandler mActiveTradeHandler;
private readonly IAiInferenceService? mAiInferenceService;
private readonly ITradeBackupClient mTradeBackupClient;
private readonly IBackupService mBackupService;
private readonly DoyLibSettings mSettings;
private event EventHandler<Guid> mTradeClosedSuccessfully;
// TODO: Add some kind of Containerization / DI to Doylib to avoid redundant class initializations.
public Doylib(DoyLibSettings settings)
{
mLogger = LoggerProvider.CreateLogger<Doylib>();
mDoyExceptionHandler = new DoyExceptionHandler(new ProcessTerminationService());
mCandleWindowService = new CandleWindowService(LoggerProvider.CreateLogger<CandleWindowService>(), mDoyExceptionHandler);
mCandleWindowService.Initialize(settings.MaxCandleWindowSize);
mActiveTradeHandler = new ActiveTradeHandler(mDoyExceptionHandler);
var backupHttpClient = new HttpClient();
mTradeBackupClient = new TradeBackupClient(settings.ApiBaseUrl, backupHttpClient);
mBackupService = new BackupService(mTradeBackupClient, mActiveTradeHandler);
if (settings.Ai != null && settings.Ai.Enabled)
{
var aiSettings = settings.Ai;
mAiInferenceService = new OnnxInferenceService(aiSettings);
}
mDecisionEngine = new DecisionEngine(settings, mAiInferenceService);
mDecisionEngine.Register(new ExampleModule(mCandleWindowService));
mDecisionEngine.Register(new ExampleAiModule(mCandleWindowService));
mTradeClosedSuccessfully += mActiveTradeHandler.OnTradeClosedSuccessfully;
mSettings = settings;
}
public DoyLibTradeResponse Execute(Candle candle)
{
if (mLogger.IsEnabled(LogLevel.Debug))
{
mLogger.LogDebug("Execute called");
}
mCandleWindowService.AddCandle(candle);
mActiveTradeHandler.RemoveTpOrSlHit(candle);
var decision = mDecisionEngine.Evaluate();
if (mLogger.IsEnabled(LogLevel.Debug))
{
mLogger.LogDebug("Got decision: {Decision}", decision);
}
DoyLibTradeResponse response;
if (mActiveTradeHandler.ExampleHandle(decision, out var doyTradeId))
{
response = new DoyLibTradeResponse(doyTradeId!.Value, TradeAction.CLOSE, null, null);
return response;
}
response = new DoyLibTradeResponse(
Guid.NewGuid(),
decision,
null,
null);
mActiveTradeHandler.AddActiveTrade(response);
return response;
}
public async Task RestoreActiveTrades()
{
await mBackupService.RestoreActiveTrades();
}
public void Warmup()
{
mDecisionEngine.Warmup();
}
public string[] GetActiveModules()
{
return mDecisionEngine.GetActiveModules();
}
public void AddCandle(Candle[] candles)
{
mCandleWindowService.AddCandle(candles);
}
public void FireTradeClosedSuccessfully(Guid doyTradeId)
{
mTradeClosedSuccessfully.Invoke(this, doyTradeId);
}
public void Dispose()
{
mAiInferenceService?.Dispose();
}
}