const TradingCup = () => { const cupWorkerRef =… · The Chat Archive
Explore gpt-3.5-turbo-0613 · OpenAI WildChat-4.8M (AI2)const TradingCup = () => { const cupWorkerRef = useRef<Worker>(); const… Save Share Download ▾ Reportconst TradingCup = () => {
const cupWorkerRef = useRef();
const orderFeedWorkerRef = useRef();
const symbol = useSelector((state: AppState) => state.screenerSlice.symbol);
useEffect(() => {
const channel = new MessageChannel();
cupWorkerRef.current?.postMessage({type: “set_port”, port: channel.port1}, [channel.port1]);
orderFeedWorkerRef.current?.postMessage({type: “set_port”, port: channel.port2}, [channel.port2]);
}, []);
return
export default TradingCup;
const Cup = ({workerRef, symbol,
}: CupProps) => {
const cupParams = useSelector((state: AppState) => state.cupSlice);
const [dpiScale, setDpiScale] = useState(Math.ceil(window.devicePixelRatio));
const [canvasSize, setCanvasSize] = useState({height: 0, width: 0});
const containerRef = useRef<HTMLDivElement|null>(null);
const canvasRef = useRef<HTMLCanvasElement|null>(null);
const [zoom, setZoom] = useState(1);
const size = useComponentResizeListener(canvasRef);
const dispatch = useDispatch();
const {diaryToken} = useAuthContext();
const {selectedSingleApiKey} = useApiKeyProvider();
const {enqueueSnackbar} = useSnackbar();
const cupSubscribe = useCallback(async(pair: string, zoom: number) => {
workerRef.current?.postMessage(JSON.stringify({type: “subscribe”, pair, zoom}));
}, []);
const cupUnsubscribe = useCallback(async(pair: string) => {
workerRef.current?.postMessage(JSON.stringify({type: “unsubscribe”, pair}));
}, []);
const wheelHandler = (e: WheelEvent) => {
e.preventDefault();
workerRef.current?.postMessage(JSON.stringify({type: e.deltaY < 0 ? “camera_up” : “camera_down”}));
};
const zoomAdd = () => {
let newZoom;
if (zoom >= 1 && zoom < 10) {
newZoom = zoom + 1;
} else if (zoom >= 10 && zoom < 30) {
newZoom = zoom + 5;
}
setZoom(newZoom);
};
const zoomSub = () => {
let newZoom;
if (zoom > 1 && zoom <= 10) {
newZoom = zoom - 1;
} else if (zoom > 10 && zoom <= 30) {
newZoom = zoom - 5;
}
setZoom(newZoom);
};
useEffect(() => {
workerRef.current = new Worker(new URL(“/workers/cup-builder.ts”, import.meta.url));
canvasRef.current?.addEventListener(“wheel”, wheelHandler, {passive: false});
return () => {
workerRef.current?.terminate();
canvasRef.current?.removeEventListener(“wheel”, wheelHandler);
};
}, []);
useEffect(() => {
if (!workerRef.current) return;
let animationFrameId: number|null = null;
if (event?.data?.type === “update_cup”) {
if (null !== animationFrameId) {
cancelAnimationFrame(animationFrameId);
}
animationFrameId = requestAnimationFrame(() => {
const data = event.data as UpdateCupEvent;
const context = canvasRef.current?.getContext(“2d”);
const zoomedTickSize = data.priceStep * data.aggregation;
if (context) {
const rowsOnScreenCount = cupTools.getRowsCountOnScreen(
canvasSize.height,
cupOptions().cell.defaultHeight * dpiScale,
);
const realCellHeight = parseInt((canvasSize.height / rowsOnScreenCount).toFixed(0));
Assistant2023-08-31 15:20 UTC Copy response
Copy first prompt
Details
Model gpt-3.5-turbo-0613
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Aug 31, 2023
Added to archive Nov 17, 2026
ID 01m7h41eaqma6haapz3m970adzSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 26d8fcb61bf343c86c70b68d59421853
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
Dataset releases Not yet included in a published release.
if (data.rowsCount !== rowsOnScreenCount) {
workerRef.current?.postMessage(JSON.stringify({type: “change_rows_count”, value: rowsOnScreenCount}));
}
cupDrawer.clear(context, canvasSize);
if (cupParams.rowCount !== rowsOnScreenCount
|| cupParams.cellHeight !== realCellHeight
|| cupParams.aggregation !== data.aggregation
) {
dispatch(setCupParams({
aggregation: data.aggregation,
rowCount: rowsOnScreenCount,
cellHeight: realCellHeight,
pricePrecision: data.pricePrecision,
priceStep: data.priceStep,
quantityPrecision: data.quantityPrecision,
}));
}
if (data.camera !== 0) {
cupDrawer.draw(
context,
canvasSize,
dpiScale,
data.bestBidPrice,
data.bestAskPrice,
data.maxVolume,
data.pricePrecision,
data.quantityPrecision,
data.priceStep,
data.aggregation,
rowsOnScreenCount,
data.camera,
realCellHeight,
{
buy: parseInt((Math.floor(data.bestBidPrice / zoomedTickSize) * zoomedTickSize).toFixed(0)),
sell: parseInt((Math.ceil(data.bestAskPrice / zoomedTickSize) * zoomedTickSize).toFixed(0)),
},
darkMode,
data.volumeAsDollars,
data.cup,
);
}
}
});
}
};
return () => {
if (null !== animationFrameId) {
cancelAnimationFrame(animationFrameId);
}
};
}, [workerRef.current, canvasSize, darkMode, dpiScale, isLoaded, quantity]);
useEffect(() => {
cupSubscribe(symbol.toUpperCase(), zoom);
return () => {
cupUnsubscribe(symbol.toUpperCase());
};
}, [symbol, zoom]);
return
<canvas
ref={canvasRef}
className={[styles.canvas, isLoaded ? “” : styles.loading].join(" ")}
width={canvasSize?.width}
height={canvasSize?.height}
/>
import {CupItem} from “…/hooks/rustWsServer”;
import {MessagePort} from “worker_threads”;
let cup: {[key: number]: CupItem} = {};
let publisherIntervalId: any = null;
let wsConnection: WebSocket|null = null;
let cameras: {[key: string]: number} = {};
let rowsCount: number = 60;
let quantityDivider = 1;
let priceDivider = 1;
let diffModifier = 2;
const connectToWs = (callback: () => void) => {
if (wsConnection) {
callback();
wsConnection = new WebSocket(${process.env.NEXT_PUBLIC_RUST_WS_SERVER});
wsConnection.onopen = () => {
if (null !== wsConnection) {
callback();
}
};
wsConnection.onmessage = async(message: MessageEvent) => {
if (!message.data) return;
const data = JSON.parse(message.data);
if (!data?.commands || data.commands.length === 0) return;
const exchangeInitial = data?.commands?.find((item: any) => “ExchangeInitial” in item);
if (exchangeInitial) {
cup = exchangeInitial.ExchangeInitial.rows;
pricePrecision = exchangeInitial.ExchangeInitial.params.pricePrecision;
priceStep = exchangeInitial.ExchangeInitial.params.priceStep;
quantityPrecision = exchangeInitial.ExchangeInitial.params.quantityPrecision;
quantityDivider = Math.pow(10, quantityPrecision);
priceDivider = Math.pow(10, pricePrecision);
}
};
};
const publish = () => {
if (!isSubscribed) return;
if (!cameras[pair]) {
cameras[pair] = 0;
}
const zoomedTickSize = priceStep * aggregation;
const rows: {[key: number]: CupItem} = {};
diffModifier
diffModifier
for (let index = 0; index <= rowsCount; index++) {
const microPrice = startMicroPrice - index * zoomedTickSize;
if (microPrice < 0) continue;
rows[microPrice] = cup[microPrice] || {};
maxVolume
Math.max(maxVolume, (ask || bid || 0) / quantityDivider
);
port?.postMessage({type: “set_camera”, value: cameras[pair]});
postMessage({
type: “update_cup”,
cup: rows,
camera: cameras[pair],
aggregation,
bestBidPrice,
bestAskPrice,
pricePrecision,
priceStep,
quantityPrecision,
rowsCount,
maxVolume: volumeIsFixed
? fixedMaxVolume
: maxVolume,
volumeAsDollars,
});
};
const publisherStart = () => {
if (publisherIntervalId) {
clearInterval(publisherIntervalId);
}
publisherIntervalId = setInterval(publish, publisherTimeoutInMs);
};
const publisherStop = () => {
if (publisherIntervalId) {
clearInterval(publisherIntervalId);
}
};
onmessage = (event: MessageEvent) => {
const data = “string” === typeof event.data ? JSON.parse(${event.data}) : event.data;
if (data && data?.type === “subscribe”) {
pair = data.pair;
aggregation = data.zoom;
isSubscribed = true;
cameras[pair] = 0;
maxVolume = 0;
cup = {};
publisherStart();
if (wsConnection?.readyState === 3) {
wsConnection = null;
}
connectToWs(() => {
if (null === wsConnection) return;
wsConnection.send(JSON.stringify({
“commands”: [
{
commandType: “SUBSCRIBE_SYMBOL”,
exchange: FT:${pair},
aggregation: aggregation,
},
],
}));
});
}
if (data && data?.type === “unsubscribe”) {
isSubscribed = false;
cameras[pair] = 0;
pair = “”;
cup = {};
publisherStop();
if (null !== wsConnection) {
wsConnection.send(JSON.stringify({
“commands”: [
{
commandType: “UNSUBSCRIBE_SYMBOL”,
exchange: FT:${data.pair},
},
],
}));
}
}
if (data && data?.type === “change_publisher_timeout”) {
publisherTimeoutInMs = data.value;
publisherStart();
}
if (data && data?.type === “set_port”) {
port = data.port;
}
};
Нужно создать новую табличку, где отображать эти данные. Сделать три колонки (монета, цена, кол-во).
wsConnection.send(JSON.stringify({
“commands”: [
{
commandType: “SUBSCRIBE_BIG_ORDERS”,
exchange: FT:${symbol},
},
],
}));
const bigOrder = data.commands.find((item: any) => “undefined” !== typeof item.BigOrder);
const tradeSymbol = bigOrder.BigOrder.exchange.replace(“FT:”, “”).toUpperCase();
if (tradeSymbol === symbol) {
price = bigOrder.BigOrder.price;
quantity = bigOrder.BigOrder.quantity;
}
}
Нужно создать новый компонент BigOrders и новый воркер для таблички big-order-builder
const BigOrders = ({ workerRef, symbol }: BigOrdersProps) => {
const [orders, setOrders] = useState<BigOrder[]>([]);
useEffect(() => {
workerRef.current = new Worker(new URL("/workers/big-order-builder.ts", import.meta.url));
workerRef.current.onmessage = (event) => {
const data = event.data;
if (data && data.type === "update_orders") {
setOrders(data.orders);
}
};
return () => {
workerRef.current?.terminate();
};
}, []);
useEffect(() => {
const fetchData = async () => {
const response = await fetch(/api/big-orders?symbol=${symbol});
const data = await response.json();
if (data && Array.isArray(data)) {
workerRef.current?.postMessage({
type: "set_orders",
orders: data,
});
}
};
fetchData();
}, [symbol]);
return (
Монета
Цена
Кол-во
{orders.map((order) => (
{order.coin}
{order.price}
{order.quantity}
))}
);
};
export default BigOrders;
// big-order-builder.ts
import { MessagePort } from "worker_threads";
let orders: BigOrder[] = [];
const updateOrders = () => {
postMessage({
type: "update_orders",
orders,
});
};
onmessage = (event: MessageEvent) => {
const data = event.data;
if (data && data.type === "set_orders") {
orders = data.orders;
updateOrders();
}
};
export {};
// TradingCup.tsx
const TradingCup = () => {
const cupWorkerRef = useRef();
const orderFeedWorkerRef = useRef();
const symbol = useSelector((state: AppState) => state.screenerSlice.symbol);
useEffect(() => {
const channel = new MessageChannel();
cupWorkerRef.current?.postMessage({ type: "set_port", port: channel.port1 }, [channel.port1]);
orderFeedWorkerRef.current?.postMessage({ type: "set_port", port: channel.port2 }, [channel.port2]);
orderFeedWorkerRef.current = new Worker(new URL("/workers/order-feed-builder.ts", import.meta.url));
}, []);
return (
);
};
export default TradingCup;
Copy first response