decorators and new mp3
This commit is contained in:
parent
a7e91de121
commit
8ce606a8c4
109
main.py
109
main.py
@ -17,25 +17,6 @@ from playsound import playsound
|
||||
from asciimatics.screen import ManagedScreen, Screen
|
||||
import dotenv
|
||||
|
||||
dotenv.load()
|
||||
API_KEY = dotenv.get('API_KEY', default='')
|
||||
API_SECRET = dotenv.get('API_SECRET', default='')
|
||||
|
||||
bfx = Client(
|
||||
API_KEY=API_KEY,
|
||||
API_SECRET=API_SECRET
|
||||
).rest
|
||||
|
||||
TAKER_FEE = 0.2
|
||||
MAKER_FEE = 0.1
|
||||
|
||||
BREAK_EVEN_PERC = TAKER_FEE
|
||||
MIN_PROFIT_PERC = 0.65
|
||||
GOOD_PROFIT_PERC = MIN_PROFIT_PERC * 2.1
|
||||
MAX_LOSS_PERC = -4.0
|
||||
|
||||
TRAILING_GOOD_PROFIT_PERC = 0.2
|
||||
|
||||
|
||||
class Ticker():
|
||||
def __init__(self, sec) -> None:
|
||||
@ -63,6 +44,28 @@ class Event():
|
||||
return f"{self.kind.name} @ Tick {self.tick}"
|
||||
|
||||
|
||||
class EventHandler:
|
||||
def __init__(self):
|
||||
self.handlers = {}
|
||||
|
||||
def call(self, event: Event):
|
||||
value = event.kind.value
|
||||
if value in self.handlers:
|
||||
for h in self.handlers[value]:
|
||||
h(event)
|
||||
|
||||
def on_event(self, kind: EventKind):
|
||||
value = kind.value
|
||||
|
||||
def registerhandler(handler):
|
||||
if value in self.handlers:
|
||||
self.handlers[value].append(handler)
|
||||
else:
|
||||
self.handlers[value] = [handler]
|
||||
return handler
|
||||
return registerhandler
|
||||
|
||||
|
||||
class PositionStatus():
|
||||
def __init__(self, position: Position) -> None:
|
||||
self.position: Position = position
|
||||
@ -116,6 +119,7 @@ class Printer():
|
||||
def to_current_screen_size(self):
|
||||
(self.current_width, self.current_height) = shutil.get_terminal_size()
|
||||
|
||||
|
||||
class Status():
|
||||
def __init__(self, tick_duration, symbol):
|
||||
self.ticker: Ticker = Ticker(tick_duration)
|
||||
@ -146,28 +150,62 @@ class Status():
|
||||
|
||||
def set_state(self, state: State):
|
||||
if self.current_state != state:
|
||||
if state == State.CRITICAL:
|
||||
self.add_event(
|
||||
Event(EventKind.REACHED_MAX_LOSS, self.get_current_tick()))
|
||||
elif state == State.LOSS:
|
||||
self.add_event(
|
||||
Event(EventKind.REACHED_LOSS, self.get_current_tick()))
|
||||
elif state == State.BREAK_EVEN:
|
||||
self.add_event(
|
||||
Event(EventKind.REACHED_BREAK_EVEN, self.get_current_tick()))
|
||||
elif state == State.MINIMUM_PROFIT:
|
||||
self.add_event(
|
||||
Event(EventKind.REACHED_MIN_PROFIT, self.get_current_tick()))
|
||||
elif state == State.PROFIT:
|
||||
self.add_event(
|
||||
Event(EventKind.REACHED_GOOD_PROFIT, self.get_current_tick()))
|
||||
event: EventKind = None
|
||||
|
||||
if state == State.CRITICAL:
|
||||
event = Event(EventKind.REACHED_MAX_LOSS,
|
||||
self.get_current_tick())
|
||||
elif state == State.LOSS:
|
||||
event = Event(EventKind.REACHED_LOSS, self.get_current_tick())
|
||||
elif state == State.BREAK_EVEN:
|
||||
event = Event(EventKind.REACHED_BREAK_EVEN,
|
||||
self.get_current_tick())
|
||||
elif state == State.MINIMUM_PROFIT:
|
||||
event = Event(EventKind.REACHED_MIN_PROFIT,
|
||||
self.get_current_tick())
|
||||
elif state == State.PROFIT:
|
||||
event = Event(EventKind.REACHED_GOOD_PROFIT,
|
||||
self.get_current_tick())
|
||||
|
||||
self.events.append(event)
|
||||
eh.call(event)
|
||||
self.current_state = state
|
||||
|
||||
def get_current_state(self) -> State:
|
||||
return self.current_state
|
||||
|
||||
|
||||
dotenv.load()
|
||||
API_KEY = dotenv.get('API_KEY', default='')
|
||||
API_SECRET = dotenv.get('API_SECRET', default='')
|
||||
|
||||
bfx = Client(
|
||||
API_KEY=API_KEY,
|
||||
API_SECRET=API_SECRET
|
||||
).rest
|
||||
eh = EventHandler()
|
||||
|
||||
TAKER_FEE = 0.2
|
||||
MAKER_FEE = 0.1
|
||||
|
||||
BREAK_EVEN_PERC = TAKER_FEE
|
||||
MIN_PROFIT_PERC = 0.65
|
||||
GOOD_PROFIT_PERC = MIN_PROFIT_PERC * 2.1
|
||||
MAX_LOSS_PERC = -4.0
|
||||
|
||||
TRAILING_GOOD_PROFIT_PERC = 0.2
|
||||
|
||||
|
||||
@eh.on_event(EventKind.REACHED_GOOD_PROFIT)
|
||||
def kaching(event):
|
||||
playsound("sounds/coin.mp3")
|
||||
|
||||
|
||||
@eh.on_event(EventKind.REACHED_MIN_PROFIT)
|
||||
def kaching(event):
|
||||
playsound("sounds/letsgo.mp3")
|
||||
|
||||
|
||||
def net_pl_percentage(perc: float, reference_fee_perc: float):
|
||||
return perc - reference_fee_perc
|
||||
|
||||
@ -260,9 +298,6 @@ async def main(screen: Screen):
|
||||
print_last_events(status, 10, printer)
|
||||
plot(status, printer)
|
||||
|
||||
if status.get_current_state() == State.PROFIT:
|
||||
playsound("sounds/coin.mp3")
|
||||
|
||||
printer.reset_current_line()
|
||||
await status.wait()
|
||||
|
||||
|
BIN
sounds/letsgo.mp3
Normal file
BIN
sounds/letsgo.mp3
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user