Language Syntax Timeseries & Extension Dedicated Experimental Examples Articles Sources Specifications Playground Home Contact

Tick Time Series dedicated functions

backward

Shift all values to n previous time. n first updates are lost if keep is false or first time is used if keep is true.

(backward ts {n {keep}})

parameter

clockref aka cf

Define the timeserie as a clock reference, or metronome. For any operation between several timeseries there will be one update for output if there is one update for the clockref serie. This synchronization mode ensure that length of output timeserie is <= to clockref timeserie. Adding a ref-value ensure length of output timeserie is = to clockref timeserie, uncomputed values due to missing data from other series will be replaced by ref-value.

(cf ts {ref-value})       ;short keywork
(clockref ts {ref-value}) ;long keyword

parameter

delta

Returns timeserie where each point is the current value minus previous value of the input timeserie. So result length is input length-1, and first output point time is time of second input point time.

(delta ts)

parameter

fields

Return all available fields for a given source, code and date.

(fields source code date)

parameter

forward

Shift all values to n next time. n last updates are lost if keep is false or last time is used if keep is true.

(forward ts {n {keep}})

parameter

graphsample

Subsample timeserie in order to minimize number of points but to keep same result on screen. This is useful to get a smaller timeserie that will be drawn like the full timeserie if your output screen is nbr pixel width. As graphsample process on a stream it can’t know the future so sends lots of points at the beginning then less and less.

(graphsample nbr ts)

parameters

example

Show all S&P E-Mini future bid prices not null on a Full HD screen (more than 7 million points)

(graphsample 1920 (timeserie @bid-price "refinitiv" "ESc1" 2016-01-20))

hist

Return histogram of data as a hash map. If argument is a simple timeserie, it returns the distribution of each value. If argument is a timeserie of array, it returns the sum of last element of array and use all previous element as the key, see sync for an example.

MEMORY WARNING hist result can be huge if there is a lot of different values.

(hist arg1 {arg2 {arg3 ...}}})

parameter

example

Show number of BTC trades per price. Here price is rounded to closer multiple of 10 to have less beans. We can see there are 1183 trades between 1455 and 1464 (1460 bean). See sync for a turn-over histogram example. See also mean, median, sd for other statistical functions.

(def prices
  (timeserie @trade-price "bitstamp" "BTC" 2018-02-28))
(hist (round prices 1))

history

Return all available dates for a given source and code.

As history can be huge depending on source better call through a LispTick client with streaming (Walk method).

(history source code)

keep

Only keep points in timeserie that fulfill the given condition

(keep ts func args2 args3 ...)

parameters

examples

Change timeserie ts to keep only positif values

(keep ts > 0)

Limit perimeter to code ending with “.PA”

(keep (perimeter "refinitiv" 2016-02-01) has-suffix ".PA")

Only keep trade prices when trade volume is greater that 2

[
  (label "all trade prices"
    (timeserie @trade-price "bitstamp" "BTC" 2018-02-28))
  (label "trade prices with volume >2"
    (keep
      (timeserie @trade-price "bitstamp" "BTC" 2018-02-28)
      (> (timeserie @trade-volume "bitstamp" "BTC" 2018-02-28) 2)))
]

Kurtosis

Return unbiased kurtosis of samples. Kurtosis (from Greek: κυρτός, kyrtos or kurtos, meaning “curved, arching”) is a measure of the “tailedness” of the probability distribution of a real-valued random variable. Like skewness, kurtosis describes the shape of a probability distribution. If argument is a timeserie, it returns online kurtosis at any time.

(kurtosis arg1 {arg2 {arg3 ...}}})

parameter

example

See also hist, mean, median for other statistical functions.

Show trade price Kurtosis for BTC on 28th february 2018.

(kurtosis (timeserie @trade-price "bitstamp" "BTC" 2018-02-28))

Show kurtosis for BTC 1 minute log-return on 28th february 2018.

(defn log-return[ts lag]
  (ln
    (+ 1
      (/ (- ts (+ ts lag)) (+ ts lag)))))
(kurtosis
  (log-return
    (timeserie @trade-price "bitstamp" "BTC" 2018-02-28)
    1m))

label

Give a name to the timeserie that can be used in a graph for example.

(label name ts)

parameters

localize

Set time location, useful to ensure time precision when created manually.

(localize location t|ts )

parameters

examples

Tests ensuring daylight saving is well handled

(assert (=
  (localize "Europe/Paris" 2017-12-18T14:12)
  (localize "US/Eastern" 2017-12-18T08:12)))
(assert (=
  (localize "Europe/Paris" 2017-11-01T14:12)
  (localize "US/Eastern" 2017-11-01T09:12)))

map

Apply a function to each element of a serie. Serie can be list, array, hash(histogram) or a timeserie.

(map fn arg)

parameter

examples

Normalisation of an histogram, sum of all bean will be 1, price rounded to the nearest hundred (10 ^ 2). See hist for the histogram.

(def prices
  (timeserie @trade-price "bitstamp" "BTC" 2018-02-28))
(def histogram (hist (round prices 2)))
;compute all beans summation
(def total (last (+ histogram)))
;normalize with anonymous function 
(map (fn[a] (/ a total)) histogram)

map-reduce

Apply function with all given argument combinations using all available ressources.

(map-reduce func reduce args1 args2 ...)

parameter

example

Compute number of trades for Bitcoin, Ether, Ripple from source bitstamp in February 2018. Show only the result value of timeserie, not the time (which is time of last trade).

(defn nbr-trade[d code]
  (subsample 1D count
    (timeserie @trade-price "bitstamp" code d)))
(vget
  (map-reduce nbr-trade sum
    (range 2018-02-01 2018-02-28 1D)
    ["BTC" "ETH" "XRP"]))

map-reduce-arg

As map-reduce but result is a timeserie of pairs like ([args1 args2 …] . result) instead of a simple timeserie of results.

(map-reduce-arg func reduce args1 args2 ...)

parameter

max

Max returns maximum of several values. If an argument is a timeserie, latest value is used for comparaison. If argument is a single timeserie, result is timeserie of maximum value at any point in time.

(max a {b} ...)

parameters

example

(max
  (timeserie @bid-price "bitstamp" "BTC" 2018-02-14)
  (forward (timeserie @bid-price "bitstamp" "BTC" 2018-02-14)))
(vget (last (max
  (timeserie @trade-price "bitstamp" "BTC" 2018-02-01 2018-02-28))))

mean

Return arithmetic mean of samples. If argument is a timeserie, it returns online mean at any time.

(mean arg1 {arg2 {arg3 ...}}})

parameter

example

See also hist, median, sd for other statistical functions.

Show mean trade price for BTC on 28th february 2018.

(mean (timeserie @trade-price "bitstamp" "BTC" 2018-02-28))

Show mean of amount in $ exchanged per trade for BTC on 28th february 2018.

(vget (last (mean
  (*
    (timeserie @trade-price "bitstamp" "BTC" 2018-02-28)
    (timeserie @trade-volume "bitstamp" "BTC" 2018-02-28)))))

median

Return median of samples. The median is the value separating the higher half from the lower half of a data sample. If argument is a timeserie, it returns median of all timeserie values.

MEMORY WARNING median memory usage can be huge if there is a lot of different values.

(median arg1 {arg2 {arg3 ...}}})

parameter

example

See also hist, mean, sd for other statistical functions.

Show median trade price for BTC on 28th february 2018.

(median (timeserie @trade-price "bitstamp" "BTC" 2018-02-28))

Show median amount in $ exchanged per trade for BTC on 28th february 2018.

(vget (last (median
  (*
    (timeserie @trade-price "bitstamp" "BTC" 2018-02-28)
    (timeserie @trade-volume "bitstamp" "BTC" 2018-02-28)))))

merge

Merge several timeseries into one, synchronize points from all timeseries.

(merge ts1 ts2 ...)

parameters

min

Min returns minimum of several values. If an argument is a timeserie latest value is used for comparaison. If argument is a single timeserie, result is timeserie of minimum value at any point in time.

(min a {b} ...)

parameters

See max for some examples

now

Retrieve current time

(now)

example

trade, bid & ask prices from previous hour until next hour for Bitcoin from Bitstamp. You can also see in this example how to define a function and how to label a timeserie with a string to have a nicer name on screen.

(defn ts[field]
  (label (str field)
  (timeserie field "bitstamp" "BTC" (- (now) 1h) (+ (now) 1h))))
[
  (ts @trade-price)
  (ts @bid-price)
  (ts @ask-price)
]

one

Replace value of every update by 1.0 .

(one ts)

parameter

perimeter

Return all available codes for a given source and date or range.

As perimeter can be huge depending on source better call through a LispTick client with streaming (Walk method).

(perimeter source date {stop})

parameter

prune

Filter points in timeserie where value doesn’t change.

(prune ts)

parameter

range

Create an interator on an range of values. Should be used as an argument for map-reduce.

(range start stop step)

parameter

example

Every day from 19 January 2018 until 1st May 2018, included

(range 2018-01-19 2018-05-01 1D)

reverse

Reverse timeserie timeline in order to apply value in past. This feature is dedicated to factor/spread adjustement that needs to be applied for all point in the past starting at a specific time.

!!! Be very carefull when using this feature as ALL timeserie points will be stored in memory!!!

(reverse ts {op})

parameters

rts (recursive timeserie)

Timeserie of rescursive summation. Allows exponential moving average. Formula is: rts(t) = at * ts(t) + at-1 * rts(t-1)

(rts ts at at-1...)

parameters

example

Compute full volume traded during the day at any time, equivalent to sigma function.

(rts (timeserie @trade-volume "bitstamp" "BTC" 2018-02-28) 1 1)

sd

Return unbiased standard deviation of samples. Standard deviation is a measure of the amount of variation or dispersion of a set of values. If argument is a timeserie, it returns online standard deviation at any time.

(sd arg1 {arg2 {arg3 ...}}})

parameter

example

See also hist, mean, median for other statistical functions.

Show standard deviation trade price for BTC on 28th february 2018.

(sd (timeserie @trade-price "bitstamp" "BTC" 2018-02-28))

Show standard deviation amount in $ exchanged per trade for BTC on 28th february 2018.

(sd
  (*
    (timeserie @trade-price "bitstamp" "BTC" 2018-02-28)
    (timeserie @trade-volume "bitstamp" "BTC" 2018-02-28)))

sigma (+ with only one timeserie)

Returns a timeserie where each point is the summation of all previous points in the input timeserie.

(+ ts)

parameter

example

Compute full volume traded during the day at any time.

(+ (timeserie @trade-volume "bitstamp" "BTC" 2018-02-28))

sign

Replace value of every update by its sign:

(sign ts)

parameter

skewness

Return unbiased skewness(asymmetry) of samples. Skewness is a measure of the asymmetry of the probability distribution of a real-valued random variable about its mean. The skewness value can be positive, zero, negative, or undefined. If argument is a timeserie, it returns online skewness at any time.

(skewness arg1 {arg2 {arg3 ...}}})

parameter

example

See also hist, mean, median for other statistical functions.

Show trade price skewness for BTC on 28th february 2018.

(skewness (timeserie @trade-price "bitstamp" "BTC" 2018-02-28))

Show skewness for BTC 1 minute log-return on 28th february 2018.

(defn log-return[ts lag]
  (ln
    (+ 1
      (/ (- ts (+ ts lag)) (+ ts lag)))))
(vget (last (skewness
  (log-return
    (timeserie @trade-price "bitstamp" "BTC" 2018-02-28)
    1m))))

slice

Only keep timeserie points, tensor dimension in [start, stop[. If no timezone is given with start or stop it will be “local” to timeserie timezone. When start and stop are integers they represent absolute position, starting at 0, and last index to keep. No stop means keep everything after start. A negative stop of -n means forget n last point(s).

(slice x [start {stop {step}}]  {[start {stop {step}}]} ...)

parameters

sliding

Replace each update by the specified value from the sliding window. Typical usage is a moving average.

(sliding period|int sampling ts)

parameters

examples

Average weighted by number of updates, like Bollinger Band base. Here the average is done on 1000 updates or on a 2h window. Using a period instead of a number of updates allows to extend Bollinger bands to high frequency data.

Use graphsample to get same graph but with less data transmission.

(defn bollinger[d ts]
  (label (str "bollinger " d)
    (/
      (sliding d + ts)
      (sliding d len ts))))
[
(graphsample 1920 (timeserie @trade-price "bitstamp" "BTC" 2018-02-23))
(graphsample 1920 (bollinger 1000 (timeserie @trade-price "bitstamp" "BTC" 2018-02-23)))
(graphsample 1920 (bollinger 2h (timeserie @trade-price "bitstamp" "BTC" 2018-02-23)))
]

str

Applied on timseries, str creates a timeserie of string. If one input timeserie is a clockref, there will be exactly the same number of update as this timeserie.

(str arg1 {arg2 {arg3...}})

parameter

example

Show computation as nice timeserie of strings.

(def ts1
  (timeserie 2020-02-01 1 2020-02-03 5))
(def ts2
  (timeserie 2020-02-01 3 2020-02-02 2))
(str ts1 "+" ts2 "=" (clockref (+ ts1 ts2)))

subsample

Keep only one point per period from input timeserie, sampling defines which point to keep. Each value is taken in the range [i * period, (i+1) * period[, each time value is the time of original point.

(subsample period sampling ts)

parameters

examples

Request 10 minutes timebar for a stock.

Open High Low Close

(def 
  ts (timeserie @trade-price "bitstamp" "BTC" 2021-02-09)
  w 10m)
[
  [
    (subsample w open ts)
    (subsample w high ts)
    (subsample w low ts)
    (subsample w close ts)
  ]
]

Volume

(subsample 10m + (timeserie @trade-volume "bitstamp" "BTC" 2021-02-09))

Number of 1st limit bid update per hour for S&P E-Mini future

(subsample 1h len (timeserie @bid-price "refinitiv" "ESc1" 2016-01-20))

sync

Synchronise several timeseries and generate a timeserie of array. Use this fonction to create an export (csv for example) of synchronized series. If no value is available from a timeserie, previous or empty value is used. If one input timeserie is a clockref, there will be exactly the same number of update as this timeserie.

(sync ts1 {ts2 {ts3...}})

parameter

example

Show turnovers histogram in $ of BTC per price. Here price is rounded to closer multiple of 10 to have less beans. We can see there is a 4.5M$ turnover between 1495 and 1504 (1500 bean). See hist for a simpler histogram example.

(def prices
  (timeserie @trade-price "bitstamp" "BTC" 2018-02-28))
(def turnovers
  (*
   (timeserie @trade-price "bitstamp" "BTC" 2018-02-28)
   (timeserie @trade-volume "bitstamp" "BTC" 2018-02-28)))
(hist (sync (round prices 1) turnovers))

temporal

Creates a timeserie of temporal vector from a timeserie. From a timeserie of scalar it will keep the n latest updates into a vector of size n. Typical usage is to transform a simple timeserie into an input for a Temporal Convolutional Network.

You can use a duration as a filter to ensure all points are separated by the desired amount of time. If duration between two points is different from d, full matrice will be forgotten.

(temporal ts n {d})

parameter

tget

Get time at a precise index in a timeserie. If index is a time return same time if an update exists or return time of update just before.

Result is “()” so isnull? true, when index unfound in serie.

(tget ts {i|time} )

parameters

example

Use the first and last timeserie times to compute the day trading duration. (tget ts) is equivalent to (tget ts 0) and (tget ts -1) is equivalent to (tget (last ts)).

(def ts (timeserie @"Turnover" "refinitiv" "BNPP.PA" 2020-03-02))
(- (tget ts -1) (tget ts))

time-as-array

Return times of timeserie as an array of datetime.

(time-as-array ts)

parameter

time-as-value

Return timeserie of times instead of values.

(time-as-value ts)

parameter

time-truncate

Return the result of rounding t or each timeserie t down to a multiple of d (since the zero time).

(time-truncate d t|ts)

parameter

timeserie

Create timeserie by hand or retrieve data from a source

(timeserie @field "source" "code"|code-fn|array start {stop} {filter} )
(timeserie date value date value ...)
(timeserie (range ...) date-fn|value)

parameters

examples

bid prices for Bitcoin from Bitstamp source, spikes come from lack of liquidity

(timeserie @bid-price "bitstamp" "BTC" 2018-02-14)

Hard coded time serie

(timeserie 2017-10-26T09:19 48.6 2017-10-26T10:30 49 2017-10-26T11:51 49.27)

a simple timeserie of day of the month

(timeserie (range 2019-01-01 2019-05-28 1D) day)

uniq

Filters out repeated values, each value appears only once in result. If argument is a timeserie, it returns a timeserie with the first occurrence of each value. If argument is an array, it returns an array with each value present only once. For all other argument type uniq is equivalent to identity.

MEMORY WARNING uniq memory usage can be huge if there is a lot of different values.

(uniq arg)

parameter

example

Show first BTC trade price occurrence. Here price is rounded to closer multiple of 10 to have less different values.

(def prices
  (timeserie @trade-price "bitstamp" "BTC" 2018-02-28))
[
  (round prices 1)
  (label "uniq" (uniq (round prices 1)))
]

value-as-array

Return values of timeserie as an array of floats.

(value-as-array ts)

parameter

version

Return LispTick version as a string.

(version)

vget

Get value at a precise index in a timeserie, works like tget. If index is a time return value at time if an update exists or just before.

Result is “()” so isnull? true, when index unfound in serie.

(vget ts {i|time} )

parameters

example

Use first timeserie value to compute the serie return

(def ts (timeserie @trade-price "refinitiv" "BNPP.PA" 2020-03-02T09:10 2020-03-02T17:30))
(/ ts (vget ts))

.