The following technique consists of adding market indicators to a trading system to prevent it from entering new positions when the market is bearish or is likely to start a bearish trend. How to create market indicators One way consists of creating these indicators using the composite tool. Each indicator is then associated with a ticker symbol. Another way is to calculate composites on fly using the "comp" function of the QuantShare programming language. The advantage is that we can easily and quickly change and optimize composites in our trading system. For example we can calculate the number of stocks trading above their 10 bar moving average, add it as a rule in our trading system then optimize the period or number of bars used by our moving average. The formula of the above indicator would be: a = comp(close > sma(10), "sum"); More information regarding the "comp" function can be found here: How to create market indicators using the composite function - Part 1, Part 2, Part 3. Here are some examples of market indicators: Number of stocks trading in an overbought RSI area Percentage of stocks making 10 bar new highs Create a list of composites Because we want to try different composites in our trading strategy, we must save composite rules in a list of rules and reference that rule later in our trading system. - Select "Analysis" then "Rules Manager" - Click on "Create" to create a new list of rules - Type the list name (Example: Composites) then click on "OK" - In the right panel, add as many rules as you want - After you add a rule, click on "Add" button to add it to the list Example of rules: - Asset trading in an overbought RSI area rsi(14) > 70 - Asset trading in an oversold RSI area rsi(14) < 30 - Asset making 10 bar new high close == hhv(close, 10) - Asset making 10 bar new low close == llv(close, 10) - Asset increased two days in a row istrue(close > close[1], 2) - Asset increased three days in a row istrue(close > close[1], 3) - Asset trading above its 5-bar moving average close > sma(5) You can even create many variations of any trading rule by optimizing one or several of its variables. Let us take the "Asset making 10 bar new high" rule for example. By tying the following rule "close == hhv(close, a)", a table will appear under "Optimization Grid". Next to the "a" variable, you can define a min, max and increment values. By optimizing the "a" variable, your trading rule will use different high periods. Now that we have prepared our list, we can create an even better list by removing certain rules based on some specific criteria. - Select your list of rules - Click on "Analyze the list" button - Select a list of symbols, period and start & end dates - Select "Outputs" from the left panel - Click on select "Outputs/Exits" - Uncheck existing items - Check for example, "Performance use N-Bar Stop -> Buy then sell after 50 bars" - Click on "OK" then "Continue" to start the backtesting/analyzing process You can remove any rules by checking all rules then un-checking the ones that you want to ignore. After that, click on "Create a new list from the checked rules". Create a trading system If you already have a trading system that you want to improve using market indicators, please continue to the next section. In the other case, follow the next steps to create a basic trading system: - Select "Analysis -> Simulator" - Click on "New" to create a new trading system - Select "Symbols & Dates" tab to choose the symbols to include in your trading system and define the start & end dates. - Click on "Strategy" tab, choose "Create trading system using the formula editor" then type this basic formula: buy = rsi(14) > 70; sell = rsi(14) < 30; The strategy consists of entering long if RSI is above 70 and exiting any position if its RSI drops below 30. The best market indicator for your trading system Update the previous or one of your trading systems then select the formula editor. The idea consists of optimizing this trading system by adding each time a different market indicator rule to exit existing positions (For example when the number of stocks trading below RSI 30 reaches a high). The rule is selected from the list of rules we have created previously. To calculate the result of a trading rule, we can use the "ApplyRule" function. This function gets the list of rules name and an index. The index tells QuantShare which trading rule to use to return the result. The total number of rules (and variations) can be obtained by specifying "-1" in the index parameter. Here are the lines to add after your original strategy formula: ct = ApplyRule("", "Composites", -1); Optimize("a", 0, ct - 1, 1); rule1 = ApplyRule("", "Composites", a); rule1 = comp(rule1, "sum"); rule1 = sma(rule1, 25); rule1 = (rule1 == hhv(rule1, 250)); sell = sell or rule1; buy = buy and !rule1; Explanation: Line 1: Gets the number of trading rules in our list of rules Line 2: Optimizes the variable "a". The value of this variable will vary from zero to (ct - 1) during the optimization Line 3: Calculates the result of the trading rule that is located at index (a) Line 4: Creates a composite. This composite calculates the number of stocks whose rule value is true, for each trading bar Line 5: Returns the simple moving average of the composite Line 6: Returns true if the composite (its moving average) reaches a new year high Line 7: Sells an asset if the above composite value is true Line 8: Buys an asset only if the above composite value is false After you update your formula and save your trading system, click on "Optimize" button to start the optimization process. To get the rule formula from its index, open the rules manager, select your list of rules then select "Tools -> Display all rules".
|