-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtc_plot_daily.sh
More file actions
executable file
·84 lines (69 loc) · 2.28 KB
/
btc_plot_daily.sh
File metadata and controls
executable file
·84 lines (69 loc) · 2.28 KB
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
#!/usr/bin/env bash
set -euo pipefail
WORKDIR="${WORKDIR:-/home/bot/Workspace}"
STATE_DIR="${STATE_DIR:-$WORKDIR/state}"
IN="$STATE_DIR/btc-prices.jsonl"
DATA_OUT="$STATE_DIR/btc-daily.tsv"
PNG_OUT="${PNG_OUT:-/home/bot/.openclaw/media/outbound/btc-daily.png}"
mkdir -p "$STATE_DIR"
mkdir -p "$(dirname "$PNG_OUT")"
if [[ ! -f "$IN" ]]; then
echo "No data yet: $IN" >&2
exit 1
fi
# Build 1 point per day from the 5pm (17:00) local sample ONLY.
# If multiple 5pm-ish samples exist (e.g., reruns), take the latest one.
python - "$IN" "$DATA_OUT" <<'PY'
import sys, json
from datetime import datetime
from zoneinfo import ZoneInfo
in_path, out_path = sys.argv[1], sys.argv[2]
tz = ZoneInfo('America/Edmonton')
last_5pm_by_day = {} # YYYY-MM-DD -> record
with open(in_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
rec = json.loads(line)
ts = int(rec['ts'])
dt = datetime.fromtimestamp(ts, tz)
# only consider samples taken during the 17th hour (5pm local)
if dt.hour != 17:
continue
day = dt.strftime('%Y-%m-%d')
prev = last_5pm_by_day.get(day)
if (prev is None) or (ts > prev['ts']):
last_5pm_by_day[day] = rec
rows = []
for day in sorted(last_5pm_by_day.keys()):
rec = last_5pm_by_day[day]
rows.append((day, rec['btc_usd'], rec['btc_cad'], rec['usd_cad']))
with open(out_path, 'w', encoding='utf-8') as out:
out.write('date\tbtc_usd\tbtc_cad\tusd_cad\n')
for day, btc_usd, btc_cad, usd_cad in rows:
out.write(f"{day}\t{btc_usd:.2f}\t{btc_cad:.2f}\t{usd_cad:.6f}\n")
print(f"Wrote {len(rows)} daily rows -> {out_path}")
PY
# Plot with gnuplot (2 panels)
gnuplot <<GNUPLOT
set terminal pngcairo size 1200,700
set output "${PNG_OUT}"
set datafile separator "\t"
set xdata time
set timefmt "%Y-%m-%d"
set format x "%b %d"
set grid
set key left top
set multiplot layout 2,1 title "Daily BTC + FX (5pm sample each day)"
# Top: BTC in USD + CAD
set ylabel "BTC price"
plot \
"${DATA_OUT}" using 1:2 with lines lw 2 title "BTC/USD", \
"${DATA_OUT}" using 1:3 with lines lw 2 title "BTC/CAD"
# Bottom: USD/CAD
set ylabel "USD/CAD"
plot "${DATA_OUT}" using 1:4 with lines lw 2 title "USD/CAD"
unset multiplot
GNUPLOT
echo "$PNG_OUT"