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:
|
def get_position_summary(ticker: str) -> dict:
|
||||||
"""Get summary of existing positions for a ticker"""
|
"""Get summary of existing positions for a ticker"""
|
||||||
|
print(f"\n=== Getting Position Summary for {ticker} ===") # Debug
|
||||||
with create_client() as client:
|
with create_client() as client:
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT
|
SELECT
|
||||||
position_id,
|
position_id,
|
||||||
sum(CASE WHEN direction = 'buy' THEN shares
|
sum(shares) as total_shares,
|
||||||
WHEN direction = 'sell' THEN -shares
|
avg(entry_price) as avg_entry_price,
|
||||||
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,
|
|
||||||
min(entry_date) as first_entry,
|
min(entry_date) as first_entry,
|
||||||
max(entry_date) as last_entry,
|
max(entry_date) as last_entry,
|
||||||
count() as num_orders,
|
count() as num_orders,
|
||||||
@ -339,14 +333,18 @@ def get_position_summary(ticker: str) -> dict:
|
|||||||
WHERE ticker = '{ticker}'
|
WHERE ticker = '{ticker}'
|
||||||
AND exit_price IS NULL
|
AND exit_price IS NULL
|
||||||
GROUP BY position_id
|
GROUP BY position_id
|
||||||
HAVING total_shares > 0
|
|
||||||
ORDER BY first_entry DESC
|
ORDER BY first_entry DESC
|
||||||
"""
|
"""
|
||||||
|
print(f"Executing query: {query}") # Debug
|
||||||
result = client.query(query).result_rows
|
result = client.query(query).result_rows
|
||||||
|
print(f"Query returned {len(result)} rows") # Debug
|
||||||
|
|
||||||
columns = ['position_id', 'total_shares', 'avg_entry_price',
|
columns = ['position_id', 'total_shares', 'avg_entry_price',
|
||||||
'first_entry', 'last_entry', 'num_orders',
|
'first_entry', 'last_entry', 'num_orders',
|
||||||
'target_price', 'stop_loss', 'strategy']
|
'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]:
|
def get_order_type() -> Optional[str]:
|
||||||
"""Get order type from user"""
|
"""Get order type from user"""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user