renamed main to bfxbox. minor cleanup

This commit is contained in:
Giulio De Pasquale 2020-11-28 18:07:04 +00:00
parent 07eba48250
commit ffdfd6fb9c

62
main.py → bfxbot.py Normal file → Executable file
View File

@ -1,22 +1,20 @@
#!/usr/bin/env python #!/usr/bin/env python
import asyncio import asyncio
from asyncio import events import shutil
from hashlib import blake2b
import time import time
from enum import Enum from enum import Enum
from os import system, terminal_size from os import system
from time import sleep from time import sleep
from bfxapi import Client from typing import Dict, List
from bfxapi.models.order import Order
from bfxapi.models.position import Position
from termcolor import colored
import shutil
import termplotlib
import numpy
from playsound import playsound
from asciimatics.screen import ManagedScreen, Screen
import dotenv import dotenv
import termplotlib
from asciimatics.screen import Screen
from bfxapi import Client
from bfxapi.models.position import Position
from playsound import playsound
from termcolor import colored
class Ticker(): class Ticker():
@ -100,9 +98,9 @@ class Printer():
class Status(): class Status():
def __init__(self, tick_duration, symbol, printer): def __init__(self, tick_duration, symbol, printer):
self.ticker: Ticker = Ticker(tick_duration) self.ticker: Ticker = Ticker(tick_duration)
self.events: list[Event] = [] self.events: List[Event] = []
self.symbol = symbol self.symbol = symbol
self.ticks: dict[int, (float, Position)] = {} self.ticks: Dict[int, (float, Position)] = {}
self.current_state: State = State.LOSS self.current_state: State = State.LOSS
self.printer: Printer = printer self.printer: Printer = printer
self.stop_percentage: float = None self.stop_percentage: float = None
@ -128,13 +126,11 @@ class Status():
eh.call_event(event, self) eh.call_event(event, self)
async def last_price(self) -> float: async def last_price(self) -> float:
if self.get_current_tick() not in self.ticks.keys(): return await get_current_price(self.symbol)
return await get_current_price(self.symbol)
self.ticks[self.get_current_tick()]
def set_state(self, state: State): def set_state(self, state: State):
if self.current_state != state: if self.current_state != state:
event: EventKind = None event: Event = None
if state == State.CRITICAL: if state == State.CRITICAL:
event = Event(EventKind.REACHED_MAX_LOSS, event = Event(EventKind.REACHED_MAX_LOSS,
@ -187,6 +183,7 @@ class EventHandler:
else: else:
self.event_handlers[value] = [handler] self.event_handlers[value] = [handler]
return handler return handler
return registerhandler return registerhandler
def on_state(self, state: State): def on_state(self, state: State):
@ -196,6 +193,7 @@ class EventHandler:
else: else:
self.state_handlers[state] = [handler] self.state_handlers[state] = [handler]
return handler return handler
return registerhandler return registerhandler
@ -278,7 +276,7 @@ def update_stop_percentage(state: State, status: Status):
status.add_event(Event(EventKind.TRAILING_STOP_SET, status.add_event(Event(EventKind.TRAILING_STOP_SET,
status.get_current_tick())) status.get_current_tick()))
status.stop_percentage = last_pl_net_perc - \ status.stop_percentage = last_pl_net_perc - \
TRAIL_STOP_PERCENTAGES[state] TRAIL_STOP_PERCENTAGES[state]
return return
# moving trailing stop # moving trailing stop
@ -286,14 +284,14 @@ def update_stop_percentage(state: State, status: Status):
status.add_event(Event(EventKind.TRAILING_STOP_MOVED, status.add_event(Event(EventKind.TRAILING_STOP_MOVED,
status.get_current_tick())) status.get_current_tick()))
status.stop_percentage = last_pl_net_perc - \ status.stop_percentage = last_pl_net_perc - \
TRAIL_STOP_PERCENTAGES[state] TRAIL_STOP_PERCENTAGES[state]
return return
@eh.on_event(EventKind.CLOSE_POSITION) @eh.on_event(EventKind.CLOSE_POSITION)
def on_close_position(event: Event, status: Status): def on_close_position(event: Event, status: Status):
status.printer.print_next_line("I WOULD HAVE CLOSED THE POSITION HERE"*100) status.printer.print_next_line("I WOULD HAVE CLOSED THE POSITION HERE" * 100)
def net_pl_percentage(perc: float, reference_fee_perc: float): def net_pl_percentage(perc: float, reference_fee_perc: float):
@ -316,13 +314,15 @@ async def main(screen: Screen):
current_price = await status.last_price() current_price = await status.last_price()
screen.clear() screen.clear()
printer.print_next_line("Balance: ${} | Current {} price: {} | Current tick ({} sec): {}".format(colored_float(balance, "white"), printer.print_next_line(
symbol, "Balance: ${} | Current {} price: {} | Current tick ({} sec): {}".format(colored_float(balance, "white"),
colored_float( symbol,
current_price, "white", attrs=["bold"]), colored_float(
status.ticker.seconds, current_price, "white",
status.get_current_tick(), attrs=["bold"]),
)) status.ticker.seconds,
status.get_current_tick(),
))
if positions: if positions:
printer.print_next_line("") printer.print_next_line("")
@ -337,11 +337,11 @@ async def main(screen: Screen):
if plfees_percentage > GOOD_PROFIT_PERC: if plfees_percentage > GOOD_PROFIT_PERC:
status.set_state(State.PROFIT) status.set_state(State.PROFIT)
elif plfees_percentage >= MIN_PROFIT_PERC and plfees_percentage < GOOD_PROFIT_PERC: elif MIN_PROFIT_PERC <= plfees_percentage < GOOD_PROFIT_PERC:
status.set_state(State.MINIMUM_PROFIT) status.set_state(State.MINIMUM_PROFIT)
elif plfees_percentage >= 0.0 and plfees_percentage < MIN_PROFIT_PERC: elif 0.0 <= plfees_percentage < MIN_PROFIT_PERC:
status.set_state(State.BREAK_EVEN) status.set_state(State.BREAK_EVEN)
elif plfees_percentage < 0.0 and plfees_percentage > MAX_LOSS_PERC: elif MAX_LOSS_PERC < plfees_percentage < 0.0:
status.set_state(State.LOSS) status.set_state(State.LOSS)
else: else:
status.set_state(State.CRITICAL) status.set_state(State.CRITICAL)