Class: Tastytrade::Order
- Inherits:
-
Object
- Object
- Tastytrade::Order
- Defined in:
- lib/tastytrade/order.rb
Overview
Represents an order to be placed
Instance Attribute Summary collapse
-
#legs ⇒ Object
readonly
Returns the value of attribute legs.
-
#price ⇒ Object
readonly
Returns the value of attribute price.
-
#time_in_force ⇒ Object
readonly
Returns the value of attribute time_in_force.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#dry_run(session, account) ⇒ Tastytrade::Models::OrderResponse
Performs a dry-run validation of the order.
-
#initialize(type:, time_in_force: OrderTimeInForce::DAY, legs:, price: nil) ⇒ Order
constructor
A new instance of Order.
- #limit? ⇒ Boolean
- #market? ⇒ Boolean
- #stop? ⇒ Boolean
- #to_api_params ⇒ Object
-
#validate!(session, account, skip_dry_run: false) ⇒ Boolean
Validates this order for a specific account using the OrderValidator.
Constructor Details
#initialize(type:, time_in_force: OrderTimeInForce::DAY, legs:, price: nil) ⇒ Order
Returns a new instance of Order.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/tastytrade/order.rb', line 75 def initialize(type:, time_in_force: OrderTimeInForce::DAY, legs:, price: nil) validate_type!(type) validate_time_in_force!(time_in_force) validate_price!(type, price) @type = type @time_in_force = time_in_force @legs = Array(legs) @price = price ? BigDecimal(price.to_s) : nil end |
Instance Attribute Details
#legs ⇒ Object (readonly)
Returns the value of attribute legs.
73 74 75 |
# File 'lib/tastytrade/order.rb', line 73 def legs @legs end |
#price ⇒ Object (readonly)
Returns the value of attribute price.
73 74 75 |
# File 'lib/tastytrade/order.rb', line 73 def price @price end |
#time_in_force ⇒ Object (readonly)
Returns the value of attribute time_in_force.
73 74 75 |
# File 'lib/tastytrade/order.rb', line 73 def time_in_force @time_in_force end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
73 74 75 |
# File 'lib/tastytrade/order.rb', line 73 def type @type end |
Instance Method Details
#dry_run(session, account) ⇒ Tastytrade::Models::OrderResponse
Performs a dry-run validation of the order. This submits the order to the API in dry-run mode to check buying power, margin requirements, and get warnings without actually placing the order.
128 129 130 |
# File 'lib/tastytrade/order.rb', line 128 def dry_run(session, account) account.place_order(session, self, dry_run: true) end |
#limit? ⇒ Boolean
90 91 92 |
# File 'lib/tastytrade/order.rb', line 90 def limit? @type == OrderType::LIMIT end |
#market? ⇒ Boolean
86 87 88 |
# File 'lib/tastytrade/order.rb', line 86 def market? @type == OrderType::MARKET end |
#stop? ⇒ Boolean
94 95 96 |
# File 'lib/tastytrade/order.rb', line 94 def stop? @type == OrderType::STOP end |
#to_api_params ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/tastytrade/order.rb', line 132 def to_api_params params = { "order-type" => @type, "time-in-force" => @time_in_force, "legs" => @legs.map(&:to_api_params) } # Add price for limit orders # API expects string representation without negative sign if limit? && @price params["price"] = @price.to_s("F") params["price-effect"] = determine_price_effect end params end |
#validate!(session, account, skip_dry_run: false) ⇒ Boolean
Validates this order for a specific account using the OrderValidator. Performs comprehensive checks including symbol existence, quantity constraints, price validation, account permissions, and optionally buying power.
111 112 113 114 |
# File 'lib/tastytrade/order.rb', line 111 def validate!(session, account, skip_dry_run: false) validator = OrderValidator.new(session, account, self) validator.validate!(skip_dry_run: skip_dry_run) end |