Let's take a step back and create a simple strategy, which opens a long position whenever theprice is higher than it was 100 bars ago, and closes that position whenever the price is lower than it was 100 bars ago.
Here's what it looks like:
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?
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 theprice is for every bar, as it is shown in the right column by default.
Let's separatein a variable and see what its value actually is. Our strategy now looks like this:
Now, when we debug the code, we can see that the variableis equal to .
Next, we'll explain whatvalues are.