The AI optimizer tool of QuantShare is capable of building a trading system based on a list of buy, sell, short and cover rules. It will optimize your trading system based on the defined fitness function by switching on and off the trading rules you provide. The fitness function is user-defined and it is a measure the AI optimizer uses to determine which the best trading system is. You can for example choose Annual Return, Sharpe Ratio or any custom trading measure to be your fitness function. Here is how to AI optimize works: - Select "AI" then "Optimize" - In the optimizer control, click on "Create" button - Select one of the available artificial intelligence algorithms (Genetic Algorithm or Population-Based Incremental Learning) - Under "What do you want to optimize?", select "Trading System" - Click on "Next" twice - Next to "How to optimize your trading system", choose "Using buy & sell list of rules" - Define the number of buy, sell, short and cover rules - Finally, specify a list of rules for each gene by clicking on the "Select Rules" button Click here for more information about how to create trading rules. Today, I wanted to show you another way to do this using the simulator tool only. Unlike the AI optimizer, which uses artificial intelligence algorithms to try to come up with the best combination of rules, this method performs exhaustive optimization and thus backtest every combination of your trading rules. But, let us start first by creating a simple trading system to demonstrate this technique. Trading System The trading system that we will use in this post contains the following rules: - Price between $5 and $50 - relative strength index (RSI) higher than 70 - Price above its 25-Bar simple moving average - Price is at a one month high - Price return for the past 10 bars is positive The complete QuantShare formula is: rule1 = close >= 5 and close <= 50; rule2 = rsi(2) > 70; rule3 = close > sma(25); rule4 = (close == hhv(close, 25)); rule5 = roc(10) > 0; buy = rule1 and rule2 and rule3 and rule4 and rule5; // Sell after 10 bars SetSimStop(_StopNBar, _Point, 10, 0); Click here to learn how to create a trading system. How to Switch On/Off Your stock trading Rules To demonstrate how to switch on and off a trading rule, let us take the second rule for example. This rule tells QuantShare to enter long if RSI is higher than 70. In order to switch off this rule, we must make sure that it always return "1" or TRUE. Why not "0" or FALSE? Because, in that case, turning off a rule will turn off all the others. Take a look at the "buy" variable; if one rule is equal to 0 then the buy variable will be automatically equal to 0. Switching off a rule can be accomplished by adding the following instruction to our rule: rule2 = rsi(2) > 70 OR 1; // No matter the result of the RSI comparison, "rule2" variable will always be equal to 1 Example: On day 1: RSI rule returns TRUE then rule2 = TRUE OR TRUE = TRUE On day 2: RSI rule returns FALSE then rule2 = FALSE OR TRUE = TRUE To switch on the same rule, we just have to do the following: rule2 = rsi(2) > 70 OR 0; Example: On day 1: RSI rule returns TRUE then rule2 = TRUE OR FALSE = TRUE On day 2: RSI rule returns FALSE then rule2 = FALSE OR FALSE = FALSE Now, the most interesting part is how to automate the switch on/off process of our stock trading rules. It is easy for those familiar with optimizations and the "Optimize" function. However, those who are not, let me introduce you the "Optimize" function. This function allows you to create several variations of a variable. If for example you create a simple trading system that compares the stock's price with a threshold value and you want to test different threshold values then the "Optimize" will come to the rescue. Here is how: Optimize("a", 10, 30, 10); // Starts at 10, increments by 10 until it reaches 30 buy = close > a; When optimized, this trading system will be backtest three times (a=10, a=20 and a=30). Combination of Stock Trading Rules Switching a rule on/off is the first part of this strategy. The second part consists of automating this process. For this, we will use the "Optimize" function. If you still don't know what this function does, please read the previous paragraph again. Here is the complete formula with the different optimizable variables: rule1 = close >= 5 and close <= 50; rule2 = rsi(2) > 70; rule3 = close > sma(25); rule4 = (close == hhv(close, 25)); rule5 = roc(10) > 0; Optimize("a1", 0, 1, 1); Optimize("a2", 0, 1, 1); Optimize("a3", 0, 1, 1); Optimize("a4", 0, 1, 1); Optimize("a5", 0, 1, 1); rule1 = rule1 or a1; rule2 = rule2 or a2; rule3 = rule3 or a3; rule4 = rule4 or a4; rule5 = rule5 or a5; buy = rule1 and rule2 and rule3 and rule4 and rule5; // Sell after 10 bars SetSimStop(_StopNBar, _Point, 10, 0); Add this formula to your trading system, save it then click on "Optimize" button in the simulator manager. Here is the report after backtesting this trading system: In the second line, "a1" is equal to "1", which means that the first rule is switched off. All other variables have a value of "0", which means the other rules are switched on. In case all rules are switched off, the trading system will enter any stock and at any bar (buy rule will always be equal to 1).
|