Class: Tastytrade::Order

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

Overview

Represents an order to be placed

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#legsObject (readonly)

Returns the value of attribute legs.



73
74
75
# File 'lib/tastytrade/order.rb', line 73

def legs
  @legs
end

#priceObject (readonly)

Returns the value of attribute price.



73
74
75
# File 'lib/tastytrade/order.rb', line 73

def price
  @price
end

#time_in_forceObject (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

#typeObject (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.

Examples:

response = order.dry_run(session, )
puts response.buying_power_effect
puts response.warnings

Parameters:

Returns:



128
129
130
# File 'lib/tastytrade/order.rb', line 128

def dry_run(session, )
  .place_order(session, self, dry_run: true)
end

#limit?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/tastytrade/order.rb', line 90

def limit?
  @type == OrderType::LIMIT
end

#market?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/tastytrade/order.rb', line 86

def market?
  @type == OrderType::MARKET
end

#stop?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/tastytrade/order.rb', line 94

def stop?
  @type == OrderType::STOP
end

#to_api_paramsObject



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.

Examples:

order.validate!(session, )  # Full validation including dry-run
order.validate!(session, , skip_dry_run: true)  # Skip buying power check

Parameters:

Returns:

  • (Boolean)

    true if valid

Raises:



111
112
113
114
# File 'lib/tastytrade/order.rb', line 111

def validate!(session, , skip_dry_run: false)
  validator = OrderValidator.new(session, , self)
  validator.validate!(skip_dry_run: skip_dry_run)
end