#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;}

### Trading System: Moving average crossover and few others buy and sell rules

Updated on 2012-03-23

First we will create a basic trading system with few buy and sell rules, then we will backtest and optimize our strategy. This is what this blog post is about.

Other examples of trading systems can be found here:
Example of a trading system implemented in QuantShare Software

These examples, including this one, are created to show you how to implement ideas and rules in QuantShare language.

The trading system we will implement can be described as follows:
- Buy when the 10-bar moving average crosses the 30-bar moving average and when the ratio of up days to down days is higher than 0.7.
- Sell when the stock decreases more than 0.1% three days in a row or when the stock decreases more than -20% from the highest high (trailing stop).

The optimization consists of varying the following variables:
- Ratio of up to down days threshold
- Trailing stop level

Moving average crossover

A crossover between two time-series (price, indicator or any other series) can be detected using the "cross" formula.

cross(a, b) => Returns 1 if a crosses above b.
cross(b, a) => Returns 1 if a crosses below b, which is equivalent to: b crosses above a.

We want to generate a bull signal when the 10-bar moving average crosses above the 30-bar moving average and thus we will use the "cross" formula as follows:

rule1 = cross(sma(10), sma(30));

The formula to generate a simple moving average of the price series is "sma".
You can also create a moving average of any other series:

a = sma(rsi(14), 30);

This will create a 30-bar moving average of the relative strength indicator.

Ratio of up to down days

This ratio is computed for the 30 previous trading bars.

How to calculate the number of up days:

a = sum(perf(close, 1) > 0, 30);

Perf: Calculate the performance/return of a time-series
Sum: Calculates the sum of the values of a time-series for the last 30 days/bars

The same formula is used to calculate the number of down days:

b = sum(perf(close, 1) < 0, 30);

The ratio rule is computed as follows:

ratio1 = a / b;
rule2 = ratio1 > 0.7;

Optimizing the threshold:

To optimize a value, simply add a new optimizable variable using "Optimize" function.

Optimize("r1", 0.5, 0.8, 0.1); // Min, Max and Step values
a = sum(perf(close, 1) > 0, 30);
b = sum(perf(close, 1) < 0, 30);
ratio1 = a / b;
rule2 = ratio1 > r1;

Consecutive price decreases

Now for the first sell criterion, we wanted to exit a position if it drops more than 0.1% three days in a row.

"istrue" function will be used in this case. This function returns true if the passed variable is true for the previous N bars. "N" is also passed as an argument to this function.

rule3 = istrue(perf(close, 1) < 0.1, 3);

In order to generate sell signals from the above function, we must associate the "rule3" variable to the "sell" variable in our trading system. See the complete formula below.

Trailing stop

The second sell rule consists of a trailing stop.

To add a trailing stop to the trading system, click on the icon next to "Trailing Stop".
Click on the numeric value then set the stop level to 20.

Optimizing the trailing stop:

Instead of typing "20", type a variable name (Example: "a").
In the optimization grid, type a minimum, maximum and step values.

Example:
Min = 10
Max = 70
Step = 10

This will vary the trailing stop level from 10 to 70 with a step increment of 10.

Backtesting and optimizing the strategy

Let us first assemble the different rules and write the complete formula:

rule1 = cross(sma(10), sma(30));

Optimize("r1", 0.5, 0.8, 0.1); // Min, Max and Step values
a = sum(perf(close, 1) > 0, 30);
b = sum(perf(close, 1) < 0, 30);
ratio1 = a / b;
rule2 = ratio1 > r1;

buy = rule1 and rule2; // Buy when rule1 and rule2 are true on the same bar

rule3 = istrue(perf(close, 1) < 0.1, 3);
sell = rule3;
// Do not forget to add the trailing stop

After you save your trading system, you can backtest your strategy by selecting it in the "Simulator Manager" then clicking on "Simulator" or "Backtest".

Click on "optimize" to optimize the strategy and generate several variations based on the two optimizable variables we have created (one for the Ratio of up to down days and one for the trailing stop)