Portfolio Design

Advanced concepts in stock investment portfolio design.  Fundamentals, technical analysis and many other related topics are discussed.

How to Perturb Your Simulation With the Random Function

The problem with stock trading systems is that they are usually based on an ideal, a single simulation that has been either consciously or subconsciously optimized. System perturbation to the rescue. It won't solve all optimization issues but it can generate more realistic profit/loss statistics than you be might able to obtain by other means. 

The Random function can be used to inject either noise or randomness in a ranking system node or Buy/Sell rule.  The randomness will cause differing results every time the simulation is run. After multiple runs, the results can be averaged, providing figures that are less optimized, less ideal, and more realistic than the "one-off" simulation.

Stock market analysis tools provided by Portfolio123.


The Random function could be used in a ranking system node to inject white noise, or in a Buy/Sell rule for margin testing. In this tutorial, the Random function will be added to a Buy rule in order to randomly reject some of the top-ranked stocks that would otherwise be purchased. The Optimizer will be used to generate multiple simulations, and for summarizing the statistics for off-line processing.

This tutorial uses a public SimulationRanking System, and Optimization Study. These should be copied to your Portfolio123 account before starting this tutorial.

The simulation is very simple. There is one Buy rule and one Sell rule. The simulation holds approximately ten stocks, and rebalances weekly. The simulation uses a ranking system that ranks each stock based on Short Interest Ratio (SiRatio). The ten top-ranked stocks are selected for purchase and held for one week. They are sold and the process is repeated.

An overview of the trading system is shown below.

Overview of the trading system used in this tutorial

This tutorial starts by running the simulation (after it has been copied to your account) . Click Re-Run as shown below.

Screenshot showing how to run the simulation backtest setup

You will see the GENERAL setup tab. Migrate over to the BUY tab and disable the Buy rule by clicking on the small button to the left of "Buy1". The color of the button will turn from green to red, indicating that the Buy rule is disabled. Then click on RUN SIMULATION. 

Screenshot showing how to disable the Buy rule

After a short period of time, the simulation backtest results will be displayed and should look similar to the screenshot below. 

Results of backtest

Sometimes the date range isn't properly set up. If the date range is incorrect then:

  • Click on Re-run
  • Go to PERIOD & RESTRICTIONS and set the time period from 2000 to present, or from the earliest date that your membership level will allow.
  • Then click on RUN SIMULATION.

Once the results look similar to those shown above, then make note of the Annualized Return. In the example provided, the Annualized Return is 15.30%, and Max Drawdown is -68.90%. These are the ideal statistics, based on a single run of the simulator, which chooses the top ten stocks (only) on a weekly basis. But what about the eleventh-ranked stock, twelve, and so forth?  They should give good results too, right? Well, we aren't really sure. They could be terrible picks, meaning the system is based more on luck than good design. But we don't know, at least not at this point in time.

To find out, the simulation is going to be "perturbed", with the addition of a randomness factor that will cause stocks further down the rankings to be picked on occasion.

To kick this exercise off, start by re-running the simulation. Go to the Buy tab and click on the small button to the left of "Buy1".  The button will turn from red to green, indicating that the Buy rule is turned ON.

The Buy rule is Random < 0.5. Random is a function that generates a pseudo-random number between 0 and 1. The Buy rule suggests that 50% of the time, top-ranked stocks will be discarded at random, causing lower-ranked stocks to be chosen instead. Keep in mind that the picks are still highly ranked stocks. This logic applies every rebalance period (weekly) so it is anticipated that each backtest will produce a different set of results. Each simulation backtest will buy highly ranked stocks, but not THE highest 10 ranked stocks.

Once the Buy rule has been enabled then click on RE-RUN SIMULATION as shown below.

Screenshot depicting how to turn on the Buy rule and run the backtest

The backtest results will be different than before.  And the user will get different results than what is shown below, simply because the simulation has randomness embedded in it.

Backtest results with randomness added.

Now the Optimizer will be used to generate multiple backtests. The Optimizer is used more of a convenience than anything. The first step is to select "Copy To the Optimizer" from the pull-down "Simulated Portfolio" menu.

Screenshot of how to copy the simulation to the Optimizer

Save the Optimizer study as shown below.

Screenshot of how to save the Optimizer study

Now the simulation permutations are going to be created. Click as shown below.

Screenshot showing how to start the process of creating simulation permutations for the Buy rule

Click on [copy] nineteen times, causing Random <= 0.5 to be copied nineteen times in the box below. Then click on UPDATE when you are finished.

The same Buy rule Random <= 0.5 is repeated nineteen times in addition to the original.

The optimizer overview screen should look like the screenshot below, with the Buy Rule Random <= 0.5 listed a total of twenty times.

The Optimizer screen shows the twenty permutations

Now click on Generate Permutations from the Study pull-down menu.

Generate the Optimizer permutations by clicking on Generate Permutations from the Study pull-down menu

The simulation runs are listed. The Optimizer is now primed and ready to go.

Screenshot of the Optimizer "ready to run"

Click on Run from the Study pull-down menu.

Screenshot shows how to run the backtest permutations by clicking Run from the Study pull-down menu

The status is displayed while the Optimizer is running.

Screenshot showing Optimizer in action. The status is displayed in the top-left corner.

When the simulation backtests are complete then download the results to EXCEL. This is done by clicking on Download from the Study pull-down menu.

Download the completed optimizer results into EXCEL.

The rest of this exercise are done off-line using EXCEL. Start by calculating the average Maximum Drawdown from the 20 backtests as shown below.

Screenshot showing how to determine the average Max Drawdown using EXCEL

Copy the formula for Max Drawdown Average over to the column for Annualized profit.

Screenshot showing how to calculate the average Annualized Return using EXCEL.

Based on system perturbation, the average annualized return is 11.3% and maximum drawdown is -67.0%. This compares to the 15.3% annualized return and -68.9% maximum drawdown determined from a single run. It appears that the single simulation backtest scores about 4% more annualized return than an average of multiple backtests with minor variations in the holdings.

Note: Although this type of test can shed light on the design, it does not prove that the design is good or fail-safe. The technique can finger a bad design however.