Sharing investing and trading ideas. Helping traders get started.
Advertisement
Showing posts with label APZ. Show all posts
Showing posts with label APZ. Show all posts

Sunday, April 29, 2012

Adaptive Price Zone APZ - Backtest Results and Implementation Issues

Adaptive Price Zone APZ - Backtest Results and Implementation Issues 

Discusses basic issues and results when attempting to back-test the APZ indicator

Data: S&P500 Emini Futures
Futures Contract Rollover: Arbitrarily set on second Friday of every Mar, Jun, Sep and Dec
Period: 28 Dec 2008 - 31 Mar 2012 (Hourly Chart)
VBA Code: Method B of Code
Excel Version: 2010

Click here for background information, formula, calculation steps and the entire VBA code.

There are multiple variations on how to apply an indicator. I seek your understanding that the work done below are experiments and are for information purposes only.

Summary of Results 
Conventional application of the APZ (i.e. long at the lower boundary and short at the upper boundary in anticipation of price reversal) is found to be not profitable. However, the reverse is somewhat profitable (long when price exceeds upper boundary of APZ and vice versa), suggesting that this technical indicator and the applied trading rules have potential for real-life implementation with further tweaking to the application method (e.g. chart indicator based on hourly data, but trades are generated as and when the trading rules are met subsequently). 

Procedure in Brief
Pasted the code into Excel with changes made as per comments in code to direct application to ranges with input data (High, Low, Close columns). Screen Shot 1 shows the output (the last seven columns). The first value of some columns are error values as their formulas include reference to cells on the first row which are actually text headings, not numbers. However, calculations in subsequent rows are unaffected. 

Screen Shot 1

Detailed Findings and Results
Inputs - Band factor is set at 2, EMA period is set at 5

Summary of Trading Rules -

1) If Close at this hour is higher than the APZ upper limit at the end of the last hour, enter long at the end of this hour
1) If Close at this hour is lower than the APZ lower limit at the end of the last hour, enter long at the end of this hour


223.25 index points of profit (before transaction costs) were generated out of this strategy on 344 trades. Average return per trade is 0.65 points on a standard deviation of 21.34 points. Therefore, this strategy does not look statistically profitable. The success rate of this strategy is 39%, i.e. only 135 trades were profitable. The profit trajectory of this indicator and strategy is shown in Chart 1. 

Chart 1: Cumulative Profit of APZ and Applied Trading Rules

Conclusion
Contemporary price behavior for the S&P500 suggests that the APZ is more suited for use as a "trend" type indicator, rather than a "price reversion" indicator as I have always thought. The key risk is that we cannot be certain that the behaviour of the APZ indicator versus the S&P500 will remain constant in the future. Under certain regimes, the APZ may behave as a price reversion indicator which would require the inversion of the above trading rules.

Further research can be in the application of this model in finer time granularity.

Any suggestions or comments are welcome.


Like what you have just read? Digg it or Tip'd it.
The objective of Finance4Traders is to help traders get started by bringing them unbiased research and ideas. Since late 2005, I have been developing trading strategies on a personal basis. Not all of these models are suitable for me, but other investors or traders might find them useful. After all, people have different investment/trading goals and habits. Thus, Finance4Traders becomes a convenient platform to disseminate my work...(Read more about Finance4Traders)

Monday, June 29, 2009

Adaptive Price Zone

Adaptive Price Zone APZ
Read on for formula, calculation steps and VBA code. For naive backtest results and findings, visit here

The Adaptive Price Zone APZ indicator is from the Technical Analysis of Stocks & Commodities Magazine, September 2006 article "Trading With An Adpative Price Zone" by Lee Leibfarth. It has an upper and lower boundary and prices are expected to reverse when it hits either boundar.

Upper APZ boundary = EMA of (EMA of Price for n periods) for n periods + APZRange

Lower APZ boundary = EMA of (EMA of Price for n periods) for n periods – APZRange

where APZRange = EMA of (High – Low) for n periods x band factor.

Both band factor and n are the user’s choice. Band factor adjusts the width of the adaptive price zone. EMA refers to the exponential moving average. As can be seen, the adaptive price zone is based on the exponential moving average of an exponential moving average.

VBA Code
Method A uses functions, while Method B uses sub procedures to calculate APZ. Method B is faster and more flexible.

Method A
Like the Chaikin oscillator, the adaptive price zone can be computed using a series of exponential moving average custom functions. Nonetheless, a custom function to compute the APZ is provided below. Note that it is an array function and outputs the center line, upper and lower bands of the adaptive price zone. To display the entire set of outputs, enter the APZ function into the one cell and select it along with the 2 cells horizontally next to it, press F2 and Enter.

Your inputs are both the current and last price and range EMAs, as well as the band factor and average period n. Your VBA code must include the EMA function for the APZ function to work.


'Paste this code into your ThisWorkBook code window in VBA. Right Click This WorkBook in Project Explorer and click View Code.
Private Sub Workbook_Open()
AddUDF
End Sub

'The rest belong to any module
Sub AddUDF()
'Tells Excel to includes these in list of functions, add descriptions to them and create a new category called Technical Indicators.
Application.MacroOptions macro:="APZ", _
Description:="Returns the center line, upper band and lower band the adaptive price zone" & Chr(10) & Chr(10) & _
"Input current EMA of price, last period's EMA of price, current EMA of (High-Low), last period's EMA of (High-Low), band_factor and n the averaging period.", _
Category:="Technical Indicators"
End Sub

Public Function APZ(CurrentEMA_Price, LastEMA_Price, CurrentEMA_Range, LastEMA_Range, band_factor, n)
Dim result(0, 1 To 3)
result(0, 1) = EMA(LastEMA_Price, CurrentEMA_Price, n)
bandwidth = EMA(LastEMA_Range, CurrentEMA_Price, n) * band_factor
result(0, 2) = result(0, 1) + bandwith
result(0, 3) = result(0, 1) - bandwidth
APZ = result
End Function

Once you are done with the above, you can calculate the adaptive price zone by entering into any cell, "=APZ(Current EMA of Price, Last EMA of Price, Current EMA of Range, Last EMA of Range, band factor, n)". As such, you need to calculate the respective EMAs before hand. Select the cell and its 2 neighbours, press F2 and Enter to display the entire set of outputs.

Method B
Method B offers the benefit of not having you calculation any exponential averages in advance. It does everything for you. To run Method B, you have to copy the Runthis sub and the EMA sub of Method B from the page on Accumulation/Distribution line and exponential moving average into your module. You will run the APZ sub from the Runthis sub.


'Copy the following line into the Runthis sub
'Just before the line End Sub
'Disable all other macros that Runthis will call e.g. CLV, ADL, by
'marking them out as comments with single quotes
Dim band_factor As Double
band_factor=2
'Range_1 high, low, output
APZ_1 high, low, close1, output, band_factor, n

Sub Range_1(high, low, output)
output(0, 1).Value = "Range"
high0 = high(1, 1).Address(False, False)
low0 = low(1, 1).Address(False, False)
output(1, 1).Value = "=" & high0 & "-" & low0
output(1, 1).Copy output
End Sub

Sub APZ_1(high As Range, low As Range, close1 As Range, output As Range, band_factor As Double, n As Long)
'Use the Range and EMA functions to calculate components of the APZ
EMA close1, output, n
output(0, 1).Value = "EMA_Price"
Range_1 high, low, output.Offset(0, 1)
EMA output.Offset(0, 1), output.Offset(0, 2), n
output(0, 3).Value = "EMA_Range"
output0 = output(1, 3).Address(False, False)
output(0, 4).Value = "APZ_Width"
output(1, 4).Value = "=" & output0 & "*" & band_factor
output(1, 4).Copy output.Offset(0, 3)
output(0, 5).Value = "APZ_Upper"
output0 = output(4, 6).Address(False, False)
output1 = output(4, 4).Address(False, False)
output(4, 5).Value = "=" & output0 & "+" & output1
output(0, 7).Value = "APZ_Lower"
output(4, 7).Value = "=" & output0 & "-" & output1
output(4, 5).Copy output.Offset(0, 4)
output(4, 7).Copy output.Offset(0, 6)
EMA output, output.Offset(0, 5), n
output(2, 6).Copy output(3, 6)
output(0, 6).Value = "APZ_Center"
Range(output(1, 5), output(2, 7)).Clear
End Sub


Other References

http://www.traders.com/documentation/FEEDbk_docs/2006/09/TradersTips/TradersTips.html#tradestation


Like what you have just read? Digg it or Tip'd it.
The objective of Finance4Traders is to help traders get started by bringing them unbiased research and ideas. Since late 2005, I have been developing trading strategies on a personal basis. Not all of these models are suitable for me, but other investors or traders might find them useful. After all, people have different investment/trading goals and habits. Thus, Finance4Traders becomes a convenient platform to disseminate my work...(Read more about Finance4Traders)

Friday, June 19, 2009

Exponential Moving Average

Exponential Moving Average EMA
Comes with formula, calcuation steps and VBA code

Introduction


The exponential moving average, EMA is calculated as follows.

EMAt = α x Price + (1 – α) x EMAt-1

,where α = 2 / (1 + n)

The subscript t is used to denote time e.g. t-1 refers to the period before t and n, to be specified by the user, refers to the "averging period" of the EMA. For example, the EMA equivalent of a 3 period simple moving average has n of 3. The larger the value of n, the smaller α becomes. This results in a larger (1-α) and the more of EMAt-1 is retained in EMAt.

The very first value of EMA in a time series may be assumed to be a simple moving average of n days’ of prices. Some users may also prefer to start the very first value of the EMA from the second period onwards where EMA on Period 2 = α x Period 2 Price + (1 – α ) x Period 1 Price.

Users should understand that the exponential moving average is actually an infinite series expansion where the earlier prices have an increasingly smaller weight on EMAt. Consider the following:

EMAt = α x Pricet + (1 – α) x EMAt-1
EMAt-1 = α x Pricet-1 + (1 – α) x EMAt-2

Therefore,

EMAt = α x Pricet + (1 – α) x (α x Pricet-1 + (1 – α) x EMAt-2)
EMAt = α x Pricet + α(1 – α)Pricet-1+(1-α)2EMAt-2
EMAt = α x Pricet + α(1 – α)Pricet-1+α(1-α)2Pricet-3+...

This results in the EMA being more responsive and less volatile than its simple moving average equivalent. A more detailed discussion of this can be found in my article about filters in finance and technical analysis.

VBA Code
Method A uses functions, while Method B uses sub procedures to calculate CMF. Method B is faster and more flexible.

Method A

'Paste this code into your ThisWorkBook code window in VBA. Right Click This WorkBook in Project Explorer and click View Code.
Private Sub Workbook_Open()
AddUDF
End Sub

'The rest belong to any module
Sub AddUDF()
'Tells Excel to includes these in list of functions, add descriptions to them and create a new category called Technical Indicators.

Application.MacroOptions macro:="EMA", _
Description:="Returns the Exponential Moving Average." & Chr(10) & Chr(10) & _
"Select last period's EMA or last period's price if current period is the first." & Chr(10) & Chr(10) & _
"Followed by current price and n." & Chr(10) & Chr(10) & Chr(10) & _
"The decay factor of the exponential moving average is calculated as alpha=2/(n+1)", _
Category:="Technical Indicators"
End Sub

Public Function EMA(EMAYesterday, price, n)
alpha = 2 / (n + 1)
EMA = alpha * price + (1 - alpha) * EMAYesterday
End Function
Once you are done with the above, you can compute exponential moving average by typing into any cell "=EMA([Last Period EMA],[Current Price],[n])". Enter last period price as last period EMA if you are computing the first EMA of your dataset.

Method B
To run Method B, you have to copy the Runthis sub from the page on Accumulation/Distribution Line into your module. You also must run EMA from the Runthis sub.

'Add the following line to the sub Runthis
'Place it right before End Sub
'And disable all other macros that Runthis will call
EMA close1, output, n

'Insert the following sub
'This sub will start calculating EMA from t=2 onwards
Sub EMA(close1 As Range, output As Range, n As Long)
output(0, 1).Value = "EMA"
close0 = close1(1, 1).Address(False, False)
close1a = close1(2, 1).Address(False, False)
output1 = output(1, 1).Address(False, False)
alpha = 2 / (n + 1)
output(2, 1).Value = "=2/(1+" & n & ")*" & close1a & "+(1-2/(1+" & n & "))*" & output1
output(2, 1).Copy output
output(2, 1).Value = "=2/(1+" & n & ")*" & close1a & "+(1-2/(1+" & n & "))*" & close0
End Sub
Other references



Like what you have just read? Digg it or Tip'd it.
The objective of Finance4Traders is to help traders get started by bringing them unbiased research and ideas. Since late 2005, I have been developing trading strategies on a personal basis. Not all of these models are suitable for me, but other investors or traders might find them useful. After all, people have different investment/trading goals and habits. Thus, Finance4Traders becomes a convenient platform to disseminate my work...(Read more about Finance4Traders)