Class: Tastytrade::Models::Account
- Defined in:
- lib/tastytrade/models/account.rb
Overview
Represents a Tastytrade account
Instance Attribute Summary collapse
-
#account_number ⇒ Object
readonly
Returns the value of attribute account_number.
-
#account_type_name ⇒ Object
readonly
Returns the value of attribute account_type_name.
-
#closed_at ⇒ Object
readonly
Returns the value of attribute closed_at.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#day_trader_status ⇒ Object
readonly
Returns the value of attribute day_trader_status.
-
#external_id ⇒ Object
readonly
Returns the value of attribute external_id.
-
#funding_date ⇒ Object
readonly
Returns the value of attribute funding_date.
-
#investment_objective ⇒ Object
readonly
Returns the value of attribute investment_objective.
-
#is_closed ⇒ Object
readonly
Returns the value of attribute is_closed.
-
#is_foreign ⇒ Object
readonly
Returns the value of attribute is_foreign.
-
#is_futures_approved ⇒ Object
readonly
Returns the value of attribute is_futures_approved.
-
#is_test_drive ⇒ Object
readonly
Returns the value of attribute is_test_drive.
-
#margin_or_cash ⇒ Object
readonly
Returns the value of attribute margin_or_cash.
-
#nickname ⇒ Object
readonly
Returns the value of attribute nickname.
-
#opened_at ⇒ Object
readonly
Returns the value of attribute opened_at.
-
#suitable_options_level ⇒ Object
readonly
Returns the value of attribute suitable_options_level.
Attributes inherited from Base
Class Method Summary collapse
-
.get(session, account_number) ⇒ Account
Get a specific account by account number.
-
.get_all(session, include_closed: false) ⇒ Array<Account>
Get all accounts for the authenticated user.
Instance Method Summary collapse
-
#cancel_order(session, order_id) ⇒ void
Cancel an order.
- #closed? ⇒ Boolean
- #foreign? ⇒ Boolean
- #futures_approved? ⇒ Boolean
-
#get_balances(session) ⇒ AccountBalance
Get account balances.
-
#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).
-
#get_order(session, order_id) ⇒ LiveOrder
Get a specific order by ID.
-
#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).
-
#get_positions(session, symbol: nil, underlying_symbol: nil, include_closed: false) ⇒ Array<CurrentPosition>
Get current positions.
-
#get_trading_status(session) ⇒ Tastytrade::Models::TradingStatus
Get trading status.
-
#get_transactions(session, **options) ⇒ Array<Transaction>
Get transaction history.
-
#place_order(session, order, dry_run: false, skip_validation: false) ⇒ OrderResponse
Places an order for this account with comprehensive validation.
-
#replace_order(session, order_id, new_order) ⇒ OrderResponse
Replace an existing order.
- #test_drive? ⇒ Boolean
Methods inherited from Base
Constructor Details
This class inherits a constructor from Tastytrade::Models::Base
Instance Attribute Details
#account_number ⇒ Object (readonly)
Returns the value of attribute account_number.
7 8 9 |
# File 'lib/tastytrade/models/account.rb', line 7 def account_number @account_number end |
#account_type_name ⇒ Object (readonly)
Returns the value of attribute account_type_name.
7 8 9 |
# File 'lib/tastytrade/models/account.rb', line 7 def account_type_name @account_type_name end |
#closed_at ⇒ Object (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_at ⇒ Object (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_status ⇒ Object (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_id ⇒ Object (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_date ⇒ Object (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_objective ⇒ Object (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_closed ⇒ Object (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_foreign ⇒ Object (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_approved ⇒ Object (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_drive ⇒ Object (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_cash ⇒ Object (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 |
#nickname ⇒ Object (readonly)
Returns the value of attribute nickname.
7 8 9 |
# File 'lib/tastytrade/models/account.rb', line 7 def nickname @nickname end |
#opened_at ⇒ Object (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_level ⇒ Object (readonly)
Returns the value of attribute suitable_options_level.
7 8 9 |
# File 'lib/tastytrade/models/account.rb', line 7 def @suitable_options_level end |
Class Method Details
.get(session, account_number) ⇒ Account
Get a specific account by account number
31 32 33 34 |
# File 'lib/tastytrade/models/account.rb', line 31 def get(session, account_number) response = session.get("/accounts/#{account_number}/") new(response["data"]) end |
.get_all(session, include_closed: false) ⇒ Array<Account>
Get all accounts for the authenticated user
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
186 187 188 189 190 191 |
# File 'lib/tastytrade/models/account.rb', line 186 def cancel_order(session, order_id) session.delete("/accounts/#{account_number}/orders/#{order_id}/") nil rescue Tastytrade::Error => e handle_cancel_error(e) end |
#closed? ⇒ Boolean
209 210 211 |
# File 'lib/tastytrade/models/account.rb', line 209 def closed? @is_closed == true end |
#foreign? ⇒ Boolean
221 222 223 |
# File 'lib/tastytrade/models/account.rb', line 221 def foreign? @is_foreign == true end |
#futures_approved? ⇒ 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
41 42 43 44 |
# File 'lib/tastytrade/models/account.rb', line 41 def get_balances(session) response = session.get("/accounts/#{account_number}/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)
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"] = if params["from-time"] = from_time.iso8601 if from_time params["to-time"] = to_time.iso8601 if to_time response = session.get("/accounts/#{account_number}/orders/live/", params) response["data"]["items"].map { |item| LiveOrder.new(item) } end |
#get_order(session, order_id) ⇒ LiveOrder
Get a specific order by ID
174 175 176 177 |
# File 'lib/tastytrade/models/account.rb', line 174 def get_order(session, order_id) response = session.get("/accounts/#{account_number}/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)
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"] = if 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/#{account_number}/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
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"] = if params["include-closed"] = include_closed if include_closed response = session.get("/accounts/#{account_number}/positions/", params) response["data"]["items"].map { |item| CurrentPosition.new(item) } end |
#get_trading_status(session) ⇒ Tastytrade::Models::TradingStatus
Get trading status
67 68 69 70 |
# File 'lib/tastytrade/models/account.rb', line 67 def get_trading_status(session) response = session.get("/accounts/#{account_number}/trading-status/") TradingStatus.new(response["data"]) end |
#get_transactions(session, **options) ⇒ Array<Transaction>
Get transaction history
122 123 124 |
# File 'lib/tastytrade/models/account.rb', line 122 def get_transactions(session, **) Transaction.get_all(session, account_number, **) 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.
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/#{account_number}/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
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/#{account_number}/orders/#{order_id}/", new_order.to_api_params) OrderResponse.new(response["data"]) rescue Tastytrade::Error => e handle_replace_error(e) end |
#test_drive? ⇒ Boolean
217 218 219 |
# File 'lib/tastytrade/models/account.rb', line 217 def test_drive? @is_test_drive == true end |