2024-10-01 10:47:59 -04:00
|
|
|
class TradeImport < Import
|
|
|
|
def import!
|
|
|
|
transaction do
|
|
|
|
mappings.each(&:create_mappable!)
|
|
|
|
|
2025-04-01 07:58:49 -04:00
|
|
|
trades = rows.map do |row|
|
2025-03-03 12:47:30 -05:00
|
|
|
mapped_account = if account
|
|
|
|
account
|
|
|
|
else
|
|
|
|
mappings.accounts.mappable_for(row.account)
|
|
|
|
end
|
2025-02-24 16:00:24 +01:00
|
|
|
|
|
|
|
# Try to find or create security with ticker only
|
|
|
|
security = find_or_create_security(
|
|
|
|
ticker: row.ticker,
|
|
|
|
exchange_operating_mic: row.exchange_operating_mic
|
|
|
|
)
|
2024-10-01 10:47:59 -04:00
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
Trade.new(
|
2025-04-01 07:58:49 -04:00
|
|
|
security: security,
|
|
|
|
qty: row.qty,
|
2025-03-03 12:47:30 -05:00
|
|
|
currency: row.currency.presence || mapped_account.currency,
|
2025-04-01 07:58:49 -04:00
|
|
|
price: row.price,
|
2025-04-14 11:40:34 -04:00
|
|
|
entry: Entry.new(
|
2025-04-01 07:58:49 -04:00
|
|
|
account: mapped_account,
|
|
|
|
date: row.date_iso,
|
|
|
|
amount: row.signed_amount,
|
|
|
|
name: row.name,
|
2025-03-03 12:47:30 -05:00
|
|
|
currency: row.currency.presence || mapped_account.currency,
|
2025-04-01 07:58:49 -04:00
|
|
|
import: self
|
2025-02-24 16:00:24 +01:00
|
|
|
),
|
2025-04-01 07:58:49 -04:00
|
|
|
)
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
2025-04-18 10:48:10 -04:00
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
Trade.import!(trades, recursive: true)
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mapping_steps
|
2025-03-03 12:47:30 -05:00
|
|
|
base = []
|
|
|
|
base << Import::AccountMapping if account.nil?
|
|
|
|
base
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def required_column_keys
|
|
|
|
%i[date ticker qty price]
|
|
|
|
end
|
|
|
|
|
|
|
|
def column_keys
|
2025-03-03 12:47:30 -05:00
|
|
|
base = %i[date ticker exchange_operating_mic currency qty price name]
|
|
|
|
base.unshift(:account) if account.nil?
|
|
|
|
base
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def dry_run
|
2025-03-03 12:47:30 -05:00
|
|
|
mappings = { transactions: rows.count }
|
|
|
|
|
|
|
|
mappings.merge(
|
2024-10-01 10:47:59 -04:00
|
|
|
accounts: Import::AccountMapping.for_import(self).creational.count
|
2025-03-03 12:47:30 -05:00
|
|
|
) if account.nil?
|
|
|
|
|
|
|
|
mappings
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def csv_template
|
|
|
|
template = <<-CSV
|
2025-02-24 16:00:24 +01:00
|
|
|
date*,ticker*,exchange_operating_mic,currency,qty*,price*,account,name
|
|
|
|
05/15/2024,AAPL,XNAS,USD,10,150.00,Trading Account,Apple Inc. Purchase
|
|
|
|
05/16/2024,GOOGL,XNAS,USD,-5,2500.00,Investment Account,Alphabet Inc. Sale
|
|
|
|
05/17/2024,TSLA,XNAS,USD,2,700.50,Retirement Account,Tesla Inc. Purchase
|
2024-10-01 10:47:59 -04:00
|
|
|
CSV
|
|
|
|
|
2025-03-03 12:47:30 -05:00
|
|
|
csv = CSV.parse(template, headers: true)
|
|
|
|
csv.delete("account") if account.present?
|
|
|
|
csv
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
2025-02-24 16:00:24 +01:00
|
|
|
|
|
|
|
private
|
2025-05-22 12:43:24 -04:00
|
|
|
def find_or_create_security(ticker: nil, exchange_operating_mic: nil)
|
|
|
|
return nil unless ticker.present?
|
2025-02-24 16:00:24 +01:00
|
|
|
|
2025-05-22 12:43:24 -04:00
|
|
|
# Avoids resolving the same security over and over again (resolver potentially makes network calls)
|
|
|
|
@security_cache ||= {}
|
2025-02-24 16:00:24 +01:00
|
|
|
|
2025-05-22 12:43:24 -04:00
|
|
|
cache_key = [ ticker, exchange_operating_mic ].compact.join(":")
|
2025-02-24 16:00:24 +01:00
|
|
|
|
2025-05-22 12:43:24 -04:00
|
|
|
security = @security_cache[cache_key]
|
2025-02-24 16:00:24 +01:00
|
|
|
|
2025-05-22 12:43:24 -04:00
|
|
|
return security if security.present?
|
2025-02-24 16:00:24 +01:00
|
|
|
|
2025-05-22 12:43:24 -04:00
|
|
|
security = Security::Resolver.new(
|
|
|
|
ticker,
|
|
|
|
exchange_operating_mic: exchange_operating_mic.presence
|
|
|
|
).resolve
|
2025-02-24 16:00:24 +01:00
|
|
|
|
2025-05-22 12:43:24 -04:00
|
|
|
@security_cache[cache_key] = security
|
|
|
|
|
|
|
|
security
|
2025-02-24 16:00:24 +01:00
|
|
|
end
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|