# Fidelity account csv rules
# Fidelity's csv can be found at: All accounts > Activity & Orders > Download icon at top right

# This csv needs leading spaces removed before we convert it.
# Eg: sed 's/^ //' History_for_Account_NNN.csv > History_for_Account_NNN.clean.csv

# Read the cleaned copy
source History_for_Account_Z30149175*.clean.csv

newest-first
intra-day-reversed

# Skip undesirable lines: all but those beginning with a digit and having 12 comma-separated fields
if ! ^[0-9]([^,]*,){11}
 skip

# Run Date,Action,Symbol,Security Description,Security Type,Quantity,Price ($),Commission ($),Fees ($),Accrued Interest ($),Amount ($),Settlement Date
# If you have multiple accounts there will also be an Account field after Run Date
# (and it might need preprocessing, see https://github.com/simonmichael/hledger/issues/2082).
fields Run_Date, Action, Symbol, Security_Description, Security_Type, Quantity, Price_Usd, Commission_Usd, Fees_Usd, Accrued_Interest_Usd, Amount_Usd, Settlement_Date

date %Run_Date
date-format %m/%d/%Y

description fidelity | %Action
currency $

# Transactions come in various types, including:

if %Action Transfer Received \(Cash\)
 account1 assets:bank:wf:checking
 account2 assets:brokerage:fi:qpctq
 amount   -%Amount_Usd

if %Action INTEREST EARNED
 account1 revenues:dividends:qpctq
 account2 assets:brokerage:fi:qpctq
 amount   -%Amount_Usd
