This commit is contained in:
MuslemRahimi 2025-02-25 23:07:24 +01:00
parent 1ae91f39de
commit 1ba38b63ce

View File

@ -1,32 +1,14 @@
<script lang="ts"> <script lang="ts">
import { import {
removeCompanyStrings,
abbreviateNumberWithColor, abbreviateNumberWithColor,
abbreviateNumber, abbreviateNumber,
monthNames,
} from "$lib/utils"; } from "$lib/utils";
import { screenWidth, displayCompanyName } from "$lib/store"; import { displayCompanyName } from "$lib/store";
import { onMount } from "svelte"; import { onMount } from "svelte";
import TableHeader from "$lib/components/Table/TableHeader.svelte"; import TableHeader from "$lib/components/Table/TableHeader.svelte";
import UpgradeToPro from "$lib/components/UpgradeToPro.svelte"; import UpgradeToPro from "$lib/components/UpgradeToPro.svelte";
import { Chart } from "svelte-echarts"; import highcharts from "$lib/highcharts.ts";
import { init, use } from "echarts/core";
import { LineChart, BarChart } from "echarts/charts";
import {
GridComponent,
TooltipComponent,
LegendComponent,
} from "echarts/components";
import { CanvasRenderer } from "echarts/renderers";
use([
LineChart,
BarChart,
GridComponent,
TooltipComponent,
LegendComponent,
CanvasRenderer,
]);
export let data; export let data;
@ -45,7 +27,7 @@
let displayList = rawData?.slice(0, 150); let displayList = rawData?.slice(0, 150);
let timePeriod = "3M"; let timePeriod = "3M";
let options = null; let config = null;
function filterDataByPeriod(historicalData, period = "3M") { function filterDataByPeriod(historicalData, period = "3M") {
const currentDate = new Date(); const currentDate = new Date();
@ -87,8 +69,8 @@
const impliedVolatility = filteredData?.map((item) => item?.iv); const impliedVolatility = filteredData?.map((item) => item?.iv);
const realizedVolatility = filteredData?.map((item) => item?.rv); const realizedVolatility = filteredData?.map((item) => item?.rv);
const volatilitySpread = filteredData?.map( const volatilitySpread = filteredData?.map((item) =>
(item) => item?.volatilitySpread, item?.volatilitySpread ? Number(item?.volatilitySpread) : null,
); );
const priceList = filteredData?.map((item) => item.price); const priceList = filteredData?.map((item) => item.price);
@ -102,6 +84,7 @@
} }
function plotData() { function plotData() {
// Sort and filter the raw data as before
const data = rawData?.sort((a, b) => new Date(a?.date) - new Date(b?.date)); const data = rawData?.sort((a, b) => new Date(a?.date) - new Date(b?.date));
const { const {
dateList, dateList,
@ -110,141 +93,167 @@
volatilitySpread, volatilitySpread,
priceList, priceList,
} = filterDataByPeriod(data, timePeriod); } = filterDataByPeriod(data, timePeriod);
console.log(volatilitySpread);
const options = { const options = {
animation: false, chart: {
tooltip: { backgroundColor: "#09090B",
trigger: "axis", animation: false,
hideDelay: 100, height: 360,
borderColor: "#969696", // Black border color },
borderWidth: 1, // Border width of 1px credits: { enabled: false },
backgroundColor: "#313131", // Optional: Set background color for contrast legend: { enabled: false },
textStyle: { title: {
color: "#fff", // Optional: Text color for better visibility text: `<h3 class="mt-3 mb-1">Volatiltiy Exposure</h3>`,
useHTML: true,
style: { color: "white" },
},
// Disable markers globally on hover for all series
xAxis: {
type: "datetime",
endOnTick: false,
categories: dateList,
crosshair: {
color: "#fff", // Set the color of the crosshair line
width: 1, // Adjust the line width as needed
dashStyle: "Solid",
}, },
formatter: function (params) { labels: {
// Get the timestamp from the first parameter style: {
const timestamp = params[0].axisValue;
// Initialize result with timestamp
let result = timestamp + "<br/>";
// Add each series data
params?.forEach((param) => {
const marker =
'<span style="display:inline-block;margin-right:4px;' +
"border-radius:10px;width:10px;height:10px;background-color:" +
param.color +
'"></span>';
result +=
marker +
param.seriesName +
": " +
abbreviateNumber(param.value) +
"<br/>";
});
return result;
},
axisPointer: {
lineStyle: {
color: "#fff", color: "#fff",
}, },
distance: 20, // Increases space between label and axis
formatter: function () {
const date = new Date(this.value);
return date.toLocaleDateString("en-US", {
month: "short",
year: "numeric",
});
},
},
tickPositioner: function () {
// Create custom tick positions with wider spacing
const positions = [];
const info = this.getExtremes();
const tickCount = 5; // Reduce number of ticks displayed
const interval = Math?.floor((info?.max - info?.min) / tickCount);
for (let i = 0; i <= tickCount; i++) {
positions?.push(info?.min + i * interval);
}
return positions;
}, },
}, },
silent: true, yAxis: [
grid: {
left: $screenWidth < 640 ? "5%" : "4%",
right: $screenWidth < 640 ? "5%" : "0%",
bottom: "10%",
containLabel: true,
},
xAxis: [
{ {
type: "category", gridLineWidth: 1,
data: dateList, gridLineColor: "#111827",
axisLabel: { labels: {
color: "#fff", style: { color: "white" },
},
title: { text: null },
opposite: true,
},
{
// Primary yAxis (left) for volatility series
labels: { enabled: false },
title: { text: null },
gridLineWidth: 0,
},
{
// Primary yAxis (left) for volatility series
labels: { enabled: false },
title: { text: null },
gridLineWidth: 0,
},
],
tooltip: {
shared: true,
useHTML: true,
backgroundColor: "rgba(0, 0, 0, 0.8)", // Semi-transparent black
borderColor: "rgba(255, 255, 255, 0.2)", // Slightly visible white border
borderWidth: 1,
style: {
color: "#fff",
fontSize: "16px",
padding: "10px",
},
borderRadius: 4,
formatter: function () {
// Format the x value to display time in hh:mm format
let tooltipContent = `<span class="text-white m-auto text-black text-[1rem] font-[501]">${new Date(
this?.x,
).toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
})}</span><br>`;
formatter: function (value) { // Loop through each point in the shared tooltip
// Assuming dates are in the format 'yyyy-mm-dd' this.points.forEach((point) => {
const dateParts = value.split("-"); tooltipContent += `<span class="text-white font-semibold text-sm">${point.series.name}:</span>
const monthIndex = parseInt(dateParts[1]) - 1; // Months are zero-indexed in JavaScript Date objects <span class="text-white font-normal text-sm" style="color:${point.color}">${abbreviateNumber(
const year = parseInt(dateParts[0]); point.y,
const day = parseInt(dateParts[2]); )}</span><br>`;
return `${day} ${monthNames[monthIndex]} ${year}`; });
return tooltipContent;
},
},
// Disable markers globally on hover for all series
plotOptions: {
series: {
marker: {
enabled: false,
states: {
hover: {
enabled: false,
},
}, },
}, },
}, },
], },
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: [ series: [
{ {
name: "Price", name: "Stock Price",
type: "line", type: "spline", // smooth line
data: priceList, data: priceList,
yAxisIndex: 1, yAxis: 1,
lineStyle: { width: 2 }, lineWidth: 2,
itemStyle: { color: "#fff",
color: "#fff", animation: false,
},
smooth: true,
showSymbol: false,
}, },
{ {
name: "Implied Volatility", name: "Implied Volatility",
type: "line", type: "spline",
lineStyle: { width: 2 },
data: impliedVolatility, data: impliedVolatility,
itemStyle: { lineWidth: 2,
color: "#FAD776", yAxis: 0,
}, color: "#FAD776",
smooth: true, animation: false,
showSymbol: false,
}, },
{ {
name: "Realized Volatility", name: "Realized Volatility",
type: "line", type: "spline",
lineStyle: { width: 2 },
data: realizedVolatility, data: realizedVolatility,
itemStyle: { lineWidth: 2,
color: "#007BFF", yAxis: 0,
}, color: "#007BFF",
smooth: true, animation: false,
showSymbol: false,
}, },
{ {
name: "Vol. Spread", name: "Vol. Spread",
type: "bar", type: "column", // Highcharts column chart for bars
lineStyle: { width: 2 }, yaxis: 2,
data: volatilitySpread, data: volatilitySpread,
itemStyle: { color: "#9B5DC4",
color: "#9B5DC4", borderColor: "#9B5DC4",
}, borderRadius: "1px",
smooth: true, animation: false,
showSymbol: false,
}, },
], ],
}; };
return options; return options;
} }
@ -389,8 +398,8 @@
}; };
$: { $: {
if (typeof window !== "undefined" && timePeriod) { if (timePeriod) {
options = plotData(); config = plotData();
} }
} }
</script> </script>
@ -402,65 +411,59 @@
Volatility Exposure Volatility Exposure
</h2> </h2>
<div class="w-full mt-2"> <div class="w-full mt-2">
{$displayCompanyName} has experienced an average implied volatility of {avgIV?.toFixed( {removeCompanyStrings($displayCompanyName)} has experienced an average implied
volatility of {avgIV?.toFixed(2)} and an average realized volatility of {avgRV?.toFixed(
2, 2,
)} and an average realized volatility of {avgRV?.toFixed(2)}. )}.
</div> </div>
<div class="w-full overflow-hidden m-auto mt-5"> <div class="w-full overflow-hidden m-auto">
{#if options !== null} {#if config !== null}
<div class="app w-full relative"> <div class="flex justify-end pt-5 pb-2 space-x-2 ml-auto z-10">
<div class="flex justify-start space-x-2 absolute right-0 top-0 z-10"> {#each ["3M", "6M", "1Y"] as item, index}
{#each ["3M", "6M", "1Y"] as item, index} {#if data?.user?.tier === "Pro" || index === 0}
{#if data?.user?.tier === "Pro" || index === 0} <label
<label on:click={() => (timePeriod = item)}
on:click={() => (timePeriod = item)} class="px-3 py-1 text-sm {timePeriod === item
class="px-3 py-1 text-sm {timePeriod === item ? 'bg-white text-black '
? 'bg-white text-black ' : 'text-white bg-table text-opacity-[0.6]'} transition ease-out duration-100 sm:hover:bg-white sm:hover:text-black rounded-md cursor-pointer"
: 'text-white bg-table text-opacity-[0.6]'} transition ease-out duration-100 sm:hover:bg-white sm:hover:text-black rounded-md cursor-pointer" >
{item}
</label>
{:else if data?.user?.tier !== "Pro"}
<a
href="/pricing"
class="px-3 py-1 text-sm flex flex-row items-center {timePeriod ===
item
? 'bg-white text-black '
: 'text-white bg-table text-opacity-[0.6]'} transition ease-out duration-100 sm:hover:bg-white sm:hover:text-black rounded-md cursor-pointer"
>
{item}
<svg
class="ml-1 -mt-w-3.5 h-3.5 inline-block"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
><path
fill="#A3A3A3"
d="M17 9V7c0-2.8-2.2-5-5-5S7 4.2 7 7v2c-1.7 0-3 1.3-3 3v7c0 1.7 1.3 3 3 3h10c1.7 0 3-1.3 3-3v-7c0-1.7-1.3-3-3-3M9 7c0-1.7 1.3-3 3-3s3 1.3 3 3v2H9z"
/></svg
> >
{item} </a>
</label> {/if}
{:else if data?.user?.tier !== "Pro"} {/each}
<a </div>
href="/pricing"
class="px-3 py-1 text-sm flex flex-row items-center {timePeriod ===
item
? 'bg-white text-black '
: 'text-white bg-table text-opacity-[0.6]'} transition ease-out duration-100 sm:hover:bg-white sm:hover:text-black rounded-md cursor-pointer"
>
{item}
<svg
class="ml-1 -mt-w-3.5 h-3.5 inline-block"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
><path
fill="#A3A3A3"
d="M17 9V7c0-2.8-2.2-5-5-5S7 4.2 7 7v2c-1.7 0-3 1.3-3 3v7c0 1.7 1.3 3 3 3h10c1.7 0 3-1.3 3-3v-7c0-1.7-1.3-3-3-3M9 7c0-1.7 1.3-3 3-3s3 1.3 3 3v2H9z"
/></svg
>
</a>
{/if}
{/each}
</div>
<Chart {init} {options} class="chart" /> <div
</div> class="border border-gray-800 rounded w-full"
{:else} use:highcharts={config}
<div class="flex justify-center items-center h-80"> ></div>
<div class="relative">
<label
class="bg-primary rounded-md 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 sm:loading-[1rem] text-gray-400"
></span>
</label>
</div>
</div>
{/if} {/if}
</div> </div>
<div class="w-full overflow-x-scroll text-white">
<h3 class="mt-5 text-xl sm:text-2xl text-white font-bold mb-2 sm:mb-0">
Volatility History
</h3>
<div class="w-full overflow-x-scroll text-white mt-5">
<table <table
class="w-full table table-sm table-compact bg-table border border-gray-800 rounded-none sm:rounded-md m-auto overflow-x-auto" class="w-full table table-sm table-compact bg-table border border-gray-800 rounded-none sm:rounded-md m-auto overflow-x-auto"
> >
@ -554,21 +557,3 @@
<UpgradeToPro {data} /> <UpgradeToPro {data} />
</div> </div>
<style>
.app {
height: 400px;
width: 100%;
}
@media (max-width: 560px) {
.app {
width: 100%;
height: 300px;
}
}
.chart {
width: 100%;
}
</style>