forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportfolioManager.js
87 lines (68 loc) · 1.86 KB
/
portfolioManager.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
/*
The Portfolio class holds data about the portfolio
*/
const _ = require('lodash');
const async = require('async');
const errors = require('./exchangeErrors');
// const EventEmitter = require('events');
class Portfolio {
constructor(config, api) {
_.bindAll(this);
this.config = config;
this.api = api;
this.balances = {};
this.fee = null;
}
getBalance(fund) {
return this.getFund(fund).amount;
}
// return the [fund] based on the data we have in memory
getFund(fund) {
return _.find(this.balances, function(f) { return f.name === fund});
}
// convert into the portfolio expected by the performanceAnalyzer
convertBalances(asset,currency) { // rename?
var asset = _.find(this.balances, a => a.name === this.config.asset).amount;
var currency = _.find(this.balances, a => a.name === this.config.currency).amount;
return {
currency,
asset,
balance: currency + (asset * this.ticker.bid)
}
}
setBalances(callback) {
let set = (err, fullPortfolio) => {
if(err) {
console.log(err);
throw new errors.ExchangeError(err);
}
// only include the currency/asset of this market
const balances = [ this.config.currency, this.config.asset ]
.map(name => {
let item = _.find(fullPortfolio, {name});
if(!item) {
// assume we have 0
item = { name, amount: 0 };
}
return item;
});
this.balances = balances;
if(_.isFunction(callback))
callback();
}
this.api.getPortfolio(set);
}
setFee(callback) {
this.api.getFee((err, fee) => {
if(err)
throw new errors.ExchangeError(err);
this.fee = fee;
if(_.isFunction(callback))
callback();
});
}
setTicker(ticker) {
this.ticker = ticker;
}
}
module.exports = Portfolio