Join the 80,000 other DTN customers who enjoy the fastest, most reliable data available. There is no better value than DTN!

(Move your cursor to this area to pause scrolling)




"Boy, probably spent a thousand hours trying to get ******* API to work right. And now two hours to have something running with IQFeed. Hmmm, guess I was pretty stupid to fight rather than switch all this time. And have gotten more customer service from you guys already than total from them… in five years." - Comment from Jim
"I've never had DTN go out on me since switching. ******* would go down a couple times every month when I was using them." - Comment from Bryce in AL.
"I just wanted to let you know how fast and easy I found it to integrate IQFeed into our existing Java code using your JNI client. In my experience, such things almost never go so smoothly - great job!" - Comment from Nate
"I have been using IQFeed now for a few years in MultiCharts and I have zero complaints. Very, very rare to have any data hiccups or anything at all go wrong." - Comment from Public Forum
"IQFeed version 4 is a real screamer compared to anything else I have seen." - Comment from Tom
"Everything is working great ! Very impressive client. The news refreshes better and is more pertinent than the ******* feed I paid $ 100/month for. I Also like the charts a lot." - Comment from Leon
"I have to tell you though that using the IQFeed API is about the easiest and cleanest I have seen for some time." - Comment from Jim
"Just a quick one to say I'm very impressed so far :) The documentation for developers is excellent and I've quickly managed to get an app written to do historical downloads. The system is very robust and pretty quick considering the extent of data that's available. The support guys have been very helpful too, in combination with the forums it's been plain sailing so far!" - Comment from Adam
"Very impressed with the quality of your feed - ******* is a real donkey in comparison." - Comment from A.C. via Email
"Just a thank you for the very helpful and prompt assistance and services. You provided me with noticeably superior service in my setup compared to a couple of other options I had looked at." - Comment from John
Home  Search  Register  Login  Recent Posts

Information on DTN's Industries:
DTN Oil & Gas | DTN Trading | DTN Agriculture | DTN Weather
Follow DTNMarkets on Twitter
DTN.IQ/IQFeed on Twitter
DTN News and Analysis on Twitter
»Forums Index »Archive (2017 and earlier) »IQFeed Developer Support »VB Code to Compute RSI -- What's wrong?
Author Topic: VB Code to Compute RSI -- What's wrong? (2 messages, Page 1 of 1)

brandon
-Interested User-
Posts: 19
Joined: Jan 4, 2006


Posted: Feb 11, 2006 09:23 AM          Msg. 1 of 2
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

nsolot
-DTN Guru-
Posts: 273
Joined: Sep 4, 2004


Posted: Feb 11, 2006 09:39 AM          Msg. 2 of 2
Sorry I can't help with your code.

You might take a look at

http://www.modulusfe.com/tasdk/vb.asp

I've used the C++ version and it might save you a lot of time/headaches.
 

 

Time: Wed May 8, 2024 12:10 PM CFBB v1.2.0 11 ms.
© AderSoftware 2002-2003