File size: 4,579 Bytes
7f88cfb | 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 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];
};
|