Class: SchwabRb::DataObjects::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/schwab_rb/data_objects/order.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration:, order_type:, complex_order_strategy_type:, quantity:, filled_quantity:, remaining_quantity:, price:, order_strategy_type:, order_id:, status:, entered_time:, close_time:, order_leg_collection: [], order_activity_collection: []) ⇒ Order

Returns a new instance of Order.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/schwab_rb/data_objects/order.rb', line 125

def initialize(
  duration:, order_type:, complex_order_strategy_type:, quantity:, filled_quantity:,
  remaining_quantity:, price:, order_strategy_type:, order_id:, status:, entered_time:, close_time:, order_leg_collection: [], order_activity_collection: []
)
  @duration = duration
  @order_type = order_type
  @complex_order_strategy_type = complex_order_strategy_type
  @quantity = quantity
  @filled_quantity = filled_quantity
  @remaining_quantity = remaining_quantity
  @price = price
  @order_leg_collection = order_leg_collection
  @order_strategy_type = order_strategy_type
  @order_id = order_id
  @status = status
  @entered_time = entered_time
  @close_time = close_time
  @order_activity_collection = order_activity_collection
end

Instance Attribute Details

#close_timeObject (readonly)

Returns the value of attribute close_time.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def close_time
  @close_time
end

#complex_order_strategy_typeObject (readonly)

Returns the value of attribute complex_order_strategy_type.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def complex_order_strategy_type
  @complex_order_strategy_type
end

#durationObject (readonly)

Returns the value of attribute duration.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def duration
  @duration
end

#entered_timeObject (readonly)

Returns the value of attribute entered_time.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def entered_time
  @entered_time
end

#filled_quantityObject (readonly)

Returns the value of attribute filled_quantity.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def filled_quantity
  @filled_quantity
end

#order_activity_collectionObject (readonly)

Returns the value of attribute order_activity_collection.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def order_activity_collection
  @order_activity_collection
end

#order_idObject (readonly)

Returns the value of attribute order_id.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def order_id
  @order_id
end

#order_leg_collectionObject (readonly)

Returns the value of attribute order_leg_collection.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def order_leg_collection
  @order_leg_collection
end

#order_strategy_typeObject (readonly)

Returns the value of attribute order_strategy_type.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def order_strategy_type
  @order_strategy_type
end

#order_typeObject (readonly)

Returns the value of attribute order_type.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def order_type
  @order_type
end

#priceObject (readonly)

Returns the value of attribute price.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def price
  @price
end

#quantityObject (readonly)

Returns the value of attribute quantity.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def quantity
  @quantity
end

#remaining_quantityObject (readonly)

Returns the value of attribute remaining_quantity.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def remaining_quantity
  @remaining_quantity
end

#statusObject (readonly)

Returns the value of attribute status.



83
84
85
# File 'lib/schwab_rb/data_objects/order.rb', line 83

def status
  @status
end

Class Method Details

.build(data) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/schwab_rb/data_objects/order.rb', line 87

def build(data)
  # Parse datetime strings into DateTime objects
  entered_time = parse_datetime(data[:enteredTime])
  close_time = parse_datetime(data[:closeTime])

  new(
    duration: data[:duration],
    order_type: data[:orderType],
    complex_order_strategy_type: data[:complexOrderStrategyType],
    quantity: data[:quantity],
    filled_quantity: data[:filledQuantity],
    remaining_quantity: data[:remainingQuantity],
    price: data[:price],
    order_leg_collection: data.fetch(:orderLegCollection, []).map { |leg| OrderLeg.build(leg) },
    order_strategy_type: data[:orderStrategyType],
    order_id: data[:orderId],
    status: data[:status],
    entered_time: entered_time,
    close_time: close_time,
    order_activity_collection: data.fetch(:orderActivityCollection, []).map do |activity|
      OrderActivity.build(activity)
    end
  )
end

Instance Method Details

#close?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/schwab_rb/data_objects/order.rb', line 159

def close?
  order_leg_collection.all? { |leg| leg.position_effect == "CLOSING" }
end

#open?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/schwab_rb/data_objects/order.rb', line 163

def open?
  order_leg_collection.all? { |leg| leg.position_effect == "OPENING" }
end

#strategyObject



149
150
151
152
153
154
155
156
157
# File 'lib/schwab_rb/data_objects/order.rb', line 149

def strategy
  if %w[VERTICAL CUSTOM].include?(complex_order_strategy_type) && order_leg_collection.all?(&:call?)
    "CALL_SPREAD"
  elsif %w[VERTICAL CUSTOM].include?(complex_order_strategy_type) && order_leg_collection.all?(&:put?)
    "PUT_SPREAD"
  else
    order_strategy_type
  end
end

#symbolsObject



145
146
147
# File 'lib/schwab_rb/data_objects/order.rb', line 145

def symbols
  order_leg_collection.map(&:symbol)
end

#to_hObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/schwab_rb/data_objects/order.rb', line 167

def to_h
  {
    duration: @duration,
    orderType: @order_type,
    complexOrderStrategyType: @complex_order_strategy_type,
    quantity: @quantity,
    filledQuantity: @filled_quantity,
    remainingQuantity: @remaining_quantity,
    price: @price,
    orderLegCollection: @order_leg_collection.map(&:to_h),
    orderStrategyType: @order_strategy_type,
    orderId: @order_id,
    status: @status,
    enteredTime: @entered_time&.iso8601,
    closeTime: @close_time&.iso8601,
    orderActivityCollection: @order_activity_collection.map(&:to_h)
  }
end