refactor: Simplify position summary query and add debug logging
This commit is contained in:
parent
814a7db5de
commit
731b4e9e2d
@ -316,19 +316,13 @@ def generate_position_id(ticker: str, entry_date: datetime = None) -> str:
|
||||
|
||||
def get_position_summary(ticker: str) -> dict:
|
||||
"""Get summary of existing positions for a ticker"""
|
||||
print(f"\n=== Getting Position Summary for {ticker} ===") # Debug
|
||||
with create_client() as client:
|
||||
query = f"""
|
||||
SELECT
|
||||
position_id,
|
||||
sum(CASE WHEN direction = 'buy' THEN shares
|
||||
WHEN direction = 'sell' THEN -shares
|
||||
ELSE 0 END) as total_shares,
|
||||
sum(CASE WHEN direction = 'buy' THEN shares * entry_price
|
||||
WHEN direction = 'sell' THEN -shares * entry_price
|
||||
ELSE 0 END) /
|
||||
abs(sum(CASE WHEN direction = 'buy' THEN shares
|
||||
WHEN direction = 'sell' THEN -shares
|
||||
ELSE 0 END)) as avg_entry_price,
|
||||
sum(shares) as total_shares,
|
||||
avg(entry_price) as avg_entry_price,
|
||||
min(entry_date) as first_entry,
|
||||
max(entry_date) as last_entry,
|
||||
count() as num_orders,
|
||||
@ -339,14 +333,18 @@ def get_position_summary(ticker: str) -> dict:
|
||||
WHERE ticker = '{ticker}'
|
||||
AND exit_price IS NULL
|
||||
GROUP BY position_id
|
||||
HAVING total_shares > 0
|
||||
ORDER BY first_entry DESC
|
||||
"""
|
||||
print(f"Executing query: {query}") # Debug
|
||||
result = client.query(query).result_rows
|
||||
print(f"Query returned {len(result)} rows") # Debug
|
||||
|
||||
columns = ['position_id', 'total_shares', 'avg_entry_price',
|
||||
'first_entry', 'last_entry', 'num_orders',
|
||||
'target_price', 'stop_loss', 'strategy']
|
||||
return [dict(zip(columns, row)) for row in result]
|
||||
positions = [dict(zip(columns, row)) for row in result]
|
||||
print(f"Processed positions: {positions}") # Debug
|
||||
return positions
|
||||
|
||||
def get_order_type() -> Optional[str]:
|
||||
"""Get order type from user"""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user