Skip to content

Instantly share code, notes, and snippets.

@C451
Last active October 20, 2020 08:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save C451/cbf161216394515d3d2234fa7d756fba to your computer and use it in GitHub Desktop.
Save C451/cbf161216394515d3d2234fa7d756fba to your computer and use it in GitHub Desktop.
Scripts: Early Adopter Guide

Scripts: Early Adopter Guide

What

TVJS Scripts is a time-series engine, similar to the tradingview's PINE script. Syntax is based on javascript, in the future it will be possible to make custom code constructions with regular expressions (extend the language).

State

At this moment it's an experimental feature, breaking changes are very likely.

Core concepts

Timeseries (TS) is a regular js Array with __id__ and __len__ properties (id & buffer length). New elements are added at the beginning of the TS:

-> [2, 2, 2, 2, 2, 2]

The array is limited by __len__ (applied each step).

Std-lib is a collection of built-in functions:

atr()
bb()
cci()
cmo()
dmi()
ema()
...

Calculation object. Overlay returns the script object from calc():

calc() {
    return {
        props: {
            start: { def: 10, text: 'Start Length' },
            number: { def: 5, text: 'Number of Lines' },
            step: { def: 10, text: 'Length Step' }
        },
        conf: {
            renderer: 'Splines'
        },
        init: `
            consol.log('init script')
        `,
        update: `
            let res = []
            for (var i = 0; i < number; i++) {
                let l = start + i * step
                var.push(ema(close, l)[0])
            }
            return res
        `
    }
}

init() is called once, before any updates update() is executed at each step. Should return a new data point (or set this[0]). Imagine that output of the function is pushed to your overlay's data array.

If renderer is specified, the lib will use another overlay for rendering the script (it should be added to the overlays prop ofc).

If you need to create a persistent variable, just make a new property in this:

{
    init: `
        this.state = 0
    `,
    update: `
        this.state++
    `
}

How To Apply PINE Knowledge

Both engines are very similar, except TVJS has less syntax sugar (this improvement is planned). For example:

// PINE:
hl2 = (high + low) / 2

// TVJS:
let hl2 = ts((high[0] + low[0]) / 2)

Sometimes you need to build a new TS with the ts(x) function. This function takes x and records it every step. Then you can use it as a source in the build-in functions:

let hl2 = ts((high[0] + low[0]) / 2)
return ema(hl2, 100)[0]

What To Avoid

Currently you can get issues with the following code constructions:

// Avoid complex indices (brackets inside brackets):

ts1[[i][0] + arr[n]]

Contribution

If you want to help the development you can try to reimplement your PINE scripts (see Squeeze Momentum as an example). Testing is crucial.

Or you could boost the std-lib (see TODOs):

https://github.com/tvjsx/trading-vue-js/blob/master/src/helpers/script_std.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment