[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

KH (Feb 11, 2018, 11:16 PM +0100) 1c5145b2 a57ebbb3

+185 -73
+3
.env
··· 7 7 PORT_SECURE=443 8 8 PORT_DB=27017 9 9 PORT_MONGO_EXPRESS=8081 10 + 11 + CRYPTO_COMPARE_HOUR_RATE_LIMIT_HISTORY=8000 12 + FETCH_CANDLESTICKS=false
+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 + 1 9 # Cryp 2 10 Cryptocurrency portfolio tracker 3 11
+150
web/node/candlestick.js
··· 1 + const Candlestick = require("./mongoose-models").Candlestick; 2 + 3 + global.fetch = require('node-fetch'); 4 + const cc = require('cryptocompare'); 5 + 6 + function ccToDate(cryptoCompareTimestamp) { 7 + timestamp = Number(cryptoCompareTimestamp+"000"); 8 + return new Date(timestamp); 9 + } 10 + 11 + const hourRateLimit = process.env.CRYPTO_COMPARE_HOUR_RATE_LIMIT_HISTORY; 12 + const msToWait = Math.ceil(1/(hourRateLimit/60/60/1000)); 13 + const endDate = new Date("2011-01-01 00:00:00Z"); 14 + 15 + const crypto = "BTC"; 16 + const fiat = "USD"; 17 + 18 + function checkCandlesticks(data) { 19 + // for (let i = 0; i < data.length; i += 200) { 20 + for (let i = 0; i < data.length; i++) { 21 + const currentDate = ccToDate(data[i].time); 22 + console.log(currentDate); 23 + Candlestick.findOne({ 24 + fromSymbol: crypto, 25 + toSymbol: fiat, 26 + date: currentDate, 27 + timeframe: "hour" 28 + }, (err, candlestick) => { 29 + if (err) { 30 + console.log("----------------------------------err"); 31 + console.error(err); 32 + } 33 + if (candlestick == null) { 34 + new Candlestick({ 35 + fromSymbol: crypto, 36 + toSymbol: fiat, 37 + date: currentDate, 38 + timeframe: "hour", 39 + open: data[i].open, 40 + high: data[i].high, 41 + low: data[i].low, 42 + close: data[i].close, 43 + }).save().then((newCandlestick) => { 44 + console.log(`Created candlestick for time ${newCandlestick.date}`); 45 + }).catch(console.error); 46 + } 47 + 48 + }); 49 + } 50 + } 51 + 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, { 57 + limit: 2000, 58 + timestamp: currentDate 59 + }).then((data) => { 60 + 61 + checkCandlesticks(data); 62 + currentDate = ccToDate(data[0].time); 63 + currentDate.setHours(currentDate.getHours() - 1); 64 + console.log(",,,,,", currentDate); 65 + i++; 66 + setTimeout(() => { 67 + if (currentDate > endDate) { 68 + fetchDates(); 69 + } else { 70 + "all dates fetched :)"; 71 + } 72 + }, msToWait); 73 + 74 + }).catch(console.error); 75 + } 76 + 77 + 78 + 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)
-22
web/node/ccxt.js
··· 1 - const ccxt = require("ccxt"); 2 - let exchange1 = new ccxt.bittrex(); 3 - let exchange2 = new ccxt.binance(); 4 - let exchange3 = new ccxt.kucoin(); 5 - let exchange4 = new ccxt.cryptopia(); 6 - let sleep = (ms) => new Promise (resolve => setTimeout (resolve, ms)); 7 - (async () => { 8 - let markets = await exchange1.fetchMarkets(); 9 - for (let i = 0; i < markets.length; i++) { 10 - const market = markets[i]; 11 - // console.log(market.base); 12 - const symbol = "SC/BTC"; 13 - if (market.symbol == symbol) { 14 - await sleep(exchange1.rateLimit); // milliseconds 15 - // console.log (await exchange1.fetchOHLCV (symbol, '1d', new Date("01-12-2018").valueOf(), 1)) // one minute 16 - const ohlcv = await exchange1.fetchOHLCV (symbol, '1d', new Date("05-01-2017"), 50); 17 - console.log(ohlcv.length); 18 - console.log(ohlcv[0]); 19 - console.log(ohlcv[ohlcv.length-1]); 20 - } 21 - } 22 - })();
-20
web/node/https-redirect-server.js
··· 1 - const http = require("http"); 2 - const redirectHTTPS = require("redirect-https"); 3 - 4 - module.exports = { 5 - start: (insecurePort, securePort) => { 6 - 7 - const server = http.createServer(); 8 - 9 - server.on("request", redirectHTTPS({ 10 - port: securePort, 11 - body: "<!-- Please use HTTPS -->", 12 - trustProxy: true // default is false 13 - })); 14 - 15 - server.listen(insecurePort, function () { 16 - console.log("http redirect server started"); 17 - }); 18 - 19 - } 20 - }
+15 -3
web/node/mongoose-models.js
··· 18 18 exchange: String, 19 19 group: String, 20 20 note: String, 21 - date: String 21 + date: String, 22 22 }, { typeKey: "$Type", _id: false }); 23 23 24 24 const userSchema = new Schema({ 25 25 displayName: String, 26 26 googleId: String, 27 27 profilePictureURL: String, 28 - transactions: [transactionSchema] 28 + transactions: [transactionSchema], 29 29 }, { typeKey: "$Type" }); 30 30 31 + const candlestickSchema = new Schema({ 32 + fromSymbol: String, 33 + toSymbol: String, 34 + date: Date, 35 + timeframe: String, 36 + open: String, 37 + high: String, 38 + low: String, 39 + close: String, 40 + }); 41 + 31 42 module.exports = { 32 - User: mongoose.model("user", userSchema) 43 + User: mongoose.model("User", userSchema), 44 + Candlestick: mongoose.model("Candlestick", candlestickSchema), 33 45 };
-22
web/node/temp-server.js
··· 1 - const express = require("express"); 2 - const http = require("http"); 3 - let server; 4 - 5 - module.exports = { 6 - start: (insecurePort) => { 7 - 8 - const app = express(); 9 - app.use("/", express.static(dir("static"), { redirect: false })); 10 - server = http.createServer(app).listen(insecurePort, () => { 11 - console.log("temp http server started"); 12 - }); 13 - 14 - }, 15 - close: () => { 16 - 17 - server.close(() => { 18 - console.log("temp http server closed"); 19 - }); 20 - 21 - } 22 - }
+2 -1
web/package.json
··· 24 24 "babel-register": "6.26.x", 25 25 "babel-preset-env": "1.6.x", 26 26 27 - "ccxt": "1.10.x", 27 + "node-fetch": "2.0.x", 28 + "cryptocompare": "0.4.x", 28 29 29 30 "express": "4.16.x", 30 31 "redirect-https": "1.1.x",
+1
web/sass/cards.sass
··· 135 135 .chart-container 136 136 margin-top: calc(15px + 12px*2) 137 137 width: 0px 138 + min-height: 0px 138 139 flex-grow: 1 139 140 min-width: 200px 140 141 .crypto-info-table
+5 -5
web/server.js
··· 8 8 const PORT_INSECURE = process.env.PORT_INSECURE; 9 9 const PORT_SECURE = process.env.PORT_SECURE; 10 10 11 - // ccxt api 12 - require("./node/ccxt"); 13 - 14 11 // http 15 12 (() => { 16 - 13 + 17 14 const http = require("http"); 18 15 const server = http.createServer(); 19 16 ··· 24 21 25 22 server.listen(PORT_INSECURE, () => { 26 23 console.log(); 27 - }) 24 + }); 28 25 29 26 // const httpApp = express(); 30 27 // ··· 90 87 })); 91 88 app.use(passport.initialize()); 92 89 app.use(passport.session()); 90 + 91 + // candlestick 92 + require("./node/candlestick"); 93 93 94 94 // routes 95 95 require("./node/routes")(app);
+1
web/static/global.css
··· 294 294 html .page-card .crypto-info .chart-container { 295 295 margin-top: calc(15px + 12px*2); 296 296 width: 0px; 297 + min-height: 0px; 297 298 flex-grow: 1; 298 299 min-width: 200px; } 299 300 html .page-card .crypto-info .crypto-info-table {