Skip to content

pallocchi/vista

Repository files navigation

Vista, technical analysis

!> Vista is currently under heavy development 🛠

Vista is a technical analysis library for Kotlin, inspired in Pine Script from TradingView.

val data = dataOf("https://bulltimate.github.io/vista/amzn.csv")

val fast = data.sma(9)
val slow = data.sma(30)

when {
    fast crossOver slow -> print("I'm going long!")
    fast crossUnder slow -> print("I'm going short!")
}

Loading the market data

All starts with... data! before using any indicator, you have to load the market data.

val data = Data()

data.add(
  Data.Bar(
    date = "05/29/2020",
    open = 2361.01,
    high = 2391.37,
    low = 2353.21,
    close = 2388.85,
    volume = 3648128.0
  )
)

You can also load the data directly from a CSV file using the dataOf() function.

val data = dataOf("https://bulltimate.github.io/vista/amzn.csv")

Alright! you have the market data, now let's see what we can do with that in the next section.

Working with series

Almost anything in Vista is a series, that's why it's the most important thing you need to learn. Series are structures that allow us to use the data in a more fashion way.

Let's start with a simple series of numbers:

val series = seriesOf(1,2,3)

The seriesOf function is a simple way to create a series and it's enough for this example.

Accesing values

So, we have our series, let's try to get its values:

val lastValue = series[0]       // last value is 3
val previousValue = series[1]   // previous value is 2
val oldestValue = series[2]     // oldest value is 1

Since Vista is inspired in Pine Script, the way to get values from a series is pretty similar, using the array accesory [] and passing as an argument the number of periods to look back. Fortunately, since the last and previous values are frequently used, we provide a shortcut to get these values: last and prev fields.

val lastValue = series.last       // same as series[0]
val previousValue = series.prev   // same as series[1]

Operators and series

Series can be operated just like numbers:

val x = seriesOf(1,2,3)
val y = seriesOf(2,4,6)

val sum = y + x // sum is (3,6,9)
val dif = y - x // dif is (1,2,3)
val mul = y * x // mul is (2,8,18)
val div = y / x // div is (2,2,2)

and it also works for numbers:

val x = seriesOf(1,2,3)

val sum = x + 1 // sum is (2,3,4)

Working with previous periods

As you already know, using the [] accesory in a series you can get values from past periods, for instance, with x[1] you get the previous value of the x series. But now let's suppose we want to create a series which is the change between the value at a given period, and the previous one:

$change(x) = x - x_1$

Note in this guide we use $x_1$ to represent the previous value

We need to create another series which is the original one ($x$) but delayed by 1 period.

Shifting series

Using the invoke operator like (n) we can delay a series by n periods.

val x = seriesOf(1,2,3) // x is (1,2,3)
val y = x(1)            // y is (1,2)

print("The last value in x is ${x.last}")
print("The last value in y is ${y.last}")
The last value in x is 3
The last value in y is 2

So back to our example, we can implement the change() function in this way:

val x = seriesOf(1,3,6)

val change = x - x(1) // change is (2,3)

Since x(1) returns a series, we are getting the difference between both series: the original and the shifted one.

Data series

We saw the seriesOf function which is really useful to learn Vista, but it's more likely you use what we call the data series, which are basically views of the data you already know. These are the open, high, low, close and volume series.

data.open     // series of close prices
data.high     // series of high prices
data.low      // series of low prices
data.close    // series of close prices
data.volume   // series of volume prices

Based on the previous series, we can derive another one called the typical price.

$typical = \frac{\small close + high + low}{3}$

Since this series is widely used, we have a data series for that.

data.typical  // series of typical prices

Introduction to indicators

The core of the technical analysis are indicators. Vista has many common indicators out of the box that you can find in next sections of this guide, and many others are comming in next releases. But in any case you can always create your custom indicators in a really simple way, using the functions already supported by Vista.

For instance, let's see how we can implement the classic MACD indicator, which is calculated by subtracting the 26-period exponential moving average (EMA) from the 12-period EMA. This is the Vista-way to create it:

fun macd(source: Series) = ema(source, 26) - ema(source, 12)

Simple, right? now you can use your new indicator just like macd(close). Of course you don't actually need to implement this indicator since Vista has a built-in function for that, but it's a great example of how you can create new indicators based on the already implemeted ones, thanks to the fluidity of working with series.

?> Vista only performs the calculation of the values when they are actually needed.

Check the Built-In indicators list for more info.

About rules and strategies

Rules are the way to detect entry and exit signals. Let's suppose we want to implement a classic strategy of moving average crossovers. This strategy uses two simple moving averages (SMA) with different periods (slow and fast), and we want to go long whenever the 9-period SMA crosses over the 30-period SMA, and go short when the frist one crosses under the other. In order to do that, we have the crossOver and crossUnder rules, which are available in any series and we can use that in this way:

val data = dataOf("https://bulltimate.github.io/vista/amzn.csv")

val fast = data.sma(9)
val slow = data.sma(30)

when {
    fast crossOver slow -> print("I'm going long!")
    fast crossUnder slow -> print("I'm going short!")
}

Amazing! we've just implemented our very basic strategy.

If you are familiar with Pine Script, you can also use the crossover() and crossunder() functions and it works in the same way (actually those functions use the previous ones under the covers). Take a look at the KDoc.

Built-in indicators

These are the indicators available in Vista.

Name Type Leading Lagging Since
Accumulation/Distribution (ADL) Volume - 0.0.1
Average True Range (ATR) Volatility - 0.0.1
Awesome Oscillator (AO) Momentum - 0.1.0
Bollinger Bands® Volatility - 0.0.1
Chainkin Oscillator Volume - 0.0.1
Commodity Channel Index (CCI) Momentum - 0.0.1
Elder-Ray Index Trend - 0.1.0
Exponential Moving Average (EMA) Trend - 0.0.1
Hull Moving Average (HMA) Trend - 0.0.1
Ichimoku Cloud Combined - 0.1.0
Momentum (MOM) Momentum - 0.1.0
Moving Average Convergence/Divergence (MACD) Trend - 0.0.1
On-Balance Volume (OBV) Volume - 0.0.1
Relative Strength Index (RSI) Momentum - 0.0.1
Simple Moving Average (SMA) Trend - 0.0.1
Standard Deviation Volatility - 0.0.1
Stochastic Oscillator Momentum - 0.0.1
Stochastic RSI Momentum - 0.1.0
Ultimate Oscillator Momentum - 0.1.0
Volume Rate of Change (VROC) Volume - 0.0.1
Volume Weighted Moving Average (VWMA) Trend - 0.1.0
Weighted Moving Average (WMA) Trend - 0.0.1
Williams %R Momentum - 0.1.0

About

A modern technical analysis library (docs)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages