From 047f4cf84b6b6cf5c89c29b252be7e76579e5469 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Fri, 4 Dec 2020 15:28:21 +0000 Subject: [PATCH] screen testing --- main.py | 84 ++++++++++++++++++++--------------------------------- strategy.py | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 53 deletions(-) create mode 100644 strategy.py diff --git a/main.py b/main.py index 4f707fa..d1a60a0 100755 --- a/main.py +++ b/main.py @@ -485,8 +485,11 @@ # if __name__ == "__main__": # asyncio.run(Screen.wrapper(main)) import asyncio +import shutil from typing import List +from asciimatics.screen import Screen, ManagedScreen +from asciimatics.widgets import Frame, Layout, Text from bfxapi import Position from bfxbot import BfxBot @@ -497,80 +500,55 @@ import bfxapi from bfxbot.currency import Symbol from bfxbot.models import Strategy, SymbolStatus, PositionState, Event, EventKind from bfxbot.utils import TAKER_FEE, net_pl_percentage +from strategy import TrailingStopStrategy dotenv.load_dotenv() -class TrailingStopStrategy(Strategy): - BREAK_EVEN_PERC = TAKER_FEE - MIN_PROFIT_PERC = TAKER_FEE * 2.5 - GOOD_PROFIT_PERC = MIN_PROFIT_PERC * 1.5 - MAX_LOSS_PERC = -3.75 - OFFER_PERC = 0.01 - - TRAIL_STOP_PERCENTAGES = { - PositionState.MINIMUM_PROFIT: 0.27, - PositionState.PROFIT: 0.14 - } - - def position_on_tick(self, position: Position, ss: SymbolStatus) -> (PositionState, List[Event]): - events = [] - - pl_perc = net_pl_percentage(position.profit_loss_percentage, TAKER_FEE) - prev = ss.previous_position_w(position.id) - - if pl_perc > self.GOOD_PROFIT_PERC: - state = PositionState.PROFIT - elif self.MIN_PROFIT_PERC <= pl_perc < self.GOOD_PROFIT_PERC: - state = PositionState.MINIMUM_PROFIT - elif 0.0 <= pl_perc < self.MIN_PROFIT_PERC: - state = PositionState.BREAK_EVEN - elif self.MAX_LOSS_PERC < pl_perc < 0.0: - state = PositionState.LOSS - else: - state = PositionState.CRITICAL - - if not prev or prev.state == state: - return state, events - - if state ==PositionState.PROFIT: - events.append(Event(EventKind.REACHED_GOOD_PROFIT, position.id, ss.current_tick)) - elif state == PositionState.MINIMUM_PROFIT: - events.append(Event(EventKind.REACHED_MIN_PROFIT, position.id, ss.current_tick)) - elif state == PositionState.BREAK_EVEN: - events.append(Event(EventKind.REACHED_BREAK_EVEN, position.id, ss.current_tick)) - elif state== PositionState.LOSS: - events.append(Event(EventKind.REACHED_LOSS, position.id, ss.current_tick)) - else: - events.append(Event(EventKind.REACHED_MAX_LOSS, position.id, ss.current_tick)) - events.append(Event(EventKind.CLOSE_POSITION, position.id, ss.current_tick)) - - return state, events - async def main(): API_KEY = os.getenv("API_KEY") API_SECRET = os.getenv("API_SECRET") - if API_KEY == None: + if API_KEY is None: print("API_KEY is not set! Set the var in the .env file.") return - if API_SECRET == None: + if API_SECRET is None: print("API_SECRET is not set! Set the var in the .env file.") return - bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET) + bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET, tick_duration=20) strategy = TrailingStopStrategy() bot.set_strategy(Symbol.BTC, strategy) + eh = bot.event_handler(Symbol.BTC) await bot.start() - eh = bot.event_handler(Symbol.BTC) - while True: - print("WAITING...") - await bot.update() + await bot_loop(bot) +@ManagedScreen +async def bot_loop(bot: BfxBot, screen: Screen = None): + prepare_tui(screen) + screen.play() + + await bot.update() + +def prepare_tui(screen: Screen): + w, h = shutil.get_terminal_size() + + frame = Frame(screen, w, h, has_border=False) + + info_layout = Layout([1]) + graph_layout = Layout([1]) + footer_layout = Layout([1]) + + frame.add_layout(info_layout) + frame.add_layout(graph_layout) + frame.add_layout(footer_layout) + + info_layout.add_widget(Text(label="Test")) + if __name__ == '__main__': asyncio.run(main()) \ No newline at end of file diff --git a/strategy.py b/strategy.py new file mode 100644 index 0000000..0fa6d9d --- /dev/null +++ b/strategy.py @@ -0,0 +1,53 @@ +from typing import List + +from bfxapi import Position + +from bfxbot.models import Strategy, PositionState, SymbolStatus, Event, EventKind +from bfxbot.utils import TAKER_FEE, net_pl_percentage + + +class TrailingStopStrategy(Strategy): + BREAK_EVEN_PERC = TAKER_FEE + MIN_PROFIT_PERC = TAKER_FEE * 2.5 + GOOD_PROFIT_PERC = MIN_PROFIT_PERC * 1.5 + MAX_LOSS_PERC = -3.75 + OFFER_PERC = 0.01 + + TRAIL_STOP_PERCENTAGES = { + PositionState.MINIMUM_PROFIT: 0.27, + PositionState.PROFIT: 0.14 + } + + def position_on_tick(self, position: Position, ss: SymbolStatus) -> (PositionState, List[Event]): + events = [] + + pl_perc = net_pl_percentage(position.profit_loss_percentage, TAKER_FEE) + prev = ss.previous_position_w(position.id) + + if pl_perc > self.GOOD_PROFIT_PERC: + state = PositionState.PROFIT + elif self.MIN_PROFIT_PERC <= pl_perc < self.GOOD_PROFIT_PERC: + state = PositionState.MINIMUM_PROFIT + elif 0.0 <= pl_perc < self.MIN_PROFIT_PERC: + state = PositionState.BREAK_EVEN + elif self.MAX_LOSS_PERC < pl_perc < 0.0: + state = PositionState.LOSS + else: + state = PositionState.CRITICAL + + if not prev or prev.state == state: + return state, events + + if state ==PositionState.PROFIT: + events.append(Event(EventKind.REACHED_GOOD_PROFIT, position.id, ss.current_tick)) + elif state == PositionState.MINIMUM_PROFIT: + events.append(Event(EventKind.REACHED_MIN_PROFIT, position.id, ss.current_tick)) + elif state == PositionState.BREAK_EVEN: + events.append(Event(EventKind.REACHED_BREAK_EVEN, position.id, ss.current_tick)) + elif state== PositionState.LOSS: + events.append(Event(EventKind.REACHED_LOSS, position.id, ss.current_tick)) + else: + events.append(Event(EventKind.REACHED_MAX_LOSS, position.id, ss.current_tick)) + events.append(Event(EventKind.CLOSE_POSITION, position.id, ss.current_tick)) + + return state, events