Class: Schwab::Resources::Transaction

Inherits:
Base
  • Object
show all
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

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_idString?

Get account ID associated with transaction

Returns:

  • (String, nil)

    The account ID



314
315
316
# File 'lib/schwab/resources/transaction.rb', line 314

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

#assignment?Boolean

Check if this is an assignment

Returns:

  • (Boolean)

    True if 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

Returns:

  • (Boolean)

    True if buy



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

Returns:

  • (Boolean)

    True if 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

#commissionFloat

Get commission

Returns:

  • (Float)

    The 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

Returns:

  • (Boolean)

    True if 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_basisFloat?

Get the cost basis for trade transactions

Returns:

  • (Float, nil)

    The cost basis



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

Returns:

  • (Boolean)

    True if 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

#descriptionString

Get transaction description

Returns:

  • (String)

    The 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

Returns:

  • (Boolean)

    True if dividend



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

Returns:

  • (Boolean)

    True if 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

Returns:

  • (Boolean)

    True if 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

Returns:

  • (Boolean)

    True if fee



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

#feesFloat

Get fees

Returns:

  • (Float)

    The 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

Returns:

  • (Boolean)

    True if interest



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_amountFloat Also known as: amount

Get the net amount

Returns:

  • (Float)

    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

Returns:

  • (Boolean)

    True if 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

Returns:

  • (Boolean)

    True if 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

#priceFloat?

Get the price

Returns:

  • (Float, nil)

    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

#quantityFloat

Get the quantity

Returns:

  • (Float)

    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

Returns:

  • (Boolean)

    True if sell



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_dateDate, String

Get settlement date

Returns:

  • (Date, String)

    The settlement date



56
57
58
# File 'lib/schwab/resources/transaction.rb', line 56

def settlement_date
  self[:settlementDate] || self[:settlement_date]
end

#symbolString?

Get the symbol associated with the transaction

Returns:

  • (String, nil)

    The symbol



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_stringString

Get formatted display string for the transaction

Returns:

  • (String)

    Formatted transaction string



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_costFloat?

Get total cost including fees

Returns:

  • (Float, nil)

    The total cost



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

Returns:

  • (Boolean)

    True if trade



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_dateTime, ... Also known as: date

Get transaction date

Returns:

  • (Time, Date, String)

    The 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_idString Also known as: id

Get transaction ID

Returns:

  • (String)

    The 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_subtypeString Also known as: subtype

Get transaction subtype

Returns:

  • (String)

    The 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_typeString Also known as: type

Get transaction type

Returns:

  • (String)

    The 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

Returns:

  • (Boolean)

    True if 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

Returns:

  • (Boolean)

    True if 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