Clojure Backtesting

Menu

  • Home
  • Get Started
  • User Guide
  • Basic APIs
  • Porfolio Mangement
  • Evaluation
  • Indicators
  • Parameters
  • Examples
  •  Github

Portfolio Management

  1. Portfolio Initialization
    1. init_portfolio
  2. Portfolio Inspection
    1. print_portfolio
    2. print-portfolio-record

This page features functions associated with manipulating and viewing the portfolio.


Portfolio Initialization

init_portfolio

This function initialises the portfolio with cash and a date. Note that is is a must to call the function before executing functions e.g. available-tics and those in the counter namespace.

Parameters:

  • date - the starting date of the portfolio, in format "YYYY-MM-DD"
  • init-capital - the desired initial capital for the portfolio (non-negative integer)

Example:

(init_portfolio "1980-12-1" 1000000)

;; output:
null

Portfolio Inspection

print_portfolio

This function prints the portfolio in a table format.

Parameters:

  • none

Ouput explanation:

 Column  Format   Meaning
asset stringCash or ticker of the stock
price float, $Price of the stock  
aprc float, %Adjusted price of the stock  
quantity int or floatQuantity of the stock owned  
tot_val int, $Total value of the stock  

Example:

(print-portfolio)

;; output:
| :asset |  :price | :aprc | :quantity | :tot_val |
|--------+---------+-------+-----------+----------|
|   cash |     N/A |   N/A |       N/A |    10295 |
|   58043 |   34.19 | 29.42 |         0 |        0 |

print-portfolio-record

This function prints the historical values and daily returns of the portfolio in a table format.

Parameters:

  • n - no. of rows to print, if n <= 0, print entire record

Ouput explanation:

 Column  Format   Meaning
date YYYY-MM-DDDate of record
tot_value int, $Total value of the portfolio  
daily_ret float, %Daily return of the portfolio  
tot_ret float, %Total return of the portfolio  
loan int, $Amount of loan made (cumulative)  
leverage float, %Leverage ratio given by (total debt / total equity)  
margin float, %Portfolio margin given by (cash / total value of securities), > 0 if there is leverage  

Example:

(print-portfolio-record)

;; output:
|      :date | :tot-value | :daily-ret | :tot-ret |  :loan | :leverage | :margin |
|------------+------------+------------+----------+--------+-----------+---------|
| 1980-12-15 |    $210.00 |      0.00% |    0.00% |  $0.00 |      0.00 |   0.00% |
| 1980-12-16 |    $210.00 |      0.00% |    0.00% | $48.19 |      0.23 |  81.34% |
| 1980-12-17 |    $212.75 |      0.13% |    0.13% | $48.19 |      0.23 |  81.53% |
| 1980-12-18 |    $216.00 |      0.15% |    0.27% | $48.19 |      0.22 |  81.76% |
| 1980-12-19 |    $222.87 |      0.29% |    0.57% | $48.19 |      0.22 |  82.22% |
| 1980-12-22 |    $228.51 |      0.23% |    0.80% | $48.19 |      0.21 |  82.58% |
| 1980-12-23 |    $233.51 |      0.19% |    0.99% | $48.19 |      0.21 |  82.89% |
| 1980-12-24 |    $239.84 |      0.23% |    1.23% | $48.19 |      0.20 |  83.27% |
| 1980-12-26 |    $251.08 |      0.38% |    1.61% | $48.19 |      0.19 |  83.90% |
(print-portfolio-record 3)

;; output:
|      :date | :tot-value | :daily-ret | :tot-ret |  :loan | :leverage | :margin |
|------------+------------+------------+----------+--------+-----------+---------|
| 1980-12-15 |    $210.00 |      0.00% |    0.00% |  $0.00 |      0.00 |   0.00% |
| 1980-12-16 |    $210.00 |      0.00% |    0.00% | $48.19 |      0.23 |  81.34% |
| 1980-12-17 |    $212.75 |      0.13% |    0.13% | $48.19 |      0.23 |  81.53% |


Copyright © 2023 clojure-backtesting

Powered by Cryogen