Skip to content

Commit 715b688

Browse files
authored
Merge pull request modelcontextprotocol#18 from major/1st-triggers-all-order-fix
More 1st triggers all order fixes
2 parents 79880de + cf5aa9b commit 715b688

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/schwab_mcp/tools/orders.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,21 +453,43 @@ async def place_first_triggers_second_order(
453453
Params: account_hash, first_order_spec (dict), second_order_spec (dict).
454454
*Use build_equity_order_spec() or build_option_order_spec() to create the required spec dictionaries.* *Write operation.*
455455
"""
456-
# Manually construct the Trigger order dictionary structure
457-
# According to schwab-py's trigger_builder, the second order becomes a child of the first.
458-
# We modify the first spec dictionary directly.
456+
# Use the schwab-py library's construct_repeat_order to convert dicts to OrderBuilder objects,
457+
# then use the trigger_builder helper (same approach as place_bracket_order)
458+
from schwab.contrib.orders import construct_repeat_order
459+
459460
client = ctx.orders
460461

461-
# Use deep copy to avoid any reference issues with nested structures
462-
trigger_order_spec = copy.deepcopy(first_order_spec)
463-
trigger_order_spec["orderStrategyType"] = "TRIGGER"
464-
trigger_order_spec["childOrderStrategies"] = [copy.deepcopy(second_order_spec)]
462+
# Deep copy to avoid modifying the original specs
463+
first_spec_copy = copy.deepcopy(first_order_spec)
464+
second_spec_copy = copy.deepcopy(second_order_spec)
465+
466+
# Add orderLegType to each leg (required by construct_repeat_order)
467+
# Detect type based on instrument assetType
468+
for leg in first_spec_copy.get("orderLegCollection", []):
469+
if "orderLegType" not in leg:
470+
asset_type = leg.get("instrument", {}).get("assetType", "EQUITY")
471+
leg["orderLegType"] = asset_type
472+
473+
for leg in second_spec_copy.get("orderLegCollection", []):
474+
if "orderLegType" not in leg:
475+
asset_type = leg.get("instrument", {}).get("assetType", "EQUITY")
476+
leg["orderLegType"] = asset_type
477+
478+
# Convert dict specs to OrderBuilder objects using schwab-py's construct_repeat_order
479+
first_order_builder = construct_repeat_order(first_spec_copy)
480+
second_order_builder = construct_repeat_order(second_spec_copy)
481+
482+
# Use the schwab-py trigger_builder to create the TRIGGER order (same as bracket order does)
483+
trigger_order_builder = trigger_builder(first_order_builder, second_order_builder)
484+
485+
# Build the final order dictionary
486+
trigger_order_dict = cast(dict[str, Any], trigger_order_builder.build())
465487

466488
# Place the order
467489
return await call(
468490
client.place_order,
469491
account_hash=account_hash,
470-
order_spec=trigger_order_spec,
492+
order_spec=trigger_order_dict,
471493
)
472494

473495

0 commit comments

Comments
 (0)