Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Kaufman Adaptive moving average Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
jwnewsom
Posted : Saturday, December 15, 2007 11:16:51 AM
Registered User
Joined: 10/27/2005
Posts: 21
I'd like help to set up a 10-period bollinger band breakout scan based on the Kaufman adaptive moving average in Blocks.

Info on Kaufman's AMA:

8.9 Kaufman Adaptive Moving Average

The Kaufman adaptive moving average (KAMA) by Perry J. Kaufman is an exponential style average but with a smoothing that varies according to recent data. In a steadily progressing move the latest prices are tracked closely, but when going back and forward with little direction the latest prices are given only small weights.
he smoothing factor is determined based on a given past N days (default 10). The net change (up or down) over that time is expressed as a fraction of the total daily changes (up and down). This is called the “efficiency ratio”. If every day goes in the same direction then the two amounts are the same and the ratio is 1. But in the usual case where prices backtrack to some extent then the net will be smaller than the total. The ratio is 0 if no net change at all.
abs (close[today] - close[N days ago])
ER = --------------------------------------
Sum abs (close - close[prev])
past N days
The ER is rescaled to between 0.0645 and 0.666 and then squared to give an alpha factor for the EMA of between 0.444 and 0.00416. This corresponds to EMA periods from a fast 3.5 days to a very slow 479.5 days.
alpha = (ER * 0.6015 + 0.0645) ^ 2
An exponential moving average of prices is then taken, using each alpha value calculated.
KAMA = alpha * close + (1-alpha) * KAMA[prev]
These alpha values can be viewed directly with “KAMA alpha” in the lower indicator window (a low priority option, near the end of the lists). High values show where KAMA is tracking recent prices closely, low values show where it's responding only slowly.
Bruce_L
Posted : Monday, December 17, 2007 1:03:01 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
The one thing I can't figure out from the description is how to initialize the Kaufman Moving Average. Four options jump immediately to mind, but I do not know which would be best:

1 - Not include the first n=Period prices in the average.
2 - Calculate the efficiency ratio and alpha at bar Period+1 and use this alpha for the first Period+1 bars.
3 - Calculate the efficiency ratio and alpha using a shorter Period for the the early bars and start using the entered period for the efficiency ratio at bar=Period+1?
4 - Initialize the Moving Average using a different Moving Average type (such as Simple).

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
jwnewsom
Posted : Monday, December 17, 2007 4:53:42 PM
Registered User
Joined: 10/27/2005
Posts: 21
I see what you mean, Bruce.

I see the most accurate as option 3 and the easiest as option 1, while 2 and 4 use a substitution that would be reasonable.

The problem is in the choice of how to boot strap a process that requires N periods to calculate. I'm going to make a confession right here and now. I'm something of a math geek, what with the MS in computer science. My preference as a math geek would be to use what is available to calculate as closely as possible to the intended value, which I believe would be option 3. 

Meaning, if there are only 3 bars of data available, use N =3, so more generally, for bars 1 through (N-1), use the bar number as N until you reach N bars with which to calculate. 

However, practically speaking, there probably isn't much to gained by doing all that. I'd be overjoyed with even the 1st one, since I could avoid any trouble by selecting from charts having sufficient price history to make the bootstrap period irrelevant.

I'd be happy with whatever you can do.

Thanks again,

Jim
diceman
Posted : Tuesday, December 18, 2007 7:48:55 AM
Registered User
Joined: 1/28/2005
Posts: 6,049
Typically the way I've seen this is once the ER/alpha
is available. It is applied to the previous close as
the first KAMA data point.
 
I'm not certain data length is all that important. In the real
world the smoothing value can go from 20 to 150. We
are not looking for the "proper" 150 EMA value but
rather the "slower" movement supplied by the changing
filter.
 
I would guess that in theory. If we waited for all lengths
to achieve their proper EMA equivalent the KAMA
would lose all its value. 
 
 
Thanks
diceman
Bruce_L
Posted : Tuesday, December 18, 2007 9:22:24 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
jwnewsom,
Please start by creating the following Code Block as Line and Int to Line (please keep in mind I'm not a programmer):

Kaufman Adaptive Moving Average:

Replace everything in the Code Block with (Firefox seems to work for copy and paste, IE does not):

<WBIGuid("fbef863d-6cfd-4bfa-8d3c-6a9b666b2812"),FriendlyName("Kaufman Adaptive Moving Average")> _
Public  Class Kaufman_Adaptive_Moving_Average
inherits BaseTemplateDLStoDLSPeriod
    Public Overrides Sub calculate()
        If ParameterValue > 1
            Dim ema As Double = InputValue(0)
            AddToOutput(InputDate(0),ema)
            Dim termratio As Double
            Dim sum As Double = 0
            For bar As Integer = 1 To ParameterValue - 1
                sum += System.Math.Abs(InputValue(bar) - InputValue(bar-1))
                termratio = (System.Math.Abs(InputValue(bar) - InputValue(0)) / (sum + .0000001) * .6015 + .0645) ^ 2
                ema = ema * (1 - termratio) + termratio * InputValue(bar)
                AddToOutput(InputDate(bar),ema)
            Next
            If InputCount > ParameterValue
                For bar As Integer = ParameterValue To InputCount - 1
                    sum += System.Math.Abs(InputValue(bar) - InputValue(bar-1)) - System.Math.Abs(InputValue(bar-ParameterValue+1) - InputValue(bar-ParameterValue))
                    termratio = (System.Math.Abs(InputValue(bar) - InputValue(bar-ParameterValue+1)) / (sum + .0000001) * .6015 + .0645) ^ 2
                    ema = ema * (1 - termratio) + termratio * InputValue(bar)
                    AddToOutput(InputDate(bar),ema)
                Next
            End If
        Else
            For bar As Integer = 0 To InputCount - 1
                AddToOutput(InputDate(bar),InputValue(bar))
            Next
        End If
    End Sub
End Class


You may wish to review the following for help with creating Custom Code Blocks:

VB.Net (code block) to color down Monday red
VB.net exercise to access Quickedit from your codeblock
Hello World! Writing Code Blocks in Visual Basic .Net

If you explain your "10-period bollinger band breakout scan based on the Kaufman adaptive moving average" in more detail, we will hopefully be able to help you create it now that we have a KAMA available.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
jwnewsom
Posted : Wednesday, December 26, 2007 12:10:09 PM
Registered User
Joined: 10/27/2005
Posts: 21
Bruce,

I have the code block created now.

I'm really new to blocks, so I need help with a few simple things I'd like to do next:

  1. Be able to plot the new moving average on a bar chart. (I confess I could not figure it out)
  2. Do a QuickEdit thingy to change the KAMA period.
  3. Plot Bollinger bands based on that moving average. (I was going to do 10 period 2 std dev bands)
  4. Set strategy scan conditions up for when closing price > upper BB of KAMA, and when closing price < lower BB of KAMA
PS. That code looks good enough to me, though I might have placed any variable declarations up top rather than inside the IF statement. I did not read it closely enough to determine whether that would have an unintended effect, so I did not change anything.

Thanks again for your help,

Jim
Bruce_L
Posted : Wednesday, December 26, 2007 12:50:30 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Moving the DIM statements would not cause significant harm. I placed the DIM statements inside the IF statement because they are not needed unless the Period is greater than one. I didn't see a reason to allocate memory unless necessary.

One way to create a KAMA indicator using the Custom Code Block is to add a Moving Average. If you right-click and select Properties to view the Block Diagram, you can double-left click on the Moving Average Block to adjust the QuickEdit Replace settings. Then Add | Kaufman Adaptive Moving Average. The result is a Moving Average Study that allows you to adjust both the type and period of the Moving Average.

You can add Bollinger Bands to any plotted Study by selecting Add Study to Chart | Bollinger Bands and choosing the parent Study.

Please save the attached .plot file to:

My Documents\Blocks Files\Tool Parts\Chart

You can add it to any chart using the ADD STUDY button and then selecting Add Study | My Computer.

Please download the attached .scond file to:

\My Documents\Blocks Files\Tool Parts\Strategy Conditions

Once downloaded, you should be able to select Add Condition | My Computer when adding a Strategy Condition to access it.

You can use QuickEdit to adjust the settings or view the Block Diagrams.

Attachments:
Moving Average.plot - 3 KB, downloaded 1,105 time(s).
Price vs BB of MA.scond - 7 KB, downloaded 1,194 time(s).



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
jwnewsom
Posted : Wednesday, December 26, 2007 3:04:02 PM
Registered User
Joined: 10/27/2005
Posts: 21
Thanks again Bruce,

I have all that set up and working well now.
tuckandturn
Posted : Monday, April 4, 2011 9:53:07 AM
Registered User
Joined: 6/30/2009
Posts: 57
QUOTE (jwnewsom)
I'd like help to set up a 10-period bollinger band breakout scan based on the Kaufman adaptive moving average in Blocks.

Info on Kaufman's AMA:

8.9 Kaufman Adaptive Moving Average

The Kaufman adaptive moving average (KAMA) by Perry J. Kaufman is an exponential style average but with a smoothing that varies according to recent data. In a steadily progressing move the latest prices are tracked closely, but when going back and forward with little direction the latest prices are given only small weights.
he smoothing factor is determined based on a given past N days (default 10). The net change (up or down) over that time is expressed as a fraction of the total daily changes (up and down). This is called the “efficiency ratio”. If every day goes in the same direction then the two amounts are the same and the ratio is 1. But in the usual case where prices backtrack to some extent then the net will be smaller than the total. The ratio is 0 if no net change at all.
abs (close[today] - close[N days ago])
ER = --------------------------------------
Sum abs (close - close[prev])
past N days
The ER is rescaled to between 0.0645 and 0.666 and then squared to give an alpha factor for the EMA of between 0.444 and 0.00416. This corresponds to EMA periods from a fast 3.5 days to a very slow 479.5 days.
alpha = (ER * 0.6015 + 0.0645) ^ 2
An exponential moving average of prices is then taken, using each alpha value calculated.
KAMA = alpha * close + (1-alpha) * KAMA[prev]
These alpha values can be viewed directly with “KAMA alpha” in the lower indicator window (a low priority option, near the end of the lists). High values show where KAMA is tracking recent prices closely, low values show where it's responding only slowly.


Is there anyway to perform the same set of calculations, but in Telechart 2000?  I've been able to create the ER calculations as a PCF, but am not sure how to proceed to create the KAMA as described so I can seet it in Telechart.

Many thanks,
Doug
Bruce_L
Posted : Monday, April 4, 2011 12:42:03 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I cannot think of a way to create a Personal Criteria Formula short enough to be practical for KAMA in either the current production release of TeleChart or the TC2000.com version 11 beta.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
tuckandturn
Posted : Monday, April 4, 2011 12:51:36 PM
Registered User
Joined: 6/30/2009
Posts: 57
I couldn't either, but I had to ask!  Thanks for such a timely response.

Doug
Jester73
Posted : Thursday, November 8, 2012 6:57:45 PM
Registered User
Joined: 6/28/2007
Posts: 18

Hi Bruce,

I am a platinum member and have stockfinder. I really would like to have the KAMA indicator and based on this would like to make some scans. I am completely bad in programming. I see lots of programming in the forum on this topic but I am not sure what exactly is the right formula.

Please can you tell me which formula I could use and simply copy into the real code indicator editor?

Thanks

Marco

Bruce_L
Posted : Friday, November 9, 2012 7:36:26 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

I would just use the RealCode Indicator given in my Friday, October 09, 2009 12:27:04 PM ET post in the Adaptive Moving Average topic.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Fisher2
Posted : Thursday, December 27, 2018 9:09:47 PM
Gold Customer Gold Customer

Joined: 8/13/2009
Posts: 73

Bruce,

Can the Kaufman Adaptive Moving Average be created with the latest version 18 of TC2000?

Fisher2

Bruce_L
Posted : Wednesday, January 2, 2019 4:11:42 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

I have not been able to figure out a way to make the formula short and fast enough to be practical or post in the forums.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.