#twitter-follow { border: 0;position: fixed; top: 240px; left:0;} #live-support { border: 0;position: fixed; top: 370px; left:0;} #knowledge-base { border: 0;position: fixed; top: 170px; left:0;}

## Strategy Optimization on Random in-sample and out-of-sample Periods

by QuantShare, 672 days ago

The "RandomMonths" function picks months depending on a percentage value passed to this function. If you specify 100% then the function returns TRUE for all months, if you specify 50% then the function returns TRUE for half of the months and if you specify 0% then the function returns FALSE for all months.

The idea was brought by "mdmd" and it consists of creating random in-sample and out-of-sample periods for trading system or rules optimizations.

Here is a basic trading system formula:
Optimize("a", 30, 90, 10);
Optimize("b", 30, 90, 10);
buy = rsi(14) > a and close > 10 and close*sma(volume,5)>100000;
sell = rsi(14) < b;

Buy a stock if RSI is higher than "a" and sell it if RSI is lower than "b".
We could backtest/optimize this strategy for a specific period (let us say 2000-2006 <- This is our in-sample period). Select the best performing strategies and then perform the backtest on an out-of-sample period (for example, 2006-2011).

Or we could use the "RandomMonths" function to generate random in-sample and out-of-sample periods.

To use the technique, we should add the following lines just below the "sell" rule of the previous trading system:
pass = RandomMonths(70, 15245);
sell = sell or !pass;

The second parameter of the "RandomMonths" is called random seed and it is a number that initialize a pseudorandom number generator. In order to get different months on each execution, you should change the seed value. A unique in-sample period corresponds to each seed number.

In the above example, the in-sample period contains 70% of the months as specified by the first parameter of the "RandomMonths" function.
We instruct the trading system to buy only if "pass" is true (We are in the in-sample period) and to sell all positions if the "pass" variable becomes false (We are out of the in-sample period). Note that "pass" condition of the sell rule can be removed if you want to keep trades open and use only regular exit rules.

It is possible to optimize the strategy using different in-sample periods by creating an optimizable variable and then adding the variable value to the seed number.

To backtest the strategy during the out-of-sample period:
- Use the same seed number
- Add "!" operator (logical negation) just before the "RandomMonths" function

What is this?

 Type: Trading Indicator Object ID: 1011 Country: All Market: All Style: Technical Analysis

 Related objects Empty
Number of reviews
Average rate
Click to rate this item
Number of rates the current object received
Report an object
if you can't run it for example or if it contains errors
Click to report this object

 Random Blog Posts Compare stocks and securities by creating a relative performance chart Buy the best/top rated stocks or how to create powerful rank based trading systems Ranking stocks based on their correlation with the S&P 500 Index Creating Stock & Market Short Interest Ratios using Historical Short Sale Data Select the best ETFs combination to maximize your return and reduce your investment risk How to turn any ordinary trading strategy into a very profitable one Charting & Scripts - Manage stock charts using the global script Trading System: Buy Stocks based on their Sharpe Ratio Rank Show All

Number of reviews
Average rate
Click to rate this item