Skip to content

"Vibe-Trading: Your Personal Trading Agent"

15.9 KB
Raw Blame
{
  "$comment": "FINALIZED pt-v1.2 — ADDITIVE bump of the frozen pt-v1.1 contract (schema/tool_contracts/paper_trade_step-v1.1.json, which is NOT edited). Scope: the cn_a 5m post-close step (P0 execution plan T2), decisions D1-D8 of the paper-trade-p0-5m-execution decision record. Additions only (REQUIREMENTS §9: additive = minor bump): request-side interval/session/path_model/fill_model are all optional and default to the pt-v1.1 semantics (1d/auto/bar_median/all_or_none); result-side engine_fingerprint/canceled_orders/events are emitted ONLY for a v1.2 call, where v1.2 means at least one new request field is non-default. A call that omits every new field is a pt-v1.1 call: identical bytes, identical received_state_hash, schema_version 'pt-v1.1' (D4/D8). PHASE-2 SEMANTICS: pt-v1.2 adds NO order-level bar/timestamp field, so a fill is attributed to the bar whose reference price produced it, not to a client order timestamp. For interval='5m' the engine matches bar by bar over the 48 continuous bars; a market order fills at the first marketable bar or is canceled immediately if the price band blocks its side (pt-rules-v3), a resting limit order fills at its limit price once a bar trades strictly through it (pt-rules-v2; on the first continuous bar a strictly better reference price fills at that price) and is canceled at session end if never triggered, and no partial fills or volume share are modeled (Phase 3). HK/US 5m is out of scope. The dependency-free validator in agent/src/tools/paper_trade/schema.py implements a keyword subset (type/const/enum/required/properties/items/additionalProperties) and ignores the version-gated allOf/if/then block in the result schema; pattern/minLength are documentation for the real jsonschema library, which can consume this standard draft-2020-12 document.",
  "contract": "paper_trade_step",
  "version": "pt-v1.2",
  "status": "final",
  "schemas": {
    "result": {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "$id": "https://vibe-trading.local/schemas/tool_contracts/paper_trade_step-v1.2/result.json",
      "title": "paper_trade_step success result (pt-v1.2)",
      "type": "object",
      "required": [
        "schema_version",
        "tool",
        "account_id",
        "market",
        "range",
        "received_state_hash",
        "effective_fee_model",
        "fills",
        "rejected_orders",
        "end_state",
        "nav_series",
        "skipped_days",
        "warnings",
        "data_provenance"
      ],
      "additionalProperties": false,
      "properties": {
        "schema_version": {
          "type": "string",
          "enum": ["pt-v1.1", "pt-v1.2"],
          "$comment": "Which version a response emits: 'pt-v1.1' when every new request field is at its default (byte-identical to the frozen v1.1 document, no v1.2 keys at all); 'pt-v1.2' when at least one new request field is non-default (D8). A 'pt-v1.1' document remains governed by paper_trade_step-v1.1.json."
        },
        "tool": { "type": "string", "const": "paper_trade_step" },
        "account_id": {
          "type": "string",
          "minLength": 1,
          "$comment": "verbatim echo of the input (R4)"
        },
        "market": {
          "type": "string",
          "enum": ["cn_a", "hk", "us"]
        },
        "range": {
          "type": "object",
          "required": ["from", "to"],
          "additionalProperties": false,
          "properties": {
            "from": { "type": "string" },
            "to": { "type": "string" }
          }
        },
        "received_state_hash": {
          "type": "string",
          "$comment": "sha256(JCS(non-order inputs)) — AM-4/R7; 64 lowercase hex. The four new request fields enter the coverage object ONLY when non-default (D4), so a v1.1 call hashes exactly as before."
        },
        "effective_fee_model": {
          "type": "object",
          "$comment": "Full echo of the fee parameters in effect (R7-3). Default key names FROZEN per APPROVAL note 5 / REQUIREMENTS §4 (state_hash.py _DEFAULT_FEE_MODELS, delivery note #7): cn_a = commission_rate 0.00025, commission_min 5.0, stamp_tax_sell 0.0005, transfer_fee 0.00001; hk = commission_rate 0.0003, commission_min 3.0, stamp_tax_rate 0.001, regulatory_levy_rate 0.00003; us = commission_per_share 0.0049, commission_min 0.99, sec_fee_rate 0.0. Per-key merge: RM always injects fee_model explicitly (AM-4-3) and injected keys pass through, so the object stays open by design."
        },
        "fills": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["trade_date", "symbol", "side", "qty", "price", "fee", "reason"],
            "additionalProperties": false,
            "properties": {
              "trade_date": { "type": "string" },
              "symbol": { "type": "string" },
              "side": {
                "type": "string",
                "enum": ["buy", "sell"]
              },
              "qty": { "type": "number" },
              "price": { "type": "number" },
              "fee": { "type": "number" },
              "reason": {
                "type": "string",
                "$comment": "VD-15: orders mode = empty string; strategy_path mode = strategy reason text truncated to <=200 chars (UTF-8 safe)"
              }
            }
          }
        },
        "rejected_orders": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["trade_date", "symbol", "side", "qty", "reason_code", "detail"],
            "additionalProperties": false,
            "properties": {
              "trade_date": { "type": "string" },
              "symbol": { "type": "string" },
              "side": {
                "type": "string",
                "enum": ["buy", "sell"]
              },
              "qty": { "type": "number" },
              "reason_code": {
                "type": "string",
                "enum": [
                  "insufficient_cash",
                  "insufficient_position",
                  "t_plus_1",
                  "limit_up",
                  "limit_down",
                  "suspended",
                  "lot_size",
                  "non_trading_day",
                  "unknown_symbol",
                  "corporate_action_unresolved",
                  "limit_not_marketable"
                ],
                "$comment": "AM-14 closed vocabulary (11) — FROZEN, unchanged by pt-v1.2 (D2): the emergency-cancel event lives in canceled_orders, never here"
              },
              "detail": { "type": "string" }
            }
          }
        },
        "canceled_orders": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["trade_date", "symbol", "side", "qty", "reason_code", "detail"],
            "additionalProperties": false,
            "properties": {
              "trade_date": { "type": "string" },
              "symbol": { "type": "string" },
              "side": {
                "type": "string",
                "enum": ["buy", "sell"]
              },
              "qty": { "type": "number" },
              "reason_code": {
                "type": "string",
                "const": "canceled_after_failed_match",
                "$comment": "T3 emergency cancel: one row per failed order in 5m mode (D2). Never added to the frozen rejected_orders vocabulary."
              },
              "detail": { "type": "string" }
            }
          },
          "$comment": "pt-v1.2 only. Emitted (possibly empty) for a v1.2 call; omitted entirely for a pt-v1.1 call, which stays byte-identical (D2/D8)."
        },
        "events": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["trade_date", "symbol", "event_type", "detail"],
            "additionalProperties": false,
            "properties": {
              "trade_date": { "type": "string" },
              "symbol": { "type": "string" },
              "event_type": {
                "type": "string",
                "enum": ["non_trading_day", "halted", "price_limit", "auction_window"],
                "$comment": "Session facts (T4, D2). halted = calendar says open, the 5m fetch succeeded, another symbol has bars that day, and this symbol has zero bars for the session (D6)."
              },
              "detail": { "type": "string" }
            }
          },
          "$comment": "pt-v1.2 only. Emitted (possibly empty) for a v1.2 call; omitted entirely for a pt-v1.1 call (D2/D8)."
        },
        "end_state": {
          "type": "object",
          "required": ["cash", "positions"],
          "additionalProperties": false,
          "properties": {
            "cash": { "type": "number" },
            "positions": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["symbol", "qty", "avg_cost"],
                "additionalProperties": false,
                "properties": {
                  "symbol": { "type": "string" },
                  "qty": { "type": "number" },
                  "avg_cost": { "type": "number" }
                }
              }
            }
          }
        },
        "nav_series": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["date", "nav", "cash", "position_value"],
            "additionalProperties": false,
            "properties": {
              "date": { "type": "string" },
              "nav": { "type": "number" },
              "cash": { "type": "number" },
              "position_value": { "type": "number" }
            }
          }
        },
        "skipped_days": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["date", "reason"],
            "additionalProperties": false,
            "properties": {
              "date": { "type": "string" },
              "reason": {
                "type": "string",
                "enum": ["non_trading_day", "market_closed_inferred"],
                "$comment": "AM-14 closed vocabulary (2): non_trading_day = cn_a only (CH calendar truth); market_closed_inferred = hk/us only, always accompanied by a warning (AM-6)"
              }
            }
          }
        },
        "warnings": {
          "type": "array",
          "items": { "type": "string" },
          "$comment": "deterministic English strings, never contain timestamps (VD-7 / R1 hard semantics 1)"
        },
        "data_provenance": {
          "type": "object",
          "required": ["source"],
          "additionalProperties": false,
          "properties": {
            "source": {
              "type": "string",
              "$comment": "AM-9 merged-frame form for cn_a, e.g. 'clickhouse+tencent(merged)'"
            },
            "ch_as_of": { "type": "string" },
            "network_as_of": { "type": "string" },
            "bars_as_of": { "type": "string" },
            "corporate_actions": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["symbol", "date", "ratio", "detected"],
                "additionalProperties": false,
                "properties": {
                  "symbol": { "type": "string" },
                  "date": { "type": "string" },
                  "ratio": {
                    "type": "string",
                    "$comment": "clean hit = ratio label (e.g. '4:1'); unresolved = the observed close-to-close ratio as a decimal string (fail-closed fact, never a guess)"
                  },
                  "detected": {
                    "type": "boolean",
                    "$comment": "true = clean split applied; false = unresolved fail-closed rejection (corporate_action_unresolved). Typed to match the T4 emitter (corporate_actions.py); reconciled from the draft's string typing at G1 finalization as its module docstring assigned to T5"
                  }
                }
              }
            },
            "limit_provenance": {
              "type": "array",
              "items": { "type": "string" },
              "$comment": "VD-11 cn_a limit/ST/suspension path tokens, '<symbol>@<date>:<token>' — token set frozen (limits_cn_a.py): limits:stk_limit / limits:heuristic / limits:unavailable / st:stk_st_daily / st:stk_st_daily-no-row / suspend:stk_suspend / suspend:stk_suspend-no-row / limit_list:stk_limit_list_d. Present only for cn_a runs that consulted the band seam"
            }
          },
          "$comment": "Extension set FINALIZED at G1 (was draft-open): AM-9 merge-state fields (ch_as_of/network_as_of/bars_as_of), T4 corporate_actions, VD-11 limit_provenance. Optional fields are omitted when unset, never null. Unchanged by pt-v1.2."
        },
        "engine_fingerprint": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$",
          "$comment": "Required in a pt-v1.2 document (D3/D8): sha256(JCS({interval, path_model, fill_model, rules_version, bar_fingerprint})) as lowercase hex. No paths, no clock, no dict iteration order. Out field only — it never changes matching. bar_fingerprint is supplied by the engine (T3/T4); '' means no bar fingerprint was supplied (e.g. a cash-only account)."
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "schema_version": { "const": "pt-v1.2" }
            },
            "required": ["schema_version"]
          },
          "then": {
            "required": ["engine_fingerprint", "canceled_orders", "events"]
          },
          "$comment": "Version-gated requirement: engine_fingerprint/canceled_orders/events are mandatory ONLY for a pt-v1.2 document, so a pt-v1.1-labelled document stays valid under this additive file (it is governed by paper_trade_step-v1.1.json). The dependency-free validator in agent/src/tools/paper_trade/schema.py deliberately ignores if/then/allOf (it implements a keyword subset); wire.build_success_payload enforces the same rule in Python — it raises unless a pt-v1.2 fingerprint is 64 lowercase hex and always emits canceled_orders/events for a v1.2 result."
        }
      ]
    },
    "error_envelope": {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "$id": "https://vibe-trading.local/schemas/tool_contracts/paper_trade_step-v1.2/error_envelope.json",
      "title": "paper_trade_step in-band error envelope (pt-v1.2, AM-5)",
      "type": "object",
      "required": ["status", "code", "account_id", "market", "range", "message"],
      "additionalProperties": false,
      "properties": {
        "status": { "type": "string", "const": "error" },
        "code": {
          "type": "string",
          "enum": ["PAPER_DATA_NOT_READY", "INVALID_REQUEST"],
          "$comment": "AM-14 closed vocabulary (2 + transport reserved): PAPER_DATA_NOT_READY = retryable (RM 30min x 3); INVALID_REQUEST = non-retryable. Transport-level MCP errors are reserved for engine-internal faults. Unchanged by pt-v1.2."
        },
        "account_id": { "type": "string", "$comment": "verbatim echo even on the error path (AM-4-4/AM-5)" },
        "market": {
          "type": "string",
          "enum": ["cn_a", "hk", "us"]
        },
        "range": {
          "type": "object",
          "required": ["from", "to"],
          "additionalProperties": false,
          "properties": {
            "from": { "type": "string" },
            "to": { "type": "string" }
          }
        },
        "received_state_hash": {
          "type": "string",
          "$comment": "present only when the received account_state was parseable (AM-4-4); omitted otherwise, never null — RM reads the absence as 'state unparseable' and must not burn its retry chain"
        },
        "message": {
          "type": "string",
          "$comment": "deterministic English, no timestamps (VD-7). Frozen formats include strategy_execution_timeout:{YYYY-MM-DD} and strategy_execution_error:{YYYY-MM-DD}:{exception class} (VD-8/B1, Step 7). pt-v1.2 adds 'interval 5m is cn_a-only in pt-v1.2' and the cn_a session gate."
        }
      }
    }
  }
}