added on_any_state/event and fixed close position on stop percentage

This commit is contained in:
Giulio De Pasquale 2020-11-30 20:24:59 +00:00
parent 589379dc98
commit 6c89403a03

View File

@ -162,6 +162,8 @@ class EventHandler:
def __init__(self):
self.event_handlers = {}
self.state_handlers = {}
self.any_events = []
self.any_state = []
async def call_event(self, event: Event, status: Status):
value = event.kind.value
@ -172,6 +174,12 @@ class EventHandler:
else:
h(event, status)
for h in self.any_events:
if inspect.iscoroutinefunction(h):
await h(event, status)
else:
h(event, status)
async def call_state(self, state: State, status: Status):
if state in self.state_handlers:
for h in self.state_handlers[state]:
@ -180,6 +188,12 @@ class EventHandler:
else:
h(status)
for h in self.any_state:
if inspect.iscoroutinefunction(h):
await h(status)
else:
h(status)
def on_event(self, kind: EventKind):
value = kind.value
@ -202,6 +216,17 @@ class EventHandler:
return registerhandler
def on_any_event(self):
def registerhandle(handler):
self.any_events.append(handler)
return handler
return registerhandle
def on_any_state(self):
def registerhandle(handler):
self.any_state.append(handler)
return handler
return registerhandle
dotenv.load()
API_KEY = dotenv.get('API_KEY', default='')
@ -217,7 +242,7 @@ TAKER_FEE = 0.2
MAKER_FEE = 0.1
BREAK_EVEN_PERC = TAKER_FEE
MIN_PROFIT_PERC = 0.75
MIN_PROFIT_PERC = 0.7
GOOD_PROFIT_PERC = MIN_PROFIT_PERC * 2.1
MAX_LOSS_PERC = -3.75
OFFER_PERC = 0.01
@ -243,10 +268,9 @@ def on_critical(event: Event, status: Status):
playsound("sounds/gameover.mp3")
@eh.on_state(State.MINIMUM_PROFIT)
async def on_state_min_profit(status: Status):
await update_stop_percentage(State.MINIMUM_PROFIT, status)
@eh.on_any_state()
async def call_close_on_percentage(status):
if status.stop_percentage:
current_pl_perc = net_pl_percentage(
status.last_position().profit_loss_percentage, TAKER_FEE)
@ -254,6 +278,10 @@ async def on_state_min_profit(status: Status):
await status.add_event(Event(EventKind.CLOSE_POSITION,
status.get_current_tick()))
@eh.on_state(State.MINIMUM_PROFIT)
async def on_state_min_profit(status: Status):
await update_stop_percentage(State.MINIMUM_PROFIT, status)
@eh.on_state(State.CRITICAL)
async def on_state_critical(status: Status):
@ -265,13 +293,6 @@ async def on_state_critical(status: 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:
await status.add_event(Event(EventKind.CLOSE_POSITION,
status.get_current_tick()))
async def update_stop_percentage(state: State, status: Status):
last_position = status.last_position()