Class: SchwabRb::Orders::Builder

Inherits:
Object
  • Object
show all
Includes:
EnumEnforcer
Defined in:
lib/schwab_rb/orders/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EnumEnforcer

#convert_enum, #convert_enum_iterable, #enforce_enums=, #enforce_enums?, #get_valid_enum_values, #set_enforce_enums, #type_error

Constructor Details

#initialize(enforce_enums: true) ⇒ Builder

Returns a new instance of Builder.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/schwab_rb/orders/builder.rb', line 47

def initialize(enforce_enums: true)
  @session = nil
  @account_number = nil
  @duration = nil
  @order_type = nil
  @complex_order_strategy_type = nil
  @quantity = nil
  @destination_link_name = nil
  @stop_price = nil
  @stop_price_link_basis = nil
  @stop_price_link_type = nil
  @stop_price_offset = nil
  @stop_type = nil
  @price_link_basis = nil
  @price_link_type = nil
  @price = nil
  @order_leg_collection = nil
  @activation_price = nil
  @special_instruction = nil
  @order_strategy_type = nil
  @child_order_strategies = nil
end

Class Method Details

.build(obj) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/schwab_rb/orders/builder.rb', line 18

def build(obj)
  case obj
  when String, Integer, Float
    obj
  when Hash
    obj.each_with_object({}) do |(key, val), acc|
      acc[camel_case(key)] = build(val)
    end
  when Array
    obj.map { |i| build(i) }
  else
    ret = {}
    obj.instance_variables.each do |var|
      value = obj.instance_variable_get(var)
      next if value.nil?

      name = var.to_s[1..]
      ret[camel_case(name)] = build(value)
    end
    ret
  end
end

.camel_case(snake_str) ⇒ Object



41
42
43
44
# File 'lib/schwab_rb/orders/builder.rb', line 41

def camel_case(snake_str)
  camel_case_str = snake_str.split("_").map(&:capitalize).join
  camel_case_str[0].downcase + camel_case_str[1..]
end

Instance Method Details

#add_child_order_strategy(child_order_strategy) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/schwab_rb/orders/builder.rb', line 151

def add_child_order_strategy(child_order_strategy)
  raise "child order must be OrderBuilder or Hash" unless [Builder, Hash].any? do |type|
    child_order_strategy.is_a? type
  end

  @child_order_strategies ||= []
  @child_order_strategies << child_order_strategy
end

#add_equity_leg(instruction, symbol, quantity) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/schwab_rb/orders/builder.rb', line 175

def add_equity_leg(instruction, symbol, quantity)
  raise "quantity must be positive" if quantity <= 0

  @order_leg_collection ||= []
  @order_leg_collection << {
    "instruction" => convert_enum(instruction, SchwabRb::Orders::EquityInstructions),
    "instrument" => SchwabRb::Orders::EquityInstrument.new(symbol),
    "quantity" => quantity
  }
end

#add_option_leg(instruction, symbol, quantity) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/schwab_rb/orders/builder.rb', line 164

def add_option_leg(instruction, symbol, quantity)
  raise "quantity must be positive" if quantity <= 0

  @order_leg_collection ||= []
  @order_leg_collection << {
    "instruction" => convert_enum(instruction, SchwabRb::Orders::OptionInstructions),
    "instrument" => SchwabRb::Orders::OptionInstrument.new(symbol),
    "quantity" => quantity
  }
end

#buildObject



191
192
193
# File 'lib/schwab_rb/orders/builder.rb', line 191

def build
  Builder.build(self)
end

#clear_account_numberObject



82
83
84
# File 'lib/schwab_rb/orders/builder.rb', line 82

def 
  @account_number = nil
end

#clear_child_order_strategiesObject



160
161
162
# File 'lib/schwab_rb/orders/builder.rb', line 160

def clear_child_order_strategies
  @child_order_strategies = nil
end

#clear_complex_order_strategy_typeObject



147
148
149
# File 'lib/schwab_rb/orders/builder.rb', line 147

def clear_complex_order_strategy_type
  @complex_order_strategy_type = nil
end

#clear_durationObject



90
91
92
# File 'lib/schwab_rb/orders/builder.rb', line 90

def clear_duration
  @duration = nil
end

#clear_order_legsObject



186
187
188
189
# File 'lib/schwab_rb/orders/builder.rb', line 186

def clear_order_legs
  @order_leg_collection = nil
  self
end

#clear_order_strategy_typeObject



136
137
138
# File 'lib/schwab_rb/orders/builder.rb', line 136

def clear_order_strategy_type
  @order_strategy_type = nil
end

#clear_order_typeObject



98
99
100
# File 'lib/schwab_rb/orders/builder.rb', line 98

def clear_order_type
  @order_type = nil
end

#clear_priceObject



116
117
118
# File 'lib/schwab_rb/orders/builder.rb', line 116

def clear_price
  @price = nil
end

#clear_quantityObject



108
109
110
# File 'lib/schwab_rb/orders/builder.rb', line 108

def clear_quantity
  @quantity = nil
end

#clear_sessionObject



74
75
76
# File 'lib/schwab_rb/orders/builder.rb', line 74

def clear_session
  @session = nil
end

#clear_stop_priceObject



128
129
130
# File 'lib/schwab_rb/orders/builder.rb', line 128

def clear_stop_price
  @stop_price = nil
end

#copy_stop_price(stop_price) ⇒ Object



124
125
126
# File 'lib/schwab_rb/orders/builder.rb', line 124

def copy_stop_price(stop_price)
  @stop_price = stop_price
end

#set_account_number(account_number) ⇒ Object



78
79
80
# File 'lib/schwab_rb/orders/builder.rb', line 78

def ()
  @account_number = 
end

#set_complex_order_strategy_type(complex_order_strategy_type) ⇒ Object



140
141
142
143
144
145
# File 'lib/schwab_rb/orders/builder.rb', line 140

def set_complex_order_strategy_type(complex_order_strategy_type)
  @complex_order_strategy_type = convert_enum(
    complex_order_strategy_type,
    SchwabRb::Order::ComplexOrderStrategyTypes
  )
end

#set_duration(duration) ⇒ Object



86
87
88
# File 'lib/schwab_rb/orders/builder.rb', line 86

def set_duration(duration)
  @duration = convert_enum(duration, SchwabRb::Orders::Duration)
end

#set_order_strategy_type(order_strategy_type = "SINGLE") ⇒ Object



132
133
134
# File 'lib/schwab_rb/orders/builder.rb', line 132

def set_order_strategy_type(order_strategy_type = "SINGLE")
  @order_strategy_type = order_strategy_type
end

#set_order_type(order_type) ⇒ Object



94
95
96
# File 'lib/schwab_rb/orders/builder.rb', line 94

def set_order_type(order_type)
  @order_type = convert_enum(order_type, SchwabRb::Order::Types)
end

#set_price(price) ⇒ Object



112
113
114
# File 'lib/schwab_rb/orders/builder.rb', line 112

def set_price(price)
  @price = price
end

#set_quantity(quantity) ⇒ Object



102
103
104
105
106
# File 'lib/schwab_rb/orders/builder.rb', line 102

def set_quantity(quantity)
  raise "quantity must be positive" if quantity <= 0

  @quantity = quantity
end

#set_session(session) ⇒ Object



70
71
72
# File 'lib/schwab_rb/orders/builder.rb', line 70

def set_session(session)
  @session = convert_enum(session, SchwabRb::Orders::Session)
end

#set_stop_price(stop_price) ⇒ Object



120
121
122
# File 'lib/schwab_rb/orders/builder.rb', line 120

def set_stop_price(stop_price)
  @stop_price = stop_price.is_a?(String) ? stop_price : truncate_float(stop_price)
end