Class: Schwab::Resources::Account

Inherits:
Base
  • Object
show all
Defined in:
lib/schwab/resources/account.rb

Overview

Resource wrapper for account objects Provides account-specific helper methods and type coercions

Instance Method Summary collapse

Methods inherited from Base

#==, #[], #[]=, #attributes, #each, #empty?, field_types, #initialize, #inspect, #key?, #keys, #method_missing, #respond_to_missing?, set_field_type, #to_h, #to_s

Constructor Details

This class inherits a constructor from Schwab::Resources::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Schwab::Resources::Base

Instance Method Details

#account_numberString Also known as: id

Get the account number/ID (plain text)



23
24
25
# File 'lib/schwab/resources/account.rb', line 23

def 
  self[:accountNumber] || self[:account_number]
end

#account_typeString

Get the account type



47
48
49
# File 'lib/schwab/resources/account.rb', line 47

def 
  self[:type] || self[:accountType] || self[:account_type]
end

#account_valueFloat? Also known as: net_liquidation_value, total_value

Get account value (net liquidation value)



117
118
119
120
121
122
123
124
# File 'lib/schwab/resources/account.rb', line 117

def 
  return unless current_balances

  current_balances[:liquidationValue] ||
    current_balances[:liquidation_value] ||
    current_balances[:totalValue] ||
    current_balances[:total_value]
end

#active?Boolean

Check if account is active



75
76
77
# File 'lib/schwab/resources/account.rb', line 75

def active?
  status == "ACTIVE"
end

#api_identifierString

Get the appropriate account identifier for API calls Returns hash_value if available, otherwise account_number



40
41
42
# File 'lib/schwab/resources/account.rb', line 40

def api_identifier
  hash_value || 
end

#buying_powerFloat?

Get buying power



143
144
145
146
147
148
149
150
# File 'lib/schwab/resources/account.rb', line 143

def buying_power
  return unless current_balances

  current_balances[:buyingPower] ||
    current_balances[:buying_power] ||
    current_balances[:availableFundsTrade] ||
    current_balances[:available_funds_trade]
end

#cash_account?Boolean

Check if this is a cash account



61
62
63
# File 'lib/schwab/resources/account.rb', line 61

def cash_account?
   == "CASH"
end

#cash_balanceFloat?

Get cash balance



131
132
133
134
135
136
137
138
# File 'lib/schwab/resources/account.rb', line 131

def cash_balance
  return unless current_balances

  current_balances[:cashBalance] ||
    current_balances[:cash_balance] ||
    current_balances[:availableFunds] ||
    current_balances[:available_funds]
end

#current_balancesSchwab::Resources::Base

Get the current balances



82
83
84
# File 'lib/schwab/resources/account.rb', line 82

def current_balances
  self[:currentBalances] || self[:current_balances]
end

#day_trading_buying_powerFloat?

Get day trading buying power



155
156
157
158
159
160
# File 'lib/schwab/resources/account.rb', line 155

def day_trading_buying_power
  return unless current_balances

  current_balances[:dayTradingBuyingPower] ||
    current_balances[:day_trading_buying_power]
end

#equityFloat?

Get account equity



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

def equity
  return unless current_balances

  current_balances[:equity] ||
    current_balances[:accountEquity] ||
    current_balances[:account_equity]
end

#equity_positionsArray<Schwab::Resources::Position>

Get equity positions



239
240
241
# File 'lib/schwab/resources/account.rb', line 239

def equity_positions
  positions_by_type(:equity)
end

#has_positions?Boolean

Check if account has positions



260
261
262
# File 'lib/schwab/resources/account.rb', line 260

def has_positions?
  !positions.empty?
end

#hash_valueString? Also known as: encrypted_id

Get the encrypted hash value for this account



31
32
33
# File 'lib/schwab/resources/account.rb', line 31

def hash_value
  self[:hashValue] || self[:hash_value]
end

#initial_balancesSchwab::Resources::Base

Get the initial balances



89
90
91
# File 'lib/schwab/resources/account.rb', line 89

def initial_balances
  self[:initialBalances] || self[:initial_balances]
end

#maintenance_requirementFloat?

Get maintenance requirement



165
166
167
168
169
170
171
172
# File 'lib/schwab/resources/account.rb', line 165

def maintenance_requirement
  return unless current_balances

  current_balances[:maintenanceRequirement] ||
    current_balances[:maintenance_requirement] ||
    current_balances[:maintReq] ||
    current_balances[:maint_req]
end

#margin_account?Boolean

Check if this is a margin account



54
55
56
# File 'lib/schwab/resources/account.rb', line 54

def margin_account?
   == "MARGIN"
end

#margin_balanceFloat?

Get margin balance if applicable



177
178
179
180
181
182
# File 'lib/schwab/resources/account.rb', line 177

def margin_balance
  return unless margin_account? && current_balances

  current_balances[:marginBalance] ||
    current_balances[:margin_balance]
end

#margin_call?Boolean?

Check if account is in margin call



187
188
189
190
191
192
193
194
195
196
# File 'lib/schwab/resources/account.rb', line 187

def margin_call?
  return false unless margin_account? && current_balances

  in_call = current_balances[:isInCall] ||
    current_balances[:is_in_call] ||
    current_balances[:inCall] ||
    current_balances[:in_call]

  !!in_call
end

#option_positionsArray<Schwab::Resources::Position>

Get option positions



246
247
248
# File 'lib/schwab/resources/account.rb', line 246

def option_positions
  positions_by_type(:option)
end

#position_countInteger

Get number of positions



267
268
269
# File 'lib/schwab/resources/account.rb', line 267

def position_count
  positions.size
end

#positionsArray<Schwab::Resources::Position>

Get positions



103
104
105
106
107
108
109
110
111
112
# File 'lib/schwab/resources/account.rb', line 103

def positions
  positions_data = self[:positions] || []
  positions_data.map do |position_data|
    if position_data.is_a?(Position)
      position_data
    else
      Position.new(position_data, client)
    end
  end
end

#positions_by_type(asset_type) ⇒ Array<Schwab::Resources::Position>

Get positions filtered by asset type



227
228
229
230
231
232
233
234
# File 'lib/schwab/resources/account.rb', line 227

def positions_by_type(asset_type)
  type_str = asset_type.to_s.upcase
  positions.select do |position|
    position.asset_type == type_str ||
      position[:assetType] == type_str ||
      position[:asset_type] == type_str
  end
end

#projected_balancesSchwab::Resources::Base

Get projected balances



96
97
98
# File 'lib/schwab/resources/account.rb', line 96

def projected_balances
  self[:projectedBalances] || self[:projected_balances]
end

#statusString

Get account status



68
69
70
# File 'lib/schwab/resources/account.rb', line 68

def status
  self[:status] || self[:accountStatus]
end

#todays_pnlFloat

Calculate today’s P&L for all positions



219
220
221
# File 'lib/schwab/resources/account.rb', line 219

def todays_pnl
  positions.sum { |position| position.day_pnl || 0 }
end

#total_market_valueFloat

Calculate total market value of positions



253
254
255
# File 'lib/schwab/resources/account.rb', line 253

def total_market_value
  positions.sum { |position| position.market_value || 0 }
end

#total_pnlFloat

Calculate total P&L for all positions



212
213
214
# File 'lib/schwab/resources/account.rb', line 212

def total_pnl
  positions.sum { |position| position.unrealized_pnl || 0 }
end