From 5d5fbc91e1d7c62829015f2197dc22166d1f4204 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Mon, 30 Nov 2020 14:38:46 +0000 Subject: [PATCH] sample main and strategy --- main.py | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index fed9896..4f707fa 100755 --- a/main.py +++ b/main.py @@ -493,8 +493,10 @@ from bfxbot import BfxBot import dotenv import os import bfxapi -from bfxbot.symbolstatus import Strategy, SymbolStatus, PositionState, Event -from bfxbot.utils import TAKER_FEE + +from bfxbot.currency import Symbol +from bfxbot.models import Strategy, SymbolStatus, PositionState, Event, EventKind +from bfxbot.utils import TAKER_FEE, net_pl_percentage dotenv.load_dotenv() @@ -511,7 +513,38 @@ class TrailingStopStrategy(Strategy): } def position_on_tick(self, position: Position, ss: SymbolStatus) -> (PositionState, List[Event]): - return + 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") @@ -527,14 +560,17 @@ async def main(): bot = BfxBot(api_key=API_KEY, api_secret=API_SECRET) strategy = TrailingStopStrategy() + bot.set_strategy(Symbol.BTC, strategy) - bot.set_strategy("tBTCUSD", strategy) + await bot.start() - await bot.update() + eh = bot.event_handler(Symbol.BTC) + + while True: + print("WAITING...") + await bot.update() - return - if __name__ == '__main__': asyncio.run(main()) \ No newline at end of file