Skip to content

Commit

Permalink
feat: add volume data
Browse files Browse the repository at this point in the history
  • Loading branch information
vacekj committed Dec 12, 2024
1 parent ff51f0b commit 89dfa7a
Show file tree
Hide file tree
Showing 5 changed files with 492 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/pages/trade/api/candles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { useQuery } from '@tanstack/react-query';
import { useRefetchOnNewBlock } from '@/shared/api/compact-block.ts';
import { CandleApiResponse } from '@/shared/api/server/candles/types.ts';
import { usePathSymbols } from '@/pages/trade/model/use-path.ts';
import { OhlcData } from 'lightweight-charts';
import { DurationWindow } from '@/shared/utils/duration.ts';
import { CandleWithVolume } from '@/shared/api/server/candles/utils.ts';

export const useCandles = (durationWindow: DurationWindow) => {
const { baseSymbol, quoteSymbol } = usePathSymbols();

const query = useQuery({
queryKey: ['candles', baseSymbol, quoteSymbol, durationWindow],
queryFn: async (): Promise<OhlcData[]> => {
queryFn: async (): Promise<CandleWithVolume[]> => {
const paramsObj = {
baseAsset: baseSymbol,
quoteAsset: quoteSymbol,
Expand Down
50 changes: 45 additions & 5 deletions src/pages/trade/ui/chart.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import cn from 'clsx';
import { useEffect, useRef, useState } from 'react';
import { createChart, IChartApi, OhlcData } from 'lightweight-charts';
import { createChart, IChartApi } from 'lightweight-charts';
import { theme } from '@penumbra-zone/ui/theme';
import { Text } from '@penumbra-zone/ui/Text';
import { useCandles } from '../api/candles';
import { observer } from 'mobx-react-lite';
import { DurationWindow, durationWindows } from '@/shared/utils/duration.ts';
import { CandleWithVolume } from '@/shared/api/server/candles/utils';

const ChartLoadingState = () => {
return (
Expand Down Expand Up @@ -119,10 +120,11 @@ const ChartLoadingState = () => {
);
};

const ChartData = observer(({ candles }: { candles: OhlcData[] }) => {
const ChartData = observer(({ candles }: { candles: CandleWithVolume[] }) => {
const chartElRef = useRef<HTMLDivElement>(null);
const chartRef = useRef<IChartApi>();
const seriesRef = useRef<ReturnType<IChartApi['addCandlestickSeries']>>();
const volumeSeriesRef = useRef<ReturnType<IChartApi['addHistogramSeries']>>();

// Initialize the chart once when the component mounts
useEffect(() => {
Expand All @@ -145,7 +147,7 @@ const ChartData = observer(({ candles }: { candles: OhlcData[] }) => {
},
});

// Initialize the series
// Initialize the candlestick series
seriesRef.current = chartRef.current.addCandlestickSeries({
upColor: theme.color.success.light,
downColor: theme.color.destructive.light,
Expand All @@ -154,6 +156,31 @@ const ChartData = observer(({ candles }: { candles: OhlcData[] }) => {
wickDownColor: theme.color.destructive.light,
});

// Set the price scale margins for the candlestick series
seriesRef.current.priceScale().applyOptions({
scaleMargins: {
top: 0.1,
bottom: 0.4,
},
});

// Initialize the volume series
volumeSeriesRef.current = chartRef.current.addHistogramSeries({
color: theme.color.success.light,
priceFormat: {
type: 'volume',
},
priceScaleId: '', // Set as overlay
});

// Set the price scale margins for the volume series
volumeSeriesRef.current.priceScale().applyOptions({
scaleMargins: {
top: 0.7,
bottom: 0,
},
});

chartRef.current.timeScale().fitContent();
}

Expand All @@ -167,8 +194,21 @@ const ChartData = observer(({ candles }: { candles: OhlcData[] }) => {

// Update chart when candles change
useEffect(() => {
if (seriesRef.current) {
seriesRef.current.setData(candles);
if (seriesRef.current && volumeSeriesRef.current) {
// Set OHLC data
seriesRef.current.setData(candles.map(c => c.ohlc));

// Set volume data with colors based on price movement
volumeSeriesRef.current.setData(
candles.map(candle => ({
time: candle.ohlc.time,
value: candle.volume,
color:
candle.ohlc.close >= candle.ohlc.open
? theme.color.success.light
: theme.color.destructive.light,
})),
);
chartRef.current?.timeScale().fitContent();
}
}, [candles]);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/api/server/candles/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OhlcData, UTCTimestamp } from 'lightweight-charts';
import { CandleWithVolume } from './utils';

export type CandleApiResponse = OhlcData<UTCTimestamp>[] | { error: string };
export type CandleApiResponse = CandleWithVolume[] | { error: string };

export interface DbCandle {
close: number;
Expand Down
Loading

0 comments on commit 89dfa7a

Please sign in to comment.