This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
batch.js
83 lines (64 loc) · 2.24 KB
/
batch.js
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
require('events').EventEmitter.defaultMaxListeners = 1000
const util = require('./core/util.js')
const configsGenerator = require(util.dirs().tools + '/configsGenerator')
const resultsHandler = require(util.dirs().tools + '/resultsHandler')
const info = require(util.dirs().core + '/info')
const async = require('async')
const axios = require('axios')
const csv = require('fast-csv')
const fs = require('fs')
const _ = require('lodash')
const marky = require('marky')
const uniqid = require('uniqid')
util.createResultsFolder()
util.mode = 'batch'
let methodConfigs = configsGenerator.getAllMethodConfigs()
let combs = configsGenerator.generateAllBatchCombinations(methodConfigs)
let gekkoConfigs = configsGenerator.getAllBacktestRequestConfigs(combs)
let fileName = util.generateFileName()
info.initMessage(gekkoConfigs.length)
let csvStream
let writableStream
if (util.config.saveToCsv) {
csvStream = csv.createWriteStream({ headers: true })
writableStream = fs.createWriteStream(`${util.dirs().results}/${fileName}`)
csvStream.pipe(writableStream)
}
async.mapLimit(gekkoConfigs, util.config.parallelQueries, runBacktest, (err) => {
if (err) throw err
if (util.config.saveToCsv) {
csvStream.end()
}
info.finishMessage(fileName)
})
async function runBacktest (config) {
info.startedBacktest(config)
info.completedBacktests++
let backtestId = info.completedBacktests + '_' + uniqid()
marky.mark(backtestId)
try {
await axios.post(`${util.config.apiUrl}/api/backtest`, config).then((response) => {
let row = {}
let performanceReport = response.data.performanceReport
if (!_.isEmpty(performanceReport)) {
info.successfulBacktests++
if (util.config.saveToCsv) {
row = resultsHandler.prepareCsvRow(response.data, config)
csvStream.write(row)
}
info.spentTime += marky.stop(backtestId).duration
info.completeBacktest(config)
} else {
info.failureBacktests++
info.spentTime += marky.stop(backtestId).duration
info.withoutTrades(config)
}
info.processInfo()
})
} catch (err) {
util.errorHandler(err)
info.errorInMethod(config)
info.failureBacktests++
info.spentTime += marky.stop(backtestId).duration
}
}