Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Apr 22, 2024
1 parent d3d4318 commit 4073c9f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions sor/_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ async def close_position(self, command: ClosePosition):
return

position_size = position.size
position_side = position.side
exit_price = command.exit_price

min_size = symbol.min_position_size
Expand All @@ -292,12 +293,12 @@ async def close_position(self, command: ClosePosition):
max_spread = float("-inf")

for price in self.algo_price.calculate(symbol, self.exchange):
if not self.exchange.fetch_position(symbol, position.side):
if not self.exchange.fetch_position(symbol, position_side):
break

spread = (
price - exit_price
if position.side == PositionSide.LONG
if position_side == PositionSide.LONG
else exit_price - price
)
max_spread = max(spread, max_spread)
Expand All @@ -306,9 +307,6 @@ async def close_position(self, command: ClosePosition):
f"Trying to reduce order -> algo price: {price}, theo price: {exit_price}, spread: {spread}, max spread: {max_spread}"
)

if not self.exchange.fetch_position(symbol, position.side):
break

curr_time = time.time()
expired_orders = [
order_id
Expand All @@ -330,14 +328,17 @@ async def close_position(self, command: ClosePosition):
break

if (
not self.exchange.has_open_orders(symbol, position.side, True)
not self.exchange.has_open_orders(symbol, position_side, True)
or not len(order_timestamps.keys())
) and (spread < max_spread or spread < 0):
order_id = self.exchange.create_reduce_order(
symbol, position.side, size, price
symbol, position_side, size, price
)
if order_id:
order_timestamps[order_id] = time.time()

for order_id in list(order_timestamps.keys()):
self.exchange.cancel_order(order_id, symbol)

if self.exchange.fetch_position(symbol, position_side):
self.exchange.close_full_position(symbol, position_side)

0 comments on commit 4073c9f

Please sign in to comment.