Skip to content

Commit

Permalink
Fix missing price for market orders
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Wu Fei authored and Carlos Wu Fei committed Dec 28, 2023
1 parent 99dff48 commit 687c6c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
6 changes: 3 additions & 3 deletions api/deals/margin.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,9 +772,9 @@ def update_trailling_profit(self, close_price):

# Reset stop_loss_price to avoid confusion in front-end
self.active_bot.deal.stop_loss_price = 0
logging.info(
f"{self.active_bot.pair} Updating after broken first trailling_profit (short)"
)
milestone_msg = f"{self.active_bot.pair} Updating after broken first trailling_profit (short)"
logging.info(milestone_msg)
self.update_deal_logs(milestone_msg)

# Direction 1 (downward): breaking the current trailling
if float(close_price) <= float(self.active_bot.deal.trailling_profit_price):
Expand Down
27 changes: 19 additions & 8 deletions api/orders/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ def sell_order(self, symbol, qty, price=None):

data = self.signed_request(url=self.order_url, method="POST", payload=payload)

total_qty = 0
weighted_avg = 0
for item in data["fills"]:
weighted_avg += float(item["price"]) * float(item["qty"])
total_qty += float(item["qty"])
if data["price"] == 0:
total_qty = 0
weighted_avg = 0
for item in data["fills"]:
weighted_avg += float(item["price"]) * float(item["qty"])
total_qty += float(item["qty"])

weighted_avg_price = weighted_avg / total_qty
data["price"] = weighted_avg_price
logging.info(f'Sell transaction: {data}')
weighted_avg_price = weighted_avg / total_qty
data["price"] = weighted_avg_price

return data

Expand Down Expand Up @@ -118,6 +118,17 @@ def buy_order(self, symbol, qty, price=None):
}

data = self.signed_request(url=self.order_url, method="POST", payload=payload)

if data["price"] == 0:
total_qty = 0
weighted_avg = 0
for item in data["fills"]:
weighted_avg += float(item["price"]) * float(item["qty"])
total_qty += float(item["qty"])

weighted_avg_price = weighted_avg / total_qty
data["price"] = weighted_avg_price

return data

def get_open_orders(self):
Expand Down

0 comments on commit 687c6c6

Please sign in to comment.