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 11:27:35 +00:00
|
|
|
import {PositionCloseMessage, PositionState, socket} from '../';
|
2020-12-14 14:50:31 +00:00
|
|
|
|
|
|
|
|
2020-12-16 11:27:35 +00:00
|
|
|
export class PositionTable extends Component<{ positions: Array<PositionState> }> {
|
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
|
|
|
tableData() {
|
2020-12-16 11:27:35 +00:00
|
|
|
return this.props.positions.map((position: PositionState) => {
|
2020-12-16 11:38:58 +00:00
|
|
|
let row_bg = "";
|
|
|
|
|
|
|
|
if (!position.state.includes("break")) {
|
|
|
|
row_bg = "table-" + this.plColorFromStr(position.amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (<tr key={position.id} className={row_bg}>
|
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={() => {
|
2020-12-16 11:27:35 +00:00
|
|
|
const message: PositionCloseMessage = {
|
2020-12-16 10:18:57 +00:00
|
|
|
message_name: "close_position",
|
|
|
|
position_id: position.id,
|
|
|
|
}
|
|
|
|
socket.emit(message.message_name, (message))
|
2020-12-16 09:36:42 +00:00
|
|
|
}}>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
|
|
|
}
|