Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Function MovingAverage(theRange As Range, LastN As Long) As Variant

Dim vArr As Variant

Dim j As Long
Dim nFound As Long
Dim dSum As Double

On Error GoTo Fail


MovingAverage = CVErr(xlErrNA)
'
' handle entire column reference
'
vArr = Intersect(Application.Caller.Parent.UsedRange, theRange).Value2

If IsArray(vArr) And LastN > 0 Then


For j = UBound(vArr) To 1 Step -1
' skip empty/uncalculated
If Not IsEmpty(vArr(j, 1)) Then
' look for valid numbers
If IsNumeric(vArr(j, 1)) Then
If Len(Trim(CStr(vArr(j, 1)))) > 0 Then
nFound = nFound + 1
If nFound <= LastN Then
dSum = dSum + CDbl(vArr(j, 1))
Else
Exit For
End If
End If
End If
End If
Next j

If nFound >= LastN Then MovingAverage = dSum / LastN

End If
Exit Function
Fail:
MovingAverage = CVErr(xlErrNA)
End Function

http://stackoverflow.com/questions/5283466/calculate-moving-average-in-excel
Public Function SMA_EOD(Commodity, TrdDay, period As Integer)

Dim rst As DAO.Recordset


Dim sql As String
Dim ma As Double
Dim n As Integer

sql = "SELECT [COMM_SYMB], [TRD_DAY], [CLOSE] FROM fct_EOD" & _


" WHERE [fct_EOD]![COMM_SYMB] ='" & Commodity & "'" & _
" AND [fct_EOD]![TRD_DAY] <= #" & TrdDay & "#" & _
" ORDER BY [fct_EOD]![TRD_DAY];"

Set rst = CurrentDb.OpenRecordset(sql)


rst.MoveLast
For n = 0 To period - 1
If rst.BOF Then
SMA_EOD = 0
Exit Function
Else
ma = ma + rst.Fields("CLOSE")
End If
rst.MovePrevious
Next n
rst.Close
SMA_EOD = ma / period

Public Function SMA_EOD(Commodity, TrdDay, period As Integer)

Dim rst As DAO.Recordset


Dim sql As String
Dim ma As Double
Dim n As Integer

sql = "SELECT [COMM_SYMB], [TRD_DAY], [CLOSE] FROM fct_EOD" & _


" WHERE [fct_EOD]![COMM_SYMB] ='" & Commodity & "'" & _
" AND [fct_EOD]![TRD_DAY] <= #" & TrdDay & "#" & _
" ORDER BY [fct_EOD]![TRD_DAY];"

Set rst = CurrentDb.OpenRecordset(sql)


rst.MoveLast
For n = 0 To period - 1
If rst.BOF Then
SMA_EOD = 0
Exit Function
Else
ma = ma + rst.Fields("CLOSE")
End If
rst.MovePrevious
Next n
rst.Close
SMA_EOD = ma / period

End Function
http://stackoverflow.com/questions/16561444/pass-sas-macro-variables-to-vb-script-as-parameters

http://www.stat.pitt.edu/stoffer/tsa2/Rissues.htm

>#plot data
>latihan3$World_Oil_Prices <- ts(latihan3$World_Oil_Prices,start=c(1996,1),
freq=12) # data diubah menjadi bertipe time series
>ts.plot(latihan3$World_Oil_Prices,col="blue",main="Time Series Plot")
> adf.test(latihan3$World_Oil_Prices) # uji stasioneritas
> win.graph()
> par(mfrow=c(2,1))
> acf(latihan3$World_Oil_Prices,na.action=na.pass) #plot ACF/PACF
> pacf(latihan3$World_Oil_Prices,na.action=na.pass) #plot ACF/PACF
> # transformasi difference dan plot
>latihan3$World_Oil_Prices.Diff1 <- diff(latihan3$World_Oil_Prices,
differences=1)

You might also like