From 8b826d34349f51bc59d22d7e44e00c7aba74fb8f Mon Sep 17 00:00:00 2001 From: Ayan Chakraborty Date: Tue, 31 Jan 2023 18:50:54 +0530 Subject: [PATCH] Modified code --- src/DataManipulator.ts | 37 +++++++++++++++++++++++++++---------- src/Graph.tsx | 27 +++++++++++++++++---------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/DataManipulator.ts b/src/DataManipulator.ts index 7f622955cc..4d0876a5ce 100644 --- a/src/DataManipulator.ts +++ b/src/DataManipulator.ts @@ -1,20 +1,37 @@ 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: number | undefined, } export class DataManipulator { - static generateRow(serverResponds: ServerRespond[]) { - return serverResponds.map((el: any) => { - return { - stock: el.stock, - top_ask_price: el.top_ask && el.top_ask.price || 0, - timestamp: el.timestamp, - }; - }) + static generateRow(serverResponds: ServerRespond[]) : Row { + const abc = serverResponds[0] + const def = serverResponds[1] + + const price_abc = (abc.top_ask.price + abc.top_bid.price) / 2 + const price_def = (def.top_ask.price + def.top_bid.price) / 2 + const ratio = price_abc / price_def + const timestamp = (abc.timestamp > def.timestamp ? abc.timestamp : def.timestamp) + const lower_bound = 0.9 + const upper_bound = 1.1 + const trigger = (ratio > upper_bound || ratio < lower_bound ? ratio : undefined) + + return { + price_abc, + price_def, + ratio, + timestamp, + upper_bound, + lower_bound, + trigger + } } } diff --git a/src/Graph.tsx b/src/Graph.tsx index 277797d933..a2d627b929 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: 'float' }; if (window.perspective && window.perspective.worker()) { @@ -38,21 +41,25 @@ class Graph extends Component { elem.setAttribute('view', 'y_line'); elem.setAttribute('column-pivots', '["stock"]'); elem.setAttribute('row-pivots', '["timestamp"]'); - elem.setAttribute('columns', '["top_ask_price"]'); + // elem.setAttribute('columns', '["top_ask_price"]'); + elem.setAttribute('columns', '["ratio", "lower_bound", "upper_bound", "trigger"]'); elem.setAttribute('aggregates', JSON.stringify({ - stock: 'distinctcount', - top_ask_price: 'avg', - top_bid_price: 'avg', + price_abc: 'avg', + price_def: 'avg', + ratio: 'avg', timestamp: 'distinct count', + upper_bound: 'avg', + lower_bound: 'avg', + trigger: 'avg' })); } } componentDidUpdate() { if (this.table) { - this.table.update( + this.table.update([ DataManipulator.generateRow(this.props.data), - ); + ] as unknown as TableData); } } }