Sharing investing and trading ideas. Helping traders get started.
Advertisement

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)

0 comments:

Post a Comment