automated position closing

This commit is contained in:
Giulio De Pasquale 2020-11-29 12:11:32 +00:00
parent a775317658
commit ae1c94d9a8

View File

@ -12,7 +12,7 @@ from typing import Dict, List
import dotenv
import termplotlib
from asciimatics.screen import Screen
from bfxapi import Client
from bfxapi import Client, Order
from bfxapi.models.position import Position
from playsound import playsound
from termcolor import colored
@ -35,7 +35,8 @@ class EventKind(Enum):
REACHED_MAX_LOSS = 7,
CLOSE_POSITION = 8,
TRAILING_STOP_SET = 9,
TRAILING_STOP_MOVED = 10
TRAILING_STOP_MOVED = 10,
ORDER_SUBMITTED = 11,
class Event():
@ -219,7 +220,7 @@ 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
MAX_LOSS_PERC = -3.75
OFFER_PERC = 0.01
TRAIL_STOP_PERCENTAGES = {
@ -242,7 +243,6 @@ def on_min_profit(event: Event, status: Status):
def on_critical(event: Event, status: Status):
playsound("sounds/gameover.mp3")
@eh.on_state(State.MINIMUM_PROFIT)
def on_state_min_profit(status: Status):
update_stop_percentage(State.MINIMUM_PROFIT, status)
@ -298,8 +298,16 @@ def update_stop_percentage(state: State, status: Status):
@eh.on_event(EventKind.CLOSE_POSITION)
async def on_close_position(event: Event, status: Status):
status.printer.print_next_line(f"I WOULD HAVE CLOSED THE POSITION FOR {await calculate_best_closing_price(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()))
@eh.on_event(EventKind.ORDER_SUBMITTED)
def on_order_submitted(event: Event, status: Status):
status.printer.print_next_line("ORDER SUBMITTED!")
return
async def calculate_best_closing_price(status: Status):
p: Position = status.last_position()