Class: Tastytrade::Models::AccountBalance
- Defined in:
- lib/tastytrade/models/account_balance.rb
Overview
Represents account balance information from the API
Instance Attribute Summary collapse
-
#account_number ⇒ Object
readonly
Returns the value of attribute account_number.
-
#available_trading_funds ⇒ Object
readonly
Returns the value of attribute available_trading_funds.
-
#cash_balance ⇒ Object
readonly
Returns the value of attribute cash_balance.
-
#day_trading_buying_power ⇒ Object
readonly
Returns the value of attribute day_trading_buying_power.
-
#derivative_buying_power ⇒ Object
readonly
Returns the value of attribute derivative_buying_power.
-
#effective_trading_funds ⇒ Object
readonly
Returns the value of attribute effective_trading_funds.
-
#equity_buying_power ⇒ Object
readonly
Returns the value of attribute equity_buying_power.
-
#long_derivative_value ⇒ Object
readonly
Returns the value of attribute long_derivative_value.
-
#long_equity_value ⇒ Object
readonly
Returns the value of attribute long_equity_value.
-
#margin_equity ⇒ Object
readonly
Returns the value of attribute margin_equity.
-
#net_liquidating_value ⇒ Object
readonly
Returns the value of attribute net_liquidating_value.
-
#pending_cash ⇒ Object
readonly
Returns the value of attribute pending_cash.
-
#pending_margin_interest ⇒ Object
readonly
Returns the value of attribute pending_margin_interest.
-
#short_derivative_value ⇒ Object
readonly
Returns the value of attribute short_derivative_value.
-
#short_equity_value ⇒ Object
readonly
Returns the value of attribute short_equity_value.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Attributes inherited from Base
Instance Method Summary collapse
-
#buying_power_impact_percentage(amount, buying_power_type: :equity) ⇒ Object
Calculate buying power impact as percentage.
-
#buying_power_usage_percentage ⇒ Object
Calculate buying power usage as a percentage.
-
#day_trading_buying_power_usage_percentage ⇒ Object
Calculate day trading buying power usage percentage.
-
#derivative_buying_power_usage_percentage ⇒ Object
Calculate derivative buying power usage percentage.
-
#high_buying_power_usage?(threshold = 80) ⇒ Boolean
Check if buying power usage is above warning threshold.
-
#initialize(data) ⇒ AccountBalance
constructor
A new instance of AccountBalance.
-
#minimum_buying_power ⇒ Object
Get the minimum buying power across all types.
-
#sufficient_buying_power?(amount, buying_power_type: :equity) ⇒ Boolean
Check if account has sufficient buying power for a given amount.
-
#total_derivative_value ⇒ Object
Calculate total derivative value (long + short).
-
#total_equity_value ⇒ Object
Calculate total equity value (long + short).
-
#total_market_value ⇒ Object
Calculate total market value (equity + derivatives).
Constructor Details
#initialize(data) ⇒ AccountBalance
Returns a new instance of AccountBalance.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/tastytrade/models/account_balance.rb', line 15 def initialize(data) super @account_number = data["account-number"] # Convert all monetary values to BigDecimal for precision @cash_balance = parse_decimal(data["cash-balance"]) @long_equity_value = parse_decimal(data["long-equity-value"]) @short_equity_value = parse_decimal(data["short-equity-value"]) @long_derivative_value = parse_decimal(data["long-derivative-value"]) @short_derivative_value = parse_decimal(data["short-derivative-value"]) @net_liquidating_value = parse_decimal(data["net-liquidating-value"]) @equity_buying_power = parse_decimal(data["equity-buying-power"]) @derivative_buying_power = parse_decimal(data["derivative-buying-power"]) @day_trading_buying_power = parse_decimal(data["day-trading-buying-power"]) @available_trading_funds = parse_decimal(data["available-trading-funds"]) @margin_equity = parse_decimal(data["margin-equity"]) @pending_cash = parse_decimal(data["pending-cash"]) @pending_margin_interest = parse_decimal(data["pending-margin-interest"]) @effective_trading_funds = parse_decimal(data["effective-trading-funds"]) @updated_at = parse_time(data["updated-at"]) end |
Instance Attribute Details
#account_number ⇒ Object (readonly)
Returns the value of attribute account_number.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def account_number @account_number end |
#available_trading_funds ⇒ Object (readonly)
Returns the value of attribute available_trading_funds.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def available_trading_funds @available_trading_funds end |
#cash_balance ⇒ Object (readonly)
Returns the value of attribute cash_balance.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def cash_balance @cash_balance end |
#day_trading_buying_power ⇒ Object (readonly)
Returns the value of attribute day_trading_buying_power.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def @day_trading_buying_power end |
#derivative_buying_power ⇒ Object (readonly)
Returns the value of attribute derivative_buying_power.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def @derivative_buying_power end |
#effective_trading_funds ⇒ Object (readonly)
Returns the value of attribute effective_trading_funds.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def effective_trading_funds @effective_trading_funds end |
#equity_buying_power ⇒ Object (readonly)
Returns the value of attribute equity_buying_power.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def @equity_buying_power end |
#long_derivative_value ⇒ Object (readonly)
Returns the value of attribute long_derivative_value.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def long_derivative_value @long_derivative_value end |
#long_equity_value ⇒ Object (readonly)
Returns the value of attribute long_equity_value.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def long_equity_value @long_equity_value end |
#margin_equity ⇒ Object (readonly)
Returns the value of attribute margin_equity.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def margin_equity @margin_equity end |
#net_liquidating_value ⇒ Object (readonly)
Returns the value of attribute net_liquidating_value.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def net_liquidating_value @net_liquidating_value end |
#pending_cash ⇒ Object (readonly)
Returns the value of attribute pending_cash.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def pending_cash @pending_cash end |
#pending_margin_interest ⇒ Object (readonly)
Returns the value of attribute pending_margin_interest.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def pending_margin_interest @pending_margin_interest end |
#short_derivative_value ⇒ Object (readonly)
Returns the value of attribute short_derivative_value.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def short_derivative_value @short_derivative_value end |
#short_equity_value ⇒ Object (readonly)
Returns the value of attribute short_equity_value.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def short_equity_value @short_equity_value end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
9 10 11 |
# File 'lib/tastytrade/models/account_balance.rb', line 9 def updated_at @updated_at end |
Instance Method Details
#buying_power_impact_percentage(amount, buying_power_type: :equity) ⇒ Object
Calculate buying power impact as percentage
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/tastytrade/models/account_balance.rb', line 100 def (amount, buying_power_type: :equity) bp = case when :equity then when :derivative then when :day_trading then else end return BigDecimal("0") if bp.zero? ((BigDecimal(amount.to_s) / bp) * 100).round(2) end |
#buying_power_usage_percentage ⇒ Object
Calculate buying power usage as a percentage
39 40 41 42 43 44 |
# File 'lib/tastytrade/models/account_balance.rb', line 39 def return BigDecimal("0") if .zero? = - available_trading_funds (( / ) * 100).round(2) end |
#day_trading_buying_power_usage_percentage ⇒ Object
Calculate day trading buying power usage percentage
75 76 77 78 79 80 |
# File 'lib/tastytrade/models/account_balance.rb', line 75 def return BigDecimal("0") if .zero? = - available_trading_funds (( / ) * 100).round(2) end |
#derivative_buying_power_usage_percentage ⇒ Object
Calculate derivative buying power usage percentage
67 68 69 70 71 72 |
# File 'lib/tastytrade/models/account_balance.rb', line 67 def return BigDecimal("0") if .zero? = - available_trading_funds (( / ) * 100).round(2) end |
#high_buying_power_usage?(threshold = 80) ⇒ Boolean
Check if buying power usage is above warning threshold
47 48 49 |
# File 'lib/tastytrade/models/account_balance.rb', line 47 def (threshold = 80) > threshold end |
#minimum_buying_power ⇒ Object
Get the minimum buying power across all types
83 84 85 |
# File 'lib/tastytrade/models/account_balance.rb', line 83 def [, , ].min end |
#sufficient_buying_power?(amount, buying_power_type: :equity) ⇒ Boolean
Check if account has sufficient buying power for a given amount
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tastytrade/models/account_balance.rb', line 88 def (amount, buying_power_type: :equity) bp = case when :equity then when :derivative then when :day_trading then else end bp >= BigDecimal(amount.to_s) end |
#total_derivative_value ⇒ Object
Calculate total derivative value (long + short)
57 58 59 |
# File 'lib/tastytrade/models/account_balance.rb', line 57 def total_derivative_value long_derivative_value + short_derivative_value end |
#total_equity_value ⇒ Object
Calculate total equity value (long + short)
52 53 54 |
# File 'lib/tastytrade/models/account_balance.rb', line 52 def total_equity_value long_equity_value + short_equity_value end |
#total_market_value ⇒ Object
Calculate total market value (equity + derivatives)
62 63 64 |
# File 'lib/tastytrade/models/account_balance.rb', line 62 def total_market_value total_equity_value + total_derivative_value end |