diff --git a/bfxbot/bfxwrapper.py b/bfxbot/bfxwrapper.py index 640d432..ba0fc31 100644 --- a/bfxbot/bfxwrapper.py +++ b/bfxbot/bfxwrapper.py @@ -206,9 +206,6 @@ class BfxWrapper(BfxRest): profit_loss = end_quote - (start_quote + movements_quote) - # print(f"Start: {start}, End: {end}") - # print(f"{start_bg} {end_bg} {movements_bg}") - profit_loss_percentage = profit_loss / (start_quote + movements_quote) * 100 diff --git a/main.py b/main.py index 906f24e..a0935d9 100755 --- a/main.py +++ b/main.py @@ -95,7 +95,7 @@ def on_get_profit_loss(message): profit_loss = loop.run_until_complete(bot.get_profit_loss(start, end)) socketio.emit("put_profit_loss", { - "pl" : profit_loss[0], + "pl": profit_loss[0], "pl_perc": profit_loss[1] }) diff --git a/websrc/components/Statusbar.tsx b/websrc/components/Statusbar.tsx index 02da075..ff48e09 100644 --- a/websrc/components/Statusbar.tsx +++ b/websrc/components/Statusbar.tsx @@ -1,5 +1,7 @@ import React, {Component} from "react"; -import {NewTickMessage} from "../types"; +import {EventName, GetProfitLossMessage, NewTickMessage, PutProfitLossMessage} from "../types"; +import {socket} from "../index"; +import {DateTime} from "luxon"; type QuoteStatusProps = { percentage?: boolean, @@ -54,7 +56,7 @@ export class QuoteStatus extends Component { <> {this.renderSign()}{this.props.quote_symbol} {this.whole.toLocaleString()} - .{this.decimal} + .{Math.abs(this.decimal)} ) } @@ -75,11 +77,13 @@ export class QuoteStatus extends Component { } type DateButtonProps = { - label: string + label: string, + onClick: any, + selected_default?: boolean } type DateButtonState = { - selected: boolean + selected: boolean, } class DateButton extends Component { @@ -88,7 +92,7 @@ class DateButton extends Component { private currentClass: string; state = { - selected: false + selected: this.props.selected_default } constructor(props) { @@ -97,28 +101,116 @@ class DateButton extends Component { this.updateClass() } - toggleState() { - this.setState({selected: !this.state.selected}) - this.updateClass() + onClick() { + this.setState({selected: !this.state.selected}, this.updateClass) + + this.props.onClick() } updateClass() { - this.currentClass = this.state.selected ? this.classSelected : this.classNotSelected; + this.currentClass = this.state.selected ? this.classSelected : this.classNotSelected } render() { return ( - ) } } -export class Statusbar extends Component { +const PeriodUnit = { + SECOND: "second", + MINUTE: "minute", + HOUR: "hour", + DAY: "day", + WEEK: "week", + MONTH: "month", + YEAR: "year" +} + +type StatusBarState = { + pl_period_unit: string, + pl_period_amount: number, + pl: number, + pl_perc: number +} + +export class Statusbar extends Component { + constructor(props) { super(props); + + this.state = { + pl_period_unit: PeriodUnit.WEEK, + pl_period_amount: 1, + pl: 0.0, + pl_perc: 0.0 + } + + this.emitGetProfitLoss = this.emitGetProfitLoss.bind(this) + } + + componentDidMount() { + socket.on(EventName.PutProfitLoss, (data: PutProfitLossMessage) => { + this.setState({ + pl: data.pl, + pl_perc: data.pl_perc + }) + }) + + socket.on(EventName.FirstConnect, this.emitGetProfitLoss) + socket.on(EventName.NewTick, this.emitGetProfitLoss) + } + + + durationObjectfromStr(str: string) { + switch (str) { + case PeriodUnit.MINUTE: + return { + minutes: this.state.pl_period_amount + } + case PeriodUnit.HOUR: + return { + hours: this.state.pl_period_amount + } + case PeriodUnit.DAY: + return { + days: this.state.pl_period_amount + } + case PeriodUnit.WEEK: + return { + weeks: this.state.pl_period_amount + } + case PeriodUnit.MONTH: + return { + months: this.state.pl_period_amount + } + case PeriodUnit.YEAR: + return { + years: this.state.pl_period_amount + } + default: + return {} + } + } + + emitGetProfitLoss() { + const message: GetProfitLossMessage = { + start: DateTime.local().minus(this.durationObjectfromStr(this.state.pl_period_unit)).toMillis(), + end: DateTime.local().toMillis() + } + + socket.emit(EventName.GetProfitLoss, message) + } + + changeProfitLossPeriod(amount: number, unit: string) { + this.setState({ + pl_period_amount: amount, + pl_period_unit: unit + }, this.emitGetProfitLoss) } render() { @@ -132,24 +224,21 @@ export class Statusbar extends Component {
- -
- - - - - - + this.changeProfitLossPeriod(1, PeriodUnit.MINUTE)}/> + this.changeProfitLossPeriod(1, PeriodUnit.DAY)}/> + this.changeProfitLossPeriod(1, PeriodUnit.WEEK)}/> + this.changeProfitLossPeriod(1, PeriodUnit.MONTH)}/> + this.changeProfitLossPeriod(1, PeriodUnit.YEAR)}/> + {/* this.changeProfitLossPeriod(1, PeriodUnit.MINUTE)}/>*/}
@@ -168,8 +257,6 @@ export class Statusbar extends Component {
{
- +
- +
diff --git a/websrc/types.ts b/websrc/types.ts index 5bb9b8f..50c56e8 100644 --- a/websrc/types.ts +++ b/websrc/types.ts @@ -25,6 +25,11 @@ export type FirstConnectMessage = { balances: Array } +export type GetProfitLossMessage = { + start: number, + end: number +} + export type NewEventMessage = { tick: number, kind: string, @@ -52,6 +57,11 @@ export type PositionProp = { profit_loss_percentage: number } +export type PutProfitLossMessage = { + pl: number, + pl_perc: number +} + /******************************* * ENUMS *******************************/ @@ -60,6 +70,8 @@ export enum EventName { NewTick = "new_tick", FirstConnect = "first_connect", NewEvent = "new_event", - ClosePosition = "close_position" + ClosePosition = "close_position", + GetProfitLoss = "get_profit_loss", + PutProfitLoss = "put_profit_loss" }