From 1013b2feb55830bb74e671e27fb35c999aefbdc6 Mon Sep 17 00:00:00 2001 From: Ritish Kumar Tiwari Date: Fri, 22 May 2020 14:31:54 +0530 Subject: [PATCH 1/2] Create Patch File --- src/DataManipulator.ts | 34 ++++++++++++++++++++++++---------- src/Graph.tsx | 27 ++++++++++++++++----------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/DataManipulator.ts b/src/DataManipulator.ts index f6b15ff..d975592 100644 --- a/src/DataManipulator.ts +++ b/src/DataManipulator.ts @@ -1,20 +1,34 @@ import { ServerRespond } from './DataStreamer'; export interface Row { - stock: string, - top_ask_price: number, - timestamp: Date, + 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[]): Row[] { - return serverResponds.map((el: any) => { + static generateRow(serverRespond: ServerRespond[]): Row[] { + 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 serverRespond.map((el: any) => { 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, }; - }) - } +}) +} } diff --git a/src/Graph.tsx b/src/Graph.tsx index 58fb997..47e01ea 100644 --- a/src/Graph.tsx +++ b/src/Graph.tsx @@ -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()) { @@ -36,23 +39,25 @@ class Graph extends Component { // Load the `table` in the `` DOM reference. elem.load(this.table); elem.setAttribute('view', 'y_line'); - elem.setAttribute('column-pivots', '["stock"]'); elem.setAttribute('row-pivots', '["timestamp"]'); - elem.setAttribute('columns', '["top_ask_price"]'); + elem.setAttribute('columns', '["ratio","lower_bound","upper_bound","trigger_alert"]'); 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_alert: 'avg', })); } } componentDidUpdate() { if (this.table) { - this.table.update( - DataManipulator.generateRow(this.props.data), - ); + this.table.update([ + DataManipulator.generateRow(this.props.data), + ]); } } } From 496890c9d34c9d6228125008245cd60ac196fe6b Mon Sep 17 00:00:00 2001 From: Ritish Kumar Tiwari Date: Sun, 24 May 2020 03:40:12 +0530 Subject: [PATCH 2/2] Create Patch File --- src/DataManipulator.ts | 11 +++++------ src/Graph.tsx | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/DataManipulator.ts b/src/DataManipulator.ts index d975592..7b939bb 100644 --- a/src/DataManipulator.ts +++ b/src/DataManipulator.ts @@ -12,15 +12,14 @@ export interface Row { export class DataManipulator { - static generateRow(serverRespond: ServerRespond[]): Row[] { + static generateRow(serverRespond: ServerRespond[]): Row { 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 ratio = priceABC / priceDEF; const upperBound = 1+0.05; const lowerBound = 1-0.05; - return serverRespond.map((el: any) => { - return { - price_abc: priceABC, + return { + price_abc: priceABC, price_def: priceDEF, ratio, timestamp: serverRespond[0].timestamp> serverRespond[1].timestamp ? @@ -29,6 +28,6 @@ export class DataManipulator { lower_bound: lowerBound, trigger_alert: (ratio > upperBound || ratio < lowerBound) ? ratio : undefined, }; -}) } } + diff --git a/src/Graph.tsx b/src/Graph.tsx index 47e01ea..1574b73 100644 --- a/src/Graph.tsx +++ b/src/Graph.tsx @@ -39,7 +39,7 @@ class Graph extends Component { // Load the `table` in the `` DOM reference. elem.load(this.table); elem.setAttribute('view', 'y_line'); - elem.setAttribute('row-pivots', '["timestamp"]'); + elem.setAttribute('row-pivots','["timestamp"]'); elem.setAttribute('columns', '["ratio","lower_bound","upper_bound","trigger_alert"]'); elem.setAttribute('aggregates', JSON.stringify({ price_abc: 'avg', @@ -56,7 +56,7 @@ class Graph extends Component { componentDidUpdate() { if (this.table) { this.table.update([ - DataManipulator.generateRow(this.props.data), + DataManipulator.generateRow(this.props.data), ]); } }