mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-02 01:27:24 -04:00
Compare commits
2 Commits
v0.4.2-alp
...
v0.4.2-alp
| Author | SHA1 | Date | |
|---|---|---|---|
| f1f76b9a99 | |||
| 277714f664 |
@@ -88,9 +88,28 @@ class ContextInjector:
|
||||
|
||||
# Update position state after successful trade
|
||||
if request.name in ["buy", "sell"]:
|
||||
# Check if result is a valid position dict (not an error)
|
||||
if isinstance(result, dict) and "error" not in result and "CASH" in result:
|
||||
# Debug: Log result type and structure
|
||||
print(f"[DEBUG ContextInjector] Trade result type: {type(result)}")
|
||||
print(f"[DEBUG ContextInjector] Trade result: {result}")
|
||||
|
||||
# Extract position dict from MCP result
|
||||
# MCP tools return CallToolResult objects with structuredContent field
|
||||
position_dict = None
|
||||
if hasattr(result, 'structuredContent') and result.structuredContent:
|
||||
position_dict = result.structuredContent
|
||||
print(f"[DEBUG ContextInjector] Extracted from structuredContent: {position_dict}")
|
||||
elif isinstance(result, dict):
|
||||
position_dict = result
|
||||
print(f"[DEBUG ContextInjector] Using result as dict: {position_dict}")
|
||||
|
||||
# Check if position dict is valid (not an error) and update state
|
||||
if position_dict and "error" not in position_dict and "CASH" in position_dict:
|
||||
# Update our tracked position with the new state
|
||||
self._current_position = result.copy()
|
||||
self._current_position = position_dict.copy()
|
||||
print(f"[DEBUG ContextInjector] Updated _current_position: {self._current_position}")
|
||||
else:
|
||||
print(f"[DEBUG ContextInjector] Did NOT update _current_position - check failed")
|
||||
print(f"[DEBUG ContextInjector] position_dict: {position_dict}")
|
||||
print(f"[DEBUG ContextInjector] _current_position remains: {self._current_position}")
|
||||
|
||||
return result
|
||||
|
||||
@@ -132,11 +132,14 @@ def _buy_impl(symbol: str, amount: int, signature: str = None, today_date: str =
|
||||
# Step 1: Get current position
|
||||
# Use injected position if available (for intra-day tracking),
|
||||
# otherwise query database for starting position
|
||||
print(f"[DEBUG buy] _current_position received: {_current_position}")
|
||||
if _current_position is not None:
|
||||
current_position = _current_position
|
||||
next_action_id = 0 # Not used in new schema
|
||||
print(f"[DEBUG buy] Using injected position: {current_position}")
|
||||
else:
|
||||
current_position, next_action_id = get_current_position_from_db(job_id, signature, today_date)
|
||||
print(f"[DEBUG buy] Queried position from DB: {current_position}")
|
||||
|
||||
# Step 2: Get stock price
|
||||
try:
|
||||
@@ -189,6 +192,8 @@ def _buy_impl(symbol: str, amount: int, signature: str = None, today_date: str =
|
||||
|
||||
conn.commit()
|
||||
print(f"[buy] {signature} bought {amount} shares of {symbol} at ${this_symbol_price}")
|
||||
print(f"[DEBUG buy] Returning new_position: {new_position}")
|
||||
print(f"[DEBUG buy] new_position keys: {list(new_position.keys())}")
|
||||
return new_position
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user