validate_trades
Check trade history to ensure sell transactions do not exceed purchased share quantities, identifying any violations with specific error messages.
Instructions
Validate that no trades sell more shares than have been bought.
Parses the trade history sequentially to ensure all sell transactions occur only after a corresponding quantity of shares has been bought.
Returns: A list of error messages for invalid trades, if any.
Example: validate_trades() -> ["Invalid sell on 2024-02-10: 20 shares of AAPL (owned: 10)"]
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Output Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- trader_tools.py:162-183 (handler)The validate_trades function iterates through the trade history dataframe and checks if sell orders exceed the currently owned shares, returning a list of violations.
def validate_trades() -> list[str]: """Validate that no trades sell more shares than have been bought. Parses the trade history sequentially to ensure all sell transactions occur only after a corresponding quantity of shares has been bought. Returns: A list of error messages for invalid trades, if any. Example: validate_trades() -> ["Invalid sell on 2024-02-10: 20 shares of AAPL (owned: 10)"] """ errors = [] balances = defaultdict(int) for idx, row in df.iterrows(): if row['type'] == 'Buy': balances[row['symbol']] += row['shares'] else: if balances[row['symbol']] < row['shares']: errors.append(f"Invalid sell on {row['date']}: {row['shares']} shares of {row['symbol']} (owned: {balances[row['symbol']]})") balances[row['symbol']] -= row['shares'] return errors