[READ-ONLY] Mirror of https://github.com/probablykasper/cryp. Cryptocurrency portfolio tracker (unfinished)
0

Configure Feed

Select the types of activity you want to include in your feed.

Candlestick loading finished

KH (Feb 13, 2018, 10:54 PM +0100) 7020dac4 1c5145b2

+128 -117
-8
README.md
··· 1 - Plan: 2 - Import historical prices into database from CryptoCompare. 3 - https://min-api.cryptocompare.com/data/histohour?fsym=BTC&tsym=USD&limit=2000&toTs=1290161600 4 - CryptoCompare lets you go infinitely back. How do we detect the start of the chart? (dont put in 2004 BTC prices) 5 - Mongoose model can be 6 - - an object containing the symbol and an array of OHLC objects. Search for time. 7 - - or a single OHLC object. Search for symbol and time. 8 - 9 1 # Cryp 10 2 Cryptocurrency portfolio tracker 11 3
+47 -96
web/node/candlestick.js
··· 10 10 11 11 const hourRateLimit = process.env.CRYPTO_COMPARE_HOUR_RATE_LIMIT_HISTORY; 12 12 const msToWait = Math.ceil(1/(hourRateLimit/60/60/1000)); 13 - const endDate = new Date("2011-01-01 00:00:00Z"); 14 13 15 - const crypto = "BTC"; 16 - const fiat = "USD"; 14 + const btcToDate = new Date("2011-01-01 00:00:00Z") 15 + const pairs = [ 16 + { 17 + fromCurrency: "BTC", 18 + toCurrency: "USD", 19 + toDate: btcToDate, 20 + }, 21 + { 22 + fromCurrency: "USDT", 23 + toCurrency: "BTC", 24 + toDate: new Date("2017-05-01 00:00:00Z"), 25 + } 26 + ]; 17 27 18 - function checkCandlesticks(data) { 19 - // for (let i = 0; i < data.length; i += 200) { 28 + function checkCandlesticks(data, fromCurrency, toCurrency) { 20 29 for (let i = 0; i < data.length; i++) { 21 30 const currentDate = ccToDate(data[i].time); 22 - console.log(currentDate); 31 + // console.log(currentDate); 23 32 Candlestick.findOne({ 24 - fromSymbol: crypto, 25 - toSymbol: fiat, 33 + fromSymbol: fromCurrency, 34 + toSymbol: toCurrency, 26 35 date: currentDate, 27 36 timeframe: "hour" 28 37 }, (err, candlestick) => { ··· 32 41 } 33 42 if (candlestick == null) { 34 43 new Candlestick({ 35 - fromSymbol: crypto, 36 - toSymbol: fiat, 44 + fromSymbol: fromCurrency, 45 + toSymbol: toCurrency, 37 46 date: currentDate, 38 47 timeframe: "hour", 39 48 open: data[i].open, ··· 41 50 low: data[i].low, 42 51 close: data[i].close, 43 52 }).save().then((newCandlestick) => { 44 - console.log(`Created candlestick for time ${newCandlestick.date}`); 53 + console.log(`- - created candlestick for time ${newCandlestick.date}`); 45 54 }).catch(console.error); 46 55 } 47 56 ··· 49 58 } 50 59 } 51 60 52 - let currentDate = new Date(); 53 - let i = 0; 54 - if (process.env.FETCH_CANDLESTICKS == "true") fetchDates(); 55 - function fetchDates() { 56 - cc.histoHour(crypto, fiat, { 61 + function fetchDates(fromCurrency, toCurrency, toDate, callback, currentDate = new Date()) { 62 + console.log("- - fetch dates"); 63 + console.log(`- - toDate : ${toDate}`); 64 + console.log(`- - currentDate : ${currentDate}`); 65 + cc.histoHour(fromCurrency, toCurrency, { 57 66 limit: 2000, 58 67 timestamp: currentDate 59 68 }).then((data) => { 60 69 61 - checkCandlesticks(data); 70 + checkCandlesticks(data, fromCurrency, toCurrency); 62 71 currentDate = ccToDate(data[0].time); 63 72 currentDate.setHours(currentDate.getHours() - 1); 64 - console.log(",,,,,", currentDate); 65 - i++; 73 + console.log(`- - new currentDate: ${currentDate}`); 66 74 setTimeout(() => { 67 - if (currentDate > endDate) { 68 - fetchDates(); 69 - } else { 70 - "all dates fetched :)"; 75 + if (currentDate > toDate) { 76 + fetchDates(fromCurrency, toCurrency, toDate, callback, currentDate); 77 + } else { 78 + console.log(`- - pair ${fromCurrency}:${toCurrency} fetched`); 79 + callback(); 71 80 } 72 81 }, msToWait); 73 82 74 83 }).catch(console.error); 75 84 } 76 85 77 - 86 + function fetchCurrencies(currencyIndex = 0) { 87 + const currentPair = pairs[currencyIndex]; 88 + const fromCurrency = currentPair.fromCurrency; 89 + const toCurrency = currentPair.toCurrency; 90 + const toDate = currentPair.toDate; 91 + console.log(`- - - - - - - - - - fetching candlesticks`); 92 + console.log(`- - pair ${fromCurrency}:${toCurrency}`); 93 + console.log(`- - pair fetched to date ${toDate}`); 94 + fetchDates(fromCurrency, toCurrency, toDate, () => { 95 + currencyIndex++; 96 + if (currencyIndex < pairs.length) fetchCurrencies(currencyIndex); 97 + else console.log(`- - - - - - - - - - done fetching candlesticks`); 98 + }); 99 + } 78 100 79 - // const crypto = "BTC"; 80 - // // let fiats = ["USD", "NOK"]; 81 - // let fiats = ["USD"]; 82 - // let fiatIndex = 0; 83 - // const endDate = new Date("2011-01-01 00:00:00Z"); 84 - // function loopDates(currentDate, callback) { 85 - // cc.histoHour(crypto, fiats[fiatIndex], { 86 - // limit: 2000, 87 - // timestamp: currentDate, 88 - // }).then((data) => { 89 - // const newDate = ccToDate(data[data.length-1].time); 90 - // for (let i = 0; i < data.length; i++) { 91 - // date = ccToDate(data[i].time); 92 - // console.log(date); 93 - // } 94 - // if (endDate < new Date()) { 95 - // setTimeout(() => { 96 - // loopDates(newDate, () => { 97 - // callback(); 98 - // console.log("========================================1"); 99 - // }); 100 - // }, msToWait); 101 - // console.log("========================================2"); 102 - // } else { 103 - // callback(); 104 - // } 105 - // // loopDates(); 106 - // // console.log(data[0]); 107 - // // console.log(data[data.length-1]); 108 - // }).catch((err) => { 109 - // console.log("====================================99"); 110 - // console.error(err); 111 - // }); 112 - // } 113 - // function loopFiats() { 114 - // loopDates(new Date(), () => { 115 - // console.log("----------------------------------------------------3"); 116 - // if (fiatIndex < fiats.length) { 117 - // setTimeout(() => { 118 - // loopFiats(); 119 - // }, msToWait); 120 - // } 121 - // }); 122 - // fiatIndex++; 123 - // } 124 - // loopFiats(); 125 - 126 - 127 - 128 - 129 - 130 - 131 - // const crypto = "BTC"; 132 - // const fiat = "USD"; 133 - // const limit = "2000"; 134 - // const date = new Date("2011-01-01 00:00:00Z"); 135 - // function getDate(cryptoCompareTimestamp) { 136 - // timestamp = Number(cryptoCompareTimestamp+"000"); 137 - // const date = new Date(timestamp); 138 - // console.log(date); 139 - // } 140 - // 141 - // cc.histoHour(crypto, fiat, { 142 - // limit: limit, 143 - // timestamp: date, 144 - // }).then((data) => { 145 - // for (let i = 0; i < data.length; i++) { 146 - // getDate(data[i].time); 147 - // } 148 - // // console.log(data[0]); 149 - // // console.log(data[data.length-1]); 150 - // }).catch(console.error) 101 + if (process.env.FETCH_CANDLESTICKS == "true") fetchCurrencies();
+9
web/node/log-err.js
··· 1 + module.exports = (code, err) => { 2 + console.error( 3 + `----- =-=-=-=-=-=-=-=- ${code} -=-=-=-=-=-=-=-= -----`, 4 + "\n", 5 + new Error().stack, 6 + "\n", 7 + `----- ================ ${code} ================ -----`, 8 + ) 9 + }
+22 -11
web/node/routes.js
··· 6 6 console.log("----------"); 7 7 }; 8 8 const scope = {scope: ["profile"]}; 9 - const User = require("./mongoose-models").User; 9 + const updateTransactions = require("./update-transactions"); 10 10 11 11 function render(app, res, pugFile, variables, callback) { 12 12 app.render(pugFile, variables, (err, html) => { ··· 110 110 111 111 app.post("/update-transactions", (req, res) => { 112 112 if (typeof req.body == "object") { 113 - User.findOneAndUpdate({ 114 - _id: res.locals.userID 115 - }, { 116 - $set: { 117 - transactions: req.body 118 - } 119 - }, { 120 - upsert: true 121 - }, (err, updatedUser) => { 122 - console.log(err); 113 + updateTransactions(req.body, res.locals.userID, () => { 123 114 jsonRes(res); 124 115 }); 125 116 } 117 + 118 + // const calculateTransactions = require("./calculate-transactions"); 119 + // calculateTransactions(req.body, (transactions) => { 120 + // 121 + // if (typeof transactions == "object") { 122 + // User.findOneAndUpdate({ 123 + // _id: res.locals.userID 124 + // }, { 125 + // $set: { 126 + // transactions: transactions 127 + // } 128 + // }, { 129 + // upsert: true 130 + // }, (err, updatedUser) => { 131 + // if (err) console.log(err); 132 + // jsonRes(res); 133 + // }); 134 + // } 135 + // 136 + // }); 126 137 }); 127 138 128 139 }
+48
web/node/update-transactions.js
··· 1 + const User = require("./mongoose-models").User; 2 + const Candlestick = require("./mongoose-models").Candlestick; 3 + 4 + module.exports = (transactions, userID, callback) => { 5 + 6 + if (typeof transactions == "object") { 7 + 8 + loopTransactions(); 9 + function loopTransactions(i = 0) { 10 + 11 + const transaction = transactions[i]; 12 + // round date to nearest hour 13 + let date = new Date(transaction.date); 14 + const minutes = date.getMinutes(); 15 + if (minutes >= 30) date.setHours(date.getHours()+1); 16 + date.setMinutes(0); 17 + date.setSeconds(0); 18 + 19 + if (transaction.type == "Trade") { 20 + Candlestick.findOne({ 21 + fromSymbol: "BTC", 22 + toSymbol: "USD", 23 + date: date, 24 + timeframe: "hour", 25 + }, (err, candlestick) => { 26 + if (err) return logErr("01", "could not find candlestick"); 27 + i++; 28 + if (i < transactions.length) loopTransactions(i); 29 + }); 30 + } 31 + } 32 + 33 + User.findOneAndUpdate({ 34 + _id: userID 35 + }, { 36 + $set: { 37 + transactions: transactions 38 + } 39 + }, { 40 + upsert: true 41 + }, (err, updatedUser) => { 42 + if (err) console.log(err); 43 + callback(); 44 + }); 45 + 46 + } 47 + 48 + }
+2 -2
web/server.js
··· 4 4 global.dir = (dirPath) => { 5 5 return require("path").resolve(__dirname, dirPath); 6 6 } 7 + global.logErr = require("./node/log-err.js"); 7 8 8 9 const PORT_INSECURE = process.env.PORT_INSECURE; 9 10 const PORT_SECURE = process.env.PORT_SECURE; 10 - 11 11 // http 12 12 (() => { 13 - 13 + 14 14 const http = require("http"); 15 15 const server = http.createServer(); 16 16