The Comparative Strength Indicator
measures the relative momentum of the two stocks over a period of time.
Script below requires at least one overlay in the current view. If
more than one is present the code selects the first one in the price
series collection. To use, copy the code into a module
and create a macro having the following command line:
mymodule.CompStrength
periods
where
mymodule
=
Name of the module the
code was copied to.
periods
=
Number of periods used
in the calculation.
Sub CompStrength(pPeriods)
Dim vw 'View
Dim ds 'DataSeries to hold formula
Dim idx 'New chart index
Function
fnCSI(pPeriods)
Dim psA 'Chart price series
Dim psB '1st overlay price series
Dim pA1, pA2 'Price quotes for chart price series
Dim pB1, pB2 'Price quotes for overlay
Dim ds 'Data series
Dim pd 'Data point
Dim vw 'Current view
Dim i
Set vw = Views.CurrentView
If vw.PSCollection.Count < 2 Then
MsgBox "The Comparative Strength indicator requires an overlay."
Exit Function
End If
Set psA = vw.PSCollection.ChartSeries
For i = 1 to vw.PSCollection.Count
If Not vw.PSCollection(i) Is psA Then
Set psB = vw.PSCollection(i)
Exit For
End If
Next
Set ds = vw.DSCollection.CreateDataSeries()
For i = 1 to pPeriods
Set dp = vw.DSCollection.CreateDataPoint()
dp.Value = 0
dp.Excluded = True
ds.DataPoints.Add dp
Next
For i = pPeriods + 1 to psA.Count
pA1 = psA.Prices(i - pPeriods).ClosePrice
pA2 = psA.Prices(i).ClosePrice
pB1 = psB.Prices(i - pPeriods).ClosePrice
pB2 = psB.Prices(i).ClosePrice
Set dp = vw.DSCollection.CreateDataPoint()
On Error Resume Next
dp.Value = (pA2/pA1) * (pB1/pB2) - 1
If Err <> 0 Then
dp.Value = 0
pd.Excluded = True
Err = 0
End If
ds.DataPoints.Add dp
Next
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.