forked from Stenotti/crypto-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoingecko-api.js
91 lines (81 loc) · 2.33 KB
/
coingecko-api.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
const CoinGecko = require("coingecko-api");
const rp = require("request-promise");
const CoinGeckoClient = new CoinGecko();
const fetchAllData = async (method, params = {}) => {
const allResults = [];
let finished = false;
const paginationData = {
page: 0,
per_page: 250,
};
try {
while (finished === false) {
const { data } = await method({ ...paginationData, ...params });
console.log(
`🚀 ~ calling method for page ${paginationData.page}. Result ${data} (Total ${allResults.length})`
);
allResults.push(...data);
if (data.length < paginationData.per_page) {
finished = true;
}
paginationData.page++;
}
} catch (err) {
console.log(`error: `, err);
}
return allResults;
};
function delay(t, val) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(val);
}, t);
});
}
exports.coinsNotToAthYet = async () => {
const ath_days_diff = 365;
const coins = await fetchAllData(CoinGeckoClient.coins.markets, {
vs_currency: "usd",
});
return coins
.reduce((acc, coin) => {
const { symbol, name, ath_change_percentage, ath_date } = coin;
console.log(`[coinsNotToAthYet] checking ${name} (${symbol})`);
const eur_ath_date = new Date(ath_date);
const date_to_check = new Date();
date_to_check.setDate(date_to_check.getDate() - ath_days_diff);
if (
ath_change_percentage < 0 &&
eur_ath_date.getTime() < date_to_check.getTime()
) {
return [...acc, coin];
}
return acc;
}, [])
.filter(
({ id }) => id && id.indexOf("x-long") < 0 && id.indexOf("x-short") < 0
);
};
exports.coinsNotListedYetOn = async (exchange = "binance") => {
const {
data: { tickers },
} = await CoinGeckoClient.exchanges.fetch(exchange);
const { data: allCoins } = await CoinGeckoClient.coins.markets({
vs_currency: "usd",
order: "market_cap_desc",
price_change_percentage: "7d",
per_page: "250",
});
const coins = allCoins.filter(
({ id }) => id && id.indexOf("x-long") < 0 && id.indexOf("x-short") < 0
);
return coins.reduce((acc, coin) => {
const exists = tickers.find(
(t) => t.coin_id === coin.id || t.target_coin_id === coin.id
);
if (!exists) {
return [...acc, coin];
}
return acc;
}, []);
};