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

Tuesday, June 23, 2009

Kaufman Adaptive Moving Average

Kaufman Adaptive Moving Average KAMA
Comes with formula, calculation steps and VBA code

Introduction
The KAMA uses a smoothing constant in its calculation which requires the user to specify 3 integer values,

a) j, the number of periods to be used for the KAMA
b) Fast, the number of periods for a fast moving average
c) Slow, the number of periods for a slow moving average

Step 1: Calculate the change in direction in the last j periods.

Directional Changet = Closet – Closet-j, Close j days ago

Step 2: Sum the absolute of daily change in direction.

Total Range = Absolute (Closet – Closet-1) + Absolute (Closet-1 – Closet-2) + … + Absolute (Closet-j+1 – Closet-j)

Step 3: Calculate the Efficiency Ratio

Efficiency Ratio = Absolute (Directional Change / Total Range)

If the market closed up every day for the last j days, the Efficiency Ratio will be 1. If the market remained unchanged for the last j days despite moving up and down, the Efficiency Ratio will be 0.

Step 4: Calculate the fast and slow smoothing constant

Fast Smoothing Constant, FSC = 2 / (Fast + 1)

Slow Smoothing Constant, SSC = 2 / (Slow + 1)

Step 5: Calculate Weighted Smoothing Constant

Weighted Smoothing Constant, WSC = Efficiency Ratio x (FSC – SSC) + SSC

Hence, the Weighted Smoothing Constant shifts between FSC and SSC based on the Efficiency Ratio, i.e. how will the market has been trending in the last n days.

Step 6: Square the WSC

C = WSC x WSC

C is used in the next step to calculate KAMA. Squaring it reduces the sensitivity of the KAMA to market fluctuations.

Step 7: Calculate KAMA

KAMA = C x (Closet – KAMAt-1) + KAMAt-1
= C x Closet + (1 – C) x KAMAt-1

The very first KAMA of a series can be the close or a simple moving average. Compare the above to the formula for the exponential moving average,

EMA = alpha x Price + (1 – alpha) x EMAt-1

Recall that the alpha in EMA is calculated as 1 / (j + 1), which is the same formula to be used for the calculation of FSC and SSC.

FSC = 1 / (Fast + 1) Slow = 1 / (Slow + 1)

Thus, the KAMA indicator can be thought of as the combination of 2 different exponential moving averages, dynamically adjusting its responsiveness based on the Efficiency Ratio and the fast and slow smoothing constants dictated.

VBA Code

Method A: Using UDFs
Method A creates a custom function that can be called from the formula bar. Enter in any cell, "=KAMA([j+1 periods of prices], [n the fast EMA smoothing period], [k the slow EMA smoothing period], [the prior period KAMA])"

Sub AddUDF()
Application.MacroOptions macro:="KAMA", _
Description:="Returns Kaufman Adaptive Moving Average" & Chr(10) & Chr(10) & _
"Select last j+1 periods of Close for efficiency ratio, n the fast EMA smoothing period," & Chr(10) & _
"k the slow EMA smoothing period and last KAMA", _
Category:="Technical Indicators"
End Sub

Public Function KAMA(close_, n, k, KAMAYesterday)
'Calculate Efficiency Ratio
j = WorksheetFunction.Count(close_) - 1
dir_chg = close_(j + 1, 1) - close_(1, 1)
tot_rng = 0
For a = 1 To j
tot_rng = tot_rng + Abs(close_(a + 1, 1) - close_(a, 1))
Next a
EffRatio = Abs(dir_chg / tot_rng)
FSC = 2 / (n + 1)
SSC = 2 / (k + 1)
C = (EffRatio * (FSC - SSC) + SSC) ^ 2
KAMA = C * close_(j + 1, 1) + (1 - C) * KAMAYesterday
End Function

Method B
Method B produces the daily directional change, absolute daily directional change, efficiency ratio, KAMA smoothing constant C and KAMA. You start by running the Runthis sub which passes your inputs to the KAMA_1 sub.

Sub Runthis()
Dim close1 As Range, output As Range, n As Long
Dim k As Long, j As Long

Set close1 = Range("E2:E11955")
Set output = Range("H2:H11955")

n = 5 'number of periods for fast EMA
k = 10 'number of periods for slow EMA
j = 5 'The number of periods used in calculating efficiency ratio

KAMA_1 close1, output, n, k, j
End Sub

Sub KAMA_1(close1 As Range, output As Range, n As Long, k As Long, j As Long)
output(0, 1).Value = "Directional Change"
lastclose = close1(1, 1).Address(False, False)
currentclose = close1(2, 1).Address(False, False)
output(2, 1).Value = "=" & currentclose & "-" & lastclose
output(0, 2).Value = "Abs Dir Chg"
dirchg = output(2, 1).Address(False, False)
output(2, 2).Value = "=abs(" & dirchg & ")"
Range(output(2, 1), output(2, 2)).Copy output
output(0, 3).Value = "Efficiency Ratio"
dirsum = Range(output(2, 1), output(j + 1, 1)).Address(False, False)
absdirsum = Range(output(2, 2), output(j + 1, 2)).Address(False, False)
output(j + 1, 3).Value = "=abs(sum(" & dirsum & ")/sum(" & absdirsum & "))"
FSC = "2/(" & n & "+1)"
SSC = "2/(" & k & "+1)"
effrange = output(j + 1, 3).Address(False, False)
output(0, 4).Value = "C"
output(j + 1, 4) = "=(" & effrange & "*(" & FSC & "-" & SSC & ")+" & SSC & ")^2"
C1 = output(j + 1, 4).Address(False, False)
lastKAMA = output(j, 5).Address(False, False)
output(0, 5).Value = "KAMA"
output(j + 1, 5) = "=" & C1 & "*" & close1(j + 1, 1).Address(False, False) & "+(1-" & C1 & ")*" & lastKAMA
Range(output(j + 1, 3), output(j + 1, 5)).Copy output.Offset(0, 2)
output(j + 1, 5) = "=" & C1 & "*" & close1(j + 1, 1).Address(False, False) & "+(1-" & C1 & ")*" & close1(j, 1)
Range(output(1, 3), output(j, 5)).Clear
Range(output(1, 1), output(1, 2)).Clear
End Sub


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