added closing price function

This commit is contained in:
Giulio De Pasquale 2020-11-28 19:44:08 +00:00
parent ffdfd6fb9c
commit c3c6667201

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python
import asyncio
import inspect
import shutil
import time
from enum import Enum
@ -72,7 +73,6 @@ class Printer():
return self.current_line
def next(self) -> int:
# print("Current line: {}".format(self.current_line))
line = self.current_line
self.current_line += 1
return line
@ -121,14 +121,14 @@ class Status():
def last_position(self) -> Position:
return self.ticks[self.ticker.current_tick][1]
def add_event(self, event: Event):
async def add_event(self, event: Event):
self.events.append(event)
eh.call_event(event, self)
await eh.call_event(event, self)
async def last_price(self) -> float:
return await get_current_price(self.symbol)
def set_state(self, state: State):
async def set_state(self, state: State):
if self.current_state != state:
event: Event = None
@ -149,10 +149,10 @@ class Status():
self.get_current_tick())
self.events.append(event)
eh.call_event(event, self)
await eh.call_event(event, self)
self.current_state = state
eh.call_state(self.current_state, self)
await eh.call_state(self.current_state, self)
def get_current_state(self) -> State:
return self.current_state
@ -163,16 +163,22 @@ class EventHandler:
self.event_handlers = {}
self.state_handlers = {}
def call_event(self, event: Event, status: Status):
async def call_event(self, event: Event, status: Status):
value = event.kind.value
if value in self.event_handlers:
for h in self.event_handlers[value]:
h(event, status)
if inspect.iscoroutinefunction(h):
await h(event, status)
else:
h(event, status)
def call_state(self, state: State, status: Status):
async def call_state(self, state: State, status: Status):
if state in self.state_handlers:
for h in self.state_handlers[state]:
h(status)
if inspect.iscoroutinefunction(h):
await h(status)
else:
h(status)
def on_event(self, kind: EventKind):
value = kind.value
@ -214,6 +220,7 @@ BREAK_EVEN_PERC = TAKER_FEE
MIN_PROFIT_PERC = 0.65
GOOD_PROFIT_PERC = MIN_PROFIT_PERC * 2.1
MAX_LOSS_PERC = -4.0
OFFER_PERC = 0.01
TRAIL_STOP_PERCENTAGES = {
State.MINIMUM_PROFIT: 0.2,
@ -249,8 +256,8 @@ def on_state_min_profit(status: Status):
@eh.on_state(State.CRITICAL)
def on_state_critical(status: Status):
status.add_event(Event(EventKind.CLOSE_POSITION,
async def on_state_critical(status: Status):
await status.add_event(Event(EventKind.CLOSE_POSITION,
status.get_current_tick()))
@ -290,10 +297,26 @@ def update_stop_percentage(state: State, status: Status):
@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)
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)}")
async def calculate_best_closing_price(status: Status):
p: Position = status.last_position()
is_long_pos = p.amount < 0
pub_tick = await bfx.get_public_ticker(status.symbol)
bid_price = pub_tick[0]
ask_price = pub_tick[2]
if is_long_pos:
closing_price = bid_price * (1 - OFFER_PERC / 100)
else:
closing_price = -ask_price * (1 - OFFER_PERC/100)
return closing_price
def net_pl_percentage(perc: float, reference_fee_perc: float):
return perc - reference_fee_perc
@ -336,15 +359,15 @@ async def main(screen: Screen):
p.profit_loss_percentage, TAKER_FEE)
if plfees_percentage > GOOD_PROFIT_PERC:
status.set_state(State.PROFIT)
await status.set_state(State.PROFIT)
elif MIN_PROFIT_PERC <= plfees_percentage < GOOD_PROFIT_PERC:
status.set_state(State.MINIMUM_PROFIT)
await status.set_state(State.MINIMUM_PROFIT)
elif 0.0 <= plfees_percentage < MIN_PROFIT_PERC:
status.set_state(State.BREAK_EVEN)
await status.set_state(State.BREAK_EVEN)
elif MAX_LOSS_PERC < plfees_percentage < 0.0:
status.set_state(State.LOSS)
await status.set_state(State.LOSS)
else:
status.set_state(State.CRITICAL)
await status.set_state(State.CRITICAL)
status_color = status.get_current_state().color()
@ -353,11 +376,11 @@ async def main(screen: Screen):
#
if plfees_percentage > max_perc:
max_perc = plfees_percentage
status.add_event(Event(EventKind.NEW_MAXIMUM,
await status.add_event(Event(EventKind.NEW_MAXIMUM,
status.get_current_tick()))
if plfees_percentage < min_perc:
min_perc = plfees_percentage
status.add_event(Event(EventKind.NEW_MINIMUM,
await status.add_event(Event(EventKind.NEW_MINIMUM,
status.get_current_tick()))
min_perc_colored = colored_percentage(