Click here to Login




Backtesting chart patterns using the auto support and resistance indicator

Updated on 2012-09-05





In a previous post, I have introduced the support/resistance indicator and gave some examples of how to use it to detect trading patterns such as the rising wedge, ascending triangle and symmetrical triangle.

That post can be found here: Detect chart patterns using the auto support/resistance indicator

In this one, I will show you how to use the same function to backtest simple and advanced chart patterns using QS trading software.


Support and Resistance Lines

The auto support and resistance indicator is called "AutoSR". It allows you to detect any kind of support and resistance lines (not just horizontal ones) of an asset given a lookback period.

This function, as we have introduced in the previous post, returns the last support or resistance line. It can be used in a watchlist or when screening stocks but it simply cannot be used with the simulator or backtester tool. This is because the backtester requires that at any given trading bar, the function returns the support or resistance level at that bar. This means that these trendlines will be calculated based on that bar and the preceding N-bars (user-defined value).

To see the difference between the basic and advanced use (for backtests) of this function, let us plot them:

- Create a new chart with two panes
- Right click on the first pane then select "Edit Formula"
- Type the following formula then click on "Update Graph"

PlotCandleStick("Quotes", colorBrown|255|colorViolet|255|0, StyleSymbolVdash);
Plot(volume, "Volume", colorLime|55|colorLime|64|0, ChartBar, StyleOwnScale);

period = 50; // The period used to calculate the trendlines
res = AutoSR(1, 0, period); // Resistance level for each trading bar
sup = AutoSR(0, 0, period); // Support level for each trading bar

plot(res, "Resistance", colorBlue, ChartLine, StyleNoScale);
plot(sup, "Support", colorRed, ChartLine, StyleNoScale);


- Right click on the second pane then select "Edit Formula"
- Type the following formula then click on "Update Graph"

PlotCandleStick("Quotes", colorBrown|255|colorViolet|255|0, StyleSymbolVdash);
Plot(volume, "Volume", colorLime|55|colorLime|64|0, ChartBar, StyleOwnScale);

period = 50; // The period used to calculate the trendlines
res = AutoSR(3, 0, period); // Resistance level for each trading bar
sup = AutoSR(2, 0, period); // Support level for each trading bar

plot(res, "Resistance", colorBlue, ChartLine, StyleNoScale);
plot(sup, "Support", colorRed, ChartLine, StyleNoScale);





Why use the "lag" parameter?

The second parameter of the "AutoSR" function allows us to specify a lag period for detecting support and resistance lines. If we want to detect trendline crossovers then specifying a lag of 1 (or higher) is necessary because otherwise the crossover would never occur (support/resistance levels are always above the high/low). A lag value of 1 means that we want to get support or resistance level one bar ago. In that case, the calculation will be based on [Current Bar - Lookback period, Current Bar], where the lookback period is specified in the third parameter. Depending on the analysis you want to perform, you can use low periods to perform short-term analysis or high periods to perform long-term analysis.

Example:
a = AutoSR(2, 1, 50);


Backtesting Example: Stock Crossed Above its Resistance Line

The first example will show you how to use the auto support/resistance function to scan for simple crossovers.

To detect a stock crossing above its resistance line, simply type:

filter = close > AutoSR(3, 1, 50); // This formula can be used in QuantShare screener

To use the same formula in a trading system simply change "filter" by "buy". Once your trading system is created, you can easily backtest it and see whether this pattern is profitable or not.

To backtest a resistance breakout with a high increase in volume:

filter = close > AutoSR(3, 1, 50) and volume > 2*sma(volume, 10);


Backtesting the Falling Wedge Chart Pattern

In order to backtest the Falling Wedge trading pattern, we must calculate the slope of the resistance and support line, in addition to the levels.

When using the basic "AutoSR" function to return the latest trendlines, we can calculate that slope using the "LinearReg_Slope" function. However, when performing backtests, we will not get the complete line (only levels) and therefore it is impossible, for example, to calculate the slope of the support line for each trading bar. Fortunately, there is a solution and it consists of creating a custom function and using the "TA.AutoSR" function.
This function is almost the same as the one used in QuantShare programming language, with the difference that it is C# based and it contains other important measures (slope and intercept) besides the support or resistance level.

Let us create a custom function that returns the slope of a support or resistance:

- Select "Tools -> Create Functions"
- Create a new function (Name: SlopeAutoSR)
- Add three numeric parameters (type, lag and length)
- Type the following code:

VectorCustomDouble c = TA.AutoSR(type, lag, length);
for(int i=0;i < c.Length;i++)
{
result[i] = c[i][1]; // Slope. c[i] is an array that contains three values: Level, Slope and Intercept.
}


Once created, you can use the new "SlopeAutoSR" function to calculate the slope of a resistance or support line for each trading bar.
Note: You can also download this trading indicator directly from the sharing server: Slope of Support and Resistance Lines

Example:
a = SlopeAutoSR(3, 1, 50);


Now, let us try to backtest the Falling wedge pattern. This bullish pattern appears when the range formed by the support and resistance lines is contracting (lower highs and lower lows).
Our trading system will enter a new long position when the stock crosses above the resistance line.

Here is a formula you can use in your trading system to backtest the wedge pattern crossover:

period = 50; // The period used to calculate the trendlines
res = AutoSR(3, 1, period); // Resistance level for each trading bar
sup = AutoSR(2, 1, period); // Support level for each trading bar

resSlope = SlopeAutoSR(3, 1, period);
supSlope = SlopeAutoSR(2, 1, period);

fwedge = resSlope < 0 and supSlope < 0 and supSlope > resSlope; // Detecting Falling Wedge pattern
buy = fwedge and close > res; // The backtester will enter long if a crossover occurs



The next picture plots a falling wedge pattern and the moment the price crosses above it. In the second pane, it shows the signals generated by the above formula. Each signal represents a crossover.














7 comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1441 days ago

Profile Graphs
Posted 1546 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.