A regular Scan as defined in RealTest only has access to bar data and values calculated in the Data Section. The prior topic, Daily Setups Scan, showed how a regular scan might be used to generate a list of trading candidates for a strategy.
For situations that require access to strategy context data, such as which positions are currently open, the TestOutput Scan option provides additional capabilities.
By default, at the end of a test, RealTest "exits" all positions that remain open. These appear in the Trade List with end of test as the Exit Reason.
When a test is run with the TestOutput: Scan enabled and the script includes a TestScan section, this special scan is run automatically before the end of test exits are simulated.
This makes it possible to access all of the same position-context formula elements that are used in your strategy formulas, such as Shares, BarsHeld, FillPrice, etc.
The only slight wrinkle is that, since a scan is a stand-alone script object (not part of any specific strategy), you must always use Extern(@strategy_name, ...) when referring to a position item.
The example script mr_sample_test_scan.rts demonstrates how to use a TestScan to produce a daily order list. If you were using a script like this for live trading, you would simply run it in Import mode and then run it in Test mode once per day.
The TestScan section in mr_sample_test_scan.rts looks like this:
The purple names are data or library items defined earlier in the script. Here is the Library section:
These same items are used in the long and short strategy definitions, which avoids formula repetition:
Running the script with 17-Nov-2020 as the end date and TestOutput: Scan specified in the Settings section produces this output:
As you can see from the TestScan definition above, the names and contents of these columns can be whatever you want to define them as.