add stdev; toggle sources
This commit is contained in:
@@ -53,9 +53,28 @@ const get = async (url) => {
|
|||||||
return await response.json();
|
return await response.json();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const filterPrices = () => {
|
||||||
|
return prices.value.filter(val => {
|
||||||
|
return typeof val === 'number';
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const calcAverage = () => {
|
const calcAverage = () => {
|
||||||
const arr = prices.value;
|
const prices = filterPrices();
|
||||||
average.value = arr.reduce((sum, val) => sum + val, 0) / arr.length;
|
average.value = prices.reduce((sum, val) => sum + val, 0) / prices.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
const calcStdev = () => {
|
||||||
|
const av = average.value;
|
||||||
|
const prices = filterPrices();
|
||||||
|
stdev.value = (filterPrices().reduce((sum, price) => {
|
||||||
|
return sum + (av - price) ** 2;
|
||||||
|
}, 0) / prices.length) ** 0.5;
|
||||||
|
};
|
||||||
|
|
||||||
|
const recalc = () => {
|
||||||
|
calcAverage();
|
||||||
|
calcStdev();
|
||||||
};
|
};
|
||||||
|
|
||||||
const format = price => {
|
const format = price => {
|
||||||
@@ -64,6 +83,8 @@ const format = price => {
|
|||||||
|
|
||||||
const average = ref();
|
const average = ref();
|
||||||
const prices = ref([]);
|
const prices = ref([]);
|
||||||
|
const stdev = ref();
|
||||||
|
const showSources = ref(true);
|
||||||
|
|
||||||
(() => {
|
(() => {
|
||||||
let idx = -1;
|
let idx = -1;
|
||||||
@@ -72,8 +93,9 @@ const prices = ref([]);
|
|||||||
idx = (idx + 1) % dataSources.length;
|
idx = (idx + 1) % dataSources.length;
|
||||||
try {
|
try {
|
||||||
const source = dataSources[idx];
|
const source = dataSources[idx];
|
||||||
|
prices.value[idx] = null;
|
||||||
prices.value[idx] = source.pick(await get(source.url));
|
prices.value[idx] = source.pick(await get(source.url));
|
||||||
calcAverage();
|
recalc();
|
||||||
} catch (e) {};
|
} catch (e) {};
|
||||||
};
|
};
|
||||||
loop();
|
loop();
|
||||||
@@ -88,12 +110,16 @@ const prices = ref([]);
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<p>1 BTC = {{ format(average) }} USD</p>
|
<p>1 BTC = {{ format(average) }} USD</p>
|
||||||
|
<button @click="showSources = !showSources">Toggle Sources</button>
|
||||||
|
<div v-show="showSources">
|
||||||
<p>Data Sources:</p>
|
<p>Data Sources:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li v-for="(val, i) in dataSources">
|
<li v-for="(val, i) in dataSources">
|
||||||
{{ val.name }}: {{ format(prices[i]) }}
|
{{ val.name }}: {{ format(prices[i]) }}
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
<p>Standard Deviation: {{ stdev }}</p>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
Reference in New Issue
Block a user