forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
205 lines (188 loc) · 4.8 KB
/
util.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
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
var moment = require('moment');
var _ = require('lodash');
var path = require('path');
var fs = require('fs');
var semver = require('semver');
var program = require('commander');
var retry = require('retry');
var startTime = moment();
var _config = false;
var _package = false;
var _nodeVersion = false;
var _gekkoMode = false;
var _gekkoEnv = false;
var _args = false;
// helper functions
var util = {
getConfig: function() {
// cache
if(_config)
return _config;
if(!program.config)
util.die('Please specify a config file.', true);
if(!fs.existsSync(util.dirs().gekko + program.config))
util.die('Cannot find the specified config file.', true);
_config = require(util.dirs().gekko + program.config);
return _config;
},
// overwrite the whole config
setConfig: function(config) {
_config = config;
},
setConfigProperty: function(parent, key, value) {
if(parent)
_config[parent][key] = value;
else
_config[key] = value;
},
getVersion: function() {
return util.getPackage().version;
},
getPackage: function() {
if(_package)
return _package;
_package = JSON.parse( fs.readFileSync(__dirname + '/../package.json', 'utf8') );
return _package;
},
getRequiredNodeVersion: function() {
return util.getPackage().engines.node;
},
recentNode: function() {
var required = util.getRequiredNodeVersion();
return semver.satisfies(process.version, required);
},
// check if two moments are corresponding
// to the same time
equals: function(a, b) {
return !(a < b || a > b)
},
minToMs: function(min) {
return min * 60 * 1000;
},
defer: function(fn) {
return function(args) {
var args = _.toArray(arguments);
return _.defer(function() { fn.apply(this, args) });
}
},
logVersion: function() {
return `Gekko version: v${util.getVersion()}`
+ `\nNodejs version: ${process.version}`;
},
die: function(m, soft) {
if(_gekkoEnv === 'standalone' || !_gekkoEnv)
var log = console.log.bind(console);
else if(_gekkoEnv === 'child-process')
var log = m => process.send({type: 'error', error: m});
if(m) {
if(soft) {
log('\n ERROR: ' + m + '\n\n');
} else {
log('\n\nGekko encountered an error and can\'t continue');
log('\nError:\n');
log(m, '\n\n');
log('\nMeta debug info:\n');
log(util.logVersion());
log('');
}
}
process.exit(1);
},
dirs: function() {
var ROOT = __dirname + '/../';
return {
gekko: ROOT,
core: ROOT + 'core/',
markets: ROOT + 'core/markets/',
exchanges: ROOT + 'exchanges/',
plugins: ROOT + 'plugins/',
methods: ROOT + 'strategies/',
indicators: ROOT + 'strategies/indicators/',
budfox: ROOT + 'core/budfox/',
importers: ROOT + 'importers/exchanges/',
tools: ROOT + 'core/tools/',
workers: ROOT + 'core/workers/',
web: ROOT + 'web/',
config: ROOT + 'config/'
}
},
inherit: function(dest, source) {
require('util').inherits(
dest,
source
);
},
makeEventEmitter: function(dest) {
util.inherit(dest, require('events').EventEmitter);
},
setGekkoMode: function(mode) {
_gekkoMode = mode;
},
gekkoMode: function() {
if(_gekkoMode)
return _gekkoMode;
if(program['import'])
return 'importer';
else if(program.backtest)
return 'backtest';
else
return 'realtime';
},
gekkoModes: function() {
return [
'importer',
'backtest',
'realtime'
]
},
setGekkoEnv: function(env) {
_gekkoEnv = env;
},
gekkoEnv: function() {
return _gekkoEnv || 'standalone';
},
launchUI: function() {
if(program['ui'])
return true;
else
return false;
},
getStartTime: function() {
return startTime;
},
retry: function(fn, callback) {
var operation = retry.operation({
retries: 5,
factor: 1.2,
minTimeout: 1 * 1000,
maxTimeout: 3 * 1000
});
operation.attempt(function(currentAttempt) {
fn(function(err, result) {
if (operation.retry(err)) {
return;
}
callback(err ? operation.mainError() : null, result);
});
});
}
}
// NOTE: those options are only used
// in stand alone mode
program
.version(util.logVersion())
.option('-c, --config <file>', 'Config file')
.option('-b, --backtest', 'backtesting mode')
.option('-i, --import', 'importer mode')
.option('--ui', 'launch a web UI')
.parse(process.argv);
// make sure the current node version is recent enough
if(!util.recentNode())
util.die([
'Your local version of Node.js is too old. ',
'You have ',
process.version,
' and you need atleast ',
util.getRequiredNodeVersion()
].join(''), true);
module.exports = util;