Class: Schwab::Resources::Transaction
- Defined in:
- lib/schwab/resources/transaction.rb
Overview
Resource wrapper for transaction objects Provides transaction-specific helper methods and type identification
Instance Method Summary collapse
-
#account_id ⇒ String?
Get account ID associated with transaction.
-
#assignment? ⇒ Boolean
Check if this is an assignment.
-
#buy? ⇒ Boolean
Check if this is a buy transaction.
-
#cancelled? ⇒ Boolean
Check if transaction is cancelled.
-
#commission ⇒ Float
Get commission.
-
#completed? ⇒ Boolean
Check if transaction is completed.
-
#cost_basis ⇒ Float?
Get the cost basis for trade transactions.
-
#deposit? ⇒ Boolean
Check if this is a deposit.
-
#description ⇒ String
Get transaction description.
-
#dividend? ⇒ Boolean
Check if this is a dividend transaction.
-
#exercise? ⇒ Boolean
Check if this is an exercise.
-
#expiration? ⇒ Boolean
Check if this is an expiration.
-
#fee? ⇒ Boolean
Check if this is a fee transaction.
-
#fees ⇒ Float
Get fees.
-
#interest? ⇒ Boolean
Check if this is an interest transaction.
-
#net_amount ⇒ Float
(also: #amount)
Get the net amount.
-
#option? ⇒ Boolean
Check if this is an option transaction.
-
#pending? ⇒ Boolean
Check if transaction is pending.
-
#price ⇒ Float?
Get the price.
-
#quantity ⇒ Float
Get the quantity.
-
#sell? ⇒ Boolean
Check if this is a sell transaction.
-
#settlement_date ⇒ Date, String
Get settlement date.
-
#symbol ⇒ String?
Get the symbol associated with the transaction.
-
#to_display_string ⇒ String
Get formatted display string for the transaction.
-
#total_cost ⇒ Float?
Get total cost including fees.
-
#trade? ⇒ Boolean
Check if this is a trade transaction.
-
#transaction_date ⇒ Time, ...
(also: #date)
Get transaction date.
-
#transaction_id ⇒ String
(also: #id)
Get transaction ID.
-
#transaction_subtype ⇒ String
(also: #subtype)
Get transaction subtype.
-
#transaction_type ⇒ String
(also: #type)
Get transaction type.
-
#transfer? ⇒ Boolean
Check if this is a transfer.
-
#withdrawal? ⇒ Boolean
Check if this is a withdrawal.
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_id ⇒ String?
Get account ID associated with transaction
314 315 316 |
# File 'lib/schwab/resources/transaction.rb', line 314 def account_id self[:accountNumber] || self[:account_number] || self[:accountId] || self[:account_id] end |
#assignment? ⇒ Boolean
Check if this is an assignment
243 244 245 246 |
# File 'lib/schwab/resources/transaction.rb', line 243 def assignment? type_upper = transaction_type&.upcase type_upper == "ASSIGNMENT" || type_upper == "OPTION_ASSIGNMENT" end |
#buy? ⇒ Boolean
Check if this is a buy transaction
156 157 158 159 160 |
# File 'lib/schwab/resources/transaction.rb', line 156 def buy? type_upper = transaction_type&.upcase ["BUY", "BUY_TO_OPEN", "BUY_TO_CLOSE"].include?(type_upper) || (type_upper == "TRADE" && quantity > 0) end |
#cancelled? ⇒ Boolean
Check if transaction is cancelled
306 307 308 309 |
# File 'lib/schwab/resources/transaction.rb', line 306 def cancelled? status = self[:status] || self[:transactionStatus] || self[:transaction_status] status&.upcase == "CANCELLED" || status&.upcase == "CANCELED" end |
#commission ⇒ Float
Get commission
138 139 140 141 142 143 144 |
# File 'lib/schwab/resources/transaction.rb', line 138 def commission begin self[:commission] || self[:fees][:commission] rescue 0 end.to_f end |
#completed? ⇒ Boolean
Check if transaction is completed
298 299 300 301 |
# File 'lib/schwab/resources/transaction.rb', line 298 def completed? status = self[:status] || self[:transactionStatus] || self[:transaction_status] status.nil? || status.upcase == "COMPLETED" || status.upcase == "EXECUTED" end |
#cost_basis ⇒ Float?
Get the cost basis for trade transactions
267 268 269 270 271 272 273 274 275 |
# File 'lib/schwab/resources/transaction.rb', line 267 def cost_basis return unless trade? if price && quantity (price * quantity.abs).round(2) else net_amount.abs end end |
#deposit? ⇒ Boolean
Check if this is a deposit
190 191 192 193 |
# File 'lib/schwab/resources/transaction.rb', line 190 def deposit? type_upper = transaction_type&.upcase ["DEPOSIT", "ELECTRONIC_FUND", "WIRE_IN", "ACH_DEPOSIT"].include?(type_upper) end |
#description ⇒ String
Get transaction description
63 64 65 |
# File 'lib/schwab/resources/transaction.rb', line 63 def description self[:description] || self[:transactionDescription] || self[:transaction_description] end |
#dividend? ⇒ Boolean
Check if this is a dividend transaction
174 175 176 177 |
# File 'lib/schwab/resources/transaction.rb', line 174 def dividend? type_upper = transaction_type&.upcase ["DIVIDEND", "DIVIDEND_REINVEST", "QUALIFIED_DIVIDEND"].include?(type_upper) end |
#exercise? ⇒ Boolean
Check if this is an exercise
251 252 253 254 |
# File 'lib/schwab/resources/transaction.rb', line 251 def exercise? type_upper = transaction_type&.upcase type_upper == "EXERCISE" || type_upper == "OPTION_EXERCISE" end |
#expiration? ⇒ Boolean
Check if this is an expiration
259 260 261 262 |
# File 'lib/schwab/resources/transaction.rb', line 259 def expiration? type_upper = transaction_type&.upcase type_upper == "EXPIRATION" || type_upper == "OPTION_EXPIRATION" end |
#fee? ⇒ Boolean
Check if this is a fee transaction
214 215 216 217 |
# File 'lib/schwab/resources/transaction.rb', line 214 def fee? type_upper = transaction_type&.upcase ["FEE", "COMMISSION", "SERVICE_FEE", "TRANSACTION_FEE"].include?(type_upper) end |
#fees ⇒ Float
Get fees
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/schwab/resources/transaction.rb', line 117 def fees if self[:fees] fees_data = self[:fees] total = 0.0 # Sum up different fee types if fees is a hash if fees_data.is_a?(Hash) fees_data.each_value { |v| total += v.to_f if v } else total = fees_data.to_f end total else 0.0 end end |
#interest? ⇒ Boolean
Check if this is an interest transaction
182 183 184 185 |
# File 'lib/schwab/resources/transaction.rb', line 182 def interest? type_upper = transaction_type&.upcase ["INTEREST", "INTEREST_INCOME", "MARGIN_INTEREST"].include?(type_upper) end |
#net_amount ⇒ Float Also known as: amount
Get the net amount
109 110 111 |
# File 'lib/schwab/resources/transaction.rb', line 109 def net_amount (self[:netAmount] || self[:net_amount] || 0).to_f end |
#option? ⇒ Boolean
Check if this is an option transaction
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/schwab/resources/transaction.rb', line 222 def option? if self[:transactionItem] asset_type = begin self[:transactionItem][:instrument][:assetType] rescue nil end asset_type == "OPTION" elsif self[:instrument] self[:instrument][:assetType] == "OPTION" else # Check if transaction type indicates options type_upper = transaction_type&.upcase || "" type_upper.include?("OPTION") || ["BUY_TO_OPEN", "BUY_TO_CLOSE", "SELL_TO_OPEN", "SELL_TO_CLOSE", "ASSIGNMENT", "EXERCISE"].include?(type_upper) end end |
#pending? ⇒ Boolean
Check if transaction is pending
290 291 292 293 |
# File 'lib/schwab/resources/transaction.rb', line 290 def pending? status = self[:status] || self[:transactionStatus] || self[:transaction_status] status&.upcase == "PENDING" end |
#price ⇒ Float?
Get the price
98 99 100 101 102 103 104 |
# File 'lib/schwab/resources/transaction.rb', line 98 def price if self[:transactionItem] self[:transactionItem][:price] else self[:price] end end |
#quantity ⇒ Float
Get the quantity
87 88 89 90 91 92 93 |
# File 'lib/schwab/resources/transaction.rb', line 87 def quantity if self[:transactionItem] (self[:transactionItem][:quantity] || self[:transactionItem][:amount] || 0).to_f else (self[:quantity] || self[:amount] || 0).to_f end end |
#sell? ⇒ Boolean
Check if this is a sell transaction
165 166 167 168 169 |
# File 'lib/schwab/resources/transaction.rb', line 165 def sell? type_upper = transaction_type&.upcase ["SELL", "SELL_TO_OPEN", "SELL_TO_CLOSE"].include?(type_upper) || (type_upper == "TRADE" && quantity < 0) end |
#settlement_date ⇒ Date, String
Get settlement date
56 57 58 |
# File 'lib/schwab/resources/transaction.rb', line 56 def settlement_date self[:settlementDate] || self[:settlement_date] end |
#symbol ⇒ String?
Get the symbol associated with the transaction
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/schwab/resources/transaction.rb', line 70 def symbol if self[:transactionItem] begin self[:transactionItem][:instrument][:symbol] rescue nil end elsif self[:instrument] self[:instrument][:symbol] else self[:symbol] end end |
#to_display_string ⇒ String
Get formatted display string for the transaction
321 322 323 324 325 326 327 328 329 330 |
# File 'lib/schwab/resources/transaction.rb', line 321 def to_display_string parts = [] parts << transaction_date.to_s if transaction_date parts << transaction_type parts << symbol if symbol parts << "#{quantity} @ $#{price}" if quantity && price parts << "$#{net_amount}" if net_amount != 0 parts.compact.join(" - ") end |
#total_cost ⇒ Float?
Get total cost including fees
280 281 282 283 284 285 |
# File 'lib/schwab/resources/transaction.rb', line 280 def total_cost return unless trade? cost = cost_basis || 0 cost + fees + commission end |
#trade? ⇒ Boolean
Check if this is a trade transaction
149 150 151 |
# File 'lib/schwab/resources/transaction.rb', line 149 def trade? ["TRADE", "BUY", "SELL", "BUY_TO_OPEN", "BUY_TO_CLOSE", "SELL_TO_OPEN", "SELL_TO_CLOSE"].include?(transaction_type&.upcase) end |
#transaction_date ⇒ Time, ... Also known as: date
Get transaction date
48 49 50 |
# File 'lib/schwab/resources/transaction.rb', line 48 def transaction_date self[:transactionDate] || self[:transaction_date] || self[:date] end |
#transaction_id ⇒ String Also known as: id
Get transaction ID
24 25 26 |
# File 'lib/schwab/resources/transaction.rb', line 24 def transaction_id self[:transactionId] || self[:transaction_id] || self[:id] end |
#transaction_subtype ⇒ String Also known as: subtype
Get transaction subtype
40 41 42 |
# File 'lib/schwab/resources/transaction.rb', line 40 def transaction_subtype self[:transactionSubType] || self[:transaction_sub_type] || self[:subtype] end |
#transaction_type ⇒ String Also known as: type
Get transaction type
32 33 34 |
# File 'lib/schwab/resources/transaction.rb', line 32 def transaction_type self[:transactionType] || self[:transaction_type] || self[:type] end |
#transfer? ⇒ Boolean
Check if this is a transfer
206 207 208 209 |
# File 'lib/schwab/resources/transaction.rb', line 206 def transfer? type_upper = transaction_type&.upcase ["TRANSFER", "INTERNAL_TRANSFER", "JOURNAL"].include?(type_upper) end |
#withdrawal? ⇒ Boolean
Check if this is a withdrawal
198 199 200 201 |
# File 'lib/schwab/resources/transaction.rb', line 198 def withdrawal? type_upper = transaction_type&.upcase ["WITHDRAWAL", "WIRE_OUT", "ACH_WITHDRAWAL"].include?(type_upper) end |