From 9651e561bd890a4e47ef0cd485533923e39ad349 Mon Sep 17 00:00:00 2001 From: Carlos Wu Fei Date: Tue, 24 Sep 2024 18:06:39 +0100 Subject: [PATCH] Fix bot logs --- api/deals/margin.py | 14 ++++---- api/deals/spot.py | 79 +++++++++++++++++++++++++++++++-------------- binquant | 2 +- 3 files changed, 63 insertions(+), 32 deletions(-) diff --git a/api/deals/margin.py b/api/deals/margin.py index b5704822..16c1abcd 100644 --- a/api/deals/margin.py +++ b/api/deals/margin.py @@ -15,6 +15,7 @@ class MarginDeal(BaseDeal): def __init__(self, bot, db_collection_name) -> None: + self.active_bot: BotSchema # Inherit from parent class super().__init__(bot, db_collection_name=db_collection_name) self.base_producer = BaseProducer() @@ -92,15 +93,15 @@ def cancel_open_orders(self, deal_type): self.update_deal_logs( "Old take profit order cancelled", self.active_bot ) - except HTTPError as error: + except HTTPError: self.update_deal_logs( "Take profit order not found, no need to cancel", self.active_bot ) return - except Exception as error: + except BinanceErrors as error: # Most likely old error out of date orderId - if error.args[1] == -2011: + if error.code == -2011: return return @@ -317,7 +318,6 @@ def terminate_margin_short(self, buy_back_fiat: bool = True): try: # get new balance self.isolated_balance = self.get_isolated_balance(self.active_bot.pair) - print(f"Transfering leftover isolated assets back to Spot") if float(self.isolated_balance[0]["quoteAsset"]["free"]) != 0: # transfer back to SPOT account self.transfer_isolated_margin_to_spot( @@ -563,7 +563,7 @@ def execute_stop_loss(self): self.active_bot.deal.buy_total_qty = res["origQty"] self.active_bot.deal.margin_short_buy_back_timestamp = res["transactTime"] - msg = f"Completed Stop loss order" + msg = "Completed Stop loss order" self.active_bot.errors.append(msg) self.active_bot.status = Status.completed self.active_bot = self.save_bot_streaming(self.active_bot) @@ -616,10 +616,10 @@ def execute_take_profit(self): self.active_bot.deal.margin_short_buy_back_price = res["price"] self.active_bot.deal.margin_short_buy_back_timestamp = res["transactTime"] self.active_bot.deal.margin_short_buy_back_timestamp = res["transactTime"] - msg = f"Completed Take profit!" + msg = "Completed Take profit!" else: - msg = f"Re-completed take profit" + msg = "Re-completed take profit" self.active_bot.errors.append(msg) self.active_bot.status = Status.completed diff --git a/api/deals/spot.py b/api/deals/spot.py index 62156b67..f3246170 100644 --- a/api/deals/spot.py +++ b/api/deals/spot.py @@ -38,7 +38,9 @@ def switch_margin_short(self): 2. Calculate take_profit_price and stop_loss_price as usual 3. Create deal """ - self.update_deal_logs("Resetting bot for margin_short strategy...", self.active_bot) + self.update_deal_logs( + "Resetting bot for margin_short strategy...", self.active_bot + ) self.active_bot.strategy = Strategy.margin_short self.active_bot = self.create_new_bot_streaming(active_bot=self.active_bot) @@ -70,12 +72,14 @@ def execute_stop_loss(self, price): if not closed_orders: order = self.verify_deal_close_order() if order: - self.active_bot.errors.append("Execute stop loss previous order found! Appending...") + self.active_bot.errors.append( + "Execute stop loss previous order found! Appending..." + ) self.active_bot.orders.append(order) else: self.update_deal_logs( - f"No quantity in balance, no closed orders. Cannot execute update stop limit.", - self.active_bot + "No quantity in balance, no closed orders. Cannot execute update stop limit.", + self.active_bot, ) self.active_bot.status = Status.error self.active_bot = self.save_bot_streaming(self.active_bot) @@ -86,7 +90,9 @@ def execute_stop_loss(self, price): res = self.simulate_order(self.active_bot.pair, price, qty, "SELL") else: - self.active_bot.errors.append("Dispatching sell order for trailling profit...") + self.active_bot.errors.append( + "Dispatching sell order for trailling profit..." + ) # Dispatch real order res = self.sell_order(symbol=self.active_bot.pair, qty=qty, price=price) @@ -137,12 +143,14 @@ def trailling_profit(self) -> BotSchema | None: if not closed_orders: order = self.verify_deal_close_order() if order: - self.active_bot.errors.append("Execute trailling profit previous order found! Appending...") + self.active_bot.errors.append( + "Execute trailling profit previous order found! Appending..." + ) self.active_bot.orders.append(order) else: self.update_deal_logs( - f"No quantity in balance, no closed orders. Cannot execute update trailling profit.", - self.active_bot + "No quantity in balance, no closed orders. Cannot execute update trailling profit.", + self.active_bot, ) self.active_bot.status = Status.error self.active_bot = self.save_bot_streaming(self.active_bot) @@ -158,7 +166,9 @@ def trailling_profit(self) -> BotSchema | None: ) else: - self.active_bot.errors.append("Dispatching sell order for trailling profit...") + self.active_bot.errors.append( + "Dispatching sell order for trailling profit..." + ) # Dispatch real order # No price means market order res = self.sell_order( @@ -212,15 +222,17 @@ def streaming_updates(self, close_price, open_price): self.base_producer.update_required(self.producer, "EXECUTE_SPOT_STOP_LOSS") if self.active_bot.margin_short_reversal: self.switch_margin_short() - self.base_producer.update_required(self.producer, "EXECUTE_SWITCH_MARGIN_SHORT") - self.update_deal_logs("Completed switch to margin short bot", self.active_bot) + self.base_producer.update_required( + self.producer, "EXECUTE_SWITCH_MARGIN_SHORT" + ) + self.update_deal_logs( + "Completed switch to margin short bot", self.active_bot + ) return # Take profit trailling - if (self.active_bot.trailling) and float( - self.active_bot.deal.buy_price - ) > 0: + if (self.active_bot.trailling) and float(self.active_bot.deal.buy_price) > 0: # If current price didn't break take_profit (first time hitting take_profit or trailling_stop_loss lower than base_order buy_price) if self.active_bot.deal.trailling_stop_loss_price == 0: trailling_price = float(self.active_bot.deal.buy_price) * ( @@ -248,13 +260,22 @@ def streaming_updates(self, close_price, open_price): # trailling_profit_price always be > trailling_stop_loss_price self.active_bot.deal.trailling_profit_price = new_take_profit - if new_trailling_stop_loss > self.active_bot.deal.buy_price and new_trailling_stop_loss > self.active_bot.deal.trailling_stop_loss_price: + if ( + new_trailling_stop_loss > self.active_bot.deal.buy_price + and new_trailling_stop_loss + > self.active_bot.deal.trailling_stop_loss_price + ): # Selling below buy_price will cause a loss # instead let it drop until it hits safety order or stop loss # Update trailling_stop_loss - self.active_bot.deal.trailling_stop_loss_price = new_trailling_stop_loss + self.active_bot.deal.trailling_stop_loss_price = ( + new_trailling_stop_loss + ) - self.update_deal_logs(f"Updated {self.active_bot.pair} trailling_stop_loss_price {self.active_bot.deal.trailling_stop_loss_price}", self.active_bot) + self.update_deal_logs( + f"Updated {self.active_bot.pair} trailling_stop_loss_price {self.active_bot.deal.trailling_stop_loss_price}", + self.active_bot, + ) self.active_bot = self.save_bot_streaming(self.active_bot) # Direction 2 (downward): breaking the trailling_stop_loss @@ -269,10 +290,13 @@ def streaming_updates(self, close_price, open_price): and (float(open_price) > float(close_price)) ): self.update_deal_logs( - f"Hit trailling_stop_loss_price {self.active_bot.deal.trailling_stop_loss_price}. Selling {self.active_bot.pair}" + f"Hit trailling_stop_loss_price {self.active_bot.deal.trailling_stop_loss_price}. Selling {self.active_bot.pair}", + self.active_bot, ) self.trailling_profit() - self.base_producer.update_required(self.producer, "EXECUTE_SPOT_TRAILLING_PROFIT") + self.base_producer.update_required( + self.producer, "EXECUTE_SPOT_TRAILLING_PROFIT" + ) # Update unfilled orders unupdated_order = next( @@ -284,7 +308,9 @@ def streaming_updates(self, close_price, open_price): None, ) if unupdated_order: - order_response = self.get_all_orders(self.active_bot.pair, unupdated_order.order_id) + order_response = self.get_all_orders( + self.active_bot.pair, unupdated_order.order_id + ) logging.info(f"Unfilled orders response{order_response}") if order_response[0]["status"] == "FILLED": for i, order in enumerate(self.active_bot.orders): @@ -293,7 +319,7 @@ def streaming_updates(self, close_price, open_price): self.active_bot.orders[i].qty = order_response["origQty"] self.active_bot.orders[i].fills = order_response["fills"] self.active_bot.orders[i].status = order_response["status"] - + self.active_bot = self.save_bot_streaming(self.active_bot) def close_conditions(self, current_price): @@ -305,8 +331,13 @@ def close_conditions(self, current_price): """ if self.active_bot.close_condition == CloseConditions.market_reversal: self.render_market_domination_reversal() - if self.market_domination_reversal and current_price < self.active_bot.deal.buy_price: + if ( + self.market_domination_reversal + and current_price < self.active_bot.deal.buy_price + ): self.execute_stop_loss() - self.base_producer.update_required(self.producer, "EXECUTE_SPOT_CLOSE_CONDITION_STOP_LOSS") + self.base_producer.update_required( + self.producer, "EXECUTE_SPOT_CLOSE_CONDITION_STOP_LOSS" + ) - pass \ No newline at end of file + pass diff --git a/binquant b/binquant index e76b7501..f526c36f 160000 --- a/binquant +++ b/binquant @@ -1 +1 @@ -Subproject commit e76b75014adbf3757ff43f15d69648ac8ef28add +Subproject commit f526c36fd23c65fe818acfe496e71399176c9241