awaiting for functions in event calls, added new sound, show trail stop percentage

This commit is contained in:
Giulio De Pasquale 2020-11-30 09:13:42 +00:00
parent 77e507e204
commit b9a2345c84

View File

@ -217,14 +217,14 @@ TAKER_FEE = 0.2
MAKER_FEE = 0.1
BREAK_EVEN_PERC = TAKER_FEE
MIN_PROFIT_PERC = 0.65
MIN_PROFIT_PERC = 0.75
GOOD_PROFIT_PERC = MIN_PROFIT_PERC * 2.1
MAX_LOSS_PERC = -3.75
OFFER_PERC = 0.01
TRAIL_STOP_PERCENTAGES = {
State.MINIMUM_PROFIT: 0.2,
State.PROFIT: 0.1
State.MINIMUM_PROFIT: 0.25,
State.PROFIT: 0.125
}
@ -244,14 +244,14 @@ def on_critical(event: Event, status: Status):
@eh.on_state(State.MINIMUM_PROFIT)
def on_state_min_profit(status: Status):
update_stop_percentage(State.MINIMUM_PROFIT, status)
async def on_state_min_profit(status: Status):
await update_stop_percentage(State.MINIMUM_PROFIT, status)
current_pl_perc = net_pl_percentage(
status.last_position().profit_loss_percentage, TAKER_FEE)
if current_pl_perc < status.stop_percentage:
status.add_event(Event(EventKind.CLOSE_POSITION,
await status.add_event(Event(EventKind.CLOSE_POSITION,
status.get_current_tick()))
@ -262,25 +262,25 @@ async def on_state_critical(status: Status):
@eh.on_state(State.PROFIT)
def on_state_min_profit(status: Status):
update_stop_percentage(State.PROFIT, status)
async def on_state_min_profit(status: Status):
await update_stop_percentage(State.PROFIT, status)
current_pl_perc = net_pl_percentage(
status.last_position().profit_loss_percentage, TAKER_FEE)
if current_pl_perc < status.stop_percentage:
status.add_event(Event(EventKind.CLOSE_POSITION,
await status.add_event(Event(EventKind.CLOSE_POSITION,
status.get_current_tick()))
def update_stop_percentage(state: State, status: Status):
async def update_stop_percentage(state: State, status: Status):
last_position = status.last_position()
last_pl_net_perc = net_pl_percentage(
last_position.profit_loss_percentage, TAKER_FEE)
# set stop percentage for first time
if not status.stop_percentage:
status.add_event(Event(EventKind.TRAILING_STOP_SET,
await status.add_event(Event(EventKind.TRAILING_STOP_SET,
status.get_current_tick()))
status.stop_percentage = last_pl_net_perc - \
TRAIL_STOP_PERCENTAGES[state]
@ -288,7 +288,7 @@ def update_stop_percentage(state: State, status: Status):
# moving trailing stop
if last_pl_net_perc - TRAIL_STOP_PERCENTAGES[state] > status.stop_percentage:
status.add_event(Event(EventKind.TRAILING_STOP_MOVED,
await status.add_event(Event(EventKind.TRAILING_STOP_MOVED,
status.get_current_tick()))
status.stop_percentage = last_pl_net_perc - \
TRAIL_STOP_PERCENTAGES[state]
@ -301,9 +301,12 @@ async def on_close_position(event: Event, status: Status):
closing_price = await calculate_best_closing_price(status)
amount = status.last_position().amount * -1
await bfx.submit_order(status.symbol, closing_price, amount, Order.Type.LIMIT)
await status.add_event(Event(EventKind.ORDER_SUBMITTED, status.get_current_tick()))
open_orders = await bfx.get_active_orders(status.symbol)
if not open_orders:
await bfx.submit_order(status.symbol, closing_price, amount, Order.Type.LIMIT)
await status.add_event(Event(EventKind.ORDER_SUBMITTED, status.get_current_tick()))
playsound("sounds/goal.wav")
@eh.on_event(EventKind.ORDER_SUBMITTED)
def on_order_submitted(event: Event, status: Status):
@ -364,6 +367,9 @@ async def main(screen: Screen):
printer.print_next_line("Open {}:".format(
colored("POSITIONS", attrs=["underline"])))
if status.stop_percentage:
printer.print_next_line("Trailing stop percentage: {}".format(colored_percentage(status.stop_percentage, "yellow")))
for p in [p for p in positions if p.symbol == status.symbol]:
await status.update(p)