From babc492801808de47a88af55847081ebf26ee601 Mon Sep 17 00:00:00 2001 From: Alexander Clark <153824696+clarkaj5@users.noreply.github.com> Date: Fri, 20 Sep 2024 11:55:02 +0000 Subject: [PATCH] Main --- src/DataManipulator.ts | 32 ++++++++++++++++++++++---------- src/Graph.tsx | 20 +++++++++++++------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/DataManipulator.ts b/src/DataManipulator.ts index 7f622955cc..09a185baa9 100644 --- a/src/DataManipulator.ts +++ b/src/DataManipulator.ts @@ -1,20 +1,32 @@ import { ServerRespond } from './DataStreamer'; export interface Row { - stock: string, - top_ask_price: number, + price_abc: number, + price_def: number, + ratio: number, timestamp: Date, + upper_bound: number, + lower_bound: number, + trigger_alert: number | undefined, } export class DataManipulator { - static generateRow(serverResponds: ServerRespond[]) { - return serverResponds.map((el: any) => { + static generateRow(serverRespond: ServerRespond[]) { + const priceABC = (serverRespond[0].top_ask.price + serverRespond[0].top_bid.price) / 2; + const priceDEF = (serverRespond[1].top_ask.price + serverRespond[1].top_bid.price) / 2; + const ratio = priceABC / priceDEF; + const upperBound = 1 + 0.05; + const lowerBound = 1 - 0.05; return { - stock: el.stock, - top_ask_price: el.top_ask && el.top_ask.price || 0, - timestamp: el.timestamp, + price_abc : priceABC , + price_def : priceDEF , + ratio, + timestamp: serverRespond[0].timestamp > serverRespond[1].timestamp ? + serverRespond[0].timestamp : serverRespond[1].timestamp, + upper_bound : upperBound, + lower_bound : lowerBound, + trigger_alert : (ratio > upperBound || ratio < lowerBound) ? ratio : undefined, }; - }) - } -} + } + } \ No newline at end of file diff --git a/src/Graph.tsx b/src/Graph.tsx index 277797d933..4bca56a5b2 100644 --- a/src/Graph.tsx +++ b/src/Graph.tsx @@ -1,5 +1,5 @@ import React, { Component } from 'react'; -import { Table } from '@finos/perspective'; +import { Table, TableData } from '@finos/perspective'; import { ServerRespond } from './DataStreamer'; import { DataManipulator } from './DataManipulator'; import './Graph.css'; @@ -23,10 +23,13 @@ class Graph extends Component { const elem = document.getElementsByTagName('perspective-viewer')[0] as unknown as PerspectiveViewerElement; const schema = { - stock: 'string', - top_ask_price: 'float', - top_bid_price: 'float', + price_abc : 'float', + price_def: 'float', + ratio: 'float', timestamp: 'date', + upper_bound: 'float', + lower_bound:'float', + trigger_alert:'float', }; if (window.perspective && window.perspective.worker()) { @@ -44,15 +47,18 @@ class Graph extends Component { top_ask_price: 'avg', top_bid_price: 'avg', timestamp: 'distinct count', + upper_bound: 'avg', + lower_bound: 'avg', + trigger_alert: 'avg', })); } } componentDidUpdate() { if (this.table) { - this.table.update( - DataManipulator.generateRow(this.props.data), - ); + this.table.update([ + DataManipulator.generateRow(this.props.data), + ]as unknown as TableData) ; } } }