Debugging

Let's take a step back and create a simple strategy, which opens a long position whenever the

close
price is higher than it was 100 bars ago, and closes that position whenever the
close
price is lower than it was 100 bars ago.

Here's what it looks like:

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

We type in this code in our QuantScript editor, click on Calculate Backtest and.. the strategy works, although it's producing terrible returns.

Perhaps if we looked a bit further into the past - let's say, 500 bars?

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

We click on Calculate Backtest again, however... this time there are no positions opened!

Let's try to debug our code and see what went wrong. To do this, we click on the Debug button above our editor.

When you calculate a QuantScript strategy's backtest, QuantScript goes ahead and executes your code many times, once for each bar (as covered in Execution Model). You then get your backtest and that's it.

When debugging, though, QuantScript executes your code once, for the first bar, and pauses, showing you the results of the script for that specific bar - this includes what signals were produced, the date and time of the bar, the bar's open, high, low, close and volume values, and, last but not least, the value of every variable you've declared. You can then click the Next button to continue execution and see the results of any other bar you're interested in.

Let's try to find out why our strategy cannot open a position, even though logic states that it should, and quite often, too.

If we click on Debug, we'll only see what we already know. The column on the right shows us that there are No signals for the first bar. If we use the Next button to continue executing, we'll see that there are No signals for the rest of the bars as well.

We already know what the

close
price is for every bar, as it is shown in the right column by default.

Let's separate

close[500]
in a variable and see what its value actually is. Our strategy now looks like this:

1 close500BarsAgo = close[500]
2
3 enter long when close > close500BarsAgo
4 exit long when close < close500BarsAgo

Now, when we debug the code, we can see that the variable

close500BarsAgo
is equal to
unknown
.

Next, we'll explain what

unknown
values are.