Skip to content

Commit

Permalink
Reading data from xlsx file
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelTVMS committed Jan 3, 2024
1 parent ceb932b commit 16b08f0
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ bin/brokertotax.exe

#Ignore coverage files
coverage/**/*

#Sample data control. Only add specific files to avoid privacy issues.
.sample_data/*
!.sample_data/etoro.csv #Include etoro.csv
13 changes: 11 additions & 2 deletions data/exchange/2023.json
Original file line number Diff line number Diff line change
Expand Up @@ -1080,9 +1080,18 @@
"EUR": 0.905595
},
"2023-12-27": {
"EUR": 0.9003
"EUR": 0.900235
},
"2023-12-28": {
"EUR": 0.9003
"EUR": 0.90354
},
"2023-12-29": {
"EUR": 0.904604
},
"2023-12-30": {
"EUR": 0.904604
},
"2023-12-31": {
"EUR": 0.904604
}
}
23 changes: 23 additions & 0 deletions lib/etoro/closed_position.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "package:country_code/country_code.dart";
import "package:excel/excel.dart";

import "../entities/broker_operation.dart";
import "../entities/gains.dart";
Expand Down Expand Up @@ -76,6 +77,28 @@ class EtoroClosedPosition implements BrokerOperation {
notes: csvRow[17]);
}

factory EtoroClosedPosition.fromExcelRow(List<Data?> excelRow) {
return EtoroClosedPosition(
positionId: ExcelParsers.toInteger(excelRow[0]),
action: excelRow[1]!.value.toString(),
amount: ExcelParsers.toDouble(excelRow[2]),
units: ExcelParsers.toDouble(excelRow[3]),
openDate: ExcelParsers.toDateTime(excelRow[4], "dd/MM/yyyy HH:mm:ss"),
closeDate: ExcelParsers.toDateTime(excelRow[5], "dd/MM/yyyy HH:mm:ss"),
leverage: ExcelParsers.toInteger(excelRow[6]),
spread: ExcelParsers.toDouble(excelRow[7]),
profit: ExcelParsers.toDouble(excelRow[8]),
openRate: ExcelParsers.toDouble(excelRow[9]),
closeRate: ExcelParsers.toDouble(excelRow[10]),
takeProfitRate: ExcelParsers.toDouble(excelRow[11]),
stopLossRate: ExcelParsers.toDouble(excelRow[12]),
rolloverFeesAndDividends: ExcelParsers.toDouble(excelRow[13]),
copiedFrom: excelRow[14]!.value.toString(),
type: excelRow[15]!.value.toString(),
isin: excelRow[16]!.value.toString(),
notes: excelRow[17]?.value.toString());
}

@override
Gain toGain() {
return Gain(
Expand Down
23 changes: 23 additions & 0 deletions lib/etoro/closed_positions.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import "dart:collection";
import "dart:io";
import "package:csv/csv.dart";
import "package:excel/excel.dart";
import "package:logging/logging.dart";

import "../entities/broker_operation.dart";
Expand Down Expand Up @@ -28,6 +30,27 @@ class EtoroClosedPositions extends ListBase<EtoroClosedPosition> implements Brok
_positions[index] = value;
}

static fromExcelFile(String excelFilePath) {
var xlsxFile = File(excelFilePath);
if (!xlsxFile.existsSync()) throw Exception("File not found: $excelFilePath");

_log.info("Reading file: $excelFilePath");
var excel = Excel.decodeBytes(File(excelFilePath).readAsBytesSync());

Sheet? sheet = excel.tables["Closed Positions"];
if (sheet == null) throw Exception("Closed Positions sheet not found");

return EtoroClosedPositions.fromExcelSheet(sheet);
}

EtoroClosedPositions.fromExcelSheet(Sheet sheet, [bool skipFirstRow = true]) {
_log.fine("Parsing Excel sheet");
List<List<Data?>> excelRows = sheet.rows;

_positions = excelRows.skip(skipFirstRow ? 1 : 0).map(EtoroClosedPosition.fromExcelRow).toList();
_log.info("Found $length eToro Positions");
}

EtoroClosedPositions.fromCsv(String csvString, [bool skipFirstRow = true]) {
_log.fine("Parsing CSV string");
var csvPositions = CsvToListConverter().convert(csvString);
Expand Down
54 changes: 54 additions & 0 deletions lib/parsers.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
import "package:excel/excel.dart";
import "package:intl/intl.dart";

class ExcelParsers {
static DateTime toDateTime(Data? data, String formatString) {
if (data!.value.runtimeType == DateCellValue) {
var dateValue = data as DateCellValue;
return dateValue.asDateTimeUtc();
} else if (data.value.runtimeType == TextCellValue) {
return DynamicParsers.toDateTime(data.value.toString(), formatString);
} else {
throw Exception("Unknown type: $data.runtimeType");
}
}

static int toInteger(Data? data) {
if (data!.value.runtimeType == IntCellValue) {
return (data.value as IntCellValue).value;
} else if (data.value.runtimeType == TextCellValue) {
return DynamicParsers.toInteger(data.value.toString());
} else {
throw Exception("Unknown type: $data.runtimeType");
}
}

static double toDouble(Data? data) {
if (data!.value.runtimeType == DoubleCellValue) {
return (data.value as DoubleCellValue).value;
} else if (data.value.runtimeType == IntCellValue) {
return toInteger(data).toDouble();
} else if (data.value.runtimeType == TextCellValue) {
return DynamicParsers.toDouble(data.value.toString());
} else {
throw Exception("Unknown type: $data.runtimeType");
}
}
}

class DynamicParsers {
static DateTime toDateTime(dynamic value, String formatString) {
var dateFormat = DateFormat(formatString);
Expand All @@ -12,6 +48,24 @@ class DynamicParsers {
}
}

static int toInteger(dynamic value) {
if (value is String) {
var integerString = value.trim().replaceAll(",", "");

if (integerString.startsWith("(") && integerString.endsWith(")")) {
integerString = integerString.replaceAll("(", "").replaceAll(")", "");
integerString = "-$integerString";
}
return int.parse(integerString);
} else if (value is int) {
return value;
} else if (value is int) {
return value.toInt();
} else {
throw Exception("Unknown type: $value.runtimeType");
}
}

static double toDouble(dynamic value) {
if (value is String) {
var doubleString = value.trim().replaceAll(",", "");
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ dependencies:
args: ^2.4.0
country_code: ^1.0.0
csv: ^5.0.1
excel: ^4.0.2
intl: ^0.19.0
logging: ^1.1.1
7 changes: 7 additions & 0 deletions test/etoro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ void main() {
csvString +=
"\r\n1308606081,Buy Energy Select Sector SPDR, 3.16 ,0.064914,13/09/2021 13:31:14,07/03/2022 14:31:07,1, 0.00 , 1.78 , 48.68 , 76.03 , 0.00 , 0.00 , 0.06 ,Karlo_s,ETF,US81369Y5069,";

var excelFilePath = ".sample_data/etoro-2023.xlsx";

test("Read closed positions from Excel.", () {
var positions = EtoroClosedPositions.fromExcelFile(excelFilePath);

expect(positions.length, 3823);
});
test("Create closed positions from CSV.", () {
var positions = EtoroClosedPositions.fromCsv(csvString);

Expand Down

0 comments on commit 16b08f0

Please sign in to comment.