Optional DuckDB integration for clj-yfinance datasets.
Loads datasets fetched via clj-yfinance into an embedded DuckDB instance and lets you query them with SQL.
REQUIRES: Add to your deps.edn aliases: {:duckdb {:extra-deps {com.techascent/tmducken {:mvn/version "0.10.1-01"} techascent/tech.ml.dataset {:mvn/version "7.032"}}}}
DuckDB also requires a native shared library (libduckdb). On Linux you can install it via your package manager, or set the DUCKDB_HOME environment variable to the directory containing the library before starting your REPL.
Then start your REPL with: clojure -M:duckdb:nrepl
USAGE: (require '[clj-yfinance.duckdb :as yf-db])
;; Open an in-memory database and load some data (def db (yf-db/open-db)) (yf-db/load-historical! db "AAPL" :period "1y") (yf-db/load-multi-ticker! db ["AAPL" "GOOGL" "MSFT"] :period "1y")
;; Query with SQL (yf-db/query db "SELECT * FROM AAPL ORDER BY timestamp DESC LIMIT 5") (yf-db/query db "SELECT ticker, AVG(close) AS avg_close FROM prices GROUP BY ticker")
;; Close when done (yf-db/close! db)
Optional DuckDB integration for clj-yfinance datasets.
Loads datasets fetched via clj-yfinance into an embedded DuckDB instance
and lets you query them with SQL.
REQUIRES: Add to your deps.edn aliases:
{:duckdb {:extra-deps {com.techascent/tmducken {:mvn/version "0.10.1-01"}
techascent/tech.ml.dataset {:mvn/version "7.032"}}}}
DuckDB also requires a native shared library (libduckdb). On Linux you can
install it via your package manager, or set the DUCKDB_HOME environment
variable to the directory containing the library before starting your REPL.
Then start your REPL with: clojure -M:duckdb:nrepl
USAGE:
(require '[clj-yfinance.duckdb :as yf-db])
;; Open an in-memory database and load some data
(def db (yf-db/open-db))
(yf-db/load-historical! db "AAPL" :period "1y")
(yf-db/load-multi-ticker! db ["AAPL" "GOOGL" "MSFT"] :period "1y")
;; Query with SQL
(yf-db/query db "SELECT * FROM AAPL ORDER BY timestamp DESC LIMIT 5")
(yf-db/query db "SELECT ticker, AVG(close) AS avg_close FROM prices GROUP BY ticker")
;; Close when done
(yf-db/close! db)(close! {:keys [db conn]})Disconnect and close a database opened with open-db.
Example: (close! db)
Disconnect and close a database opened with open-db. Example: (close! db)
(load-dataset! {:keys [conn]} dataset & {:keys [table-name] :as opts})Load an arbitrary tech.v3.dataset into DuckDB under the given table name.
The dataset must have a :name in its metadata, or you must supply :table-name via options. Creates the table and inserts all rows.
Returns the number of rows inserted.
Example: (load-dataset! db my-ds :table-name "prices")
Load an arbitrary tech.v3.dataset into DuckDB under the given table name. The dataset must have a :name in its metadata, or you must supply :table-name via options. Creates the table and inserts all rows. Returns the number of rows inserted. Example: (load-dataset! db my-ds :table-name "prices")
(load-historical! {:keys [conn]} ticker & opts)Fetch historical OHLCV data for ticker and load into DuckDB.
The table name defaults to the ticker symbol (e.g. "AAPL"). Options are passed through to fetch-historical: :period, :interval, :start, :end, :adjusted, :prepost
Returns the number of rows inserted, or nil if fetch fails.
Example: (load-historical! db "AAPL" :period "1y")
Fetch historical OHLCV data for ticker and load into DuckDB. The table name defaults to the ticker symbol (e.g. "AAPL"). Options are passed through to fetch-historical: :period, :interval, :start, :end, :adjusted, :prepost Returns the number of rows inserted, or nil if fetch fails. Example: (load-historical! db "AAPL" :period "1y")
(load-multi-ticker! {:keys [conn]}
tickers
&
{:keys [table-name] :or {table-name "prices"} :as opts})Fetch historical data for multiple tickers and load into DuckDB as a single table.
The combined dataset (with a :ticker column) is loaded under the given table name, defaulting to "prices".
Options are passed through to fetch-historical: :period, :interval, :start, :end, :adjusted, :prepost
Returns the number of rows inserted, or nil if all fetches fail.
Example: (load-multi-ticker! db ["AAPL" "GOOGL" "MSFT"] :period "1y") (query db "SELECT ticker, AVG(close) FROM prices GROUP BY ticker")
Fetch historical data for multiple tickers and load into DuckDB as a single table. The combined dataset (with a :ticker column) is loaded under the given table name, defaulting to "prices". Options are passed through to fetch-historical: :period, :interval, :start, :end, :adjusted, :prepost Returns the number of rows inserted, or nil if all fetches fail. Example: (load-multi-ticker! db ["AAPL" "GOOGL" "MSFT"] :period "1y") (query db "SELECT ticker, AVG(close) FROM prices GROUP BY ticker")
(open-db)(open-db path)Open an embedded DuckDB database. Pass a file path to persist to disk, or omit for an in-memory database.
Calls duckdb/initialize! automatically on first use.
Returns a map {:db <db> :conn <conn>} that is passed to the other functions.
Examples: (open-db) ; in-memory (open-db "finance.db") ; persistent
Open an embedded DuckDB database. Pass a file path to persist to disk,
or omit for an in-memory database.
Calls duckdb/initialize! automatically on first use.
Returns a map {:db <db> :conn <conn>} that is passed to the other functions.
Examples:
(open-db) ; in-memory
(open-db "finance.db") ; persistent(query {:keys [conn]} sql)Run a SQL query against the database and return a dataset.
Example: (query db "SELECT * FROM AAPL ORDER BY timestamp DESC LIMIT 10") (query db "SELECT ticker, AVG(close) AS avg_close FROM prices GROUP BY ticker ORDER BY avg_close DESC")
Run a SQL query against the database and return a dataset. Example: (query db "SELECT * FROM AAPL ORDER BY timestamp DESC LIMIT 10") (query db "SELECT ticker, AVG(close) AS avg_close FROM prices GROUP BY ticker ORDER BY avg_close DESC")
(run! {:keys [conn]} sql)Run a SQL statement ignoring the result. Useful for DDL or DML statements.
Example: (run! db "DROP TABLE IF EXISTS prices")
Run a SQL statement ignoring the result. Useful for DDL or DML statements. Example: (run! db "DROP TABLE IF EXISTS prices")
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |