Skip to content

Data and Primitives

Where every expression starts: prices, fields from the data, and the returns, volatility and volume built from them. The panels run on the toy data.

Prices and fields

close

close() open() high() low()

Each stock's daily prices, adjusted for splits and dividends. Use them for returns and for a stock's own history; to compare stocks with each other, or with a dollar amount, use an as-traded field such as field('price') (Adjusted Data and Look-Ahead).

close()
ABCD
Jan 0210.020.030.040.0
Jan 0311.019.030.542.0
Jan 0412.018.0·41.0
Jan 0511.018.531.043.0
Jan 0813.017.032.044.0
Jan 0914.016.031.546.0

field

field('name')

Any field of the data, by name, read as numbers. A field given once per day (an index level, a rate) is given to every stock. Labels such as a sector are read with group('name') instead.

field('surprise')
ABCD
Jan 02····
Jan 030.05···
Jan 04····
Jan 05··−0.02·
Jan 08·−0.01·0.03
Jan 09····
field('mcap')                    # market capitalization, if the data has it
1 / field('price_to_book')       # book-to-price

market

market()

The market's price level: the data's market series, the same for every stock. It is whatever market the data supplies, such as an index's total-return close or an equal-weight index of the universe.

market()
ABCD
Jan 02100.0100.0100.0100.0
Jan 03101.0101.0101.0101.0
Jan 04100.5100.5100.5100.5
Jan 05102.0102.0102.0102.0
Jan 08103.0103.0103.0103.0
Jan 09102.5102.5102.5102.5

Returns, risk and volume

returns

returns(period[, 'field'])

The return over period days: today's price over the last price at or before period days ago, minus 1,

\[ r_t = \frac{p_t}{p_{t'}} - 1, \qquad t' = \text{the last day at or before } t - \text{period with a price} \]

The price is close unless another field is named; returns(1, 'market') is the market's daily return.

On a day with no price there is no return, so a halted stock drops out of rank and the group operators instead of counting as 0%. The day it trades again carries the whole move since its last price: below, C on Jan 05 is 31 / 30.5 − 1, measured from Jan 03. A price more than 10 days old no longer counts.

close()
ABCD
Jan 0210.020.030.040.0
Jan 0311.019.030.542.0
Jan 0412.018.0·41.0
Jan 0511.018.531.043.0
Jan 0813.017.032.044.0
Jan 0914.016.031.546.0
returns(1)
ABCD
Jan 02····
Jan 030.100−0.0500.0170.050
Jan 040.091−0.053·−0.024
Jan 05−0.0830.0280.0160.049
Jan 080.182−0.0810.0320.023
Jan 090.077−0.059−0.0160.045
returns(20)                 # one month
returns(1, 'market')        # the market's daily return
-returns(5)                 # last week, reversed: a reversal signal

volatility

volatility(period[, 'field'])

Annualized volatility: the standard deviation of daily returns over period days, times √252,

\[ \sigma_t = \sqrt{252} \cdot \mathrm{std}(r_{t-\text{period}+1}, \ldots, r_t) \]

Daily returns follow the rule above. Part of the window (window rules).

volatility(60)
returns(60) / volatility(60)    # risk-adjusted momentum

volume

volume(period)

Average daily volume over period days, in shares; volume(1) is the day's volume. Part of the window.

volume(1)
ABCD
Jan 02100300200400
Jan 03120280210380
Jan 04150260·500
Jan 05110300260420
Jan 08200250240440
Jan 09180240230600
volume(5) / volume(60)          # recent volume against its norm

adv

adv(period)

Average daily dollar volume, close × volume, over period days. Part of the window. Dollar volume compares across stocks; share volume does not.

log(adv(20))                    # liquidity, on a log scale
where(adv(20) > 5e6, rank(-returns(5)), None)    # trade only the liquid names