This commit is contained in:
MuslemRahimi 2025-02-25 23:50:28 +01:00
parent 1ba38b63ce
commit f9dfcb2924
4 changed files with 160 additions and 145 deletions

View File

@ -1,28 +1,10 @@
<script lang="ts"> <script lang="ts">
import { abbreviateNumberWithColor } from "$lib/utils"; import { abbreviateNumberWithColor, abbreviateNumber } from "$lib/utils";
import { screenWidth } from "$lib/store"; import { screenWidth } 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;
@ -39,90 +21,125 @@
}); });
let displayList = rawData?.slice(0, 150); let displayList = rawData?.slice(0, 150);
let options = null;
function plotData() { function plotData() {
// Process the raw data, sorting by strike value
const processedData = rawData const processedData = rawData
?.map((d) => ({ ?.map((d) => ({
strike: d?.strike, strike: d?.strike,
callValue: d?.call_oi, callValue: d?.call_oi,
putValue: d?.put_oi, putValue: d?.put_oi,
})) }))
?.sort((a, b) => a?.strike - b?.strike); ?.sort((a, b) => a.strike - b.strike);
// Extract arrays for categories and series data
const strikes = processedData?.map((d) => d.strike); const strikes = processedData?.map((d) => d.strike);
const callValues = processedData?.map((d) => d.callValue?.toFixed(2)); const callValues = processedData?.map((d) =>
const putValues = processedData?.map((d) => d.putValue?.toFixed(2)); parseFloat(d.callValue.toFixed(2)),
);
const putValues = processedData?.map((d) =>
parseFloat(d.putValue.toFixed(2)),
);
// Calculate a bar width percentage (if needed for further calculations)
const barWidthPercentage = Math.max(100 / processedData.length, 30); const barWidthPercentage = Math.max(100 / processedData.length, 30);
const options = { const options = {
animation: false, chart: {
tooltip: { backgroundColor: "#09090B",
trigger: "axis", animation: false,
axisPointer: { height: 360,
type: "shadow",
},
backgroundColor: "#313131",
textStyle: {
color: "#fff",
},
formatter: function (params) {
const strike = params[0].axisValue;
const call = params[1].data;
const put = params[0].data;
return `
<div style="text-align:left;">
<b>Strike:</b> ${strike}<br/>
<span style="color:#00FC50;">● Call OI:</span> ${abbreviateNumberWithColor(call, false, true)}<br/>
<span style="color:#FF2F1F;">● Put OI:</span> ${abbreviateNumberWithColor(put, false, true)}<br/>
</div>`;
},
}, },
grid: { credits: { enabled: false },
left: $screenWidth < 640 ? "5%" : "2%", legend: { enabled: false },
right: $screenWidth < 640 ? "5%" : "2%", title: {
bottom: "10%", text: `<h3 class="mt-3 mb-1">Open Interest By Strike</h3>`,
containLabel: true, useHTML: true,
}, style: { color: "white" },
yAxis: {
type: "value",
nameTextStyle: { color: "#fff" },
splitLine: { show: false },
axisLabel: {
show: false, // Hide x-axis labels
},
}, },
xAxis: { xAxis: {
type: "category", categories: strikes,
data: strikes, lineColor: "#fff",
axisLine: { lineStyle: { color: "#fff" } }, endOnTick: false,
axisLabel: { crosshair: {
color: "#fff", color: "#fff", // Set the color of the crosshair line
interval: (index) => index % 5 === 0, // Show every 5th label width: 1, // Adjust the line width as needed
rotate: 45, // Rotate labels for better readability dashStyle: "Solid",
fontSize: $screenWidth < 640 ? 10 : 14, // Adjust font size if needed },
margin: 20, labels: {
style: {
color: "#fff",
},
rotation: 45,
// Only display every 5th label
formatter: function () {
return this.pos % 4 === 0 ? this.value : "";
},
}, },
splitLine: { show: false },
}, },
yAxis: {
gridLineWidth: 1,
gridLineColor: "#111827",
labels: {
style: { color: "white" },
},
title: { text: null },
opposite: true,
},
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]">Strike ${this?.x}</span><br>`;
// Loop through each point in the shared tooltip
this.points.forEach((point) => {
tooltipContent += `<span class="text-white font-semibold text-sm">${point.series.name}:</span>
<span class="text-white font-normal text-sm" style="color:${point.color}">${abbreviateNumber(
point.y,
)}</span><br>`;
});
return tooltipContent;
},
},
plotOptions: {
animation: false,
column: {
grouping: true,
shadow: false,
borderWidth: 0,
},
},
series: [ series: [
{ {
name: `Put`, name: "Put",
type: "bar", type: "column",
data: putValues, data: putValues,
itemStyle: { color: "#FF2F1F" }, color: "#FF2F1F",
barWidth: `${barWidthPercentage}%`, animation: false,
}, },
{ {
name: `Call `, name: "Call",
type: "bar", type: "column",
data: callValues, data: callValues,
itemStyle: { color: "#00FC50" }, color: "#00FC50",
barWidth: `${barWidthPercentage}%`, animation: false,
}, },
], ],
credits: {
enabled: false,
},
}; };
return options; return options;
@ -229,39 +246,29 @@
displayList = [...originalData].sort(compareValues); displayList = [...originalData].sort(compareValues);
}; };
$: { let config = plotData() || null;
if (typeof window !== "undefined") {
options = plotData();
}
}
</script> </script>
<div class="sm:pl-7 sm:pb-7 sm:pt-7 w-full m-auto mt-2 sm:mt-0"> <div class="sm:pl-7 sm:pb-7 sm:pt-7 w-full m-auto mt-2 sm:mt-0">
<h2 <h2
class=" flex flex-row items-center text-white text-xl sm:text-2xl font-bold w-fit" class=" flex flex-row items-center text-white text-xl sm:text-2xl font-bold w-fit"
> >
Open Interest (OI) By Strike Open Interest Chart
</h2> </h2>
<div class="w-full overflow-hidden m-auto"> <div class="w-full overflow-hidden m-auto mt-3">
{#if options !== null} {#if config !== null}
<div class="app w-full relative"> <div
<Chart {init} {options} class="chart" /> class="border border-gray-800 rounded w-full"
</div> use:highcharts={config}
{:else} ></div>
<div class="flex justify-center items-center h-80">
<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>
<h3 class="mt-5 text-xl sm:text-2xl text-white font-bold mb-3">
Open Interest Table
</h3>
<div class="w-full overflow-x-scroll text-white"> <div class="w-full overflow-x-scroll text-white">
<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"

View File

@ -94,7 +94,6 @@
priceList, priceList,
} = filterDataByPeriod(data, timePeriod); } = filterDataByPeriod(data, timePeriod);
console.log(volatilitySpread);
const options = { const options = {
chart: { chart: {
backgroundColor: "#09090B", backgroundColor: "#09090B",

View File

@ -1,56 +1,65 @@
import Highcharts from 'highcharts'; import Highcharts from 'highcharts';
import { browser } from '$app/environment'; import { browser } from '$app/environment';
/*
if (browser) { if (browser) {
import('highcharts/modules/exporting').then(module => { Highcharts.setOptions({
module.default(Highcharts); lang: {
numericSymbols: ['K', 'M', 'B', 'T', 'P', 'E']
}
}); });
import('highcharts/modules/export-data').then(module => {
module.default(Highcharts);
});
}
*/
if (browser) {
Highcharts.setOptions({
lang: {
numericSymbols: ['K', 'M', 'B', 'T', 'P', 'E']
}
});
} }
export default (node, config) => { export default (node, config) => {
const redraw = true; const redraw = true;
const oneToOne = true; const oneToOne = true;
// Merge our watermark load event with any provided chart events
const mergedChartOptions = {
...(config.chart || {}),
events: {
...(config.chart && config.chart.events ? config.chart.events : {}),
load: function () {
const chart = this;
const marginX = 10; // Adjust horizontal margin
const marginY = 5; // Adjust vertical margin
// Destroy existing watermark if it exists
if (chart.watermark) {
chart.watermark.destroy();
}
// Get the actual width and height of the plotting area
const x = chart.chartWidth - marginX;
const y = chart.chartHeight - marginY;
// Add watermark
chart.watermark = chart.renderer
.text('stocknear.com', x, y)
.attr({
align: 'right'
})
.css({
fontSize: '12px',
color: 'rgba(255, 255, 255, 0.6)',
fontWeight: 'medium',
pointerEvents: 'none'
})
.add();
// Adjust on redraw
Highcharts.addEvent(chart, 'redraw', function () {
const newX = chart.chartWidth - marginX;
const newY = chart.chartHeight - marginY;
chart.watermark.attr({ x: newX, y: newY });
});
}
}
};
const chart = Highcharts.chart(node, { const chart = Highcharts.chart(node, {
...config, ...config,
/* chart: mergedChartOptions,
exporting: {
filename: 'event-id-metadata-graph',
fetchOptions:{
credentials: "omit", // omit, same-origin, include
mode: "cors" // cors, no-cors, same-origin
},
buttons: {
customButton: {
menuItems: [
'viewFullscreen',
'printChart',
'separator',
'downloadPNG',
'downloadJPEG',
'downloadPDF',
'downloadSVG',
],
className: 'bg-gray-500',
text: 'Custom button',
},
},
},
*/
}); });
return { return {
@ -60,8 +69,10 @@ export default (node, config) => {
destroy() { destroy() {
chart.destroy(); chart.destroy();
}, },
/*
exportChart(options = {}, chartOptions = {}) { exportChart(options = {}, chartOptions = {}) {
chart.exportChart(options, chartOptions); chart.exportChart(options, chartOptions);
}, },
*/
}; };
}; };

View File

@ -14,9 +14,7 @@
description={`Track volatility and implied volatility trends with our interactive chart. Analyze price movements, 30-day implied volatility, and realized volatility to make data-driven trading decisions.`} description={`Track volatility and implied volatility trends with our interactive chart. Analyze price movements, 30-day implied volatility, and realized volatility to make data-driven trading decisions.`}
/> />
<section <section class="w-full bg-default overflow-hidden text-white min-h-screen">
class="w-full bg-default overflow-hidden text-white min-h-screen pb-40"
>
<div class="w-full flex h-full overflow-hidden"> <div class="w-full flex h-full overflow-hidden">
<div <div
class="w-full relative flex justify-center items-center overflow-hidden" class="w-full relative flex justify-center items-center overflow-hidden"