feat: add local tradingview
This commit is contained in:
@@ -225,7 +225,7 @@ export class RWADatafeed extends Datafeeds.UDFCompatibleDatafeed {
|
||||
volume: data.bar.volume,
|
||||
};
|
||||
onTick(bar);
|
||||
});
|
||||
}, subscriberUID);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,11 +233,6 @@ export class RWADatafeed extends Datafeeds.UDFCompatibleDatafeed {
|
||||
*/
|
||||
unsubscribeBars(subscriberUID: string): void {
|
||||
console.log("[RWADatafeed]: unsubscribeBars", subscriberUID);
|
||||
const subscriber = this.subscribers.get(subscriberUID);
|
||||
tradeWebSocket.unsubscribeChannel([{
|
||||
name: "bar",
|
||||
symbol: subscriber?.symbol as string,
|
||||
resolution: subscriber?.resolution as ResolutionString,
|
||||
}]);
|
||||
tradeWebSocket.unsubscribe(subscriberUID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,14 +5,14 @@ import type { CSSProperties } from "vue";
|
||||
import { mergeWith } from "lodash-es";
|
||||
import { RWADatafeed } from "./datafeed";
|
||||
|
||||
const { VITE_TRADINGVIEW_LIBRARY_URL, VITE_API_URL } = useEnv();
|
||||
const { VITE_API_URL } = useEnv();
|
||||
|
||||
const datafeed = new RWADatafeed(`${VITE_API_URL}/api/udf`);
|
||||
|
||||
const defaultOptions = {
|
||||
container: "tradingview_chart_container",
|
||||
locale: "zh",
|
||||
library_path: `${VITE_TRADINGVIEW_LIBRARY_URL}/charting_library/`,
|
||||
library_path: "/charting_library/",
|
||||
datafeed,
|
||||
interval: "1" as ResolutionString,
|
||||
symbol: "",
|
||||
@@ -69,6 +69,7 @@ const defaultOptions = {
|
||||
} satisfies ChartingLibraryWidgetOptions;
|
||||
|
||||
export interface TradingViewChartExpose {
|
||||
widget: Ref<IChartingLibraryWidget | null>;
|
||||
}
|
||||
|
||||
export const TradingViewChart = defineComponent({
|
||||
@@ -91,7 +92,7 @@ export const TradingViewChart = defineComponent({
|
||||
required: false,
|
||||
},
|
||||
},
|
||||
setup(props) {
|
||||
setup(props, { expose }) {
|
||||
const el = ref<HTMLDivElement | null>(null);
|
||||
const { isDark } = useTheme();
|
||||
const widget = ref<IChartingLibraryWidget | null>(null);
|
||||
@@ -146,13 +147,17 @@ export const TradingViewChart = defineComponent({
|
||||
|
||||
// 监听交易对变化,动态更新图表数据
|
||||
watch(() => props.symbol, (newSymbol) => {
|
||||
if (!widget.value || !newSymbol)
|
||||
if (!widget.value || !props.symbol)
|
||||
return;
|
||||
widget.value.activeChart().setSymbol(newSymbol, () => {
|
||||
console.log(`[TradingView]: Symbol changed to ${newSymbol}`);
|
||||
widget.value.activeChart().setSymbol(props.symbol, () => {
|
||||
console.log(`[TradingView]: Re-subscribed to symbol ${props.symbol}`);
|
||||
});
|
||||
});
|
||||
|
||||
expose({
|
||||
widget,
|
||||
});
|
||||
|
||||
return () => h("div", { ref: el, class: "w-full", style: styles.value });
|
||||
},
|
||||
});
|
||||
|
||||
@@ -293,10 +293,10 @@ export class TradeWebSocket {
|
||||
/**
|
||||
* 订阅消息
|
||||
*/
|
||||
public subscribe(handler: (data: MessageData) => void): string {
|
||||
const id = `handler_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
this.messageHandlers.push({ id, handler });
|
||||
return id;
|
||||
public subscribe(handler: (data: MessageData) => void, id?: string): string {
|
||||
const _id = id ?? `handler_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
||||
this.messageHandlers.push({ id: _id, handler });
|
||||
return _id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user