Profile Picture

Kaufman Moving Average

Posted By pch 7 Years Ago
Message
pch
Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)
Posted Wednesday March 03 2010
Hi,

Attached is a version of the Kaufman moving average, published by Perry Kaufman in his very valuable book "New Trading and Systems"
(http://www.amazon.com/New-Trading-Systems-Methods-Wiley/dp/047126847X/ref=sr_1_3?ie=UTF8&s=books&qid=1267629057&sr=8-3)

You might want to review it, improve it and include it into the range of indicators you offer standard with RE.

Could you please tell me how I should modify it in order to add a second constructor which could support the iSeries of values as a parameter (like you have standard with SMA in your system.

Thanks in advance,

Pierre

Attachments
KAMA.cs (441 views, 2.00 KB)
Posted Wednesday March 03 2010
The code looks pretty good, although I didn't look at it closely enough to verify that it's using the correct algorithm.

Just add the following as an additional constructor:

public KAMA(int periods, ISeries input)

      : this(periods)

{

      SetInputs(input);
}

Thanks,
Daniel

pch
Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)
Posted Wednesday March 03 2010
Thank you.

Pierre
Posted Monday March 08 2010
I tired converting that to VB - got it compiling but all values end up as NaN - anybody know what's wrong?

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports RightEdge.Common
Imports RightEdge.Indicators



'   The Id attribute needs to be set to a unique code that will identify
'   your indicator. A GUID is a good candidate.
'   Indicators must be marked Serializable in order to be used in
'   trading systems.
'   Use the SeriesInputAttribute to specify how many inputs your
'   indicator uses.
Name:="KAMA")> _
_
_
Public Class KAMA
Inherits SeriesCalculatorBaseWithValues
Private _periods As Integer
Private smooth As Double
Private efratio As Double
Private slowLength As Integer
Private fastLength As Integer
Shared fastend As Double
Shared slowend As Double
Private todaysMA As Double = 0.0R
Private signal As Double
Private noise As Double
Private _diff As Queue(Of Double)

' A class level variable to hold the previous day's KAMA calculation.
Private yesterdaysMA As Double = Double.NaN

_
Public Sub New(ByVal periods As Integer)
MyBase.New(1)
' Requires two inputs
If periods <= 0 Then
Throw New ArgumentException("Periods1 must be greater than zero")
End If
_periods = periods
_diff = New Queue(Of Double)(_periods + 1)
fastLength = 2
slowLength = 30
noise = 0
fastend = 2 / CDbl((fastLength + 1))
slowend = 2 / CDbl((slowLength + 1))
End Sub

' Called to calculate a new value for the indicator.
Protected Overloads Overrides Function CalcNewValue(ByVal index As Integer) As Double
' Ensures that we have enough values to calculate the request EMA for the specified period.
If inputs(0).Count = 1 Then
yesterdaysMA = inputs(0).Current
End If

If inputs(0).Count > 1 Then
_diff.Enqueue(Math.Abs(inputs(0).Current - inputs(0).LookBack(1)))
noise += Math.Abs(inputs(0).Current - inputs(0).LookBack(1))
End If

If inputs(0).Count > _periods + 1 Then
signal = Math.Abs(inputs(0).Current - inputs(0).LookBack(_periods))
noise -= _diff.Dequeue()
If noise <> 0.0R Then
efratio = signal / noise
Else
efratio = 1
End If
End If
If inputs(0).Count <= _periods Then
todaysMA = inputs(0).Current
End If
smooth = Math.Pow(efratio * (fastend - slowend) + slowend, 2)
todaysMA = yesterdaysMA + smooth * (inputs(0).Current - yesterdaysMA)
yesterdaysMA = todaysMA

Return todaysMA
End Function

' This function is called when there is a request to recalculate the entire
' series. Since each indicator maintains state, it will be reused and this
' is the opportunity to flush all internal buffers and collections.
Protected Overloads Overrides Sub Reset()
yesterdaysMA = Double.NaN
_diff.Clear()
End Sub

Public Sub New(ByVal periods As Integer, ByVal input As ISeries)

Me.New(periods)

SetInputs(input)
End Sub

End Class
Posted Wednesday March 10 2010
Did you try the C# version and did it work for you? If you create a plugin DLL (using Visual Studio) for the indicator, then you can use the C# indicator code, but still use VB for your system code.

Thanks,
Daniel
pch
Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)
Posted Thursday March 11 2010
Here is an updated version (bug removed) .

I also added two other constructors :
- a second one which allows the passing of the iSeries reference to the KAMA : "public KAMA(int periods, ISeries Input)"
- a third one which gives access to the lower and upper limits of the KAMA, in case someone wants to play with that "public KAMA(int periods, ISeries Input, int fastLength, int slowLength)".

Trying to build a DLL for Blousetrader. The DLL is building OK inside VS.
Now , before posting it, how can I try it inside RE ?
I checked the Reference->Add in the Help Topic, but this seems to refer to Visual Studio, doesn't it ?
So how can I add it to a project inside RE ?
Thanks,

Pierre

Attachments
Kama.cs (345 views, 4.00 KB)

Edited: Thursday March 11 2010 by pch
Posted Thursday March 11 2010
You can also add references to RightEdge projects in much the same way as you do for a Visual Studio project. Right click on the references node in the project tree and click on Add Reference.

Thanks,
Daniel
pch
Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)
Posted Thursday March 11 2010
Quite obvious. Thank you Daniel.

Here is the DLL.

Pierre

Attachments
Kama.zip (225 views, 2.00 KB)
Posted Friday March 12 2010
Cheers guys - thanks for your efforts - I still can't get it to work though - even the new version still produces all NaN when I drag it onto t he chart - havn't tried
it from within the code.

Does it work for you ?
pch
Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)Supreme Being (2,063 reputation)
Posted Friday March 12 2010
Sorry about that. Works A-OK within code (tried 3 constructors without a problem).
Not enough experience of RE to figure out why it doesn't show up in charts.
Will try again tomorrow.

Pierre


Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems