-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
executable file
·132 lines (120 loc) · 3.77 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* crypto-trader
*
* Written by: Jonas-Taha El Sesiy
*/
require('dotenv').config();
const Kraken = require('kraken-api');
const fs = require('fs');
const util = require('util');
const kraken = new Kraken(process.env.KRAKEN_API_KEY, process.env.KRAKEN_API_SECRET, {
timeout: 60 * 60 * 48 * 1000
});
const amount = process.env.AMOUNT;
const coin = process.env.COIN;
const fiat = process.env.FIAT;
/**
* Min order amounts as of 08/29/17 (see https://support.kraken.com/hc/en-us/articles/205893708-What-is-the-minimum-order-size):
*
* Augur (REP): 0.3
* Bitcoin (XBT): 0.002
* Bitcoin Cash (BCH): 0.002
* Dash (DASH): 0.03
* Dogecoin (DOGE): 3000
* EOS (EOS): 3
* Ethereum (ETH): 0.02
* Ethereum Classic (ETC): 0.3
* Gnosis (GNO): 0.03
* Iconomi (ICN): 2
* Litecoin (LTC): 0.1
* Melon (MLN): 0.1
* Monero (XMR): 0.1
* Ripple (XRP): 30
* Stellar Lumens (XLM): 300
* Zcash (ZEC): 0.03
* Tether (USDT): 5
*/
const minOrderSize = {
REP: 0.3,
XBT: 0.002,
BCH: 0.002,
DASH: 0.03,
DOGE: 3000,
EOS: 3,
ETH: 0.02,
ETC: 0.3,
GNO: 0.03,
ICN: 2,
LTC: 0.1,
MLN: 0.1,
XMR: 0.1,
XRP: 30,
XLM: 300,
ZEC: 0.03,
USDT: 5
};
(async() => {
try {
// Check if coin is valid
if (!minOrderSize.hasOwnProperty(coin)) {
console.log('The selected coin is not available for trading on Kraken.');
return;
}
// Assemble pair
const tradingPair = 'X' + coin + 'Z' + fiat;
const cryptoPair = coin + '/' + fiat;
// Retrieve btc/eur price
const tickResponse = await kraken.api('Ticker', {
pair: tradingPair
});
const quote = tickResponse['result'][tradingPair]['a'][0];
if (typeof quote === 'undefined') {
console.log('Unable to retrieve price for selected pair ' + cryptoPair);
return;
}
const cryptoOrder = (amount / quote).toFixed(6);
const roundedOrderAmount = (cryptoOrder * quote).toFixed(3);
// Check whether order amount is sufficient
if (cryptoOrder < minOrderSize[coin]) {
console.log('Increase the investment amount. The min order size for ' + coin + ' is ' + minOrderSize[coin] + ' :(');
return;
}
// Append pending order to log
const logMessage = util.format('[%s] Buying %f %s (= %f %s at price %f %s)\n', new Date().toISOString(), cryptoOrder, coin, roundedOrderAmount, fiat, quote, cryptoPair);
fs.appendFile('order.log', logMessage, err => {
if (err) {
console.log('-- An error has occured: ' + err);
return;
}
});
// Execute order
const tradeResponse = await kraken.api('AddOrder', {
pair: tradingPair,
volume: cryptoOrder,
type: 'buy',
ordertype: 'market'
});
// Retrieve txId and append to order log
const txId = tradeResponse['result']['txid'];
if (typeof txId === 'undefined') {
console.log('Unable to retrieve the tx id for your order.');
return;
}
fs.appendFile('order.log', "Tx-Id: " + txId + "\n", err => {
if (err) {
console.log('-- An error has occured: ' + err);
return;
}
});
// Done
console.log(util.format('[%s] Trade completed successfully, see order.log for details :)', new Date().toISOString()));
} catch (e) {
console.log(e);
// Log to file in case of failure
fs.appendFile('order.log', util.format('[%s] Unable to execute order: %s\n', new Date().toISOString(), e), err => {
if (err) {
console.log('-- Runtime error: ' + err);
}
});
}
})();