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.
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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:
- Be able to plot the new moving average on a bar chart. (I confess I could not figure it out)
- Do a QuickEdit thingy to change the KAMA period.
- Plot Bollinger bands based on that moving average. (I was going to do 10 period 2 std dev bands)
- 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
|
|
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,103 time(s). Price vs BB of MA.scond - 7 KB, downloaded 1,192 time(s).
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 10/27/2005 Posts: 21
|
Thanks again Bruce,
I have all that set up and working well now.
|
|
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
|
|
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
|
|
Registered User Joined: 6/30/2009 Posts: 57
|
I couldn't either, but I had to ask! Thanks for such a timely response.
Doug
|
|
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
|
|
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
|
|
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
|
|
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
|
|
Guest-1 |