Class: Tastytrade::Models::Account

Inherits:
Base
  • Object
show all
Defined in:
lib/tastytrade/models/account.rb

Overview

Represents a Tastytrade account

Instance Attribute Summary collapse

Attributes inherited from Base

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Tastytrade::Models::Base

Instance Attribute Details

#account_numberObject (readonly)

Returns the value of attribute account_number.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def 
  @account_number
end

#account_type_nameObject (readonly)

Returns the value of attribute account_type_name.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def 
  @account_type_name
end

#closed_atObject (readonly)

Returns the value of attribute closed_at.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def closed_at
  @closed_at
end

#created_atObject (readonly)

Returns the value of attribute created_at.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def created_at
  @created_at
end

#day_trader_statusObject (readonly)

Returns the value of attribute day_trader_status.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def day_trader_status
  @day_trader_status
end

#external_idObject (readonly)

Returns the value of attribute external_id.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def external_id
  @external_id
end

#funding_dateObject (readonly)

Returns the value of attribute funding_date.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def funding_date
  @funding_date
end

#investment_objectiveObject (readonly)

Returns the value of attribute investment_objective.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def investment_objective
  @investment_objective
end

#is_closedObject (readonly)

Returns the value of attribute is_closed.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def is_closed
  @is_closed
end

#is_foreignObject (readonly)

Returns the value of attribute is_foreign.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def is_foreign
  @is_foreign
end

#is_futures_approvedObject (readonly)

Returns the value of attribute is_futures_approved.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def is_futures_approved
  @is_futures_approved
end

#is_test_driveObject (readonly)

Returns the value of attribute is_test_drive.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def is_test_drive
  @is_test_drive
end

#margin_or_cashObject (readonly)

Returns the value of attribute margin_or_cash.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def margin_or_cash
  @margin_or_cash
end

#nicknameObject (readonly)

Returns the value of attribute nickname.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def nickname
  @nickname
end

#opened_atObject (readonly)

Returns the value of attribute opened_at.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def opened_at
  @opened_at
end

#suitable_options_levelObject (readonly)

Returns the value of attribute suitable_options_level.



7
8
9
# File 'lib/tastytrade/models/account.rb', line 7

def suitable_options_level
  @suitable_options_level
end

Class Method Details

.get(session, account_number) ⇒ Account

Get a specific account by account number

Parameters:

Returns:



31
32
33
34
# File 'lib/tastytrade/models/account.rb', line 31

def get(session, )
  response = session.get("/accounts/#{}/")
  new(response["data"])
end

.get_all(session, include_closed: false) ⇒ Array<Account>

Get all accounts for the authenticated user

Parameters:

  • session (Tastytrade::Session)

    Active session

  • include_closed (Boolean) (defaults to: false)

    Include closed accounts

Returns:

  • (Array<Account>)

    List of accounts



20
21
22
23
24
# File 'lib/tastytrade/models/account.rb', line 20

def get_all(session, include_closed: false)
  params = include_closed ? { "include-closed" => true } : {}
  response = session.get("/customers/me/accounts/", params)
  response["data"]["items"].map { |item| new(item["account"]) }
end

Instance Method Details

#cancel_order(session, order_id) ⇒ void

This method returns an undefined value.

Cancel an order

Parameters:

Raises:



186
187
188
189
190
191
# File 'lib/tastytrade/models/account.rb', line 186

def cancel_order(session, order_id)
  session.delete("/accounts/#{}/orders/#{order_id}/")
  nil
rescue Tastytrade::Error => e
  handle_cancel_error(e)
end

#closed?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/tastytrade/models/account.rb', line 209

def closed?
  @is_closed == true
end

#foreign?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/tastytrade/models/account.rb', line 221

def foreign?
  @is_foreign == true
end

#futures_approved?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/tastytrade/models/account.rb', line 213

def futures_approved?
  @is_futures_approved == true
end

#get_balances(session) ⇒ AccountBalance

Get account balances

Parameters:

Returns:



41
42
43
44
# File 'lib/tastytrade/models/account.rb', line 41

def get_balances(session)
  response = session.get("/accounts/#{}/balances/")
  AccountBalance.new(response["data"])
end

#get_live_orders(session, status: nil, underlying_symbol: nil, from_time: nil, to_time: nil) ⇒ Array<LiveOrder>

Get live orders (open and orders from last 24 hours)

Parameters:

  • session (Tastytrade::Session)

    Active session

  • status (String, nil) (defaults to: nil)

    Filter by order status

  • underlying_symbol (String, nil) (defaults to: nil)

    Filter by underlying symbol

  • from_time (Time, nil) (defaults to: nil)

    Start time for order history

  • to_time (Time, nil) (defaults to: nil)

    End time for order history

Returns:

  • (Array<LiveOrder>)

    Array of live orders



134
135
136
137
138
139
140
141
142
143
# File 'lib/tastytrade/models/account.rb', line 134

def get_live_orders(session, status: nil, underlying_symbol: nil, from_time: nil, to_time: nil)
  params = {}
  params["status"] = status if status && OrderStatus.valid?(status)
  params["underlying-symbol"] = underlying_symbol if underlying_symbol
  params["from-time"] = from_time.iso8601 if from_time
  params["to-time"] = to_time.iso8601 if to_time

  response = session.get("/accounts/#{}/orders/live/", params)
  response["data"]["items"].map { |item| LiveOrder.new(item) }
end

#get_order(session, order_id) ⇒ LiveOrder

Get a specific order by ID

Parameters:

Returns:



174
175
176
177
# File 'lib/tastytrade/models/account.rb', line 174

def get_order(session, order_id)
  response = session.get("/accounts/#{}/orders/#{order_id}/")
  LiveOrder.new(response["data"])
end

#get_order_history(session, status: nil, underlying_symbol: nil, from_time: nil, to_time: nil, page_offset: nil, page_limit: nil) ⇒ Array<LiveOrder>

Get order history for this account (beyond 24 hours)

Parameters:

  • session (Tastytrade::Session)

    Active session

  • status (String, nil) (defaults to: nil)

    Filter by order status

  • underlying_symbol (String, nil) (defaults to: nil)

    Filter by underlying symbol

  • from_time (Time, nil) (defaults to: nil)

    Start time for order history

  • to_time (Time, nil) (defaults to: nil)

    End time for order history

  • page_offset (Integer, nil) (defaults to: nil)

    Pagination offset

  • page_limit (Integer, nil) (defaults to: nil)

    Number of results per page (default 250, max 1000)

Returns:

  • (Array<LiveOrder>)

    Array of historical orders



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/tastytrade/models/account.rb', line 155

def get_order_history(session, status: nil, underlying_symbol: nil, from_time: nil, to_time: nil,
                     page_offset: nil, page_limit: nil)
  params = {}
  params["status"] = status if status && OrderStatus.valid?(status)
  params["underlying-symbol"] = underlying_symbol if underlying_symbol
  params["from-time"] = from_time.iso8601 if from_time
  params["to-time"] = to_time.iso8601 if to_time
  params["page-offset"] = page_offset if page_offset
  params["page-limit"] = page_limit if page_limit

  response = session.get("/accounts/#{}/orders/", params)
  response["data"]["items"].map { |item| LiveOrder.new(item) }
end

#get_positions(session, symbol: nil, underlying_symbol: nil, include_closed: false) ⇒ Array<CurrentPosition>

Get current positions

Parameters:

  • session (Tastytrade::Session)

    Active session

  • symbol (String, nil) (defaults to: nil)

    Filter by symbol

  • underlying_symbol (String, nil) (defaults to: nil)

    Filter by underlying symbol

  • include_closed (Boolean) (defaults to: false)

    Include closed positions

Returns:



53
54
55
56
57
58
59
60
61
# File 'lib/tastytrade/models/account.rb', line 53

def get_positions(session, symbol: nil, underlying_symbol: nil, include_closed: false)
  params = {}
  params["symbol"] = symbol if symbol
  params["underlying-symbol"] = underlying_symbol if underlying_symbol
  params["include-closed"] = include_closed if include_closed

  response = session.get("/accounts/#{}/positions/", params)
  response["data"]["items"].map { |item| CurrentPosition.new(item) }
end

#get_trading_status(session) ⇒ Tastytrade::Models::TradingStatus

Get trading status

Parameters:

Returns:



67
68
69
70
# File 'lib/tastytrade/models/account.rb', line 67

def get_trading_status(session)
  response = session.get("/accounts/#{}/trading-status/")
  TradingStatus.new(response["data"])
end

#get_transactions(session, **options) ⇒ Array<Transaction>

Get transaction history

Parameters:

Options Hash (**options):

  • :start_date (Date, String)

    Start date for transactions

  • :end_date (Date, String)

    End date for transactions

  • :symbol (String)

    Filter by symbol

  • :underlying_symbol (String)

    Filter by underlying symbol

  • :instrument_type (String)

    Filter by instrument type

  • :transaction_types (Array<String>)

    Filter by transaction types

  • :per_page (Integer)

    Number of results per page (default: 250)

  • :page_offset (Integer)

    Page offset for pagination

Returns:



122
123
124
# File 'lib/tastytrade/models/account.rb', line 122

def get_transactions(session, **options)
  Transaction.get_all(session, , **options)
end

#place_order(session, order, dry_run: false, skip_validation: false) ⇒ OrderResponse

Places an order for this account with comprehensive validation. By default, performs full validation including symbol checks, quantity limits, price validation, account permissions, and buying power verification.

Examples:

Place an order with validation

response = .place_order(session, order)
puts response.order_id

Dry-run to check buying power

response = .place_order(session, order, dry_run: true)
puts response.buying_power_effect

Skip validation when certain order is valid

response = .place_order(session, order, skip_validation: true)

Parameters:

  • session (Tastytrade::Session)

    Active session

  • order (Tastytrade::Order)

    Order to place

  • dry_run (Boolean) (defaults to: false)

    Whether to simulate the order without placing it

  • skip_validation (Boolean) (defaults to: false)

    Skip pre-submission validation (use with caution)

Returns:

  • (OrderResponse)

    Response from order placement with order ID and status

Raises:



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tastytrade/models/account.rb', line 95

def place_order(session, order, dry_run: false, skip_validation: false)
  # Validate the order unless explicitly skipped or it's a dry-run
  unless skip_validation || dry_run
    validator = OrderValidator.new(session, self, order)
    validator.validate!
  end

  endpoint = "/accounts/#{}/orders"
  endpoint += "/dry-run" if dry_run

  response = session.post(endpoint, order.to_api_params)
  OrderResponse.new(response["data"])
end

#replace_order(session, order_id, new_order) ⇒ OrderResponse

Replace an existing order

Parameters:

Returns:

Raises:



201
202
203
204
205
206
207
# File 'lib/tastytrade/models/account.rb', line 201

def replace_order(session, order_id, new_order)
  response = session.put("/accounts/#{}/orders/#{order_id}/",
                          new_order.to_api_params)
  OrderResponse.new(response["data"])
rescue Tastytrade::Error => e
  handle_replace_error(e)
end

#test_drive?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/tastytrade/models/account.rb', line 217

def test_drive?
  @is_test_drive == true
end