frontend/src/lib/components/RetailVolume.svelte
MuslemRahimi 0764a3e3db ui fixes
2024-10-06 19:48:20 +02:00

353 lines
13 KiB
Svelte

<script lang ='ts'>
import { retailVolumeComponent,displayCompanyName, stockTicker, assetType, etfTicker, screenWidth, getCache, setCache} from '$lib/store';
import InfoModal from '$lib/components/InfoModal.svelte';
import { Chart } from 'svelte-echarts'
import { abbreviateNumber, formatDateRange, monthNames } from "$lib/utils";
import { init, use } from 'echarts/core'
import { LineChart, BarChart } from 'echarts/charts'
import { GridComponent, TooltipComponent } from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'
use([LineChart, BarChart, GridComponent, TooltipComponent, CanvasRenderer])
export let data;
let isLoaded = false;
let historyData = []
let rawData = [];
let optionsData;
let avgVolume;
let avgSentiment;
let monthlyVolume;
let lowestSentiment;
let highestSentiment;
function findMonthlyValue(data, lastDateStr) {
// Convert lastDateStr to Date object
const lastDate = new Date(lastDateStr);
// Set the first date to the beginning of the month of lastDate
const firstDate = new Date(lastDate.getFullYear(), lastDate.getMonth(), 1);
// Filter data to include only prices within the specified month period
const filteredData = data?.filter(item => {
const currentDate = new Date(item?.date);
return currentDate >= firstDate && currentDate <= lastDate;
});
// Extract prices from filtered data
monthlyVolume = filteredData?.reduce((accumulator, currentItem) => {
return accumulator + currentItem?.traded;
}, 0);
monthlyVolume = monthlyVolume > 100e3 ? abbreviateNumber(monthlyVolume,true) : '< $100K'
// Extract prices from filtered data
let sentiment = filteredData?.map(item => parseFloat(item?.sentiment));
// Find the lowest and highest prices
lowestSentiment = Math.min(...sentiment)?.toFixed(0);
highestSentiment = Math.max(...sentiment)?.toFixed(0);
}
function getPlotOptions() {
let dates = [];
let tradingList = [];
let sentimentList = [];
// Iterate over the data and extract required information
historyData?.forEach(item => {
dates?.push(item?.date);
tradingList?.push(item?.traded);
sentimentList?.push(item?.sentiment)
});
findMonthlyValue(historyData, rawData?.lastDate)
// Compute the average of item?.traded
const totalTraded = tradingList?.reduce((acc, traded) => acc + traded, 0);
avgVolume = totalTraded / tradingList?.length;
const totalSentiment = sentimentList?.reduce((acc, sentiment) => acc + sentiment, 0);
avgSentiment = totalSentiment / tradingList?.length > 1 ? 'Bullish' : 'Bearish';
const option = {
silent: true,
animation: false,
tooltip: {
trigger: 'axis',
hideDelay: 100, // Set the delay in milliseconds
},
grid: {
left: '3%',
right: '3%',
bottom: '0%',
top: '5%',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap: false,
data: dates,
axisLabel: {
color: '#fff',
formatter: function (value) {
// Assuming dates are in the format 'yyyy-mm-dd'
// Extract the month and day from the date string and convert the month to its abbreviated name
const dateParts = value.split('-');
const day = dateParts[2].substring(0); // Extracting the last two digits of the year
const monthIndex = parseInt(dateParts[1]) - 1; // Months are zero-indexed in JavaScript Date objects
return `${day} ${monthNames[monthIndex]}`;
}
}
},
yAxis: [
{
type: 'value',
splitLine: {
show: false, // Disable x-axis grid lines
},
axisLabel: {
show: false // Hide y-axis labels
}
},
{
type: 'value',
splitLine: {
show: false, // Disable x-axis grid lines
},
position: 'right',
axisLabel: {
show: false // Hide y-axis labels
},
},
],
series: [
{
name: 'Volume [$]',
data: tradingList,
type: 'line',
itemStyle: {
color: '#fff' // Change bar color to white
},
showSymbol: false
},
{
name: 'Retail Sentiment',
data: sentimentList,
type: 'bar',
yAxisIndex: 1,
itemStyle: {
color: (params) => {
// Set color based on positive or negative value
return params.data >= 0 ? '#22C55E' : '#F71F4F';
},
},
},
]
};
return option;
}
const getRetailVolume = async (ticker) => {
// Get cached data for the specific tickerID
const cachedData = getCache(ticker, 'getRetailVolume');
if (cachedData) {
rawData = cachedData;
historyData = rawData?.history;
} else {
const postData = {'ticker': ticker, path: 'retail-volume'};
// make the POST request to the endpoint
const response = await fetch('/api/ticker-data', {
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(postData)
});
rawData = await response.json();
historyData = rawData?.history;
// Cache the data for this specific tickerID with a specific name 'getRetailVolume'
setCache(ticker, rawData, 'getRetailVolume');
}
if(Object?.keys(rawData)?.length !== 0) {
$retailVolumeComponent = true;
} else {
$retailVolumeComponent = false;
}
};
$: {
if($assetType === 'stock' ? $stockTicker :$etfTicker && typeof window !== 'undefined') {
isLoaded=false;
const ticker = $assetType === 'stock' ? $stockTicker :$etfTicker
const asyncFunctions = [
getRetailVolume(ticker)
];
Promise.all(asyncFunctions)
.then((results) => {
if(Object?.keys(rawData)?.length !== 0) {
optionsData = getPlotOptions()
}
})
.catch((error) => {
console.error('An error occurred:', error);
});
isLoaded = true;
}
}
</script>
<section class="overflow-hidden text-white h-full pb-8">
<main class="overflow-hidden ">
<div class="flex flex-row items-center">
<label for="retailTraderTrackerInfo" class="mr-1 cursor-pointer flex flex-row items-center text-white text-xl sm:text-3xl font-bold">
Retail Trader Activity
</label>
<InfoModal
title={"Retail Trader Activity"}
content={"Gain insights into Retail Trader activity with the following visualization: The green bar illustrates the daily volume trend, signifying a bullish sentiment if it ranges from 0 to 100, or bearish if it spans from -100 to just below 0. The white line depicts the daily trading volume of retail investors."}
id={"retailTraderTrackerInfo"}
/>
</div>
{#if isLoaded}
{#if Object?.keys(rawData)?.length !== 0}
<div class="w-full flex flex-col items-start">
<div class="text-white text-[1rem] mt-2 mb-2 w-full">
In the past six months, the {$displayCompanyName} had an average retail investor volume of <span class="font-semibold">{avgVolume > 100e3 ? abbreviateNumber(avgVolume,true) : '< $100K'}</span>, with a prevailing
{#if avgSentiment === 'Bullish' }
<span class="text-[#37C97D]">
<svg class="w-6 h-6 sm:w-7 sm:h-7 inline-block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g fill="none" stroke="#37C97D" stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5"><path d="m3 17l6-6l4 4l8-8"/><path d="M17 7h4v4"/></g></svg>
{avgSentiment}
</span>
{:else if avgSentiment === 'Bearish' }
<span class="text-[#E57C34]">
<svg class="w-6 h-6 sm:w-7 sm:h-7 inline-block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><path fill="#ff2f1f" d="M244 136v64a12 12 0 0 1-12 12h-64a12 12 0 0 1 0-24h35l-67-67l-31.51 31.52a12 12 0 0 1-17 0l-72-72a12 12 0 0 1 17-17L96 127l31.51-31.52a12 12 0 0 1 17 0L220 171v-35a12 12 0 0 1 24 0Z"/></svg>
{avgSentiment}
</span>
{:else}
<span class="text-[#FF2F1F]">
<svg class="w-6 h-6 sm:w-7 sm:h-7 inline-block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#e57c34" d="m22 12l-4-4v3H3v2h15v3l4-4Z"/></svg>
Neutral
</span>
{/if} trend.
</div>
</div>
<div class="pb-8 sm:pb-2 rounded-lg bg-[#09090B]">
<div class="app w-full h-[300px] mt-5">
<Chart {init} options={optionsData} class="chart" />
</div>
</div>
<div class="text-white mt-6">
The line chart shows dollar volume, while the bar chart indicates sentiment from -100 to 100, with negative values for more selling and positive values for more buying.
</div>
<h2 class="mt-10 mr-1 flex flex-row items-center text-white text-xl sm:text-2xl font-bold mb-3">
Latest Information
</h2>
<span class="text-white">
On {new Date(rawData?.lastDate)?.toLocaleString('en-US', { month: 'short', day: 'numeric', year: 'numeric', daySuffix: '2-digit' })}, retail traders accounted for <span class="font-semibold">{rawData?.retailStrength > 0.01 ? rawData?.retailStrength : '< 0.01'}%</span> of the trading volume.
</span>
<div class="flex justify-start items-center w-full m-auto mt-6 ">
<table class="w-full" data-test="statistics-table">
<tbody>
<tr class="border-y border-gray-800 odd:bg-[#27272A]">
<td class="px-[5px] py-1.5 xs:px-2.5 xs:py-2">
<span>Date</span>
</td>
<td class="px-[5px] whitespace-nowrap py-1.5 text-right font-medium xs:px-2.5 xs:py-2">
{formatDateRange(rawData?.lastDate)}
</td>
</tr>
<tr class="border-y border-gray-800 odd:bg-[#27272A]">
<td class="px-[5px] py-1.5 xs:px-2.5 xs:py-2">
<span>Volume in $</span>
</td>
<td class="px-[5px] py-1.5 text-right font-medium xs:px-2.5 xs:py-2">
{monthlyVolume}
</td>
</tr>
<tr class="border-y border-gray-800 odd:bg-[#27272A]">
<td class="px-[5px] py-1.5 xs:px-2.5 xs:py-2">
<span>Retail Sentiment Range</span>
</td>
<td class="px-[5px] py-1.5 text-right font-medium xs:px-2.5 xs:py-2">
Between {lowestSentiment} to {highestSentiment}
</td>
</tr>
</tbody>
</table>
</div>
{/if}
{:else}
<div class="flex justify-center items-center h-80">
<div class="relative">
<label class="bg-[#09090B] rounded-xl h-14 w-14 flex justify-center items-center absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2">
<span class="loading loading-spinner loading-md text-gray-400"></span>
</label>
</div>
</div>
{/if}
</main>
</section>
<style>
.app {
height: 300px;
max-width: 100%; /* Ensure chart width doesn't exceed the container */
}
@media (max-width: 640px) {
.app {
height: 210px;
}
}
.chart {
width: 100%;
}
</style>