Skip to content

Commit

Permalink
put try/catch around fidelity csv importer to skip lines with single …
Browse files Browse the repository at this point in the history
…or double quote in account name
  • Loading branch information
rrelyea committed Jun 20, 2023
1 parent cb0b09b commit 01bc478
Showing 1 changed file with 49 additions and 42 deletions.
91 changes: 49 additions & 42 deletions Shared/Models/FamilyData/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,55 +164,62 @@ public static async Task<List<Account>> ImportCSV(string[] lines, IList<Fund> fu
while (line != "" && line != ",,,,,,,,,,,,,,,")
{
List<string> chunks = await SplitCsvLine(line);
var accountNumber = chunks[0];
var accountName = chunks[1];
var symbol = chunks[2];

// many tickers have "**" at end, signifying money market fund.
if (symbol.Length >= 2 && symbol.Substring(symbol.Length-2) == "**") {
symbol = symbol.Substring(0, symbol.Length - 2);
}
try {
var accountNumber = chunks[0];
var accountName = chunks[1];
var symbol = chunks[2];

string? investmentName;
double value;
double? price = null;
double? shares = null;
double? costBasis = null;
// many tickers have "**" at end, signifying money market fund.
if (symbol.Length >= 2 && symbol.Substring(symbol.Length-2) == "**") {
symbol = symbol.Substring(0, symbol.Length - 2);
}

// many tickers have "**" at end, signifying money market fund.
if (symbol.Length >= 2 && symbol.Substring(symbol.Length-2) == "**") {
symbol = symbol.Substring(0, symbol.Length - 2);
}
string? investmentName;
double value;
double? price = null;
double? shares = null;
double? costBasis = null;

if (symbol == "Pending Activity")
{
investmentName = symbol;
value = ParseDouble(chunks[6], allowCurrency:true);
symbol = null;
}
else
{
investmentName = chunks[3];
value = ParseDouble(chunks[7], allowCurrency:true);
price = ParseDouble(chunks[5], allowCurrency:true);
shares = ParseDouble(chunks[4]);
costBasis = ParseDouble(chunks[13], allowCurrency:true);
}
// many tickers have "**" at end, signifying money market fund.
if (symbol.Length >= 2 && symbol.Substring(symbol.Length-2) == "**") {
symbol = symbol.Substring(0, symbol.Length - 2);
}

if (value < 0.0 || value > 1.0) {
if (lastAccountNumber != accountNumber)
if (symbol == "Pending Activity")
{
newAccount = new() {
Custodian = "Fidelity",
Note = "*"+ accountNumber.Substring(accountNumber.Length-4,4)
};
newAccount.GuessAccountType();
importedAccounts.Add(newAccount);
lastAccountNumber = accountNumber;
investmentName = symbol;
value = ParseDouble(chunks[6], allowCurrency:true);
symbol = null;
}
else
{
investmentName = chunks[3];
value = ParseDouble(chunks[7], allowCurrency:true);
price = ParseDouble(chunks[5], allowCurrency:true);
shares = ParseDouble(chunks[4]);
costBasis = ParseDouble(chunks[13], allowCurrency:true);
}

Investment newInvestment = new () { funds = funds, Ticker = symbol, Name = investmentName, Price = price, Value = value, Shares = shares, CostBasis = costBasis };
newAccount?.Investments.Add(newInvestment);
if (value < 0.0 || value > 1.0) {
if (lastAccountNumber != accountNumber)
{
newAccount = new() {
Custodian = "Fidelity",
Note = "*"+ accountNumber.Substring(accountNumber.Length-4,4)
};
newAccount.GuessAccountType();
importedAccounts.Add(newAccount);
lastAccountNumber = accountNumber;
}

Investment newInvestment = new () { funds = funds, Ticker = symbol, Name = investmentName, Price = price, Value = value, Shares = shares, CostBasis = costBasis };
newAccount?.Investments.Add(newInvestment);
}
}
catch (Exception e)
{
Console.WriteLine("skipped line due to error: " + line);
Console.WriteLine(e.Message);
}

line = lines[lineIndex++];
Expand Down

0 comments on commit 01bc478

Please sign in to comment.