-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoylib.cs
More file actions
114 lines (89 loc) · 3.34 KB
/
Doylib.cs
File metadata and controls
114 lines (89 loc) · 3.34 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
using System;
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;
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 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);
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;
}
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 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();
}
}