diff options
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/index.js b/index.js new file mode 100644 index 0000000..6152685 --- /dev/null +++ b/index.js | |||
@@ -0,0 +1,68 @@ | |||
1 | const r = require('ramda') | ||
2 | const h = require('highland') | ||
3 | const fs = require('fs') | ||
4 | const path = require('path') | ||
5 | const zlib = require('zlib') | ||
6 | const BigNumber = require('bignumber.js') | ||
7 | |||
8 | BigNumber.config({ | ||
9 | DECIMAL_PLACES: 8 | ||
10 | }) | ||
11 | |||
12 | if (process.argv.length < 3) { | ||
13 | console.error(`${path.basename(process.argv[1])} | ||
14 | |||
15 | bitcoincharts historic price conversion | ||
16 | |||
17 | Input: gzipped csv of (unixtime, price, tradeamount) | ||
18 | |||
19 | Outputs beancount price directives for the weighted average price | ||
20 | of all trades executed on the first of each month in the provided file | ||
21 | |||
22 | Download files from http://api.bitcoincharts.com/v1/csv/ | ||
23 | |||
24 | usage: ${path.basename(process.argv[1])} FILENAME [CURRENCY] [COMMODITY]`) | ||
25 | process.exit(1) | ||
26 | } | ||
27 | |||
28 | const [ | ||
29 | ,, | ||
30 | filename, | ||
31 | currency = 'EUR', | ||
32 | commodity = 'BTC' | ||
33 | ] = process.argv | ||
34 | |||
35 | h( | ||
36 | fs.createReadStream(filename) | ||
37 | .pipe(zlib.createGunzip()) | ||
38 | ) | ||
39 | .split() | ||
40 | .filter(line => line.length > 1) | ||
41 | .map(line => line.split(',', 3)) | ||
42 | .map(([time, price, amount]) => [ | ||
43 | new Date(parseInt(time, 10) * 1000), | ||
44 | new BigNumber(price), | ||
45 | new BigNumber(amount) | ||
46 | ]) | ||
47 | .filter(([date]) => date.getDate() === 1) | ||
48 | .group(([date]) => date.toDateString()) | ||
49 | .map(group => Object.values(group)) | ||
50 | .sequence() | ||
51 | .map(group => group | ||
52 | .map( | ||
53 | ([date, price, amount]) => [date, price.times(amount), amount] | ||
54 | ) | ||
55 | .reduce( | ||
56 | ([date, sumPrice, sumAmount], [,weightPrice, amount]) => [ | ||
57 | date, | ||
58 | sumPrice.plus(weightPrice), | ||
59 | sumAmount.plus(amount) | ||
60 | ] | ||
61 | )) | ||
62 | .map(([date, sumprice, sumamount]) => [date, sumprice.dividedBy(sumamount)]) | ||
63 | .map(([date, avg]) => | ||
64 | `${date.toISOString().substr(0, 10)} price ${commodity} ${avg.toFixed(2)} ${currency}` | ||
65 | ) | ||
66 | .intersperse("\n") | ||
67 | .pipe(process.stdout) | ||
68 | |||