Hello everyone!
I modified the sample Visual Basic code that was given to retrieve historical data via COM so that I could compute RSI on the fly from historical 5-minute bars. It would start with the first bar returned (the oldest) and calculate all the way to the last bar. I assumed a 14-period (15 data points) period for Wilder RSI.
I used the formula taken from here for Wilder RSI:
http://www.stockcharts.com/education/IndicatorAnalysis/indic_RSI.html. However, my results don't seem to be returning the same RSI as sites like prophet.net and other technical analysis tools. Also, when I put in a longer timeframe for the data pulled (like 5 days), the RSI remains the same although it should be different since it is smoothed. I have pasted the code below. Maybe someone could help me out here? I am not sure what I am doing wrong!
This is my modified version of the original history.frm. I added a list box, List1, that contains ticker symbols. The code of the form is below (with comments). Most of the code is in
CalcRS and
hlookup_MinuteCompleted.
For whatever reason, the RSI is not changing when I give it more data (5 days instead of, say, 2) which is bizaare. I must be doing something wrong. Does anybody have any experience with this that could help me?
Option Explicit
Dim WithEvents hlookup As HistoryLookup ' History COM object
Dim loaded As Boolean
Dim curSymbol As String
Dim globSpun As Boolean
Dim lAG, lAL
Dim okayz As Boolean
'-----------------------
' Registers the application with IQFeed, and starts the IQConnect data service
' Also populates the HistoryType combo box with type of history requests
Private Sub Form_Load()
loaded = False
Dim strName As String
Dim strVersion As String
Dim strKey As String
strName = "IQFEED_DEMO"
strVersion = "1"
strKey = "1"
loaded = IQFeedY1.RegisterClientApp(strName, strVersion, strKey)
Set hlookup = New HistoryLookup
Go.Enabled = False
lAG = 0
lAL = 0
End Sub
Private Sub Form_Unload(icancel As Integer)
Set hlookup = Nothing
IQFeedY1.RemoveClientApp
End Sub
'-----------------------
'Initialize COM object and make the request when user clicks on the go button.
Private Sub Go_Click()
Dim i As Integer
If Not loaded Then
Set hlookup = New HistoryLookup
End If
okayz = True
For i = 1 To List1.ListCount
' Wait for the last request to finish
While okayz = False
DoEvents
Wend
' List1 contains a list of ticker symbols
curSymbol = List1.List(i)
okayz = False
hlookup.RequestMinuteHistory curSymbol, 1, 5
Next i
End Sub
Private Sub hlookup_AbortedLoad(ByVal bsReason As String)
MsgBox bsReason
End Sub
' This function calculates the RS
Private Function CalcRS(closes As Variant)
Dim UpTotal, DownTotal
Dim i As Integer
Dim Diff
Dim AvgGain, AvgLoss, RS
UpTotal = 0
DownTotal = 0
' We start at 2 since the first value is only used for reference
For i = 2 To 15
Diff = closes(i) - closes(i - 1)
If Diff > 0 Then
' Up Session
UpTotal = UpTotal + Abs(Diff)
ElseIf Diff < 0 Then
' Down Session
DownTotal = DownTotal + Abs(Diff)
Else
' Flat leave it the same
End If
Next i
' Compute "average" gain & loss
AvgGain = UpTotal / 14
AvgLoss = DownTotal / 14
' Set global variables
lAG = AvgGain
lAL = AvgLoss
' Return RS (AvgGain / AvgLoss)
CalcRS = AvgGain / AvgLoss
End Function
'-----------------------
' If minute request databars are returned then display them
Private Sub hlookup_MinuteCompleted(ByVal lMinutesLoaded As Long, ByVal aTime As Variant, ByVal aOpen As Variant, ByVal aClose As Variant, ByVal aHigh As Variant, ByVal aLow As Variant, ByVal aVolume As Variant, ByVal IntVolume As Variant)
Dim data As String
Dim i, c, n As Integer ' Counters
Dim closes(15) ' Variable holding closing prices
Dim rsi ' Actual RSI computed at end from smooth RSI
Dim offset As Integer
Dim pGain, pLoss ' Previous gain & loss
Dim srs ' Will contain smoothed RS
pGain = 0
pLoss = 0
' Set closing prices array to get first RS from CalcRS()
For i = 1 To 15
closes(i) = aClose(i)
Next i
' Calculate first RS and set global variables
CalcRS (closes)
' Now, we start from the second closing price instead of the first
offset = 2
' For each data point, run the CalcRS function and smooth it
For i = 16 To lMinutesLoaded
c = 1
' Set closes array from offset to the closing price data point we're currently at
For n = offset To i
closes(c) = aClose(n)
c = c + 1
Next n
' Remember previous gain and loss
pGain = lAG
pLoss = lAL
' Set new gain/loss into lAG and lAL. This is why we saved them.
CalcRS (closes)
' Now calculate the smoothed RS.
srs = (((pGain * 13) + lAG) / 14) / (((pLoss * 13) + lAL) / 14)
' Increase offset so we start off with the next closing price
offset = offset + 1
Next i
' Get the actual RSI value from the smoothed RS
rsi = 100 - (100 / (1 + srs))
HistoryList.Text = HistoryList.Text & "RSI for " & CStr(curSymbol) & ": " + CStr(rsi) + vbCrLf
' Set this variable to TRUE so that the requesting loop knows its okay to go on
okayz = True
End Sub
'-----------------------
' If day request databars are returned then display them
Private Sub hlookup_DayCompleted(ByVal lDaysLoaded As Long, ByVal aTime As Variant, ByVal aOpen As Variant, ByVal aClose As Variant, ByVal aHigh As Variant, ByVal aLow As Variant, ByVal aVolume As Variant)
Dim data As String
Dim i As Integer
For i = 1 To lDaysLoaded
data = data & aTime(i) & "," & aOpen(i) & "," & aClose(i) & "," & aHigh(i) & "," & aLow(i) & "," & aVolume(i) & vbCrLf
Next i
HistoryList.Text = data
End Sub
'-----------------------
' Receives the status of the IQConnect data service
' status = 0, Then IQConnect is unavailable
' status = -1, Then IQConnect could not be loaded (Possible installation issue)
Private Sub IQFeedY1_IQConnectStatus(strStatus As String)
If strStatus = "-1" Then
MsgBox "IQFeedY Reports an error loading IQFeed."
ElseIf strStatus = "0" Then
MsgBox "IQFeedY Reports IQConnect shutdown."
Unload History
End If
End Sub
Private Sub IQFeedY1_SystemMessage(strSystemData As String)
If InStr(strSystemData, "S,SERVER CONNECTED") > 0 Then
Go.Enabled = True
End If
End Sub
Thanks!
Brandon
Edited by brandon on Feb 11, 2006 at 09:24 AM