(close-records!)Closes any open CSV records.
Closes any open CSV records.
(init-portfolio date capital)This function initialises or restarts the backtester at date with
capital in cash. A restart clears the previous run: the portfolio,
its records, indicators, automations and any orders still pending.
When OUTPUT-DIR is set, the order, portfolio-value and evaluation
records are also written there as CSV files.
This function initialises or restarts the backtester at `date` with `capital` in cash. A restart clears the previous run: the portfolio, its records, indicators, automations and any orders still pending. When OUTPUT-DIR is set, the order, portfolio-value and evaluation records are also written there as CSV files.
(log-return curr prev)Natural-log return from prev to curr, or 0.0 when either is not positive.
Natural-log return from `prev` to `curr`, or 0.0 when either is not positive.
(on-init-portfolio key f)Registers f, a function of no arguments, to be called by every
init-portfolio so that it clears run state this namespace cannot reach.
Registering again under the same key replaces the earlier hook.
Registers `f`, a function of no arguments, to be called by every init-portfolio so that it clears run state this namespace cannot reach. Registering again under the same `key` replaces the earlier hook.
(portfolio-exposure)The current portfolio map summarised in dollars: :cash, :long (market value of long positions), :short (absolute market value of short positions), :equity (cash + long - short), :gross (long + short) and :loan (cash borrowed, i.e. the negative cash balance).
The current portfolio map summarised in dollars: :cash, :long (market value of long positions), :short (absolute market value of short positions), :equity (cash + long - short), :gross (long + short) and :loan (cash borrowed, i.e. the negative cash balance).
(record-portfolio-value date)Appends (or replaces) today's entry in the portfolio-value vector from the current portfolio map: equity as :tot-value, its log return since the previous trading day's entry (or since the starting capital on the first day), the cumulative return, cash borrowed (:loan), the value of borrowed stock (:short), :leverage = (loan + short) / equity and :margin = equity / gross position value (1.0 with no positions).
Appends (or replaces) today's entry in the portfolio-value vector from the current portfolio map: equity as :tot-value, its log return since the previous trading day's entry (or since the starting capital on the first day), the cumulative return, cash borrowed (:loan), the value of borrowed stock (:short), :leverage = (loan + short) / equity and :margin = equity / gross position value (1.0 with no positions).
(revalue-holdings! date info-map)Brings every holding that has a row in today's info-map to today's
price, crediting dividends to cash, then records today's portfolio value.
A holding with no row for MISSING-DAYS-LIMIT trading days in a row is
treated as delisted and written off to cash at its last value.
Brings every holding that has a row in today's `info-map` to today's price, crediting dividends to cash, then records today's portfolio value. A holding with no row for MISSING-DAYS-LIMIT trading days in a row is treated as delisted and written off to cash at its last value.
(revalue-position {:keys [price aprc quantity cfacpr] :as position} info)Carries a position to today's data row info. Returns [position cash]
where cash is the dividend paid out to the cash balance.
With REINVEST-DIVIDENDS the position's value follows the security's
total return (the APRC ratio since it was last valued) and the share
count absorbs both dividends and splits; nothing is paid out.
Without it the share count changes only by the split factor and the
remainder of the total return is paid out as cash. Short positions pay
the dividend instead of receiving it.
Carries a position to today's data row `info`. Returns [position cash] where cash is the dividend paid out to the cash balance. With REINVEST-DIVIDENDS the position's value follows the security's total return (the APRC ratio since it was last valued) and the share count absorbs both dividends and splits; nothing is paid out. Without it the share count changes only by the split factor and the remainder of the total return is paid out as cash. Short positions pay the dividend instead of receiving it.
(total-value)This function returns the remaining total value including the cash and stock value
This function returns the remaining total value including the cash and stock value
(update-loan date amount is-interest)Records today's portfolio value after a change in borrowing. When
is-interest is true, amount is interest debited from cash first;
otherwise it is the amount just borrowed, which is informational since
the loan balance is read from the cash balance.
Records today's portfolio value after a change in borrowing. When `is-interest` is true, `amount` is interest debited from cash first; otherwise it is the amount just borrowed, which is informational since the loan balance is read from the cash balance.
(update-portfolio date permno quantity info loan)Applies a trade of quantity shares of permno at today's row info
and records today's portfolio value. loan is the amount borrowed for it.
Applies a trade of `quantity` shares of `permno` at today's row `info` and records today's portfolio value. `loan` is the amount borrowed for it.
(update-portfolio-map date permno quantity info loan)Buys (quantity > 0) or sells quantity shares of permno at the price in
today's row info, moving quantity * price between cash and the holding.
Buys (quantity > 0) or sells quantity shares of `permno` at the price in today's row `info`, moving quantity * price between cash and the holding.
(write-off! permno)Books the holding in permno as cash at its last value and removes it,
for a security that has stopped trading.
Books the holding in `permno` as cash at its last value and removes it, for a security that has stopped trading.
(write-record! wrtr-atom line)Appends line to the record held in wrtr-atom, if that record is being written.
Appends `line` to the record held in `wrtr-atom`, if that record is being written.
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 |