Implementation:HKUDS AI Trader Handle Trading Result
| Knowledge Sources | |
|---|---|
| Domains | Trading_Systems, Data_Persistence |
| Last Updated | 2026-02-09 14:00 GMT |
Overview
Concrete tool for recording daily trading outcomes and ensuring position log continuity in the BaseAgent class.
Description
The BaseAgent._handle_trading_result() async method checks the IF_TRADE runtime config flag. If False (no trade occurred), it calls add_no_trade_record(today_date, self.signature) which appends a no-trade entry to position.jsonl. Regardless, it resets IF_TRADE to False via write_config_value().
The companion add_no_trade_record() function reads the latest position from position.jsonl, creates a record with action "no_trade" and the carried-forward positions, and appends it.
Usage
Called internally at the end of BaseAgent.run_trading_session() after the reasoning loop completes. Not intended for direct external use.
Code Reference
Source Location
- Repository: AI-Trader
- File: agent/base_agent/base_agent.py (L521-534), tools/price_tools.py (L928-969)
Signature
# BaseAgent._handle_trading_result (base_agent.py:L521-534):
async def _handle_trading_result(self, today_date: str) -> None:
"""
Record trading outcome for the day.
Args:
today_date: Trading date YYYY-MM-DD
Returns:
None. Writes to position.jsonl if no trade occurred.
"""
# add_no_trade_record (price_tools.py:L928-969):
def add_no_trade_record(today_date: str, signature: str):
"""
Append a no-trade position record for the given date.
Args:
today_date: Trading date YYYY-MM-DD
signature: Agent identifier
Returns:
None. Appends to position.jsonl.
"""
Import
from tools.price_tools import add_no_trade_record
from tools.general_tools import write_config_value, get_config_value
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| today_date | str | Yes | Trading date YYYY-MM-DD |
| self.signature | str | Yes | Agent identifier for position file lookup |
| IF_TRADE | bool (config) | Yes | Runtime flag set by MCP trade tools |
Outputs
| Name | Type | Description |
|---|---|---|
| position.jsonl | File (append) | No-trade record appended if IF_TRADE was False |
| IF_TRADE | bool (config) | Reset to False |
Usage Examples
Internal Usage
# At end of run_trading_session():
await self._handle_trading_result(today_date)
# Checks IF_TRADE, records no-trade if needed, resets flag
Related Pages
Uses Heuristic
- Heuristic:HKUDS_AI_Trader_A_Share_Lot_Size_Rule
- Heuristic:HKUDS_AI_Trader_Position_File_Locking
- Heuristic:HKUDS_AI_Trader_Market_Type_Auto_Detection