calling event handlers on add_event

This commit is contained in:
Giulio De Pasquale 2020-11-28 16:54:37 +00:00
parent 04c5279826
commit d3208835e2

28
main.py
View File

@ -125,6 +125,7 @@ class Status():
def add_event(self, event: Event):
self.events.append(event)
eh.call_event(event, self)
async def last_price(self) -> float:
if self.get_current_tick() not in self.ticks.keys():
@ -224,24 +225,12 @@ TRAIL_STOP_PERCENTAGES = {
@eh.on_event(EventKind.REACHED_GOOD_PROFIT)
def on_good_profit(event: Event, status: Status):
pos_pl_perc = net_pl_percentage(
event.position.profit_loss_percentage, TAKER_FEE)
if status.stop_percentage < pos_pl_perc:
status.printer.print_next_line("I WOULD SELL NOW!")
else:
playsound("sounds/coin.mp3")
playsound("sounds/coin.mp3")
@eh.on_event(EventKind.REACHED_MIN_PROFIT)
def on_min_profit(event: Event, status: Status):
pos_pl_perc = net_pl_percentage(
event.position.profit_loss_percentage, TAKER_FEE)
if status.stop_percentage < pos_pl_perc:
status.printer.print_next_line("I WOULD SELL NOW!")
else:
playsound("sounds/1up.mp3")
playsound("sounds/1up.mp3")
@eh.on_event(EventKind.REACHED_MAX_LOSS)
@ -286,20 +275,27 @@ def update_stop_percentage(state: State, status: Status):
# set stop percentage for first time
if not status.stop_percentage:
status.add_event(Event(EventKind.TRAILING_STOP_SET, status.get_current_tick()))
status.add_event(Event(EventKind.TRAILING_STOP_SET,
status.get_current_tick()))
status.stop_percentage = last_pl_net_perc - \
TRAIL_STOP_PERCENTAGES[state]
return
# moving trailing stop
if last_pl_net_perc - TRAIL_STOP_PERCENTAGES[state] > status.stop_percentage:
status.add_event(Event(EventKind.TRAILING_STOP_MOVED, status.get_current_tick()))
status.add_event(Event(EventKind.TRAILING_STOP_MOVED,
status.get_current_tick()))
status.stop_percentage = last_pl_net_perc - \
TRAIL_STOP_PERCENTAGES[state]
return
@eh.on_event(EventKind.CLOSE_POSITION)
def on_close_position(event: Event, status: Status):
status.printer.print_next_line("I WOULD HAVE CLOSED THE POSITION HERE"*100)
def net_pl_percentage(perc: float, reference_fee_perc: float):
return perc - reference_fee_perc