From 2a6dc24300b3866d75e9584a64d2d132eb383634 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Wed, 16 Dec 2020 15:06:16 +0000 Subject: [PATCH] retrying bitfinex api operations --- bfxbot/bfxbot.py | 2 +- bfxbot/bfxwrapper.py | 24 ++++++++++++++++++++++++ strategy.py | 12 +++++++----- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/bfxbot/bfxbot.py b/bfxbot/bfxbot.py index c8b8ba8..119bfbb 100644 --- a/bfxbot/bfxbot.py +++ b/bfxbot/bfxbot.py @@ -58,7 +58,7 @@ class BfxBot: await self.__status[Symbol.from_str(p.symbol)].add_position(p) # updating orders - active_orders = await self.__bfx.get_active_orders(str(symbol)) + active_orders = await self.__bfx.get_active_orders(symbol) for o in active_orders: self.__status[symbol].add_order(o) diff --git a/bfxbot/bfxwrapper.py b/bfxbot/bfxwrapper.py index 39c097d..d58d1f6 100644 --- a/bfxbot/bfxwrapper.py +++ b/bfxbot/bfxwrapper.py @@ -1,11 +1,35 @@ from bfxapi.rest.bfx_rest import BfxRest +from retrying_async import retry + +from bfxbot.currency import Symbol class BfxWrapper(BfxRest): def __init__(self, api_key: str, api_secret: str): super().__init__(API_KEY=api_key, API_SECRET=api_secret) + @retry() + async def get_public_ticker(self, symbol): + if isinstance(symbol, Symbol): + symbol = str(symbol) + + return await super().get_public_ticker(symbol) + + @retry() + async def get_active_position(self): + return await super().get_active_position() + + @retry() + async def get_active_orders(self, symbol): + if isinstance(symbol, Symbol): + symbol = str(symbol) + + return await super().get_active_orders(symbol) + async def get_current_prices(self, symbol) -> (float, float, float): + if isinstance(symbol, Symbol): + symbol = str(symbol) + tickers = await self.get_public_ticker(symbol) bid_price = tickers[0] diff --git a/strategy.py b/strategy.py index 6253e10..3d941fb 100644 --- a/strategy.py +++ b/strategy.py @@ -54,12 +54,12 @@ class TrailingStopStrategy(Strategy): def __init__(self): self.stop_percentage: float = None - def position_on_new_tick(self, position: Position, ss: SymbolStatus) -> (PositionState, List[Event]): + def position_on_new_tick(self, current_position: Position, ss: SymbolStatus) -> (PositionState, List[Event]): events = [] - pl_perc = net_pl_percentage(position.profit_loss_percentage, TAKER_FEE) - prev = ss.previous_pw(position.id) - event_metadata = EventMetadata(position_id=position.id) + pl_perc = net_pl_percentage(current_position.profit_loss_percentage, TAKER_FEE) + prev = ss.previous_pw(current_position.id) + event_metadata = EventMetadata(position_id=current_position.id) if pl_perc > self.GOOD_PROFIT_PERC: state = PositionState.PROFIT @@ -73,12 +73,14 @@ class TrailingStopStrategy(Strategy): events.append(Event(EventKind.CLOSE_POSITION, ss.current_tick, event_metadata)) state = PositionState.CRITICAL - pw = PositionWrapper(position, state=state, net_profit_loss=position.profit_loss, + pw = PositionWrapper(current_position, state=state, net_profit_loss=current_position.profit_loss, net_profit_loss_percentage=pl_perc) if not prev or prev.state() == state: return pw, events + print(f"{prev.state()} vs {state}") + if state == PositionState.PROFIT: events.append(Event(EventKind.REACHED_GOOD_PROFIT, ss.current_tick, event_metadata)) elif state == PositionState.MINIMUM_PROFIT: