Class: Tastytrade::Models::AccountBalance

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

Overview

Represents account balance information from the API

Instance Attribute Summary collapse

Attributes inherited from Base

#data

Instance Method Summary collapse

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_numberObject (readonly)

Returns the value of attribute account_number.



9
10
11
# File 'lib/tastytrade/models/account_balance.rb', line 9

def 
  @account_number
end

#available_trading_fundsObject (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_balanceObject (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_powerObject (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
  @day_trading_buying_power
end

#derivative_buying_powerObject (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
  @derivative_buying_power
end

#effective_trading_fundsObject (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_powerObject (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
  @equity_buying_power
end

#long_derivative_valueObject (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_valueObject (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_equityObject (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_valueObject (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_cashObject (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_interestObject (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_valueObject (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_valueObject (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_atObject (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 buying_power_impact_percentage(amount, buying_power_type: :equity)
  bp = case buying_power_type
       when :equity then equity_buying_power
       when :derivative then derivative_buying_power
       when :day_trading then day_trading_buying_power
       else equity_buying_power
  end

  return BigDecimal("0") if bp.zero?
  ((BigDecimal(amount.to_s) / bp) * 100).round(2)
end

#buying_power_usage_percentageObject

Calculate buying power usage as a percentage



39
40
41
42
43
44
# File 'lib/tastytrade/models/account_balance.rb', line 39

def buying_power_usage_percentage
  return BigDecimal("0") if equity_buying_power.zero?

  used_buying_power = equity_buying_power - available_trading_funds
  ((used_buying_power / equity_buying_power) * 100).round(2)
end

#day_trading_buying_power_usage_percentageObject

Calculate day trading buying power usage percentage



75
76
77
78
79
80
# File 'lib/tastytrade/models/account_balance.rb', line 75

def day_trading_buying_power_usage_percentage
  return BigDecimal("0") if day_trading_buying_power.zero?

  used_day_trading_buying_power = day_trading_buying_power - available_trading_funds
  ((used_day_trading_buying_power / day_trading_buying_power) * 100).round(2)
end

#derivative_buying_power_usage_percentageObject

Calculate derivative buying power usage percentage



67
68
69
70
71
72
# File 'lib/tastytrade/models/account_balance.rb', line 67

def derivative_buying_power_usage_percentage
  return BigDecimal("0") if derivative_buying_power.zero?

  used_derivative_buying_power = derivative_buying_power - available_trading_funds
  ((used_derivative_buying_power / derivative_buying_power) * 100).round(2)
end

#high_buying_power_usage?(threshold = 80) ⇒ Boolean

Check if buying power usage is above warning threshold

Returns:

  • (Boolean)


47
48
49
# File 'lib/tastytrade/models/account_balance.rb', line 47

def high_buying_power_usage?(threshold = 80)
  buying_power_usage_percentage > threshold
end

#minimum_buying_powerObject

Get the minimum buying power across all types



83
84
85
# File 'lib/tastytrade/models/account_balance.rb', line 83

def minimum_buying_power
  [equity_buying_power, derivative_buying_power, day_trading_buying_power].min
end

#sufficient_buying_power?(amount, buying_power_type: :equity) ⇒ Boolean

Check if account has sufficient buying power for a given amount

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
# File 'lib/tastytrade/models/account_balance.rb', line 88

def sufficient_buying_power?(amount, buying_power_type: :equity)
  bp = case buying_power_type
       when :equity then equity_buying_power
       when :derivative then derivative_buying_power
       when :day_trading then day_trading_buying_power
       else equity_buying_power
  end

  bp >= BigDecimal(amount.to_s)
end

#total_derivative_valueObject

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_valueObject

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_valueObject

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