· 4 years ago · May 28, 2021, 02:54 PM
1var request = require("request");
2let base = 'USD'
3var cryptoarr = ['DOGE', 'ETH', 'BCH'];
4var amounts = ['45', '0.01157735', '0.06606691']
5let verbose = false //only change this if you don't want to see the default error output generated by the program (TypeErrors)
6let price = require('crypto-price')
7var prices = []
8var finalpricetotal = []
9//alphavantage api key: L2S0FJKBI8EBZ376
10var getDate = new Date();
11var datestring = getDate.getFullYear() + '-' + '0' + parseInt(getDate.getMonth()+1) + '-' + parseInt(getDate.getDate()-1)
12var datestring_lastmonth = getDate.getFullYear() + '-' + '0' + parseInt(getDate.getMonth()) + '-' + parseInt(getDate.getDate())
13var datestring_lastyear = parseInt(getDate.getFullYear()-1) + '-' + '0' + parseInt(getDate.getMonth()+1) + '-' + parseInt(getDate.getDate())
14
15function highlightedCrypto(highlight) {
16 return new Promise(function(resolve, reject) {
17 price.getCryptoPrice(base, highlight).then(obj => {
18 resolve(obj.price)
19 }).catch(err => {
20 if(verbose === false) {
21 if(err = "TypeError: Cannot read property 'price' of undefined") {
22 console.log("Crypto/Fiat invalid: not found.")
23 } else {
24 console.log(err)
25 }
26 } else if(verbose === true) {
27 console.log(err)
28 } else {
29 console.log('Please specify true or false while defining the verbose variable.')
30 }
31 })
32 })
33}
34
35async function listall() {
36 console.log('Portfolio Prices:')
37 for (arrlength = 0; arrlength < cryptoarr.length; arrlength++) {
38 const cryptolist = await highlightedCrypto(cryptoarr[arrlength])
39 prices.push(cryptolist)
40 }
41 for (portfolioarrlength = 0; portfolioarrlength < prices.length; portfolioarrlength++) {
42 var cryptoarr_length = cryptoarr[portfolioarrlength]
43 var prices_length = prices[portfolioarrlength]
44 console.log(prices_length)
45 var crypto_url = `https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_DAILY&symbol=${cryptoarr_length}&market=${base}&apikey=L2S0FJKBI8EBZ376`
46 request(crypto_url, function (error, response, body, portfolioarrlength, cryptoarr_length, prices_length) { if (!error && response.statusCode == 200) {
47 const money_round = (number, places) => {
48 let rounder = '1';
49
50 for (let i = 0; i < places; i++) {
51 rounder += '0';
52 }
53
54 return (Math.round(number * rounder) / rounder).toFixed(places);
55 };
56 let crypto_price = JSON.parse(body)
57 yesterday_crypto_price = crypto_price["Time Series (Digital Currency Daily)"][datestring]["4b. close (USD)"];
58 last_month_crypto_price = crypto_price["Time Series (Digital Currency Daily)"][datestring_lastmonth]['4b. close (USD)'];
59 last_year_crypto_price = crypto_price["Time Series (Digital Currency Daily)"][datestring_lastyear]['4b. close (USD)'];
60 var crypto_price_change = 100*(1-(last_month_crypto_price/yesterday_crypto_price))
61 var rounded_crypto_price_change = Math.round(crypto_price_change)
62 var finalpriceafterrounding = money_round(prices_length*amounts[portfolioarrlength], 2)
63 console.log('My balance of ' + cryptoarr_length + ': $' + finalpriceafterrounding + rounded_crypto_price_change + '%')
64 finalpricetotal.push(finalpriceafterrounding)
65 function getSum(total, num) {
66 return total + num;
67 }
68 var finalpricetotal_toInt = finalpricetotal.map(i=>Number(i));
69 var finalpricetotal_added = finalpricetotal_toInt.reduce(getSum, 0)
70 console.log('Your total portfolio balance is: $' + finalpricetotal_added)
71 }
72 })
73 }
74}
75
76listall()
77
78//portfolioPrices()
79
80// https://zellwk.com/blog/async-await-in-loops/
81// https://lavrton.com/javascript-loops-how-to-handle-async-await-6252dd3c795/