From 726114d99e1fe4d3249ab096265e80fd943937a8 Mon Sep 17 00:00:00 2001 From: enarjord Date: Fri, 12 May 2023 19:41:14 +0200 Subject: [PATCH] bug fix: update server time and add delay time in countdown print --- passivbot.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/passivbot.py b/passivbot.py index 0cc00ceec..3f7716a51 100644 --- a/passivbot.py +++ b/passivbot.py @@ -58,6 +58,8 @@ calc_clock_entry_short, calc_clock_close_long, calc_clock_close_short, + calc_delay_between_fills_ms_bid, + calc_delay_between_fills_ms_ask, ) from typing import Union, Dict, List @@ -216,6 +218,7 @@ async def _init(self): self.init_exchange_config(), self.init_order_book(), self.update_server_time(), + self.update_last_fills_timestamps(), ) await self.init_emas() print("done") @@ -1105,7 +1108,7 @@ async def on_market_stream_event(self, ticks: [dict]): await self.cancel_and_create() def heartbeat_print(self): - logging.info(f"heartbeat {self.symbol}") + logging.info(f"heartbeat {self.symbol} ") self.log_position_long() self.log_position_short() liq_price = self.position["long"]["liquidation_price"] @@ -1406,7 +1409,24 @@ async def start_ohlcv_mode(self): if self.stop_websocket: break if self.countdown: - print(f"\rcountdown: {i} last price: {self.price} ", end=" ") + line = f"\rcountdown: {i} last price: {self.price}" + do_long = self.do_long or self.position["long"]["size"] != 0.0 + do_short = self.do_short or self.position["short"]["size"] != 0.0 + if (do_long or do_short) and self.passivbot_mode == "clock": + line += " | mins delay until next: " + try: + res = self.calc_minutes_until_next_orders() + if do_long: + line += f"entry long: {res['entry_long']:.1f}, close long: {res['close_long']:.1f}" + if do_short: + line += f"entry short: {res['entry_short']:.1f}, close short: {res['close_short']:.1f}" + except Exception as e: + print("error computing minutes until next order", e) + traceback.print_exc() + if i == 0: + print("\r" + " " * (len(line) + 10), end=" ") + else: + print(line + " ", end=" ") await asyncio.sleep(1) if self.stop_websocket: break @@ -1414,6 +1434,60 @@ async def start_ohlcv_mode(self): await self.on_minute_mark() await asyncio.sleep(1.0) + def calc_minutes_until_next_orders(self): + res = {"entry_long": 0.0, "close_long": 0.0, "entry_short": 0.0, "close_short": 0.0} + if self.position["long"]["size"] != 0.0: + millis_delay_next_entry_long = calc_delay_between_fills_ms_bid( + self.position["long"]["price"], + self.price, + self.xk["delay_between_fills_ms_entry"][0], + self.xk["delay_weight_entry"][0], + ) + millis_since_prev_close_long = ( + self.server_time - self.last_fills_timestamps["clock_entry_long"] + ) + res["entry_long"] = max( + 0.0, (millis_delay_next_entry_long - millis_since_prev_close_long) / 1000 / 60 + ) + millis_delay_next_close_long = calc_delay_between_fills_ms_ask( + self.position["long"]["price"], + self.price, + self.xk["delay_between_fills_ms_close"][0], + self.xk["delay_weight_close"][0], + ) + millis_since_prev_close_long = ( + self.server_time - self.last_fills_timestamps["clock_close_long"] + ) + res["close_long"] = max( + 0.0, (millis_delay_next_close_long - millis_since_prev_close_long) / 1000 / 60 + ) + if self.position["short"]["size"] != 0.0: + millis_delay_next_entry_short = calc_delay_between_fills_ms_ask( + self.position["short"]["price"], + self.price, + self.xk["delay_between_fills_ms_entry"][0], + self.xk["delay_weight_entry"][0], + ) + millis_since_prev_close_short = ( + self.server_time - self.last_fills_timestamps["clock_entry_short"] + ) + res["entry_short"] = max( + 0.0, (millis_delay_next_entry_short - millis_since_prev_close_short) / 1000 / 60 + ) + millis_delay_next_close_short = calc_delay_between_fills_ms_bid( + self.position["short"]["price"], + self.price, + self.xk["delay_between_fills_ms_close"][0], + self.xk["delay_weight_close"][0], + ) + millis_since_prev_close_short = ( + self.server_time - self.last_fills_timestamps["clock_close_short"] + ) + res["close_short"] = max( + 0.0, (millis_delay_next_close_short - millis_since_prev_close_short) / 1000 / 60 + ) + return res + async def on_minute_mark(self): # called each whole minute try: @@ -1432,7 +1506,7 @@ async def on_minute_mark(self): ] if self.passivbot_mode == "clock": to_update.append(self.update_last_fills_timestamps()) - to_update.append(self.get_server_time()) + to_update.append(self.update_server_time()) res = await asyncio.gather(*to_update) self.update_emas(self.ob[0], self.prev_price) """