Click here to Login




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
Trading System: Buy stocks with the highest Sharpe ratio
Day Trading: A trading system that combines intraday and EOD data

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)




Any questions? Please add a comment below.












no comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1240 days ago

Profile Graphs
Posted 1345 days ago

QuantShare
Previous Posts

More Posts

Back







QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.