2020-12-14 18:52:43 +00:00
|
|
|
import React, {Component} from "react"
|
2020-12-14 20:26:14 +00:00
|
|
|
import {Badge, Button, Container, Table} from "react-bootstrap"
|
2020-12-16 09:36:42 +00:00
|
|
|
import {NewTickData, PositionState, socket} from '../';
|
2020-12-14 14:50:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
export class PositionTable extends Component<{}, { positions: Array<PositionState> }> {
|
2020-12-14 18:52:43 +00:00
|
|
|
state = {
|
2020-12-16 09:36:42 +00:00
|
|
|
positions: []
|
2020-12-14 18:52:43 +00:00
|
|
|
}
|
2020-12-14 14:50:31 +00:00
|
|
|
|
2020-12-14 18:52:43 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
}
|
2020-12-14 14:50:31 +00:00
|
|
|
|
2020-12-14 20:26:14 +00:00
|
|
|
stateVariantFromStr(state: string): string {
|
|
|
|
const lower_state = state.toLowerCase()
|
|
|
|
let res: string;
|
|
|
|
|
|
|
|
if (lower_state.includes("profit")) {
|
|
|
|
res = "success"
|
|
|
|
} else if (lower_state.includes("break")) {
|
|
|
|
res = "primary"
|
|
|
|
} else {
|
|
|
|
res = "danger"
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
plColorFromStr(amount: number): string {
|
|
|
|
if (amount > 0) {
|
|
|
|
return "success"
|
|
|
|
} else {
|
|
|
|
return "danger"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 18:52:43 +00:00
|
|
|
componentDidMount() {
|
2020-12-16 09:36:42 +00:00
|
|
|
socket.on('new_tick', (data: NewTickData) => {
|
|
|
|
this.setState({
|
|
|
|
positions: data.positions
|
|
|
|
})
|
2020-12-14 18:52:43 +00:00
|
|
|
})
|
|
|
|
}
|
2020-12-14 14:50:31 +00:00
|
|
|
|
2020-12-14 18:52:43 +00:00
|
|
|
tableData() {
|
2020-12-14 19:04:04 +00:00
|
|
|
return this.state.positions.map((position: PositionState) => {
|
2020-12-14 18:52:43 +00:00
|
|
|
return (<tr key={position.id}>
|
2020-12-14 20:26:14 +00:00
|
|
|
<td className={"align-middle"}><Badge
|
|
|
|
variant={this.stateVariantFromStr(position.state)}>{position.state}</Badge></td>
|
|
|
|
<td className={"align-middle"}>{position.symbol}</td>
|
|
|
|
<td className={"align-middle"}>{position.base_price.toFixed(2)}</td>
|
2020-12-16 09:44:25 +00:00
|
|
|
<td className={"align-middle"}>{position.amount.toFixed(5)}</td>
|
2020-12-14 20:26:14 +00:00
|
|
|
<td className={"align-middle"}>{position.profit_loss.toFixed(2)}</td>
|
|
|
|
<td className={"align-middle"}>{position.profit_loss_percentage.toFixed(2)} %</td>
|
2020-12-16 09:36:42 +00:00
|
|
|
<td className={"align-middle"}><Button size={"sm"} variant={"danger"} onClick={() => {
|
|
|
|
socket.emit("close", ({"id": position.id}))
|
|
|
|
}}>Close</Button></td>
|
2020-12-14 18:52:43 +00:00
|
|
|
</tr>)
|
|
|
|
})
|
|
|
|
}
|
2020-12-14 14:50:31 +00:00
|
|
|
|
2020-12-14 18:52:43 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Container className="d-flex flex-column mt-2">
|
|
|
|
<div className="border-bottom">
|
|
|
|
<h2>Open positions</h2>
|
|
|
|
</div>
|
2020-12-14 20:26:14 +00:00
|
|
|
<Table id="positions" size="sm" hover striped bordered className="mt-2 text-center align-middle">
|
2020-12-14 18:52:43 +00:00
|
|
|
<thead>
|
|
|
|
<tr>
|
2020-12-14 20:26:14 +00:00
|
|
|
<th>State</th>
|
2020-12-14 18:52:43 +00:00
|
|
|
<th>Symbol</th>
|
2020-12-14 19:04:04 +00:00
|
|
|
<th>Base price</th>
|
2020-12-16 09:44:25 +00:00
|
|
|
<th>Amount</th>
|
2020-12-14 18:52:43 +00:00
|
|
|
<th>P/L</th>
|
|
|
|
<th>P/L %</th>
|
2020-12-14 20:26:14 +00:00
|
|
|
<th>Action</th>
|
2020-12-14 18:52:43 +00:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{this.tableData()}
|
|
|
|
</tbody>
|
|
|
|
</Table>
|
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|
2020-12-14 14:50:31 +00:00
|
|
|
}
|