-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·104 lines (92 loc) · 3.15 KB
/
index.js
File metadata and controls
executable file
·104 lines (92 loc) · 3.15 KB
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
#!/usr/bin/env node
import args from "./src/arguments.js"
import asciichart from "asciichart"
import { CryptoCompareAPI } from "./src/CryptoCompareAPI.js"
import { print, normalize, time, interpolate } from "./src/utils.js"
import { printTopList } from "./src/toplist.js"
import {
printTechIndicatorChart,
getTechIndicator,
getTechIndicatorColors,
} from "./src/technical-indicator.js"
const printCoins = async () => {
const coins = await CryptoCompareAPI.fetchCoinList()
coins
.map(coin => coin.trim())
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
.forEach(coin => print(coin))
}
const getMinRange = (max, min) => {
if (max - min > args.minRange) return []
const dist = max - min
const range = args.minRange / 2 - dist / 2
return [max + range, min - range]
}
const formatDate = (date) => {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
let hours = date.getHours()
const minutes = String(date.getMinutes()).padStart(2, '0')
const ampm = hours >= 12 ? 'pm' : 'am'
hours = hours % 12 || 12
hours = String(hours).padStart(2, '0')
return `${year}-${month}-${day} ${hours}:${minutes} ${ampm}`
}
const main = async () => {
const [timePast, timeName, timeApi] = time()
const pastDate = new Date()
if (timeName === 'minutes') {
pastDate.setMinutes(pastDate.getMinutes() - timePast)
} else if (timeName === 'hours') {
pastDate.setHours(pastDate.getHours() - timePast)
} else if (timeName === 'days') {
pastDate.setDate(pastDate.getDate() - timePast)
}
const past = formatDate(pastDate)
const fullHistroy = await CryptoCompareAPI.fetchCoinHistory(
timeApi,
args.coin,
args.currency,
timePast
)
const history = interpolate(fullHistroy)
const value = await CryptoCompareAPI.fetchCoinPrice(args.coin, args.currency)
const baseLegend = `\t ${args.coin} last ${timePast} ${timeName}`
const now = `. Now: ${value[args.currency]} ${args.currency}`
const legend = baseLegend + ` since ${past}` + now
const smallLegend = baseLegend + now
const fixed = normalize(Math.max(...history))
const fixedHist = history.map((x) => Number(x.toFixed(fixed)))
const padding = " ".repeat(2 + Math.max(...fixedHist).toString().length)
const [maxH, minH] = getMinRange(Math.max(...fixedHist), Math.min(...fixedHist))
const chart = getTechIndicator(fullHistroy).concat([fixedHist])
try {
print(
asciichart.plot(chart, {
height: args.maxHeight,
max: args.minRange ? maxH : args.max,
min: args.minRange ? minH : args.min,
padding: padding,
colors: getTechIndicatorColors(),
format: (x) => (padding + x.toFixed(fixed)).slice(-padding.length),
})
)
} catch (e) {
console.log(
"Couldn't plot chart. Please try different width or height settings."
)
process.exit(1)
}
if (args.maxWidth > 40 && !args.disableLegend) {
print(args.maxWidth < 65 ? smallLegend : legend)
}
printTechIndicatorChart(fullHistroy, padding)
}
if (args.showCoinList) {
printCoins()
} else if (args.topList) {
printTopList()
} else {
main()
}