This repository has been archived by the owner on Sep 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrader.js
77 lines (66 loc) · 1.93 KB
/
trader.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
'use strict';
var DEBUG = false; // when true, orders are just validated by Kraken, not fulfilled
var SATOSHI_FACTOR = Math.pow(10,8);
// Translating currency code for Kraken
var currencies = {'USD' : 'ZUSD',
'EUR' : 'ZEUR',
'BTC' : 'XXBT'
};
var KrakenClient = require('kraken-api');
var KrakenTrade = function(config) {
this.client = new KrakenClient(config.key, config.secret);
if (config.currency != 'EUR' || config.currency != 'USD') {
// need to raise error, can trade only EUR
}
this._currency = config.currency;
};
KrakenTrade.factory = function factory(config) {
return new KrakenTrade(config);
};
// Public functions
KrakenTrade.prototype.balance = function balance(callback) {
var kcurrency = currencies[this._currency];
this.client.api('Balance', null, function(error, response) {
if (error) {
return callback(error);
}
else {
var balances = response.result;
var eur_balance = 0.0;
if (balances[kcurrency]) {
try {
eur_balance = parseFloat(balances[kcurrency], 10)
} catch(e) {
return callback("Can't parse balance information!");
}
}
return callback(null, eur_balance);
}
});
};
KrakenTrade.prototype.currency = function currency() {
return this._currency;
};
KrakenTrade.prototype.purchase = function purchase(satoshis, currentPrice, callback) {
var bitcoins = satoshis / SATOSHI_FACTOR;
var amountStr = bitcoins.toFixed(8);
// Buy at market price, when validate is true only check but do not fulfill
var orderInfo = { 'pair' : 'XXBT'+currencies[this._currency],
'type' : 'buy',
'ordertype' : 'market',
'volume': bitcoins,
'validate' : DEBUG
};
this.client.api('AddOrder', orderInfo, function(error, response) {
if (error) {
return callback(error);
}
else {
if (DEBUG) {
console.log(response.result);
}
return callback();
}
});
};
module.exports = KrakenTrade;