tradelog/profit.awk
2021-04-07 00:04:06 +02:00

56 lines
1003 B
Awk

function sumItUp(units, cpu) {
# avoid floating point arithmetic AT ALL COST!
partsLen = split(cpu, parts, ".")
if (partsLen == 1) {
cost = parts[1] * 100
return cost * units
}
dec = parts[2]
dec = substr(dec, 0, 2)
for (i = length(dec); i < 2; i++) {
dec = dec"0"
}
cost = parts[1] * 100 + dec
return cost * units
}
BEGIN {
RS="\n"
FS=";"
split(ARG_TICKER, _tmp_tickers, ";")
for (i in _tmp_tickers) {
tickers[_tmp_tickers[i]] = ""
hasTickers = 1
}
gsub(/[-: ]/, "", ARG_DATE_AFTER);
gsub(/[-: ]/, "", ARG_DATE_BEFORE);
profit = 0
}
(hasTickers == 1) && ($2 in tickers == 0) { next }
{
fieldDate=$1
gsub(/[-: ]/, "", fieldDate);
if (ARG_DATE_AFTER != "" && ARG_DATE_AFTER > fieldDate) { next }
if (ARG_DATE_BEFORE != "" && ARG_DATE_BEFORE < fieldDate) { next }
}
{
if ($3 == "buy") {
profit -= sumItUp($6, $4)
} else if ($3 == "sell") {
profit += sumItUp($6, $4)
}
}
END {
# taking the lazy way here, not cool!
profit /= 100
printf "%.2f\n", profit
}