info = { type = "app", title = "BTC price", author = "BRRRO", license = "MIT", desc = "Price and 24h chart", category = "btc", api = 43, } txt = { nodata = "NO DATA", cur = "Currency", cur_h = "USD is the USDT pair; CZK, CHF, CAD, HUF are not on Binance.", bg = "Background", bg_o = { "none", "plain", "grid" }, glint = "Chart highlight", glint_o = { "none", "slow", "fast" }, ath = "Show ATH", loading = "LOADING", nowifi = "NO WIFI", offline = "OFFLINE", console = "SEE CONSOLE" } for k, v in pairs(({ sk = { nodata = "BEZ DAT", cur = "Mena", cur_h = "USD je pár USDT; CZK, CHF, CAD a HUF na Binance nie sú.", bg = "Pozadie", bg_o = { "žiadne", "jednofarebné", "mriežka" }, glint = "Odlesk grafu", glint_o = { "žiadny", "pomalý", "rýchly" }, ath = "Ukázať ATH", loading = "NACITAVAM", nowifi = "BEZ WIFI", console = "POZRI KONZOLU" }, cs = { nodata = "BEZ DAT", cur = "Měna", cur_h = "USD je pár USDT; CZK, CHF, CAD a HUF na Binance nejsou.", bg = "Pozadí", bg_o = { "žádné", "jednobarevné", "mřížka" }, glint = "Odlesk grafu", glint_o = { "žádný", "pomalý", "rychlý" }, ath = "Ukázat ATH", loading = "NACITAM", nowifi = "BEZ WIFI", console = "VIZ KONZOLI" }, })[lang] or {}) do txt[k] = v end local CUR = { "USD", "EUR", "GBP", "PLN", "JPY", "AUD", "BRL", "MXN", "TRY", "ZAR", "RON", "UAH", "ARS" } settings = { { key = "cur", type = "int", default = "USDT", label = txt.cur, hint = txt.cur_h, opts = { keys = { "USDT", "EUR", "GBP", "PLN", "JPY", "AUD", "BRL", "MXN", "TRY", "ZAR", "RON", "UAH", "ARS" }, table.unpack(CUR) } }, { key = "bg", type = "int", default = "grid", label = txt.bg, opts = { keys = { "none", "plain", "grid" }, table.unpack(txt.bg_o) } }, { key = "glint", type = "int", default = 1, label = txt.glint, opts = txt.glint_o }, { key = "ath", type = "bool", default = true, label = txt.ath }, } sources = { ticker = { url = "https://api.binance.com", limit = 15, fields = { "lastPrice", "priceChangePercent" }, demo = [[{"symbol":"BTCUSDT","lastPrice":"118432.00000000","priceChangePercent":"2.400"}]] }, hist = { url = "https://api.binance.com", limit = 60, demo = [=[[[0,"112500","112500","112500","112500"],[0,"112750","112750","112750","112750"],[0,"113000","113000","113000","113000"], [0,"113250","113250","113250","113250"],[0,"113500","113500","113500","113500"],[0,"113750","113750","113750","113750"], [0,"114000","114000","114000","114000"],[0,"114250","114250","114250","114250"],[0,"114500","114500","114500","114500"], [0,"114750","114750","114750","114750"],[0,"115000","115000","115000","115000"],[0,"115250","115250","115250","115250"], [0,"115500","115500","115500","115500"],[0,"115750","115750","115750","115750"],[0,"116000","116000","116000","116000"], [0,"116250","116250","116250","116250"],[0,"116500","116500","116500","116500"],[0,"116750","116750","116750","116750"], [0,"117000","117000","117000","117000"],[0,"117250","117250","117250","117250"],[0,"117500","117500","117500","117500"], [0,"117750","117750","117750","117750"],[0,"118000","118000","118000","118000"],[0,"118250","118250","118250","118250"]]]=] }, ath = { url = "https://api.coingecko.com", limit = 60, fields = { "[].ath", "[].ath_date" }, demo = [=[[{"ath":126080,"ath_date":"2025-10-06T10:57:42.000Z"}]]=] }, } local W, H = gfx.W, gfx.H local clear, px, blend, rect = gfx.clear, gfx.px, gfx.blend, gfx.rect local text, text_center, text_width = gfx.text, gfx.text_center, gfx.text_width local F_SMALL = { font = "3x5" } local F_MID = { font = "8x13b" } local F_BIG = { font = "9x15b" } local blit_rgb, clip = gfx.blit_rgb, gfx.clip local floor, abs, fmod, min = math.floor, math.abs, math.fmod, math.min local cos, sin = math.cos, math.sin local hash32, umod = math.hash32, math.umod local fmt, char, concat = string.format, string.char, table.concat local UP_R, UP_G, UP_B = 90, 220, 130 local DN_R, DN_G, DN_B = 235, 90, 90 local ICON do local ROWS = { "00101000", "11111100", "01100110", "01100110", "01111100", "01100011", "01100011", "11111110", "00101000" } local rows = {} for r = 1, 9 do local row = {} for c = 1, 8 do if ROWS[r]:sub(c, c) == "1" then row[c] = char(255, 196, 64) else row[c] = char(0, 0, 0) end end rows[r] = concat(row) end ICON = concat(rows) end local GRID = 9 local BG = nil local GRID_R, GRID_G, GRID_B = 26, 32, 62 local BG_R, BG_G, BG_B = 12, 14, 30 local function on_grid(v, size) local n = (size - 1) // GRID - 1 local o = (size - 1 - GRID * (n - 1)) // 2 return v >= o and v <= o + GRID * (n - 1) and (v - o) % GRID == 0 end local function build_bg(mode) if mode == "none" then BG = nil return end local base, line = char(BG_R, BG_G, BG_B), char(GRID_R, GRID_G, GRID_B) local rows = {} for y = 0, H - 1 do local row = {} if mode == "grid" and on_grid(y, H) then for x = 1, W do row[x] = line end else for x = 0, W - 1 do if mode == "grid" and on_grid(x, W) then row[x + 1] = line else row[x + 1] = base end end end rows[y + 1] = concat(row) end BG = concat(rows) end local CH_Y = 42 local CH_H = H - CH_Y local CHART, chart_key, chart_up = nil, nil, nil local TOP = {} local FILL_TOP, FILL_BOT = 0.55, 0.08 local function build_chart(hist, n, up) local h, lo, hi = {}, 1e18, -1e18 for i = 1, n do local c = tonumber(hist[i][5]) or 0 h[i] = c if c < lo then lo = c end if c > hi then hi = c end end local span = hi - lo if span < 1 then span = 1 end local fr, fg, fb if up then fr, fg, fb = UP_R, UP_G, UP_B else fr, fg, fb = DN_R, DN_G, DN_B end local fill, none = char(fr, fg, fb), char(0, 0, 0) local cols = {} for x = 0, W - 1 do local t = x / (W - 1) * (n - 1) local i0 = floor(t) local i1 = (i0 + 1 < n) and (i0 + 1) or i0 local f = t - i0 local v = h[i0 + 1] + (h[i1 + 1] - h[i0 + 1]) * f local y = CH_Y + CH_H - 1 - floor((v - lo) / span * (CH_H - 1) + 0.5) TOP[x] = y cols[x + 1] = y end local rows = {} for yy = CH_Y, CH_Y + CH_H - 1 do local k = FILL_TOP + (FILL_BOT - FILL_TOP) * (yy - CH_Y) / (CH_H - 1) local dim = char(floor(fr * k), floor(fg * k), floor(fb * k)) local row = {} for x = 0, W - 1 do local y = cols[x + 1] if yy < y then row[x + 1] = none elseif yy == y then row[x + 1] = fill else row[x + 1] = dim end end rows[yy - CH_Y + 1] = concat(row) end CHART = concat(rows) end local GLINT_SPEED = { 0, 21, 48 } local TAIL, PAUSE = 10, 24 local FPS = gfx.FPS local function draw_glint(f) local sp = GLINT_SPEED[(cfg.glint or 1) + 1] or 0 if sp <= 0 then return end local period = (W + TAIL + PAUSE) / sp local head = floor(fmod(f / FPS, period) * sp) - TAIL for k = 0, TAIL - 1 do local x = head + TAIL - 1 - k if x >= 0 and x < W then local a = 1.0 - k / TAIL local y = TOP[x] blend(x, y, 255, 255, 255, 0.8 * a) blend(x, y - 1, 255, 255, 255, 0.25 * a) end end end local function draw_arrow(x, y, up, r, g, b) if up then px(x + 2, y, r, g, b); rect(x + 1, y + 1, 3, 1, r, g, b); rect(x, y + 2, 5, 1, r, g, b) else rect(x, y, 5, 1, r, g, b); rect(x + 1, y + 1, 3, 1, r, g, b); px(x + 2, y + 2, r, g, b) end end local function fmt_pct(v) local neg = v < 0 local av = abs(v) local ip = floor(av) local frac = floor((av - ip) * 10 + 0.5) if frac >= 10 then frac = 0; ip = ip + 1 end return fmt("%s%d.%d%%", neg and "-" or "+", ip, frac) end local ATH_Y = 34 local PCT_Y = 29 local ATH_R, ATH_G, ATH_B = 255, 196, 64 local E2026, D2026 = 1767225600, 20454 local ath_key, ath_s = nil, nil local function days_civil(y, m, d) if m <= 2 then y = y - 1 end local era = y // 400 local yoe = y - era * 400 local doy = (153 * (m > 2 and m - 3 or m + 9) + 2) // 5 + d - 1 return era * 146097 + yoe * 365 + yoe // 4 - yoe // 100 + doy - 719468 end local function ath_secs(str) local y, mo, d, h, mi, se = (str or ""):match("^(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)") if not y then return nil end return (days_civil(tonumber(y), tonumber(mo), tonumber(d)) - D2026) * 86400 + tonumber(h) * 3600 + tonumber(mi) * 60 + tonumber(se) end local FW_S = 8 local FW_N, FW_T, FW_UP, FW_BURST = 3, 2.0, 0.6, 1.2 local FW_V, FW_G, FW_RAYS = 11, 8, 16 local FW_PAL = { { 255, 80, 80 }, { 140, 255, 150 }, { 150, 235, 255 }, { 255, 214, 80 }, { 255, 120, 240 }, { 247, 147, 26 } } local FW_DIR = {} for k = 0, FW_RAYS - 1 do FW_DIR[k + 1] = { cos(k * 6.28318 / FW_RAYS), sin(k * 6.28318 / FW_RAYS) } end local function draw_fireworks(t) clip(0, 0, W, CH_Y) for i = 0, FW_N - 1 do local tt = t + i * FW_T / FW_N local c, tau = floor(tt / FW_T), fmod(tt, FW_T) local key = c * 7 + i * 131 local x0, yb = 8 + umod(hash32(key + 1), W - 16), 4 + umod(hash32(key + 2), 12) local col = FW_PAL[umod(hash32(key + 3), #FW_PAL) + 1] if tau < FW_UP then local q = 1 - tau / FW_UP local y = floor(yb + (CH_Y - yb) * q * q) blend(x0, y, 255, 240, 200, 1) blend(x0, y + 1, 255, 180, 90, 0.5) elseif tau < FW_UP + FW_BURST then local s = tau - FW_UP local a = min(1, 2 * (1 - s / FW_BURST)) for _, d in ipairs(FW_DIR) do for j = 0, 2 do local sj = s - j * 0.07 if sj > 0 then blend(floor(x0 + d[1] * FW_V * sj + 0.5), floor(yb + d[2] * FW_V * sj + FW_G * sj * sj + 0.5), col[1], col[2], col[3], a * (1 - j * 0.35)) end end end if s < 0.12 then blend(x0, yb, 255, 255, 255, 1) end end end clip() end local was_ath, burst_at, last_f = false, nil, -1 local TITLE = "BTC/USD" function on_settings() net.fetch("ticker", "/api/v3/ticker/24hr?symbol=BTC" .. cfg.cur, 60) net.fetch("hist", "/api/v3/klines?interval=1h&limit=24&symbol=BTC" .. cfg.cur, 900) if cfg.ath and cfg.cur ~= "RON" then net.fetch("ath", "/api/v3/coins/markets?ids=bitcoin&vs_currency=" .. (cfg.cur == "USDT" and "usd" or cfg.cur), 86400) else net.stop("ath") end TITLE = "BTC/" .. (cfg.cur == "USDT" and "USD" or cfg.cur or "USD") build_bg(cfg.bg or "grid") PCT_Y = cfg.ath and 24 or 29 end local F_GATE = { font = "4x6" } local function gate(s, y, r, g, b) if s.ok then return true end r, g, b = r or 255, g or 250, b or 232 text_center(y, data.net.mode ~= "online" and txt.offline or not data.net.up and txt.nowifi or s.code ~= 0 and "ERR " .. s.code or txt.loading, r, g, b) if s.code ~= 0 then text_center(y + 10, txt.console, r, g, b, F_GATE) end return false end function render() clear(0, 0, 0) if BG then blit_rgb(BG, 0, 0, W, H) end local t = web.ticker local p = t and tonumber(t.lastPrice) local aj = cfg.ath and web.ath and web.ath[1] local a = aj and tonumber(aj.ath) if aj ~= ath_key then ath_key, ath_s = aj, aj and ath_secs(aj.ath_date) end local un = data.time and data.time.unix or 0 local is_ath = p and a and a > 0 and (p >= a or (ath_s and un > E2026 and un - E2026 - ath_s < 86400)) or false if is_ath and (not was_ath or frame < last_f) then burst_at = frame end was_ath, last_f = is_ath, frame if is_ath and burst_at and frame - burst_at < FW_S * FPS then draw_fireworks((frame - burst_at) / FPS) end local start_x = (W - (8 + 3 + text_width(TITLE))) // 2 blit_rgb(ICON, start_x, 0, 8, 9, 0, 0, 0) text(start_x + 11, 1, TITLE, 255, 250, 232) if not gate(data.src.ticker, 30) then return end if not p or p <= 0 then text_center(28, txt.nodata, 120, 120, 130, F_SMALL) return end local pct = tonumber(t.priceChangePercent) or 0 local up = pct >= 0 local r, g, b if up then r, g, b = UP_R, UP_G, UP_B else r, g, b = DN_R, DN_G, DN_B end local cr, cg, cb = r, g, b if is_ath then cr, cg, cb = ATH_R, ATH_G, ATH_B end local ip = floor(p + 0.5) if ip < 0 then ip = 0 end local s = fmt("%d", ip) if text_width(s, F_BIG) <= W - 2 then text_center(11, s, cr, cg, cb, F_BIG) elseif text_width(s, F_MID) <= W - 2 then text_center(12, s, cr, cg, cb, F_MID) else text_center(15, s, cr, cg, cb, F_SMALL) end local ps = fmt_pct(pct) local x0 = (W - (7 + 3 + text_width(ps))) // 2 draw_arrow(x0, PCT_Y + 1, up, r, g, b) text(x0 + 10, PCT_Y, ps, r, g, b) if is_ath then text_center(ATH_Y, "NEW ATH", ATH_R, ATH_G, ATH_B, F_SMALL) elseif a and a > 0 then local as = fmt_pct((p / a - 1) * 100) local ax = (W - text_width("ATH " .. as, F_SMALL)) // 2 text(ax, ATH_Y, "ATH", 160, 160, 170, F_SMALL) text(ax + text_width("ATH ", F_SMALL), ATH_Y, as, ATH_R, ATH_G, ATH_B, F_SMALL) end local hist = web.hist if hist then local n = #hist if n > 24 then n = 24 end if n >= 2 then if hist ~= chart_key or up ~= chart_up then build_chart(hist, n, up) chart_key, chart_up = hist, up end blit_rgb(CHART, 0, CH_Y, W, CH_H, 0, 0, 0) draw_glint(frame) end end end