Correlation
| The Correlation
function measures the correlation between two data series over a period
of time. To use, copy the code into a module and insert as a
Scripted DSFunction with the following syntax for the CommandString:
=mymodule.Correlation("dsname1", "dsname2", periods)
where |
| |
mymodule |
= |
Name of the module the
code was copied to. |
| |
dsname1 |
= |
Name of one data series. |
| |
dsname2 |
= |
Name of the other data series. |
| |
periods |
= |
Number of periods used
in the calculation.
|
Function Correlation(pstrDSName1, pstrDSName2, pintPeriods)
Dim vw
Dim ds1, ds2, ds3
Dim dp1, dp2, dp3
Dim v1, v2
Dim sumx, sumy, sumxx, sumyy, sumxy
Dim Sxx, Syy, Sxy
Dim i, p1, p2, n
Dim txt
Set vw = Views.CurrentView
Set ds1 = vw.DSCollection.ItemNamed(pstrDSName1)
If ds1 Is Nothing Then
MsgBox "Can not find data series " &
pstrDSName1 & ".", vbExclamation, "Script: Correlation"
Exit Function
End If
Set ds2 = vw.DSCollection.ItemNamed(pstrDSName2)
If ds2 Is Nothing Then
MsgBox "Can not find data series " &
pstrDSName2 & ".", vbExclamation, "Script: Correlation"
Exit Function
End If
txt = "Correlation("
txt = txt & chr(34) & pstrDSName1 & chr(34) & ", "
txt = txt & chr(34) & pstrDSName2 & chr(34) & ", "
txt = txt & pintPeriods & ")"
Set ds3 = vw.DSCollection.CreateDataSeries(txt)
ds3.Formula = "=Indicators." & ds3.Name
'NOTE: This procedure must be in a module named Indicators
'Ignore excluded data points that may begin either of the two
series.
p1 = ds1.FirstIncludedPeriod
p2 = ds2.FirstIncludedPeriod
If p2 > p1 Then
p1 = p2
End If
For i = 1 To p1 - 1
Set dp3 =
vw.DSCollection.CreateDataPoint()
dp3.Excluded = True
ds3.Datapoints.Add dp3
Next
'Get sums for the initial moving period but exclude data
points
For i = p1 To p1+ pintPeriods -1
Set dp1 = ds1.Datapoints(i)
Set dp2 = ds2.Datapoints(i)
sumx = sumx + dp1.Value
sumy = sumy + dp2.Value
sumxx = sumxx + dp1.Value^2
sumyy = sumyy + dp2.Value^2
sumxy = sumxy + dp1.Value * dp2.Value
Set dp3 =
vw.DSCollection.CreateDataPoint()
If i = p1 + pintPeriods - 1 Then
Sxx = n*
sumxx - sumx^2
Syy = n*
sumyy - sumy^2
Sxy = n*
sumxy - sumx*sumy
On Error
Resume Next
dp3.Value =
Sxy/(Sxx*Syy)^.5
If Err <> 0
Then
dp3.Excluded = True
Err = 0
Else
dp3.Excluded = False
End If
Else
dp3.Excluded
= True
End If
ds3.Datapoints.Add dp3
Next
'Calculate the running correlation coefficient for the rest
of series
n = pintPeriods
For i = p1 + pintPeriods To ds1.Count
Set dp1 = ds1.Datapoints(i)
Set dp2 = ds2.Datapoints(i)
v1 = ds1.DataPoints(i - n).Value
v2 = ds2.DataPoints(i - n).Value
sumx = sumx + dp1.Value - v1
sumy = sumy + dp2.Value - v2
sumxx = sumxx + dp1.Value^2 - v1^2
sumyy = sumyy + dp2.Value^2 - v2^2
sumxy = sumxy + dp1.Value * dp2.Value
- v1 * v2
Sxx = n* sumxx - sumx^2
Syy = n* sumyy - sumy^2
Sxy = n* sumxy - sumx*sumy
Set dp3 =
vw.DSCollection.CreateDataPoint()
dp3.Value = Sxy/(Sxx*Syy)^.5
ds3.Datapoints.Add dp3
Next
ds3.Mean = DSFunctions.DSAvg(ds3)
ds3.StdDeviation = DSFunctions.DSStdDev(ds3)
Set Correlation = ds3
End Function
Back DISCLAIMER:
All examples are intended
for illustrative purposes only . No representations or warranties of any
kind are made about their effectiveness
in making trading decisions.
|