/
benchmarkService.go
246 lines (198 loc) · 8.07 KB
/
benchmarkService.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package benchmark
import (
"fmt"
"path"
"runtime"
"time"
"github.com/fabiodmferreira/crypto-trading/accounts"
"github.com/fabiodmferreira/crypto-trading/app"
"github.com/fabiodmferreira/crypto-trading/assets"
"github.com/fabiodmferreira/crypto-trading/broker"
"github.com/fabiodmferreira/crypto-trading/collectors"
"github.com/fabiodmferreira/crypto-trading/decisionmaker"
"github.com/fabiodmferreira/crypto-trading/domain"
"github.com/fabiodmferreira/crypto-trading/indicators"
"github.com/fabiodmferreira/crypto-trading/trader"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
adadatahistory "github.com/fabiodmferreira/crypto-trading/data-history/ada"
btcdatahistory "github.com/fabiodmferreira/crypto-trading/data-history/btc"
btccashdatahistory "github.com/fabiodmferreira/crypto-trading/data-history/btc-cash"
eosdatahistory "github.com/fabiodmferreira/crypto-trading/data-history/eos"
etcdatahistory "github.com/fabiodmferreira/crypto-trading/data-history/etc"
ethdatahistory "github.com/fabiodmferreira/crypto-trading/data-history/eth"
ltcdatahistory "github.com/fabiodmferreira/crypto-trading/data-history/ltc"
monerodatahistory "github.com/fabiodmferreira/crypto-trading/data-history/monero"
stellardatahistory "github.com/fabiodmferreira/crypto-trading/data-history/stellar"
xrpdatahistory "github.com/fabiodmferreira/crypto-trading/data-history/xrp"
)
// Input is an alias for BenchmarkInput
type Input = domain.BenchmarkInput
// Output is an alias for BenchmarkOutput
type Output = domain.BenchmarkOutput
// Service is a service with all methods to interact with benchmark related functions
type Service struct {
repository domain.BenchmarksRepository
assetpriceRepository domain.AssetPriceRepository
applicationExecutionStatesRepository domain.ApplicationExecutionStateRepository
}
// NewService returns an instance of Service
func NewService(repo domain.BenchmarksRepository, assetpriceRepository domain.AssetPriceRepository, applicationExecutionStatesRepository domain.ApplicationExecutionStateRepository) *Service {
return &Service{repo, assetpriceRepository, applicationExecutionStatesRepository}
}
// Create inserts one benchmark in database
func (s *Service) Create(input domain.BenchmarkInput) (*domain.Benchmark, error) {
benchmark := &domain.Benchmark{ID: primitive.NewObjectID(), Input: input, Status: "Pending", CreatedAt: time.Now()}
return benchmark, s.repository.InsertOne(benchmark)
}
// DeleteByID removes one benchmark from database
func (s *Service) DeleteByID(id string) error {
err := s.repository.DeleteByID(id)
if err != nil {
return err
}
go s.applicationExecutionStatesRepository.BulkDeleteByExecutionID(id)
return nil
}
// FindAll returns every benchmark
func (s *Service) FindAll() (*[]domain.Benchmark, error) {
return s.repository.FindAll()
}
// BulkRun runs multiple benchmarks concurrently
func (s *Service) BulkRun(inputs []Input, c chan domain.BenchmarkResult) {
for _, input := range inputs {
go s.routineRun(&input, c)
}
}
// ChannelService passes a benchmark output to a channel. Useful to run benchmarks in routines.
func (s *Service) routineRun(input *Input, done chan domain.BenchmarkResult) {
result, err := s.Run(*input, nil)
done <- domain.BenchmarkResult{Input: input, Output: result, Err: err}
}
// Run executes benchmark and returns performance results
func (s *Service) Run(input Input, benchmarkID *primitive.ObjectID) (*Output, error) {
benchmarkApplication, err := s.setupApplication(input)
if err != nil {
return nil, err
}
var states []bson.M
var LastPrice float32
benchmarkApplication.RegistOnNewAssetPrice(func(ohlc *domain.OHLC) {
if benchmarkID != nil {
states = append(states, bson.M{
"date": ohlc.Time,
"executionId": *benchmarkID,
"state": benchmarkApplication.GetState(),
})
if len(states) == 1000 {
s.applicationExecutionStatesRepository.BulkCreate(&states)
states = []bson.M{}
}
LastPrice = ohlc.Close
}
})
benchmarkApplication.Start()
assetsDocs, _ := benchmarkApplication.FetchAssets()
benchmarkAssetsInfo := assets.GroupAssetsByState(assetsDocs)
amount, _ := benchmarkApplication.GetAccountAmount()
output := domain.BenchmarkOutput{
Buys: benchmarkAssetsInfo.Buys,
Sells: benchmarkAssetsInfo.Sells,
SellsPending: benchmarkAssetsInfo.SellsPending,
AssetsAmountPending: benchmarkAssetsInfo.AssetsAmountPending,
AssetsValuePending: LastPrice * benchmarkAssetsInfo.AssetsAmountPending,
LastPrice: LastPrice,
FinalAmount: amount,
Assets: assetsDocs,
}
return &output, nil
}
// setupApplication create the necessary application to run the benchmark
func (s *Service) setupApplication(input Input) (*app.App, error) {
statisticsOptions := domain.StatisticsOptions{
NumberOfPointsHold: input.StatisticsOptions.NumberOfPointsHold / 2,
}
priceIndicator := indicators.NewPriceIndicator(indicators.NewMetricStatisticsIndicator(statisticsOptions))
volumeIndicator := indicators.NewVolumeIndicator(indicators.NewMetricStatisticsIndicator(statisticsOptions))
assetsRepository := &assets.AssetsRepositoryInMemory{}
accountService := accounts.NewAccountServiceInMemory(float32(input.AccountInitialAmount), assetsRepository)
buyStrategy := decisionmaker.NewBuyStrategy(priceIndicator, volumeIndicator, accountService, input.DecisionMakerOptions)
sellStrategy := decisionmaker.NewSellStrategy(priceIndicator, volumeIndicator, accountService, input.DecisionMakerOptions)
decisionMaker := decisionmaker.NewDecisionMaker(buyStrategy, sellStrategy)
_, currentFilePath, _, _ := runtime.Caller(0)
currentDir := path.Dir(currentFilePath)
historyFile, err := collectors.GetCsv(fmt.Sprintf("%v/../data-history/%v", currentDir, input.DataSourceFilePath))
if err != nil {
return nil, err
}
input.CollectorOptions.DataSource = historyFile
collector := collectors.NewFileTickerCollector(input.CollectorOptions, &[]domain.Indicator{priceIndicator, volumeIndicator})
broker := broker.NewBrokerMock()
trader := trader.NewTrader(broker)
application := app.NewApp(&[]domain.Collector{collector}, decisionMaker, trader, accountService)
return application, err
}
// HandleBenchmark executes benchmark and updates database accordingly
func (s *Service) HandleBenchmark(benchmark *domain.Benchmark) error {
output, err := s.Run(benchmark.Input, &benchmark.ID)
if err != nil {
return err
}
// updates benchmark status and benchmark output
err = s.repository.UpdateBenchmarkCompleted(benchmark.ID.Hex(), output)
if err != nil {
return err
}
return nil
}
// GetDataSources returns all available data sources
func (s *Service) GetDataSources() map[string]map[string]string {
return map[string]map[string]string{
"btc": {
"2019": btcdatahistory.Twenty19,
"2020 H1": btcdatahistory.TwentyTwentyH1,
"2019-2020 H1": btcdatahistory.Twenty1920H1,
},
"btc-cash": {
"2019": btccashdatahistory.Twenty19,
"2020 H1": btccashdatahistory.TwentyTwentyH1,
},
"eos": {
"2019": eosdatahistory.Twenty19,
"2020 H1": eosdatahistory.TwentyTwentyH1,
},
"etc": {
"2019": etcdatahistory.Twenty19,
"2020 H1": etcdatahistory.TwentyTwentyH1,
},
"ltc": {
"2019": ltcdatahistory.Twenty19,
"2020 H1": ltcdatahistory.TwentyTwentyH1,
},
"monero": {
"2019": monerodatahistory.Twenty19,
"2020 H1": monerodatahistory.TwentyTwentyH1,
},
"stellar": {
"2019": stellardatahistory.Twenty19,
"2020 H1": stellardatahistory.TwentyTwentyH1,
},
"xrp": {
"2019": xrpdatahistory.Twenty19,
"2020 H1": xrpdatahistory.TwentyTwentyH1,
},
"eth": {
"2019": ethdatahistory.Twenty19,
"2020 H1": ethdatahistory.TwentyTwentyH1,
},
"ada": {
"2019": adadatahistory.Twenty19,
"2020 H1": adadatahistory.TwentyTwentyH1,
},
}
}
// AggregateApplicationState returns an aggregate of application state
func (s *Service) AggregateApplicationState(pipeline mongo.Pipeline) (*[]bson.M, error) {
return s.applicationExecutionStatesRepository.Aggregate(pipeline)
}