The RealTest backtest engine is written to support multi-strategy portfolio-level backtesting as effectively and efficiently as possible.
In most other software, portfolio-based backtesting is implemented by first generating all the entry and exit signals for each symbol for the entire date range, and then running a second pass that models the portfolio for each date using the generated signals.
In contrast to this, RealTest loops through the data by date first, then loops through each strategy, then finally each symbol for that strategy.
This turns out to be no less efficient and much more effective, as it gives strategies full access to their own stats while they are running.
It also more closely models what people do when actually trading, and is therefore more realistic.
Here is a simplified outline of what goes on "under the hood" when you run a test:
1.Apply the TestSettings (if present) to the Settings Panel (where they will persist until next changed)
2.Load the specified data file if not already in memory
3.Get the current parameter values (defaults for a single test or next optimization values for multiple tests)
4.Adjust the test date range to match the data date range and/or next optimization interval
5.Recalculate Data Section items (arrays) as needed
6.Loop through the dates in the date range for this test and do the following:
a.initialize daily stats
b.do at-open exits then entries for each strategy (exits first to free capital for positions being entered)
c.do intraday entries then exits for each strategy (entries first to avoid assuming capital can be freed when intraday fill sequence is unknown)
d.do at-close exits then entries for each strategy (exits first to free capital for positions being entered)
e.calculate daily stats for each strategy
f.recalculate allocation for each strategy
When entries are done for each strategy (steps b-d of item 6 above), this involves:
1.Loop through all stocks and evaluate EntrySetup, making a list of stocks where it returns TRUE
2.Sort this list of entry setups by SetupScore and prune the list at MaxSetups (if specified)
3.Sort the pruned setup list by EntryScore and loop through the sorted list, adding new positions when ALL of the following are true:
a.the time of day matches the strategy EntryTime
b.EntryLimit and/or EntryStop price have been touched
c.not already a position in this stock for this strategy (unless Pyramiding is TRUE)
d.MaxPositions or MaxInvested has not been reached
e.EntrySkip does not evaluate to TRUE
When exits are done for each strategy (steps b-d of item 6 above), this involves:
1.Loop through all strategy positions and exit each if any of the following are true:
a.ExitRule evaluates to TRUE and ExitTime matches time of day (exit at open or close)
b.ExitLimit or ExitStop price was touched without ambiguity (exit at that price)
c.ExitLimit and/or ExitStop price were touched with ambiguity and Ambiguity setting permits exit anyway
d.this is the last bar of data or last date of the test (exit at close)
Note that, in all cases, strategies are processed in the order in which they appear in your script.