For those who want to master the QuantShare programming language, we will start a series of posts that will show how different rules and actions are implemented in QuantShare trading software. We will begin by creating a random trading system. The system has been created only to show you how to implement trading rules and strategies. It was not tested or backtested and it will certainly yield poor results. Strategy Rules Buy Rules - The technical analysis indicator ADX is above 20 - Close price crosses above 30-bar Exponential moving average - Relative strength index is higher than 70 for at least 5 bars Sell Rules - Stock is down for three days in a row - MACD decreased more than 10% in the last 12 bars Stop - 30% Trailing Stop - 20% Profit Stop How to create a trading system - In the main menu, select "Analysis" then "Simulator" - In the "Simulator" manager, click on "New" You can add trading rules using the wizard. To add a rule simply click on "Add rule" under the appropriate section (Buy, Sell, Short and Cover). To enable "Short" and "Cover" sections, select "Short" or "Long/Short" in "System Type" (at the top). In this example, we will not use the wizard, but instead the formula editor, which is a more flexible, advanced and complex tool. - Select "Create trading system using the formula editor" tab. Use CONTROL+SPACE shortcut to display the list of available functions. To display the list of all the indicators, type "[variable name] = ", then use CONTROL+SPACE shortcut. Example: "a = " ADX above 20 The first buy rule is the simplest one. It consists of comparing the Average Directional Index indicator, whose function name is "Adx", to the threshold "20". rule1 = adx(14) > 20; To get the function name of an indicator: - Click on "Add Indicator" in the formula editor. - In "Add Indicators" tab, type the indicator name next to "Search" field. - Select one indicator in the list below - In the right panel, you can see the indicator name, its description and you may update the value of its parameters - Click on "OK" to add the indicator to your formula Price crosses above EMA To detect when a time-series crosses above another time-series, you may use the "cross" function. The function accepts two time-series and it returns a signal ("1") when the first one crosses above the second one. EMA or exponential moving average can be calculated using the "EMA" function. It has two definitions: One parameter: The lookback period, the calculation will be based on the close price. Two parameters: A time-series and a lookback period Many trading indicators have two or more parameter definitions. When a definition does not allow entering a time-series then most likely the "close" time-series is used. rule2 = cross(close, ema(30)); In case you want to detect the close price crossing below the EMA, simply switch the parameter values. cross(ema(30), close) RSI higher than 70 To return the 14-bar Relative strength index, we use "RSI(14)" function. In this rule, we have to check whether RSI is higher than 70 for at last five bars or not. This means that "RSI(14) > 70" must be true for the current bar plus the previous four bars. One way to do this by checking the latter rule for these five bars. We can get the value of a previous bar using the "ref" function. a = RSI(14) > 70; rule3 = a AND ref(a, 1) AND ref(a, 2) AND ref(a, 3) AND ref(a, 4); AND Operator: Example: rule1 = a AND b; "rule1" becomes true only if both operands (a, b) are true. Note: Instead of using the "ref" function, we can use "[]". -> ref(a, 1) is equivalent to a[1] A more elegant way of translating our RSI rule consists of using the "IsTrue" function: IsTrue Definition: Returns 1 (TRUE) if all values between the current bar and past N bar are superior to 0 (equivalent to TRUE). Our rule syntax becomes: rule3 = istrue(RSI(14) > 70, 5); Asset down three days in a row A security or asset is down when the current bar close price is lower than previous bar close. As you may have guessed, "Three days in a row" could be implemented using the previous "IsTrue" function. down1 = close < close[1]; rule4 = istrue(down1, 3); MACD losses 10% There are two functions you can use to compute the performance or return of a series: ROC and PERF The "Perf" function is a more advanced one because it allows you to enter a non-constant value as a lookback period. Example: a = iff(close > open, 100, 50); // "iff" is a conditional function that returns the value of the second parameter (100) if close is higher than open and the value of the third parameter (50) is close is lower or equal to open. roc1 = roc(close, a); // All returns are based on either 100 or 50 (depends on the first bar value of the variable "a") perf1 = perf(close, a); // Returns are correct here. They are based on 100 when close is higher than open, and on 50 when the condition is false. Back to our rule, the MACD or Moving Average Convergence-Divergence indicator can be referenced by the "MACD" function. Here is how to measure the return of the MACD for the previous 12 bars: perf(Macd(), 12) Once we have created our return time-series, we must compare it with the 10% level: rule5 = perf(Macd(), 12) < -10; Complete Trading System Formula rule1 = adx(14) > 20; rule2 = cross(close, ema(30)); rule3 = istrue(RSI(14) > 70, 5); buy = rule1 and rule2 and rule3; rule4 = istrue(close < close[1], 3); rule5 = perf(Macd(), 12) < -10; sell = rule4 and rule5; Strategy Stops In "Create a trading system" form, click on the icons next to "Trailing Stop" and "Profit Stop". Set the trailing stop value, in percent, to 30 and the profit stop value to 20.
|