-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavanza.js
94 lines (79 loc) · 2.53 KB
/
avanza.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
var az = require("avanza");
var avanza = new az.default();
module.exports = function(username, password, callback) {
var az_position = {};
var az_overview = {};
var az_instruments = {};
var getOverview = function() {
avanza.getOverview().then(overview => {
if (overview) {
az_overview = overview;
}
}, err => {
console.log(err);
});
setTimeout(getOverview, 900000);
};
var getInstrument = function(id) {
if (az_instruments[id] == undefined) {
avanza.getStock(id).then(stock => {
if (stock) {
az_instruments[id] = stock;
}
}, err => {
console.log(id, err);
});
}
};
var getPositions = function() {
avanza.getPositions().then(positions => {
if (positions) {
az_position = positions;
for (var k in az_position) {
if (!az_position.hasOwnProperty(k)) continue;
getInstrument(az_position[k].instrumentId);
}
}
}, err => {
console.log(err);
});
setTimeout(getPositions, 60000);
};
var exportMetrics = function() {
setTimeout(exportMetrics, 30000);
if (az_overview.accounts == undefined) return;
var cap = 0;
for (var j in az_overview.accounts) {
if (!az_overview.accounts.hasOwnProperty(j)) continue;
cap += az_overview.accounts[j].buyingPower;
}
var metrics = {
CASH: {
name: "Cash deposit",
cash: cap
}
};
for (var k in az_position) {
if (!az_position.hasOwnProperty(k)) continue;
var pos = az_position[k];
var id = pos.instrumentId;
if (!az_instruments[id]) continue;
var stock = az_instruments[id];
metrics[pos.accountId + "-" + id] = {
name: stock.name,
type: stock.marketPlace == 'Fondmarknaden' ? 'fund' : 'stock',
code: stock.ticker,
account: pos.accountId,
amount: pos.volume,
value: pos.value,
rate: pos.value/pos.volume
}
}
callback(metrics);
};
avanza.authenticate({username: username, password: password}).then(() => {
getOverview();
getPositions();
exportMetrics();
});
};