Wednesday, September 08, 2010     | Register
TradersStudio Forums
Subject: Adaptive Moving Average Help Please

You are not authorized to post a reply.   
Author Messages
Lotus
Posts:4

11-19-2009 4:43 PM Alert 
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Ώ]), 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Ώ] + ScaledSFSqr * (Price - AMAΏ])
    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!
murray
Posts:519

11-19-2009 5:27 PM Alert 
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.
Lotus
Posts:4

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

11-20-2009 6:31 PM Alert 
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Ώ]), 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Ώ] + ScaledSFSqr * (Price - AMAΏ])
    End If

End Function



quest
Posts:20

11-23-2009 1:00 PM Alert 
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.
Forums > TradersStudio 2.x > TradersStudio Basic > Adaptive Moving Average Help Please



ActiveForums 3.6
TradersStudio® Copyright 2004-2010 All Rights Reserved   |  Privacy Statement  |  Terms Of Use