LispTick Wiki

Some Time Series LispTick examples

Returns

Compare two timeserie returns during the day.

Create a function ts asking for trade price for a stock for a day.

Create another function tsreturn computing return by dividing by open value and substarcting 1.

(defn ts[code date] 
  (timeserie @trade-price "trth" code date)
)
(defn tsreturn[code date]  
  (- (/ (ts code date) (vget (ts code date) 0 )) 1)
)
[
(tsreturn "BNPP.PA" 2016-02-01)
(tsreturn "SOGN.PA" 2016-02-01)
]

graph See in playground

VWAP

Compute Volum Weighted Average Price since beginning of the day by creating vwap function. Graph versus trade prices.

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

(defn vwap [source code date] 
  (/
    (sigma 
      (*
        (timeserie @trade-price source code date)
        (timeserie @trade-volume source code date)
      )
    )
    (sigma 
      (timeserie @trade-volume source code date)
    )
  )
)
[
(graphsample 1920 (timeserie @trade-price "bitstamp" "BTC" 2018-02-17))
(graphsample 1920 (vwap "bitstamp" "BTC" 2018-02-17))
]

graph See in playground

TWAP

Compute Time-Weighted Average Price since 1st trade day by creating twap function. Graph versus trade prices.

1st trade as no impact on twap as its time, so its weight, is 0.

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

(defn twap [source code date]
  (def times
    (delta
      (time-as-value
        (timeserie @trade-price source code date)
      )
    )
  )
  (/
    (sigma 
      (*
        (backward (timeserie @trade-price source code date))
        times
      )
    )
    (keep 
      (sigma times) not= 0
    )
  )
)
[
(graphsample 1920 (timeserie @trade-price "bitstamp" "BTC" 2018-02-17))
(graphsample 1920 (twap "bitstamp" "BTC" 2018-02-17))
]

graph See in playground

Hayashi Yoshida

Compute Hayashi Yoshida correlation estimator incrementally for each point of timeserie.

Here is for example how Hayashi Yoshida could be implemented directly in LispTick. To have a nicer input we create a midserie function computing mid from bid and ask, keeping values in right time range.

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

(def start T09:10) (def stop T17:30)
(defn midserie [code date]
  (prune
    (* 0.5
      (+
         (restrict (timeserie @bid-price "trth" code date) start stop)
         (restrict (timeserie @ask-price "trth" code date) start stop)
      )
    )
  )
)
(defn hayashi-yoshida [x y]
  (/
   (sigma
    (*
     (delta x)
     (delta y)))
   (sqrt
    (*
     (sigma (* (delta x) (delta x)))
     (sigma (* (delta y) (delta y)))
    )
   )
  )
)
(graphsample 1920
  (hayashi-yoshida (midserie "BNPP.PA" 2016-02-01) (midserie "SOGN.PA" 2016-02-01))
)

graph See in playground