PureBytes Links
Trading Reference Links
|
Rudolf,
for the RSI calculation I use the VB routine below. It's a Wilder's smoothed
percentage of Up-days vs. Down-days. Achelis says in his book "Technical
Analysis from A to Z" that "The RSI is a fairly simple formula, but
difficult to explain without pages of examples. Refer to Wilder's book for
additional calculation information. The basic formula is:
100 - ( 100 / (1 + (U/D))) Where: U = An average of upward price change, D
= An average of downward price change."
The result from a straightforward implementation of the above does not match
the results from Metastock, so after a bit more research, I found that U and
D had to be smoothed by Wilder's smoothing, which in fact is the quite the
same as exponential smoothing.
You can surely identify the relevant code and find another definition of Up
and Down days using O-H-L.
For your investigations, I have also included the VB code for the Relativ
Momentum Index, Chandes Momentum Oscillator and some other studies on
Momentum, Velocity and Acceleration.
I would be interested in your findings. If you need more on this, just yell.
Good luck,
Andreas
'Welles Wilder's Relative Strength Index.
Public Sub djinRSI(ByRef dataIn() As Double, ByRef dataOut() As Double,
ByVal RSIperiod As Long)
Dim Up As Double, Down As Double
Dim upgrade As Double
Dim i As Long
'Make sure array are of same size
ReDim dataOut(UBound(dataIn))
If RSIperiod = 0 Or RSIperiod >= UBound(dataIn) Then Exit Sub
Up = 0: Down = 0: dataOut(1) = 0
For i = 1 To RSIperiod - 1
dataOut(i) = 0
If dataIn(i) > dataIn(i - 1) Then Up = Up + dataIn(i) - dataIn(i -
1)
If dataIn(i) < dataIn(i - 1) Then Down = Down + dataIn(i - 1) -
dataIn(i)
Next i
Up = Up / (RSIperiod - 1)
Down = Down / (RSIperiod - 1)
For i = RSIperiod To UBound(dataIn)
upgrade = dataIn(i) - dataIn(i - 1)
Select Case upgrade
Case Is > 0
Up = (Up * (RSIperiod - 1) + Abs(upgrade)) / RSIperiod
Down = (Down * (RSIperiod - 1) + 0) / RSIperiod
Case Is = 0
Up = (Up * (RSIperiod - 1) + 0) / RSIperiod
Down = (Down * (RSIperiod - 1) + 0) / RSIperiod
Case Is < 0
Up = (Up * (RSIperiod - 1) + 0) / RSIperiod
Down = (Down * (RSIperiod - 1) + Abs(upgrade)) / RSIperiod
End Select
' Strong up-trending may lead to Down equals Zero
If Down = 0 Then
dataOut(i) = 100
Else
dataOut(i) = 100 - (100 / (1 + (Up / Down)))
End If
Next i
End Sub
' Relative Momentum Index
' Implemententation: RSI(C / Ref(C, -B), A)
Public Sub djinRMI(ByRef dataIn() As Double, ByRef dataOut() As Double,
ByVal Lb As Long, ByVal Periods As Long)
Dim i As Long
Dim arrTmp() As Double
ReDim arrTmp(UBound(dataIn))
For i = Lb To UBound(dataIn) ' Build momentum array
arrTmp(i) = dataIn(i) / dataIn(i - Lb)
Next i
Call djinRSI(arrTmp, dataOut, Periods)
End Sub
'Chande Momentum Oscillator
'This is a momentum oscillator that is bounded between -1 and 1.
'It is calculated over some period on time (N) and is defined as follows:
'
'CMO = (UpSum - DownSum) / (UpSum + DownSum)
'
'Where:
'
'UpSum = Sum of (Today's Close - Yesterday's Close) over N days if Today's
Close > Yesterday's Close
'DownSum = Sum of (Yesterday's Close - Today's Close) over N days if Today's
Close < Yesterday's Close
'
'It can be filtered using a threshold. If ( Today's Close - Yesterday's
Close ) > Threshold
'then that difference is added to UpSum, otherwise zero is added.
'For down days, if ( Yesterday's Close - Today's Close ) > Threshold then
that difference is added
'to DownSum, otherwise zero is added. This reduces noise in the calculation
due to small differences
'in daily closes and results in more significant indications of momentum
change.
'
'Reference:
'Chande, Tushar S. and Kroll, Stanley.
'The New Technical Trader, New York: John Wiley, 1994.
Public Sub djinCMO(ByRef dataIn() As Double, ByRef dataOut() As Double,
ByVal Period As Long)
Dim i As Long, j As Long
Dim dblUp As Double, dblDown As Double
Dim dblTmp As Double
ReDim dataOut(UBound(dataIn))
For i = Period To UBound(dataIn)
dblUp = 0: dblDown = 0
For j = 0 To Period - 2
dblTmp = dataIn(i - j) - dataIn(i - j - 1)
Select Case dblTmp
Case Is > 0
dblUp = dblUp + dblTmp
Case Is < 0
dblDown = dblDown + Abs(dblTmp)
End Select
Next j
dataOut(i) = (dblUp - dblDown) / (dblUp + dblDown)
Next i
End Sub
' Momentum: the difference between today's close and the close n1 periods
back.
'
' momentum = c(1) - c(n1)
'
' Velocity: the slope of the line between today's momentum and the momentum
n2 period back,
'
' velocity = momentum / n2
'
' Acceleration: the slope of the line between today's velocity and the
velocity of n3 periods back,
'
' acceleration = velocity / n3
Public Sub djinMomentum(ByRef eq As eqData_struct, ByRef dataOut() As
Double, ByVal n As Long)
Dim i As Long
ReDim dataOut(UBound(eq.fOpen))
For i = n To UBound(eq.fOpen)
dataOut(i) = eq.fClose(i) - eq.fClose(i - n)
Next i
End Sub
Public Sub djinVelocity(ByRef dataIn() As Double, ByRef dataOut() As Double,
ByVal n As Long)
Dim i As Long
ReDim dataOut(UBound(dataIn))
For i = n To UBound(dataIn)
dataOut(i) = dataIn(i) - dataIn(i - n)
Next i
End Sub
Public Sub djinAcceleration(ByRef dataIn() As Double, ByRef dataOut() As
Double, ByVal n As Long)
Call djinVelocity(dataIn, dataOut, n)
End Sub
> -----Ursprungliche Nachricht-----
> Von: owner-metastock@xxxxxxxxxxxxx
> [mailto:owner-metastock@xxxxxxxxxxxxx]Im Auftrag von rudolf stricker
> Gesendet: Mittwoch, 6. Februar 2002 18:16
> An: metastock@xxxxxxxxxxxxx
> Betreff: Re: AW: momentum indicators, using also o-h-l
>
>
> On Tue, 5 Feb 2002 06:27:39 +0100, you wrote:
>
> >'Chande's Dynamic Momentum Index
> >'DMI is a momentum index that uses a variable length (number of days)
> >determined by the volatility
> >'in price. If volatility is low, the calculation uses a long period. If
> >volatility is high, the
> >'calculation uses a short period.
> >'
> >'The number of days is determined as follows:
> >'
> >'Std5 = Standard Deviation of the Close over past 5 days
> >'AvgStd = 10 day Average of Std5
> >'V = Today 's Std5 / AvgStd
> >'TD = Int(14 / V)
> >'
> >'Note that if the Std5 is greater than AvgStd then V will be
> greater than 1
> >and Td will be
> >'less than 14. If Std5 is less than AvgStd then V will be less
> than 1 and Td
> >will be greater
> >'the 14. Td is limited to the range 5 to 30.
> >'
> >'Td is used as the period for calculating RSI ( Relative
> Strength Index ).
>
> Thank you much Andreas for posting the formulas for the time period
> calculation of the DMI ! Should be easy for me to use them in Excel.
> Unfortunately, I couldn't find any formulas for the RSI itself in my
> mail archive from this list. So, please, do you have also comparable
> info on the RSI formulas? - Would be of much help ...
> Then I could try to integrate o-h-l, and I'd be glad to post the
> resulting formulas here, if someone is interested.
>
> mfg rudolf stricker
> | Disclaimer: The views of this user are strictly his own.
>
|