Download pool/shard008/JavaScript/0050211_price-service.js from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 4.58 kB
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard008/JavaScript/0050211_price-service.js
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/pool/shard008/JavaScript/0050211_price-service.js
-
curl -L -o 0050211_price-service.js https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard008/JavaScript/0050211_price-service.js
4.58 kB
| import _ from "lodash"; | |
| import * as http from "./api"; | |
| const _5_YEAR = 86400 * 366 * 5; | |
| function _getPriceSymbol(symbol, from, to) { | |
| const url = "https://dchart-api.vndirect.com.vn/dchart/history"; | |
| const params = { | |
| symbol, | |
| resolution: "D", | |
| from, | |
| to | |
| } | |
| return http.get(url, params).then(data => { | |
| data.symbol = symbol; | |
| return data; | |
| }); | |
| } | |
| const priceCached = {}; | |
| function getPriceSymbol(symbol) { | |
| symbol = symbol.toUpperCase(); | |
| if (priceCached[symbol]) { | |
| return priceCached[symbol]; | |
| } | |
| const to = Math.round((new Date()).getTime() / 1000); | |
| const from = to - _5_YEAR; | |
| const middle = Math.round((from + to) / 2); | |
| priceCached[symbol] = Promise.all([ | |
| _getPriceSymbol(symbol, from, middle), | |
| _getPriceSymbol(symbol, middle, to) | |
| ]).then(res => { | |
| const check = {}; | |
| _.forEach(res[0].t, time => check[time] = 1); | |
| _.forEach(res[1].t, (time, index) => { | |
| if(check[time]) { | |
| res[1].t.splice(index, 1); | |
| res[1].o.splice(index, 1); | |
| res[1].h.splice(index, 1); | |
| res[1].l.splice(index, 1); | |
| res[1].c.splice(index, 1); | |
| } | |
| }); | |
| return { | |
| t: res[0].t.concat(res[1].t), | |
| o: res[0].o.concat(res[1].o), | |
| h: res[0].h.concat(res[1].h), | |
| l: res[0].l.concat(res[1].l), | |
| c: res[0].c.concat(res[1].c), | |
| symbol: res[0].symbol | |
| }; | |
| }).catch(err => { | |
| delete priceCached[symbol]; | |
| return Promise.reject(err); | |
| }); | |
| return priceCached[symbol]; | |
| } | |
| function getAvgProfitNav(dataNav) { | |
| const now = Math.round((new Date()).getTime() / 1000); | |
| const _1_YEAR = 86400 * 365; | |
| const listNav = []; | |
| const match = [0, 1, 2, 3, 4, 5].map(i => now - i * _1_YEAR); | |
| let total = 0; | |
| let index = 1; | |
| let i = dataNav.length - 1; | |
| listNav.push(dataNav[i].nav); | |
| for(i = dataNav.length - 1; i >= 0; i--) { | |
| if(dataNav[i].time <= match[index]) { | |
| index++; | |
| listNav.push(dataNav[i].nav); | |
| } | |
| } | |
| for(i = 0; i < listNav.length - 1; i++) { | |
| total += (listNav[i] - listNav[i + 1]) / listNav[i + 1]; | |
| } | |
| return listNav.length > 1? total / (listNav.length - 1) : 0; | |
| } | |
| function beautifyData(res) { | |
| let targetTime = res[0].t; | |
| _.forEach(res, data => { | |
| if(data.t.length > targetTime.length) { | |
| targetTime = data.t; | |
| } | |
| }); | |
| _.forEach(res, dataSymbol => { | |
| const dataTime = dataSymbol.t; | |
| if(dataSymbol.t.length < targetTime.length) { | |
| _.forEach(targetTime, (time, index) => { | |
| if(index >= dataSymbol.t.length || dataSymbol.t[index] > targetTime[index]) { | |
| dataSymbol.t.splice(index, 0, dataSymbol.t[index - 1] || dataSymbol.t[index]); | |
| dataSymbol.o.splice(index, 0, dataSymbol.o[index - 1] || dataSymbol.o[index]); | |
| dataSymbol.h.splice(index, 0, dataSymbol.h[index - 1] || dataSymbol.h[index]); | |
| dataSymbol.l.splice(index, 0, dataSymbol.l[index - 1] || dataSymbol.l[index]); | |
| dataSymbol.c.splice(index, 0, dataSymbol.c[index - 1] || dataSymbol.c[index]); | |
| } | |
| }) | |
| } | |
| }); | |
| return res; | |
| } | |
| export const getNavWatchlist = function (watchlists) { | |
| return new Promise(function(resolve, reject) { | |
| Promise.all( | |
| _.map(watchlists, (value, symbol) => getPriceSymbol(symbol)) | |
| ).then(res => { | |
| if(_.isEmpty(res)) { | |
| return res; | |
| } | |
| res = beautifyData(res); | |
| const kl = {}; | |
| const rootPrice = {}; | |
| _.forEach(res, (data) => { | |
| rootPrice[data.symbol] = data.c[0]; | |
| }); | |
| _.forEach(watchlists, (value, symbol) => { | |
| kl[symbol] = value * 1000 / rootPrice[symbol]; | |
| }) | |
| const numSymbol = res.length; | |
| const result = _.map(res[0].t, (time, index) => { | |
| const nav = _.sum( | |
| _.map(res, data => data.c[index] * kl[data.symbol]) | |
| ); | |
| const _time = new Date(time * 1000); | |
| const date = `${_time.getMonth() + 1}/${_time.getFullYear()}`; | |
| return { | |
| date, | |
| time, | |
| nav: Math.round(nav / 1000) | |
| } | |
| }); | |
| const avgProfitNav = getAvgProfitNav(result); | |
| resolve({data: result, avgProfitNav}); | |
| }) | |
| }); | |
| }; | |
| const symbolInfoCached = {}; | |
| export const getSymbolInfo = (symbol) => { | |
| symbol = symbol.toUpperCase(); | |
| if(symbolInfoCached[symbol]) { | |
| return symbolInfoCached[symbol]; | |
| } | |
| const url = "https://finfoapi-hcm.vndirect.com.vn/stocks"; | |
| const params = {symbol}; | |
| symbolInfoCached[symbol] = http.get(url, params).then(data => data.data[0]).catch(err => { | |
| delete symbolInfoCached[symbol]; | |
| return Promise.reject(err); | |
| }); | |
| return symbolInfoCached[symbol]; | |
| }; | |