In this guide, I show you exactly how to create a trading system template in 10 minutes and let that template generates profitable trading systems for you automatically. The following instructions will guide through the process of finding trading rules, creating them, setting up a trading system, defining your objectives, optimizing/backtesting your strategy. The trading system template we are going to use in the post is a combination of a stock picking and stock rotational system. I am referring to stocks here by you can use ETFs or any other securities instead. Find Trading Rules Ideas Since the trading system template we are going to use is a mix between a stock picking and a stock rational system, we will need to gather here a list of trading rules (X > Y…) and a list of measures (RSI, ROC…). This part is very important, so we have to choose our rules carefully. We can of course select a set of rules, perform the process then repeat it again with another list. The number of rules/measures is unlimited so it may be a good idea to test these rules and only select the best ones. This can be done using the rules analyzer tool of QuantShare. Let us build our list of rules now. Just concentrate, think about all the trading systems you know or built and just write some rules. An example of list would be: rsi(14) > 70 roc(30) > 5 close > open high > high[1] // High higher than previous bar high For measures (rotation), we can have: rsi(14) roc(30) close / open high / low Of course you can have also fundamental analysis rules or any other kind of rules (such as rules using put/call volume data, sentiment analysis rules, news rules…) If you need inspiration, you can check and download the different list of rules shared by QuantShare members on the sharing server. Implement Your Trading Rules Let us build a list of rules item from the rules you have gathered. - Select "Analysis -> Rules Manager" - Click on "Create" then type the list name Remember that we have to create two lists. Once for stock picking and one for stock rotation. One quick way to add several rules to a list would be to select your list, click on "Tools -> Create Rules from Text", type your rules then click on "OK". You can also copy rules from one list to another by selecting one or several rules (Keep CONTROL pressed), dragging/dropping rules into the clipboard (bottom/left corner of the rules manager form) then clicking on "Copy" (in same clipboard control) and dragging/dropping into the new list of rules. The "Rules Manager" tool can also help you create several variables from a single rule/measure, for example by varying a specific parameter or threshold. Here is how it works: - Add a rule. Example: rsi(14) > 0 - Replace "14" by "a" or any keyword to make the "Optimization Grid" appears at the bottom - Update the "min", "max" and "step" fields to create several variations of the same rule. You may want to check the following blog post for more details: 10 masks to create thousands of rules to use into your trading system I have already prepared two list of rules. You can download them here: Stock Picking Trading Rules Stock/ETF Rotational System Metrics Create the Trading System Template The trading system logic is very simple: - Consider only stocks that meet the stock picking criterion - Rank all stocks based on two metrics - Buy the top 5 stocks (stock rotational) - Rebalance monthly You can download the template (Ultimate Trading System). But if you want to learn how it was created and maybe tweak it later, just keep on reading. If you are new to QuantShare, you may want to check this blog post to learn how to create trading systems. Now, let us see how we can implement this strategy in QuantShare. - Consider only stocks that meet the stock picking criterion ct = ApplyRule("", "stock_picking", -1); // Return number of rules Optimize("a", 0, ct - 1, 1); // Optimize variable "a" from 0 to Number of rules -1 rule1 = ApplyRule("", "stock_picking", a); // Execute the rule specified by the index value referenced by variable "a" Notes: Here was assume that the stock picking list of rules we created previously is named "stock_picking". - Rank all stocks based on two metrics ct = ApplyRule("", "stock_rotation", -1); // Return number of rules Optimize("r1", 0, ct - 1, 1); Optimize("r2", 0, ct - 1, 1); rot1 = ApplyRule("", "stock_rotation", r1); // Get the first metric given the optimizable variable r1 rot2 = ApplyRule("", "stock_rotation", r2); // Get the second metric rot1 = comp(rot1, "percentile"); // Rank stocks based on the first metric (result is from 0 to 100) rot2 = comp(rot2, "percentile"); // Rank stocks based on the second metric Optimize("w1", 0, 100, 10); // Optimize the weight to be given to the first metric rot = rot1 * w1 + rot2 * (100 - w1); // Calculate ranking based both metrics - Buy the top 5 stocks (stock rotational) SetSimLongRank(rot); // Instruct QS to first buy stocks with the highest rank newmonth = 1; // Detect a new month buy = rule1 and newmonth; // Consider only stocks that meet the stock picking criterion and buy at the beginning of the month - Rebalance monthly sell = newmonth; // Sell all position at the end of the month (next bar open) Note: You can add any fixed rule just by using the "and" operator in the "buy" variable. Example: buy = buy and close > 2; // Enter long only stocks whose price is above $2 Optimize Your Strategy We have four optimizable variables in our trading system's formula. Suppose, the stock picking list of rules contains 20 rules and the stock rotation list contains 10 rules or metrics. Given that, our system would have exactly 22,000 combinations (20*10*10*11) If you add more trading rules to both lists (say 150 for the first one and 50 for the second one), the number of combinations that could be generated by our system would be: 4,125,000 (More than 4 millions). Let us backtest now those 4 millions systems and for that we will use the AI Optimizer tool of QuantShare. Clearly the optimizer tool will no backtest every single system, but it will use advanced algorithms (Genetic algorithm or PBIL) to find the best systems. - Select "AI -> Optimize" from the menu - In the new Optimizer form, click on "Create" - Select "Population-based incremental learning" as optimization method and below this select "Trading System" - Update PBIL algorithm settings if necessary then click on "Next" ( More info here ) - Next to "How to optimize your trading system", select "Using optimize variables in a formula" - Click on "Load Trading System" then select the trading system we created previously - The total number of variations or combinations should be displayed now - Click on "Next" - Click "Symbols & Dates" - Select the symbols you want to backtest and the start & end dates of the simulation - Click "Ok" then "Next" to create the optimize item - Select that item then click on "Run" to start the optimization process Define the Expected Performance/Risk Every trader is different. Some dislike risk while others are looking for performance no matter the risk. The fitness formula available in the AI Optimizer tool allows you to define your ideal trading system. If you are looking for the best performing systems, just use the default formula: fitness = AnnualReturn; If you want a system with the highest Sharpe Ratio, type: fitness = SharpeRatio; If you want a system that had positive return every year, type: Fitness = AnnualReturn; for(int i=0;i < YearlyReturn.Count;i++) { if(YearlyReturn[i] < 0) { Fitness = 0; } } Note: If you get an error after clicking on "Compile", make sure C# language is used instead of JScript (Bottom/Right corner of the script editor) Is Your Trading System Robust Enough? While the optimization is running, you can click any time on the "Show Report" button of the "Optimize Item" form. This will display the best trading strategies ranked by the fitness value. You will most likely find many profitable systems in the list, but we need to make sure that these systems are robust enough to be considered for live trading. For this, let us do an out-of-sample test. The idea is to backtest the trading system again another period or set of symbols. - Click on "Settings" - Select a different list of symbols (maybe symbols from another market or that belong to a different index) and/or a different start/end period - Click on "Ok" then on "Start" to begin the out-of-performance testing - Two new columns will be added (Out-of-sample fitness value and the difference between the in-sample and out-of-sample results) The trading systems that pass the out-of-sample test can now be considered for paper trading. You should always consider paper trading a system before investing any money in it. In order to generate daily buy/sell signals from our system, you need to add it to a portfolio: - Select "Portfolio -> Portfolio" from the menu - Click on "New" - Select "End-of-day" - Click on "Next" - Click on "Add Trading System" then add your trading system - Click on "Next" twice What To Do Next? Once the template is built it would take you only few clicks to generate thousands of new and different trading strategies. Here is what you can do next: - Run the optimizer again to get different set of trading systems - Play with optimize PBIL settings to generate more systems - Add/Remove trading rules then optimize your template again - Add future optimizations (next paragraph) Add Further Optimizations The trading system we used in this article is simple but very powerful. The fact that the optimization is done to test different rules (rather that parameter like the moving average period) allow us to quickly test different varieties of systems. Depending on the number of rules you have, you can easily come up with many profitable and interesting systems. As we saw earlier, the template can easily generate millions of systems but we can update it to generate several millions of billions of systems. To do this, let us add three further optimizations: 1/ Vary the number of securities to hold at the same time As you saw earlier, the system buys the top 5 securities (Stocks, ETFs, or any other asset type) Of course, the number of securities to purchase can be optimized. To do this, just add the following lines at the top of the trading system formula. Optimize("nbs", 1, 10, 1); SetSimSetting(_NbPositions, nbs); This will multiply the number of potential trading strategies by 10 and would allow us to test different systems that can hold from 1 to 10 securities at the same time. 2/ Specify whether to use the stock picking rule or not We could add here an optimizable variable that either turns on or off the stock picking rule. Remember the stock picking rule's data is saved in the variable "rule1". What we need to add here is simply: Optimize("t1", 0, 1, 1); rule1 = rule1 * t1; // "t1" variable can either get a value of 0 or 1. If it is equal to 1 then the "rule1" remains unchanged. If it is equal to 0 then "rule1" will be equal to 0 and thus will be turned off. Very easy, isn't it? If needed, more info can be found here: How to Switch On/Off Trading Rules in Your Stock Trading System 3/ Adding a third ranking rule In order to add a third ranking rule, we just need to create a new optimizable variable (that will choose the third ranking rule) and define new "weight" variables (one for each ranking measure). Here is what you should add: Optimize("r3", 0, ct - 1, 1); rot3 = ApplyRule("", "stock_rotation", r3); // Get the first metric given the optimizable variable r3 rot3 = comp(rot3, "percentile"); // Rank stocks based on the second metric Now, we need to replace: Optimize("w1", 0, 100, 10); // Optimize the weight to be given to the first metric rot = rot1 * w1 + rot2 * (100 - w1); // Calculate ranking based both metrics By: Optimize("w1", 0, 100, 10); Optimize("w2", 0, 100, 10); Optimize("w3", 0, 100, 10); rot = rot1 * w1 + rot2 * w2 + rot3 * w3; What else could be optimized? - Apply several money management scripts and create a variable to choose only one among the list (More Info here: Disable Money Management Script Programmatically ) - Add stop rules and optimize thresholds - Add a fourth or fifth ranking rule - Add a second stock picking rule Of course there is no limit regarding the number optimizations that can be done to our template and we encourage you to post any new idea in the comments section below. Thank you.
|