-
Notifications
You must be signed in to change notification settings - Fork 0
/
buyByPercentage.js
61 lines (53 loc) · 1.66 KB
/
buyByPercentage.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
const Gdax = require('gdax');
/**
* Buy a given amount of crypto currency, spread as specified in gdaxConfig.json
*/
const BASE_CURRENCY = 'USD';
/** @const {key:string, secret:string, pass:string} */
const credentials = require('./gdaxConfig.json');
// get cli arguments
var argv = require('minimist')(process.argv.slice(2));
const buyAmount = argv[BASE_CURRENCY] || argv[BASE_CURRENCY.toLowerCase()];
/**
* Fail and print usage info
*/
function quitAndPrintUsage() {
console.log(' usage: node buyByPercentage.js --usd=1 [--limit=10%]');
console.log(' (buy amount in $), (percent limit below market)');
process.exit(1);
}
if (!credentials.buyPercentages) {
console.error('buyPercentage key missing in config');
quitAndPrintUsage();
}
else if (!buyAmount) {
console.error('no currency or amount specified')
quitAndPrintUsage();
}
if (argv.limit) {
// TODO: support limit orders
}
else {
// market order
for (const key of Object.keys(credentials.buyPercentages)) {
buyCurrencyAtMarket(key, credentials.buyPercentages[key]*buyAmount);
}
}
/**
* Post a market order for a currency
* @param {string} name currency name
* @param {} amount
*/
function buyCurrencyAtMarket(name, amount) {
var buyParams = {
'funds': Math.round(amount * 1000) / 1000, // USD,
'product_id': `${name}-${BASE_CURRENCY}`,
'type': 'market',
};
const authedClient = new Gdax.AuthenticatedClient(credentials.key, credentials.secret, credentials.pass);
authedClient.buy(buyParams).then(data => {
console.log(data);
}).catch(error => {
console.error(error);
});
}