-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapital_com_loader.cpp
121 lines (96 loc) · 3.21 KB
/
capital_com_loader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Created by Saeid Yazdani on 9/30/2024.
//
#include <algorithm>
#include "macros.h"
#include "capital_com_loader.h"
namespace embedonix::trading_tax_calculator::capitalcom {
auto
generateTransactionsFromCsvFile(std::string file) -> std::vector<Transaction> {
__BENCHMARK_TIMER_START(timer)
namespace sl = embedonix::simplelibs;
auto rows = std::move(sl::parsers::csv_file_with_wrapper(
sl::fileio::readers::read_file_string(file), ',', '"', true));
auto transactions = std::vector<Transaction>();
for (const auto& row: rows) {
auto action = Transaction();
Asset asset;
action.setType(stringToTransactionType(row[4]));
// Assign asset by type
switch (action.getType()) {
case TransactionType::Transfer: // Transfer has no asset name
asset = {"transfer", "transfer"};
break;
case TransactionType::Deposit: // Deposit has no asset name
asset = {"deposit", "deposit"};
break;
default:
asset = {row[9], row[8]};
break;
}
asset.setAssetType(stringToAssetType(row[11]));
action.setAsset(asset);
action.setValue(atof(row[2].c_str())); // Amount
action.setDateString(row[6]);
action.setCurrency(row[3]);
transactions.emplace_back(action);
}
__BENCHMARK_TIMER_STOP(timer)
return transactions;
}
std::unordered_set<Asset>
getUniqueAssetsInTransactions(const std::vector<Transaction>& transactions) {
__BENCHMARK_TIMER_START(timer)
std::unordered_set<Asset> assets;
#ifdef __DEBUG__
int numDuplicates = 0;
int numUniqueAssts = 0;
#endif
for (const auto& tr: transactions) {
#ifdef __DEBUG__
assets.insert(tr.getAsset()).second ? numUniqueAssts++ : numDuplicates++;
#else
assets.insert(tr.getAsset());
#endif
}
#ifdef __DEBUG__
__PRINT_DEBUG__(std::format("Found {} unique and {} duplicate assets.",
numUniqueAssts, numDuplicates))
#endif
__BENCHMARK_TIMER_STOP(timer)
return assets;
}
std::unordered_set<TransactionType>
getTransactionTypesForAsset(const std::vector<Transaction> transactions, const Asset& asset) {
__BENCHMARK_TIMER_START(timer)
auto set = std::unordered_set<TransactionType>();
for (const auto& action: transactions) {
if (action.getAsset() == asset) {
set.insert(action.getType());
}
}
__BENCHMARK_TIMER_STOP(timer)
return set;
}
std::unordered_set<TransactionType> getTypesInTransactions(const std::vector<Transaction>& transactions) {
__BENCHMARK_TIMER_START(timer)
auto set = std::unordered_set<TransactionType>();
for (const auto& tr: transactions) {
set.insert(tr.getType());
}
#ifdef __DEBUG__
__PRINT_DEBUG__(std::format("Found {} types", set.size()))
#endif
__BENCHMARK_TIMER_STOP(timer)
return set;
}
std::unordered_set<std::string> getYearsInTransactions(const std::vector<Transaction>& transactions) {
__BENCHMARK_TIMER_START(timer)
auto years = std::unordered_set<std::string>();
for (const auto& tr: transactions) {
years.insert(tr.getDateString().substr(0, 4));
}
return years;
__BENCHMARK_TIMER_STOP(timer)
}
} // end NS