Unknown Values

QuantScript doesn't know everything

Sometimes, when you ask QuantScript to compute something, it just... doesn't know what the result is.

When this happens, you get an

unknown
value.

One scenario where QuantScript returns an

unknown
is if you try to perform a mathematical operation which is undefined, such as dividing by zero.

Another, is when using the

[]
time travel operator to go back more than 499 bars. Due to performance reasons, QuantScript has an imposed limit and cannot resolve a value that's more than 499 bars in the past - so it just returns an
unknown
value.

This explains why our Debugging strategy failed to open any positions. Its code was:

1 enter long when close > close[500]
2 exit long when close < close[500]

We know now that

close[500]
resolves to
unknown
. But what happens when we compare
close
to
close[500]
?

Let's say

close
is equal to 1.1234. Does
1.1234 > unknown
equal
true
or
false
?

The answer is - neither. When you ask QuantScript to compare a number to an

unknown
, it doesn't know how to compare them, so it returns
unknown
again.

As a matter of fact, any operator which receives an

unknown
as one of its values(operands), returns an
unknown
value as its result. There is only one exception to this rule, but more on that in a bit.

Let's look at some expressions which result in an

unknown
value:

1 function GetAnswerToLife(): 42
2
3 close[500] # cannot go back more than 500 bars in the past
4 5 + unknown # don't know how to add an unknown to a number
5 5 + true # don't know how to add 'true' to a number
6 5 + GetAnswerToLife # don't know how to add a function to a number
7 true and unknown # don't know whether this is true or false
8 5 / 0 # don't know how to divide by zero

Checking for unknown values

The following expression returns

unknown
, too:

1 unknown == unknown

How are we supposed to check if something is

unknown
, then?

Easy - we have a special operator for this and that operator is the one exception to the rule mentioned earlier - it's called

isknown
.

You use the operator

isknown
to check if a value is known. Makes sense, right?

To do that, you simply write

isknown
, followed by some value. If the value is known, the result is
true
, otherwise the result is
false
.

Let's look at some examples:

1 isknown 42 # -> true
2
3 isknown unknown # -> false (unknown is not known)
4
5 isknown (close / 0) # -> false (division by zero)

This pretty much wraps up

unknown
values.

Next, we will see why time travelling works just fine, even on the very first bar of a strategy's backtest.