first step to automated closing

This commit is contained in:
Giulio De Pasquale 2020-11-27 18:13:06 +00:00
parent 8ce606a8c4
commit aa4db10d5b

89
main.py
View File

@ -36,41 +36,15 @@ class EventKind(Enum):
class Event():
def __init__(self, kind: EventKind, tick: int) -> None:
def __init__(self, kind: EventKind, tick: int, position: Position) -> None:
self.kind: EventKind = kind
self.tick: int = tick
self.position: Position = position
def __repr__(self) -> str:
return f"{self.kind.name} @ Tick {self.tick}"
class EventHandler:
def __init__(self):
self.handlers = {}
def call(self, event: Event):
value = event.kind.value
if value in self.handlers:
for h in self.handlers[value]:
h(event)
def on_event(self, kind: EventKind):
value = kind.value
def registerhandler(handler):
if value in self.handlers:
self.handlers[value].append(handler)
else:
self.handlers[value] = [handler]
return handler
return registerhandler
class PositionStatus():
def __init__(self, position: Position) -> None:
self.position: Position = position
class State(Enum):
CRITICAL = -1,
LOSS = 0,
@ -121,12 +95,14 @@ class Printer():
class Status():
def __init__(self, tick_duration, symbol):
def __init__(self, tick_duration, symbol, printer):
self.ticker: Ticker = Ticker(tick_duration)
self.events: list[Event] = []
self.symbol = symbol
self.ticks: dict[int, float] = {}
self.current_state: State = State.LOSS
self.printer: Printer = printer
self.stop_percentage = 999.0
async def wait(self) -> None:
sleep(self.ticker.seconds)
@ -168,13 +144,35 @@ class Status():
self.get_current_tick())
self.events.append(event)
eh.call(event)
eh.call(event, self)
self.current_state = state
def get_current_state(self) -> State:
return self.current_state
class EventHandler:
def __init__(self):
self.handlers = {}
def call(self, event: Event, status: Status):
value = event.kind.value
if value in self.handlers:
for h in self.handlers[value]:
h(event, status)
def on_event(self, kind: EventKind):
value = kind.value
def registerhandler(handler):
if value in self.handlers:
self.handlers[value].append(handler)
else:
self.handlers[value] = [handler]
return handler
return registerhandler
dotenv.load()
API_KEY = dotenv.get('API_KEY', default='')
API_SECRET = dotenv.get('API_SECRET', default='')
@ -193,16 +191,31 @@ MIN_PROFIT_PERC = 0.65
GOOD_PROFIT_PERC = MIN_PROFIT_PERC * 2.1
MAX_LOSS_PERC = -4.0
TRAILING_GOOD_PROFIT_PERC = 0.2
TRAIL_STOP_PERCENTAGES = {
EventKind.REACHED_MIN_PROFIT: 0.2,
EventKind.REACHED_GOOD_PROFIT: 0.1
}
@eh.on_event(EventKind.REACHED_GOOD_PROFIT)
def kaching(event):
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")
@eh.on_event(EventKind.REACHED_MIN_PROFIT)
def kaching(event):
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/letsgo.mp3")
@ -216,7 +229,7 @@ async def main(screen: Screen):
symbol = "tBTCUSD"
printer = Printer(screen)
status = Status(20, symbol)
status = Status(20, symbol, printer)
while True:
positions = [p for p in await bfx.get_active_position() if p.symbol == status.symbol]
@ -326,7 +339,7 @@ def plot(status: Status, printer: Printer):
y = [x for x in status.ticks.values()]
figure.plot(x, y, width=printer.screen.width,
height=printer.screen.height - printer.get_current_line() - 1)
height=printer.screen.height - printer.get_current_line())
printer.print_next_line(figure.get_string())
@ -340,11 +353,5 @@ def clear():
system("clear")
def screen_main():
return
if __name__ == "__main__":
asyncio.run(Screen.wrapper(main))
# asyncio.run(main())
# screen_main()