Register Login
Forums    February 6, 2012
TradersStudio Forums
Adaptive Moving Average Help Please
Last Post 11-23-2009 01:00 PM by David Rooke. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Rina Anderson
New Member
New Member
Posts:4

--
11-19-2009 04:43 PM
    I translated the AdaptiveMovAvg function from TradeStation with the migration tool and although the syntax check came out ok, the results are different from what TradeStation gives me on the same data (exported from TradeStation). For example, in TradesStation on a Daily TF.D bar for 9/18/09, the value was 561.42, while in TradersStudio I got a value of 589.03.

    Would someone either post a correct Adaptive Moving Average function or indicator or help me figure out what is wrong with this code?


    'Kaufman's Adaptive Moving Average
    Function AMA(Price As BarArray, EffRatioLength, FastAvgLength, SlowAvgLength) As BarArray
        Dim NetChg
        Dim TotChg
        Dim EffRatio
        Dim ScaledSFSqr
        Dim SlowAvgSF
        Dim FastAvgSF
        Dim SFDiff As BarArray

        If BarNumber=FirstBar Then
            NetChg = 0
            TotChg = 0
            EffRatio = 0
            ScaledSFSqr = 0
            SlowAvgSF = 2/SlowAvgLength+1
            FastAvgSF = 2/FastAvgLength+1
            SFDiff = FastAvgSF-SlowAvgSF
        End If


    'this input assumed to be a constant >= 1
    'Eff = Efficiency
    'Eff = Efficiency, SF = Smoothing Factor
        If CurrentBar = 1 Then
            AMA = Price
        Else
            NetChg = Abs(Price - Price[ EffRatioLength ])
            TotChg = Summation(Abs(Price - Price[1]), EffRatioLength)
            If TotChg > 0 Then
                EffRatio = NetChg / TotChg
            Else
                EffRatio = 0
            End If
    'note that EffRatio is somewhat similar to RSI
            ScaledSFSqr = Sqr(SlowAvgSF + EffRatio * SFDiff)
            AMA = AMA[1] + ScaledSFSqr * (Price - AMA[1])
        End If

    End Function

    I plotted it with the following indicator code:

    Sub AMA_Plot(EffRatioLength as integer, FastAvgLength as integer, SlowAvgLength as integer)
    Dim MyAMA

    MyAMA = AMA(Close, EffRatioLength, FastAvgLength, SlowAvgLength)

        Plot1(MyAMA)

    end sub

    Thanks!
    mur ang
    Advanced Member
    Advanced Member
    Posts:525

    --
    11-19-2009 05:27 PM
    Dim NetChg as bararray
        Dim TotChg   as bararray
        Dim EffRatio  as barrray
        Dim ScaledSFSqr As BarArray
        Dim SlowAvgSF As BarArray
        Dim FastAvgSF As BarArray
        Dim SFDiff As BarArray

    Try this change, make them all bararrays.
    Rina Anderson
    New Member
    New Member
    Posts:4

    --
    11-19-2009 06:02 PM
    No, that didn't help. I tried that and making them each BarArrays one by one, but the values were still messed up.
    Rina Anderson
    New Member
    New Member
    Posts:4

    --
    11-20-2009 06:31 PM
    I got it working!

    I went line by line and checked the calculations. There were several problems:

    1) Varibles declared with calculations lost there parentheses in translation
        SlowAvgSF( 2 / ( SlowAvgLength + 1 ) ),
        FastAvgSF( 2 / ( FastAvgLength + 1 ) ),

    became SlowAvgSF =  2 / SlowAvgLength + 1
    and        FastAvgSF =  2 /  FastAvgLength + 1

    2) These statements had to come out of the If BarNumber=FirstBar Then loop
       SlowAvgSF = 2/(SlowAvgLength+1)
        FastAvgSF = 2/(FastAvgLength+1)
        SFDiff = FastAvgSF-SlowAvgSF

    3) Lastly, the TradeStation function Square does not have a corresponding function in TradersStudio Basic and I mistakenly used Sqr  instead.

    So, here is the working code for anyone who would like to use it:

    'Kaufman's Adaptive Moving Average
    Function AMA( EffRatioLength as Integer, FastAvgLength as Integer, SlowAvgLength as Integer) As BarArray
        Dim NetChg As Double
        Dim TotChg As Double
        Dim EffRatio As BarArray
        Dim ScaledSFSqr As Double
        Dim SlowAvgSF As Double
        Dim FastAvgSF As Double
        Dim SFDiff As Double
        Dim Price As BarArray

        Price = Input1  

        If BarNumber=FirstBar Then
            NetChg = 0
            TotChg = 0
            EffRatio = 0
            ScaledSFSqr = 0
        End If    
           
        SlowAvgSF = 2/(SlowAvgLength+1)
        FastAvgSF = 2/(FastAvgLength+1)
        SFDiff = FastAvgSF-SlowAvgSF



    'this input assumed to be a constant >= 1
    'Eff = Efficiency
    'Eff = Efficiency, SF = Smoothing Factor
        If CurrentBar = 1 Then
            AMA = Price
        Else
           NetChg = Abs(Price - Price[ EffRatioLength ])
           TotChg = Summation(Abs(Price - Price[1]), EffRatioLength)
            If TotChg > 0 Then
               EffRatio = NetChg / TotChg
            Else
               EffRatio = 0
            End If
    'note that EffRatio is somewhat similar to RSI
            ScaledSFSqr = (SlowAvgSF + EffRatio * SFDiff)^2
            AMA = AMA[1] + ScaledSFSqr * (Price - AMA[1])
        End If

    End Function



    David Rooke
    New Member
    New Member
    Posts:22

    --
    11-23-2009 01:00 PM
    Call the code from a session and then step through it in debug mode. All should be revealed.
    You are not authorized to post a reply.


     TradersStudio, Inc. ® Copyright 2004-2012 All Rights Reserved   Terms Of Use  Privacy Statement