When I request Historical data for a ticker, if the data does not exist locally, why doesn't PSM automatically download it for me and return it? Why does MY code need to check local history itself, download the data if it does not exist, and then read through the entire history file for the ticker to find the ONE date I need?
In other words, I would expect to write the following code to get a single historical price ...
HistoryStartDate = "7/20/10"
HistoryEndDate = "7/20/10"
ticker.SetProperty "HistoryStart", HistoryStartDate
ticker.SetProperty "HistoryEnd", HistoryEndDate
' The following Set should return ONE record if StartDate = EndDate, AND the
' date is a valid trading date (stock was available for trading, not a weekend
' and not a market holiday). If the dates are different, then multiple records
' would be returned.
Set HistoryRecs = ticker.HistoryData
If HistoryRecs.Count = 0 Then
MsgBox "No History data for requested date.", vbOKOnly, "Test"
Else
MsgBox "HistoryRecs.Count = " & HistoryRecs.Count, vbOKOnly, "Test"
' Code to process the SINGLE Historical data record for the ONE date requested
' ...
End If
HOWEVER, that doesn't work.
The code that seems to be REQUIRED is the following -- as MY code must first check to see if the data exists locally or not (ticker.HistoryData), and when it does not exist, my code must then download it (ticker.DownloadHistoryData HistoryStartDate, HistoryEndDate), re-Set ticker.HistoryData, and then loop through ALL of the local history data to locate the ONE date I want ...
HistoryStartDate = "7/20/10"
HistoryEndDate = "7/20/10"
ticker.SetProperty "HistoryStart", HistoryStartDate
ticker.SetProperty "HistoryEnd", HistoryEndDate
Set HistoryRecs = ticker.HistoryData
Continue = 1
If ( (HistoryRecs.Count > 0) And (HistoryRecs.Count < 3) ) Then
' (Can't use = 1 because it returns 2 when it should be 1)
' We got the record we need ...
Continue = 0 ' (False/No)
HistoryRecNo = 0
HistoryPrice = HistoryRecs.Item(HistoryRecNo).GetProperty("Close")
MsgBox "On-file HistoryPrice: " & HistoryPrice, vbOKOnly, "Test"
ElseIf HistoryRecs.Count = 0 Then
' There are NO history records in this ticker, we need to download them ...
MsgBox "Downloading history data...", vbOKOnly, "Test"
On Error Resume Next
ticker.DownloadHistoryData HistoryStartDate, HistoryEndDate
On Error Goto 0
Set HistoryRecs = ticker.HistoryData ' Set this again since we just downloaded data
MsgBox "HistoryRecs.Count: " & HistoryRecs.Count, vbOKOnly, "Test"
' Check the download results ...
If Not ticker.HistoryData.Count > 0 Then
' History download failed ...
MsgBox "Unable to download historical data for " _
& ticker.GetProperty("Symbol") _
& ". This ticker will be skipped!", vbOKOnly, "Test"
Set HistoryRecs = Nothing
HistoryPrice = 0
Continue = 0 ' (False/No)
rtnCode = 1 ' (True/Yes there was an error)
End If ' History download failed
End If ' (HistoryRecs.Count > 0) And (HistoryRecs.Count < 3)
If Continue = 1 Then
' We have history records to look through ...
MsgBox "Do Loop - HistoryRecs.Count: " & HistoryRecs.Count, vbOKOnly, "Test"
' Loop through the history data looking for the ONE date we want ...
i = 0
Do Until HistoryRecs.Item(i).GetProperty("Date") = HistoryStartDate
' MsgBox "Date: " & HistoryRecs.Item(i).GetProperty("Date"), vbOKOnly, "Test"
i = i + 1
If i > HistoryRecs.Count Then
MsgBox "A history record for Ticker " & ticker.GetProperty("Symbol") _
& " on Date " & HistoryStartDate _
& " cannot be located in the History records." _
& " This Ticker will be skipped.", vbOKOnly, "Test"
HistoryPrice = -99 ' (Set it to an invalid number for later checking)
Continue = 0 ' (False/No)
Exit Do
End If
Loop
If HistoryPrice => 0 Then
HistoryRecNo = i
HistoryPrice = HistoryRecs.Item(HistoryRecNo).GetProperty("Close")
MsgBox "Do Loop HistoryPrice: " & HistoryPrice, vbOKOnly, "Test"
rtnCode = 0 ' (False/No error)
Else
' There is no history record for the date we want ...
Set HistoryRecs = Nothing
HistoryPrice = 0
rtnCode = 1 ' (True/Yes there was an error)
End If ' HistoryPrice => 0
End If ' We have history records to process (Continue = 1)
This just seems like a lot of code to simply get ONE historical price.
Thank you,
-Don