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 |

Adaptive Moving Average Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
Mark17
Posted : Friday, October 9, 2009 5:35:58 AM
Registered User
Joined: 7/1/2009
Posts: 97
Searching for "adaptive moving average" returned "an error occurred in the database."  Before I go into detail about what I'm looking for, can you tell me if there's a pre-existing thread on anything related to Perry Kaufman's AMA?  Thanks.
Bruce_L
Posted : Friday, October 9, 2009 12:27:04 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
The only topic of which I am aware about the Kaufman Adaptive Moving Average was for creating a Custom Code Block (Kaufman Adaptive moving average). I believe replacing everything in the Code tab of the RealCode Editor with the following should create a RealCode Indicator of a KAMA of Price:

'# Period = UserInput.Integer = 10
'# Cumulative
Static KAMA As Single
Static Sum As Single
If Period >= 1 Then
    If CurrentIndex >= Period Then
        Sum += System.Math.Abs(Price.Last - Price.Last(1)) - _
            System.Math.Abs(Price.Last(Period - 1) - Price.Last(Period))
        Dim ER As Single = 0
        If Sum > 0 Then
            ER = System.Math.Abs(Price.Last - Price.Last(Period - 1)) / Sum
        End If
        Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
        KAMA = (1 - Alpha) * KAMA + Alpha * Price.Last
    Else If isFirstBar Then
        Sum = 0
        KAMA = Price.Last
    Else
        Sum += System.Math.Abs(Price.Last - Price.Last(1))
        Dim ER As Single = 0
        If Sum > 0 Then
            ER = System.Math.Abs(Price.Last - Price.Last(CurrentIndex)) / Sum
        End If
        Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
        KAMA = (1 - Alpha) * KAMA + Alpha * Price.Last
    End If
    Plot = KAMA
Else
    Plot = Single.NaN
End If

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Mark17
Posted : Sunday, October 11, 2009 8:46:38 AM
Registered User
Joined: 7/1/2009
Posts: 97
Great... now is there an easy way to create rules such as when price crosses above/below the AMA or when other MAs in that pain cross over it?  When I open a rule box for either price history or the AMA, the AMA or price history, respectively, are not identified as rule triggers (I'm guessing the reason is because the AMA was synthetically created in RC).
Mark17
Posted : Sunday, October 11, 2009 8:47:25 AM
Registered User
Joined: 7/1/2009
Posts: 97
"Pane," of course--sorry, it's Sunday morning.  :)
Bruce_L
Posted : Monday, October 12, 2009 11:36:00 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Drag and Drop the AMA to the Price History Pane and select Overlay to get it in the same Pane. Then right-click on the AMA and select Scaling | Scale With | Price History to get it in the same Scale.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Mark17
Posted : Tuesday, October 13, 2009 11:02:14 AM
Registered User
Joined: 7/1/2009
Posts: 97
Thanks!
thnkbigr
Posted : Tuesday, December 1, 2009 11:32:45 PM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207
Bruce 

Can this be written for Indicators

I dragged and dropped the indicator and changed all the Price.Last ro AA.Value  but doesn't seem accurate

'# AA = indicator.MyAD5WithVolume
'# Period = UserInput.Integer = 10
'# Cumulative
Static KAMA As Single
Static Sum As Single
If Period >= 1 Then
 If CurrentIndex >= Period Then
  Sum += System.Math.Abs(AA.Value - AA.Value(1)) - _
   System.Math.Abs(AA.Value(Period - 1) - AA.Value(Period))
  Dim ER As Single = 0
  If Sum > 0 Then
   ER = System.Math.Abs(AA.Value - AA.Value(Period - 1)) / Sum
  End If
  Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
  KAMA = (1 - Alpha) * KAMA + Alpha * AA.Value
 Else If isFirstBar Then
  Sum = 0
  KAMA = AA.Value
 Else
  Sum += System.Math.Abs(AA.Value - AA.Value(1))
  Dim ER As Single = 0
  If Sum > 0 Then
   ER = System.Math.Abs(AA.Value - AA.Value(CurrentIndex)) / Sum
  End If
  Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
  KAMA = (1 - Alpha) * KAMA + Alpha * AA.Value
 End If
 Plot = KAMA
Else
 Plot = Single.NaN
End If
Bruce_L
Posted : Wednesday, December 2, 2009 9:41:02 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
thnkbigr,
I'm not sure exactly what the issue might be (I don't know anything about the Indicator being averaged), but it might be due to a mismatch between the number of Price Bars and the number of Bars in your Indicator. If that is the case, using AutoLoop = False in the Class tab might be a better option (replace everything below the Inherits line in the Class tab of a RealCod Indicator with the following):

    Sub New
        AutoLoop = False
        '# AA = indicator.MyAD5WithVolume
        '# Period = UserInput.Integer = 10
        '# Cumulative
    End Sub
    Public Overrides Function Plot() As System.Single
        If Period >= 1 Then
            Dim KAMA As Single = AA.Bar.Value(0)
            AddToOutput(AA.Bar.DateValue(0), KAMA)
            If AA.Bar.Count >= 2 Then
                Dim Sum As Single = 0
                For i As Integer = 1 To System.Math.Min(Period, AA.Bar.Count) - 1
                    Sum += System.Math.Abs(AA.Bar.Value(i) - AA.Bar.Value(i - 1))
                    Dim ER As Single = 0
                    If Sum > 0 Then
                        ER = System.Math.Abs(AA.Bar.Value(i) - AA.Bar.Value(0)) / Sum
                    End If
                    Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
                    KAMA = (1 - Alpha) * KAMA + Alpha * AA.Bar.Value(i)
                    AddToOutput(AA.Bar.DateValue(i), KAMA)
                Next
                If AA.Bar.Count >= Period + 1 Then
                    For i As Integer = Period To AA.Bar.Count - 1
                        Sum += System.Math.Abs(AA.Bar.Value(i) - AA.Bar.Value(i - 1)) - _
                            System.Math.Abs(AA.Bar.Value(i - Period) - _
                            AA.Bar.Value(i - Period + 1))
                        Dim ER As Single = 0
                        If Sum > 0 Then
                            ER = System.Math.Abs(AA.Bar.Value(i) - _
                                AA.Bar.Value(i - Period + 1)) / Sum
                        End If
                        Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
                        KAMA = (1 - Alpha) * KAMA + Alpha * AA.Bar.Value(i)
                        AddToOutput(AA.Bar.DateValue(i), KAMA)
                    Next
                End If
            End If
        End If
    End Function
End Class

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
thnkbigr
Posted : Wednesday, December 2, 2009 11:33:27 AM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207

Bruce,

I have never done a Class tab yet no matter what I do I get errors

This is what comes up when I click the class tab can you highlight what I need to erase and paste the above.

thx

<WBIGuid("41edfbc8-8f4f-4876-b7de-285c3c216832"),FriendlyName("RealCodeIndicator"),BlockTemplateEmptyMethodOnly()> _
Public partial Class RealCodeIndicator
inherits RealCodeIndicator_base
    Public Overrides function Plot() as System.Single
'***************************************
'* Example: Returns the net change     *
'* Plot = Price.Close - Price.Close(1) *
'***************************************

 

end function
End Class

Bruce_L
Posted : Wednesday, December 2, 2009 11:39:14 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Replace everything below the Inherits line in the Class tab of a RealCod Indicator means you need to keep:

<WBIGuid("41edfbc8-8f4f-4876-b7de-285c3c216832"),FriendlyName("RealCodeIndicator"),BlockTemplateEmptyMethodOnly()> _
Public partial Class RealCodeIndicator
inherits RealCodeIndicator_base

And Replace:

    Public Overrides function Plot() as System.Single
'***************************************
'* Example: Returns the net change     *
'* Plot = Price.Close - Price.Close(1) *
'***************************************

end function
End Class

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
thnkbigr
Posted : Wednesday, December 16, 2009 11:59:19 PM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207
Can I plot the Adaptive MA on High and Lows 

I tried the following but is this accurate?

'# Period = UserInput.Integer = 10
'# Cumulative
Static KAMA As Single
Static Sum As Single
If Period >= 1 Then
 If CurrentIndex >= Period Then
  Sum += System.Math.Abs(Price.Last - Price.Last(1)) - _
   System.Math.Abs(Price.Last(Period - 1) - Price.Last(Period))
  Dim ER As Single = 0
  If Sum > 0 Then
   ER = System.Math.Abs(Price.Last - Price.Last(Period - 1)) / Sum
  End If
  Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
  KAMA = (1 - Alpha) * KAMA + Alpha * Price.High
 Else If isFirstBar Then
  Sum = 0
  KAMA = Price.High
 Else
  Sum += System.Math.Abs(Price.High - Price.High(1))
  Dim ER As Single = 0
  If Sum > 0 Then
   ER = System.Math.Abs(Price.High - Price.High(CurrentIndex)) / Sum
  End If
  Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
  KAMA = (1 - Alpha) * KAMA + Alpha * Price.High
 End If
 Plot = KAMA
Else
 Plot = Single.NaN
End If
Bruce_L
Posted : Thursday, December 17, 2009 8:18:30 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I'm suspecting you would want to replace every instance of Price.Last with Price.High:

'# Period = UserInput.Integer = 10
'# Cumulative
Static KAMA As Single
Static Sum As Single
If Period >= 1 Then
    If CurrentIndex >= Period Then
        Sum += System.Math.Abs(Price.High - Price.High(1)) - _
            System.Math.Abs(Price.High(Period - 1) - Price.High(Period))
        Dim ER As Single = 0
        If Sum > 0 Then
            ER = System.Math.Abs(Price.High - Price.High(Period - 1)) / Sum
        End If
        Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
        KAMA = (1 - Alpha) * KAMA + Alpha * Price.High
    Else If isFirstBar Then
        Sum = 0
        KAMA = Price.High
    Else
        Sum += System.Math.Abs(Price.High - Price.High(1))
        Dim ER As Single = 0
        If Sum > 0 Then
            ER = System.Math.Abs(Price.High - Price.High(CurrentIndex)) / Sum
        End If
        Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
        KAMA = (1 - Alpha) * KAMA + Alpha * Price.High
    End If
    Plot = KAMA
Else
    Plot = Single.NaN
End If

But I suppose it is possible that you would want the Alpha based on Price.Last instead of Price.High (if you have multiple Moving Averages and want to use the same Alpha for all of the MAs, you would need to choose something to use that was common to all of the MAs for example - although I'm not sure if this is desirable or not). In such a case, Price.Last might be as good a choice as any:

'# Period = UserInput.Integer = 10
'# Cumulative
Static KAMA As Single
Static Sum As Single
If Period >= 1 Then
    If CurrentIndex >= Period Then
        Sum += System.Math.Abs(Price.Last - Price.Last(1)) - _
            System.Math.Abs(Price.Last(Period - 1) - Price.Last(Period))
        Dim ER As Single = 0
        If Sum > 0 Then
            ER = System.Math.Abs(Price.Last - Price.Last(Period - 1)) / Sum
        End If
        Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
        KAMA = (1 - Alpha) * KAMA + Alpha * Price.High
    Else If isFirstBar Then
        Sum = 0
        KAMA = Price.High
    Else
        Sum += System.Math.Abs(Price.Last - Price.Last(1))
        Dim ER As Single = 0
        If Sum > 0 Then
            ER = System.Math.Abs(Price.Last - Price.Last(CurrentIndex)) / Sum
        End If
        Dim Alpha As Single = (ER * .6015 + .0645) ^ 2
        KAMA = (1 - Alpha) * KAMA + Alpha * Price.High
    End If
    Plot = KAMA
Else
    Plot = Single.NaN
End If

-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.