Series Conditions

Besides

once
and
rest
statements, we can also use
if
statements when defining a series.

Let's look at a "counter" series which counts from 1 to 5. After it reaches 5, it starts counting from 1 again, ad infinitum.

1234
series counter: once: 1
if counter == 5: 1 rest: counter + 1

Here's how this plays out:

Bar 1:

  • Series has not been initialized - the
    once
    statement is executed.
  • After executing the
    once
    statement, the value of
    counter
    is now
    1

Bar 2:

  • Series is already initialized. The once statement is skipped.
  • The
    if
    condition is evaluated.
    1
    is not equal to
    5
    . The body of the
    if
    statement is not executed.
  • The
    rest
    statement is executed.
  • After executing the
    rest
    statement, the value of
    counter
    is now
    2

Bar 3:

  • Series is already initialized. The once statement is skipped.
  • The
    if
    condition is evaluated.
    2
    is not equal to
    5
    . The body of the
    if
    statement is not executed.
  • The
    rest
    statement is executed.
  • After executing the
    rest
    statement, the value of
    counter
    is now
    3

Bar 4:

  • Series is already initialized. The once statement is skipped.
  • The
    if
    condition is evaluated.
    3
    is not equal to
    5
    . The body of the
    if
    statement is not executed.
  • The
    rest
    statement is executed.
  • After executing the
    rest
    statement, the value of
    counter
    is now
    4

Bar 5:

  • Series is already initialized. The once statement is skipped.
  • The
    if
    condition is evaluated.
    4
    is not equal to
    5
    . The body of the
    if
    statement is not executed.
  • The
    rest
    statement is executed.
  • After executing the
    rest
    statement, the value of
    counter
    is now
    5

Bar 6:

  • Series is already initialized. The once statement is skipped.
  • The
    if
    condition is evaluated.
    5
    is equal to
    5
    . The body of the
    if
    statement is executed.
  • After executing the body of the
    if
    statement, the value of
    counter
    is now
    1

Bar 7:

  • Series is already initialized. The once statement is skipped.
  • The
    if
    condition is evaluated.
    1
    is not equal to
    5
    . The body of the
    if
    statement is not executed.
  • The
    rest
    statement is executed.
  • After executing the
    rest
    statement, the value of
    counter
    is now
    2

... and so on.

We can have as many conditions as we want when defining a series. Conditions are evaluated in top-to-bottom order and the first one that resolves to

true
will have its body executed and will determine the value of the series for the current bar.

If none if the conditions resolved to a

true
value, the
rest
statement is executed.

However, the

once
and
rest
statements are not mandatory. QuantScript supports series that are defined exclusively by conditions. In this case, if none of the conditions match, the series will not have a value for this bar.

Let's look at an example.

We'll implement a series which holds all values of

open
for which
open > open[1]
was true:

12
series filteredOpen:
if open > open[1]: open

Now,

filteredOpen
holds the last value of
open
, for which
open > open[1]
was
true
,
filteredOpen[1]
holds the second to last value, etc. The bars for which the condition was not true are not part of the series.