This repository was archived by the owner on Feb 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (37 loc) · 2.2 KB
/
index.js
File metadata and controls
55 lines (37 loc) · 2.2 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
'use strict'
require('dotenv').config()
const indicators = require('./helpers/indicators')
const functions = require('./utils/functions')
const geckoApi = require('coingecko-api')
const logger = require('./utils/logger')
const boot = async () => {
const gecko = new geckoApi(), vs_currency = process.env.VS_CURRENCY.toLowerCase()
logger.info(`vs_currency setted to '${vs_currency}'`)
let indexify = []
await gecko.coins.markets({ order: geckoApi.ORDER.MARKET_CAP_DESC, vs_currency }).then(async ({ data }) => {
const markets = data.filter((d) => !process.env.EXCLUDED_MARKETS.includes(d.symbol)).map((m) => ({ id: m.id, name: m.name})).slice(0, process.env.TOTAL_MARKETS)
logger.info(`Markets sucessfully fetched. Using a total of ${markets.length} markets`)
await functions.asyncForEach(markets, async ({ id, name }) => {
const period = process.env.WMA_PERIOD
logger.info(`Adjusting market cap of ${name} using a ${period}-day weighted moving average`)
await gecko.coins.fetchMarketChart(id, { vs_currency, days: period, interval: 'daily' }).then(async ({ data }) => {
const { market_caps } = data, mcap = market_caps.map((m) => m[1]), wma = await indicators.wma(mcap, period)
indexify.push({ name, mcap: { val: mcap[mcap.length - 1], wma: wma[wma.length - 1] } })
})
})
logger.info(`All market caps adjusted... Indexing markets now`)
})
const ourMarketCap = indexify.reduce((p, c) => p + Math.sqrt(c.mcap.wma), 0)
let allocations = []
for (let idx = 0; idx < indexify.length; idx++) {
const { name:coin, mcap } = indexify[idx], adj = Math.sqrt(mcap.wma), ratio = (adj / ourMarketCap) * 100
allocations.push({ coin, mcap, ratio })
}
const file = `./indexed/${Date.now()}_${process.env.TOTAL_MARKETS}M_${process.env.WMA_PERIOD}DWMA.json`
functions.writeResults(file, allocations, (err) => {
const message = err ? `An error happened. Message: ${err.message}` : `Markets sucessfully indexed. Check ${file} to see the results`
logger.info(message)
process.exit()
})
}
boot().catch(console.error)