Class: SchwabRb::Orders::OcoOrder

Inherits:
Object
  • Object
show all
Defined in:
lib/schwab_rb/orders/oco_order.rb

Class Method Summary collapse

Class Method Details

.build(child_order_specs:) ⇒ SchwabRb::Orders::Builder

Build an OCO (One Cancels Another) order with 2 or more child orders

Examples:

Simple OCO with two equity orders

OcoOrder.build(
  child_order_specs: [
    {
      strategy_type: 'single',
      symbol: 'XYZ   240315C00500000',
      price: 45.97,
      account_number: '12345',
      order_instruction: :close,
      credit_debit: :credit,
      quantity: 2
    },
    {
      strategy_type: 'single',
      symbol: 'XYZ   240315C00500000',
      price: 37.00,
      stop_price: 37.03,
      account_number: '12345',
      order_instruction: :close,
      credit_debit: :credit,
      quantity: 2
    }
  ]
)

Parameters:

  • child_order_specs (Array<Hash>)

    Array of order specifications for child orders. Each spec should include all parameters needed for OrderFactory.build

Returns:

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/schwab_rb/orders/oco_order.rb', line 39

def build(child_order_specs:)
  raise ArgumentError, "OCO orders require at least 2 child orders" if child_order_specs.length < 2

  builder = schwab_order_builder.new
  builder.set_order_strategy_type(SchwabRb::Order::OrderStrategyTypes::OCO)

  # Build each child order using OrderFactory
  child_order_specs.each do |child_spec|
    child_order = build_child_order(child_spec)
    builder.add_child_order_strategy(child_order)
  end

  builder
end