1 | var moment = require('moment');
|
2 | var _ = require('underscore');
|
3 |
|
4 |
|
5 | var util = {
|
6 | getConfig: function() {
|
7 | var path = require('path');
|
8 | var configFile = path.resolve(util.getArgument('config') || 'config.js');
|
9 | return require(configFile);
|
10 | },
|
11 | getArgument: function(argument) {
|
12 | var ret;
|
13 | _.each(process.argv, function(arg) {
|
14 | var pos = arg.indexOf(argument + '=');
|
15 | if(pos !== -1) {
|
16 | ret = arg.substr(argument.length + 1);
|
17 | }
|
18 | });
|
19 | return ret;
|
20 | },
|
21 | minToMs: function(min) {
|
22 | return min * 60 * 1000;
|
23 | },
|
24 | toMicro: function(moment) {
|
25 | return moment.format('X') * 1000 * 1000;
|
26 | },
|
27 | intervalsAgo: function(amount) {
|
28 | return moment().subtract('minutes', config.EMA.interval * amount);
|
29 | },
|
30 | average: function(list) {
|
31 | var total = _.reduce(list, function(m, n) { return m + n }, 0);
|
32 | return total / list.length;
|
33 | },
|
34 |
|
35 |
|
36 | calculatePriceSince: function(treshold, trades) {
|
37 | var sample = [];
|
38 | _.every(trades, function(trade) {
|
39 | if(moment.unix(trade.date) < treshold)
|
40 | return false;
|
41 |
|
42 | var price = parseFloat(trade.price);
|
43 | sample.push(price);
|
44 | return true;
|
45 | });
|
46 |
|
47 | return util.average(sample);
|
48 | },
|
49 |
|
50 |
|
51 | calculatePriceTill: function(treshold, trades) {
|
52 | var sample = [];
|
53 | _.every(trades, function(trade) {
|
54 | if(moment.unix(trade.date) > treshold)
|
55 | return false;
|
56 |
|
57 | var price = parseFloat(trade.price);
|
58 | sample.push(price);
|
59 | return true;
|
60 | });
|
61 |
|
62 | return util.average(sample);
|
63 | }
|
64 | }
|
65 |
|
66 | var config = util.getConfig();
|
67 |
|
68 | module.exports = util; |
\ | No newline at end of file |