Class: Tastytrade::OrderValidator

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

Overview

Validates orders before submission to ensure they meet all requirements. Performs comprehensive checks including symbol validation, quantity constraints, price validation, account permissions, buying power, and market hours.

Examples:

Basic usage

validator = OrderValidator.new(session, , order)
validator.validate! # Raises OrderValidationError if invalid

With dry-run validation

validator = OrderValidator.new(session, , order)
response = validator.dry_run_validate!
puts validator.warnings if validator.warnings.any?

Constant Summary collapse

TICK_SIZES =

Common tick sizes for different price ranges

{
  penny: BigDecimal("0.01"),
  nickel: BigDecimal("0.05"),
  dime: BigDecimal("0.10")
}.freeze
MIN_QUANTITY =

Minimum quantity constraints

1
MAX_QUANTITY =
999_999

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, account, order) ⇒ OrderValidator

Creates a new OrderValidator instance



42
43
44
45
46
47
48
49
50
# File 'lib/tastytrade/order_validator.rb', line 42

def initialize(session, , order)
  @session = session
  @account = 
  @order = order
  @errors = []
  @warnings = []
  @trading_status = nil
  @dry_run_response = nil
end

Instance Attribute Details

#errorsArray<String> (readonly)



21
22
23
# File 'lib/tastytrade/order_validator.rb', line 21

def errors
  @errors
end

#warningsArray<String> (readonly)



24
25
26
# File 'lib/tastytrade/order_validator.rb', line 24

def warnings
  @warnings
end

Instance Method Details

#dry_run_validate!Tastytrade::Models::OrderResponse?

Performs pre-flight validation via dry-run API call. This checks buying power, margin requirements, and API-level validation rules without placing the order.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tastytrade/order_validator.rb', line 83

def dry_run_validate!
  @dry_run_response = @account.place_order(@session, @order, dry_run: true)

  # Check for API-level errors
  if @dry_run_response.errors.any?
    @errors.concat(@dry_run_response.errors.map { |e| format_api_error(e) })
  end

  # Check for warnings
  if @dry_run_response.warnings.any?
    @warnings.concat(@dry_run_response.warnings)
  end

  # Check buying power effect
  if @dry_run_response.buying_power_effect
    validate_buying_power_effect!(@dry_run_response.buying_power_effect)
  end

  @dry_run_response
rescue StandardError => e
  @errors << "Dry-run validation failed: #{e.message}"
  nil
end

#validate!(skip_dry_run: false) ⇒ Boolean

Performs comprehensive order validation including structure, symbols, quantities, prices, account permissions, market hours, and optionally buying power via dry-run.

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tastytrade/order_validator.rb', line 59

def validate!(skip_dry_run: false)
  # Reset errors and warnings
  @errors = []
  @warnings = []

  # Run all validations
  validate_order_structure!
  validate_symbols!
  validate_quantities!
  validate_prices!
  
  validate_market_hours!
  validate_buying_power! unless skip_dry_run

  # Raise error if any validation failed
  raise OrderValidationError, @errors if @errors.any?

  true
end