summaryrefslogtreecommitdiff
path: root/scripts/lib.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib.sh')
-rw-r--r--scripts/lib.sh18
1 files changed, 18 insertions, 0 deletions
diff --git a/scripts/lib.sh b/scripts/lib.sh
new file mode 100644
index 0000000..3a28552
--- /dev/null
+++ b/scripts/lib.sh
@@ -0,0 +1,18 @@
+# Shared helpers for fetch.sh and fetch_tw_daily.sh (source this file).
+header="Date,Open,High,Low,Close,Volume"
+
+# merge_into FILE NEWROWS: merge by date (new wins), sorted, warn on >1% close mismatch.
+merge_into() {
+ local file=$1 new=$2 tmp; tmp=$(mktemp)
+ { echo "$header"
+ awk -F, -v sym="$(basename "$file" .csv)" '
+ FNR == 1 { fileno++ }
+ $1 !~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/ { next }
+ fileno == 1 { r[$1] = $0; oldclose[$1] = $5; next }
+ { if (($1 in oldclose) && oldclose[$1] > 0) {
+ d = ($5 - oldclose[$1]) / oldclose[$1]; if (d < 0) d = -d
+ if (d > 0.01) printf " warning: %s %s close %s -> %s (>1%% change; split or capital reduction? re-download from scratch)\n", sym, $1, oldclose[$1], $5 > "/dev/stderr" }
+ r[$1] = $0 }
+ END { for (d in r) print r[d] }' "$file" "$new" | sort
+ } > "$tmp" && mv "$tmp" "$file"
+}