Skip to content

Commit

Permalink
Merge pull request #20 from r0fls/rd/fix-sync-again
Browse files Browse the repository at this point in the history
fix sync again
  • Loading branch information
r0fls authored Jun 10, 2024
2 parents 8584aae + 9a01373 commit f3e5620
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions strategies/constant_percentage_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,25 @@ def sync_positions_with_broker(self):
for symbol, data in broker_positions.items():
current_price = self.broker.get_current_price(symbol)
target_quantity = self.should_own(symbol, current_price)
position = session.query(Position).filter_by(
broker=self.broker.broker_name,
strategy=None,
symbol=symbol
).first()
if position:
if target_quantity > 0:
position.strategy = self.strategy_name
position.quantity = data['quantity']
position.latest_price = current_price
position.last_updated = datetime.utcnow()
logger.info(f"Updated uncategorized position for {symbol} to strategy {self.strategy_name} with quantity {data['quantity']} and price {current_price}")
else:
if target_quantity > 0:
# We should own this, let's see if we know about it
position = session.query(Position).filter_by(
broker=self.broker.broker_name,
strategy=self.strategy_name,
strategy=None,
symbol=symbol
).first()
# There is already an existing position as part of this strategy
if not position:
position = session.query(Position).filter_by(
broker=self.broker.broker_name,
strategy=self.strategy_name,
symbol=symbol
).first()
if position:
position.strategy = self.strategy_name
position.quantity = data['quantity']
position.latest_price = current_price
position.last_updated = datetime.utcnow()
logger.info(f"Updated position for {symbol} with strategy {self.strategy_name} with quantity {data['quantity']} and price {current_price}")
logger.info(f"Updated uncategorized position for {symbol} to strategy {self.strategy_name} with quantity {data['quantity']} and price {current_price}")
else:
# Create a new position
position = Position(
Expand All @@ -121,7 +115,6 @@ def sync_positions_with_broker(self):
)
session.add(position)
logger.info(f"Created new position for {symbol} with quantity {data['quantity']} and price {current_price}")

session.commit()
logger.debug("Positions synced with broker")

Expand Down

0 comments on commit f3e5620

Please sign in to comment.