2020-12-14 18:52:43 +00:00
|
|
|
import React, {Component} from "react"
|
2020-12-13 19:32:33 +00:00
|
|
|
import Plot from "react-plotly.js"
|
|
|
|
|
2020-12-14 18:52:43 +00:00
|
|
|
import {NewTickData, socket} from '../';
|
2020-12-13 19:32:33 +00:00
|
|
|
|
|
|
|
|
2020-12-13 20:40:55 +00:00
|
|
|
type FirstConnectData = {
|
|
|
|
ticks: Array<number>,
|
|
|
|
prices: Array<number>
|
|
|
|
}
|
2020-12-13 19:32:33 +00:00
|
|
|
|
2020-12-14 15:37:12 +00:00
|
|
|
|
|
|
|
type PriceLine = {
|
|
|
|
x0: number,
|
|
|
|
y0: number,
|
|
|
|
x1: number,
|
|
|
|
y1: number
|
|
|
|
}
|
|
|
|
|
2020-12-13 19:32:33 +00:00
|
|
|
type PlotState = {
|
|
|
|
x: Array<number>,
|
2020-12-14 15:37:12 +00:00
|
|
|
y: Array<number>,
|
2020-12-16 09:40:45 +00:00
|
|
|
current_price_line: PriceLine,
|
2020-12-16 10:08:11 +00:00
|
|
|
positions_price_lines: Array<PriceLine>,
|
2020-12-13 19:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class RPlot extends Component<{}, PlotState> {
|
|
|
|
state = {
|
|
|
|
x: [],
|
2020-12-14 15:37:12 +00:00
|
|
|
y: [],
|
2020-12-16 09:40:45 +00:00
|
|
|
current_price_line: {x0: 0, x1: 0, y0: 0, y1: 0},
|
2020-12-16 10:08:11 +00:00
|
|
|
positions_price_lines: []
|
2020-12-14 18:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2020-12-13 19:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-12-13 20:40:55 +00:00
|
|
|
socket.on("first_connect", (data: FirstConnectData) => {
|
2020-12-14 19:04:04 +00:00
|
|
|
const last_tick = data.ticks[data.ticks.length - 1];
|
|
|
|
const last_price = data.prices[data.prices.length - 1];
|
2020-12-14 15:37:12 +00:00
|
|
|
|
2020-12-13 20:40:55 +00:00
|
|
|
this.setState({
|
|
|
|
x: data.ticks,
|
2020-12-14 15:37:12 +00:00
|
|
|
y: data.prices,
|
2020-12-16 09:40:45 +00:00
|
|
|
current_price_line: {
|
2020-12-14 15:37:12 +00:00
|
|
|
x0: 0,
|
|
|
|
y0: last_price,
|
|
|
|
x1: last_tick,
|
|
|
|
y1: last_price
|
2020-12-14 16:02:01 +00:00
|
|
|
},
|
2020-12-13 20:40:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-12-13 19:32:33 +00:00
|
|
|
socket.on("new_tick", (data: NewTickData) => {
|
2020-12-14 19:04:04 +00:00
|
|
|
const new_x = [...this.state.x, data.tick];
|
|
|
|
const new_y = [...this.state.y, data.price];
|
2020-12-14 13:14:32 +00:00
|
|
|
|
|
|
|
// cutting to up to 500 entries (last 500)
|
2020-12-14 19:04:04 +00:00
|
|
|
const x = new_x.slice(Math.max(new_x.length - 500, 0));
|
|
|
|
const y = new_y.slice(Math.max(new_y.length - 500, 0));
|
2020-12-14 13:14:32 +00:00
|
|
|
|
2020-12-16 10:08:11 +00:00
|
|
|
const position_price_lines = data.positions.map((pstat): PriceLine => {
|
|
|
|
return {
|
|
|
|
x0: 0,
|
|
|
|
y0: pstat.base_price,
|
|
|
|
x1: data.tick,
|
|
|
|
y1: pstat.base_price
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-12-13 19:32:33 +00:00
|
|
|
this.setState({
|
2020-12-14 13:14:32 +00:00
|
|
|
x: x,
|
|
|
|
y: y,
|
2020-12-16 09:40:45 +00:00
|
|
|
current_price_line: {
|
2020-12-14 15:37:12 +00:00
|
|
|
x0: 0,
|
|
|
|
y0: data.price,
|
|
|
|
x1: data.tick,
|
|
|
|
y1: data.price
|
2020-12-14 16:02:01 +00:00
|
|
|
},
|
2020-12-16 10:08:11 +00:00
|
|
|
positions_price_lines: position_price_lines
|
2020-12-13 19:32:33 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-12-16 10:08:11 +00:00
|
|
|
let additional_shapes = []
|
|
|
|
|
|
|
|
if (this.state.positions_price_lines.length > 0) {
|
|
|
|
additional_shapes = this.state.positions_price_lines.map((priceline: PriceLine) => {
|
|
|
|
return {
|
|
|
|
type: 'line',
|
|
|
|
x0: priceline.x0,
|
|
|
|
y0: priceline.y0,
|
|
|
|
x1: priceline.x1,
|
|
|
|
y1: priceline.y1,
|
|
|
|
line: {
|
|
|
|
color: 'rgb(1, 1, 1)',
|
|
|
|
width: 1,
|
|
|
|
dash: 'solid'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-13 19:32:33 +00:00
|
|
|
return (
|
|
|
|
<Plot
|
|
|
|
data={[
|
|
|
|
{
|
|
|
|
x: this.state.x,
|
|
|
|
y: this.state.y,
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'lines+markers',
|
|
|
|
},
|
|
|
|
]}
|
2020-12-14 15:37:12 +00:00
|
|
|
layout={{
|
2020-12-14 16:02:01 +00:00
|
|
|
margin: {
|
|
|
|
l: 50,
|
|
|
|
r: 50,
|
|
|
|
b: 50,
|
|
|
|
t: 50,
|
|
|
|
pad: 4
|
|
|
|
},
|
|
|
|
dragmode: "pan",
|
|
|
|
shapes: [
|
2020-12-14 15:37:12 +00:00
|
|
|
{
|
|
|
|
type: 'line',
|
2020-12-16 09:40:45 +00:00
|
|
|
x0: this.state.current_price_line.x0,
|
|
|
|
y0: this.state.current_price_line.y0,
|
|
|
|
x1: this.state.current_price_line.x1,
|
|
|
|
y1: this.state.current_price_line.y1,
|
2020-12-14 15:37:12 +00:00
|
|
|
line: {
|
|
|
|
color: 'rgb(50, 171, 96)',
|
|
|
|
width: 2,
|
|
|
|
dash: 'dashdot'
|
|
|
|
}
|
|
|
|
},
|
2020-12-16 10:08:11 +00:00
|
|
|
].concat(additional_shapes),
|
2020-12-14 16:02:01 +00:00
|
|
|
xaxis: {
|
|
|
|
title: {
|
|
|
|
text: 'Tick',
|
|
|
|
font: {
|
|
|
|
family: 'Courier New, monospace',
|
|
|
|
size: 18,
|
|
|
|
color: '#7f7f7f'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
yaxis: {
|
|
|
|
title: {
|
|
|
|
text: 'Price',
|
|
|
|
font: {
|
|
|
|
family: 'Courier New, monospace',
|
|
|
|
size: 18,
|
|
|
|
color: '#7f7f7f'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tickformat: 'r',
|
|
|
|
}
|
2020-12-14 15:37:12 +00:00
|
|
|
}}
|
2020-12-13 19:32:33 +00:00
|
|
|
config={{
|
|
|
|
scrollZoom: true,
|
|
|
|
displayModeBar: false,
|
|
|
|
responsive: true,
|
|
|
|
}}
|
2020-12-14 18:52:43 +00:00
|
|
|
style={{width: '100%', height: '90%'}}
|
2020-12-13 19:32:33 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2020-12-16 10:08:11 +00:00
|
|
|
|
2020-12-13 19:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default RPlot;
|