This is the Personal Stock Monitor scripting developers forum.
Subscribe to RSS Feed
Developing Custom Stock Market Software using PSM -> Problems with DownloadHistoryData
Not logged in.
2010-11-26 00:23:39
1 of 13
#2564
Am having problems with DownloadHistoryData(dateStart, dateEnd) ...

When I want to get a single Closing price (ONE date) for the selected ticker, I set the HistoryStart and HistoryEnd dates to the same value ...

HistoryStartDate = "01/04/10
HistoryEndDate = "01/04/10

ticker.SetProperty "HistoryStart", HistoryStartDate
ticker.SetProperty "HistoryEnd", HistoryEndDate

However, when using ...

ticker.DownloadHistoryData, HistoryStartDate, HistoryEndDate

... the result is an array of ALL history data stored in the history database or the endofday text file, instead of returning the SINGLE historical data record requested!

Start EDIT Dec 1, 2010:
After downloading the data, I re-set HistoryRecs. It is HistoryRecs.Count that = the entire history file for this ticker.
Set HistoryRecs = ticker.HistoryData
End EDIT Dec 1, 2010:

How do I get the Close price for the SINGLE date I want?

Thank you,

-Don

Posted by: dgoyette
2010-11-27 15:50:58
2 of 13
#2577
in reply to #2564
DownloadHistoryData requests the data from the quote server, not the database. What you want is the ticker.HistoryData property after setting HistoryStart and HistoryEnd as you did. I haven't tested this in a while, but you will probably need to set the end date one day after the start date so that you get data for just one day.
Posted by: Anatoly
2010-11-27 19:47:52
4 of 13
#2585
in reply to #2577
Sorry for the confusion.

Yes, the problem was with ticker.HistoryData. When the start and end dates are the same, it returns a .Count value of 2 instead of 1. When the end date is one day later, it returns a .Count value of 3 instead of 2.

DownloadHistoryData functions as expected, returning a count of 1 or 2 respectively.
Posted by: dgoyette
2010-11-30 21:21:37
5 of 13
#2611
in reply to #2585
In any case you can iterate over the data retrieved and check the date for the one you want.
Posted by: Anatoly
2010-12-01 16:47:32
6 of 13
#2614
in reply to #2611
Below is the code I wrote. Is this correct?

Or is there a way to get ONLY the ONE record being downloaded by DownloadHistoryData, withOUT having to iterate through all of ticker.HistoryData AFTER calling DownloadHistoryData? It would be something similar to the following [ Set HistoryRecs = ticker.DownloadHistoryData HistoryStartDate, HistoryEndDate ].

Here's my code...

HistoryStartDate = "7/20/10"
HistoryEndDate = "7/20/10"
ticker.SetProperty "HistoryStart", HistoryStartDate
ticker.SetProperty "HistoryEnd", HistoryEndDate

On Error Resume Next
ticker.DownloadHistoryData HistoryStartDate, HistoryEndDate
On Error Goto 0

Set HistoryRecs = ticker.HistoryData
MsgBox "HistoryRecs.Count: " & HistoryRecs.Count, vbOKOnly, "Msg"

The MsgBox from the above code displays: "HistoryRecs.Count: 180", which is the total number of History records on-file (locally). These must be read through one at a time, looking for the ONE date I want, which seems silly. That's why I'm asking if there is another way to code what I want.

Also ... every time a new record is added to local history, via ticker.DownloadHistoryData, the HistoryRecs.Count value displayed increases by several digits, NOT just ONE digit.

Posted by: dgoyette
2010-12-02 11:01:49
7 of 13
#2626
in reply to #2614
Tested your code here, and I get HistoryRecs.Count = 2 no matter how much data is actually in the database. So at the moment I'm not sure why you're seeing what you're seeing.
Posted by: Anatoly
2010-12-02 11:04:14
8 of 13
#2627
in reply to #2626
Also, keep in mind that ticker.DownloadHistoryData will run asynchronously on any other data source, because right now Yahoo is the only data source that downloads historical data synchronously, and that is only because it is legacy code.
Posted by: Anatoly
2010-12-02 22:13:57
9 of 13
#2638
in reply to #2626

Anatoly wrote
Tested your code here, and I get HistoryRecs.Count = 2 no matter how much data is actually in the database. So at the moment I'm not sure why you're seeing what you're seeing.


Do you have the "Enable history logging" option checked? I DO, as well as entering directories for endofday and intraday. Could this be the problem?
Posted by: dgoyette
2010-12-02 22:20:25
10 of 13
#2639
in reply to #2627
Anatoly wrote
Also, keep in mind that ticker.DownloadHistoryData will run asynchronously on any other data source, because right now Yahoo is the only data source that downloads historical data synchronously, and that is only because it is legacy code.


You also wrote in another topic ...
Anatoly wrote
BTW, keep in mind that DownloadHistoryData executes asynchronously. You have to wait for the OnHistoryUpdated event to actually retrieve the historical data.


Then I asked you, "How do I "wait" for the OnHistoryUpdated event? How should a ticker.DownloadHistoryData process be coded? Am I doing it correctly (below)?" ... and included the following [ extension ] code ...

...
MsgBox "Downloading history data...", vbOKOnly, "Update 1-Share Transactions"
On Error Resume Next  ' Activate error handling
  ticker.DownloadHistoryData HistoryStartDate, HistoryEndDate
On Error Goto 0  ' De-Activate error handling

' Check the download results ...
  If ( (Not ticker.HistoryData.Count > 0) Or (ticker.HistoryData Is Nothing) ) Then
' History download failed ...
    MsgBox "Unable to download historical data. This ticker will be skipped.", vbOKOnly, "Update 1-Share Transactions"
...
' We have history records to process ...
  Set HistoryRecs = ticker.HistoryData
...

But you haven't replied to these questions. How do I "wait for the OnHistoryUpdated event to actually retrieve the historical data"? What is the proper code sequence?

Thank you Anatoly.
Posted by: dgoyette
2010-12-03 00:43:35
11 of 13
#2645
in reply to #2638
dgoyette wrote

Do you have the "Enable history logging" option checked? I DO, as well as entering directories for endofday and intraday. Could this be the problem?

It shouldn't, but I'll test that tomorrow if I get a chance.
Posted by: Anatoly
2010-12-03 01:13:07
12 of 13
#2646
in reply to #2639
dgoyette wrote

But you haven't replied to these questions. How do I "wait for the OnHistoryUpdated event to actually retrieve the historical data"? What is the proper code sequence?


I did reply, I believe that was in one of the other threads. The answer is that you don't explicitly wait for anything, that what I mean by it being asynchronous. You set up your event handler as in the sample code, then just call the DownloadHistoryData function. The OnHistoryUpdate function in the event handler gets called when the historical database update is completed, at which point you can retrieve the data.

Now when I said that the DownloadHistoryData function was asynchronous, that is generally true because of its design, and that's how it works with most data services. However the actual implementation of the data download for Yahoo is currently synchronous because it is based on some legacy code, which means that the data is available immediately after the DownloadHistoryData call, assuming the download was successful. So you can take advantage of that and not deal with event handlers, OnHistoryUpdated, and all that, but I wouldn't recommend it if you're going to give your extension to anyone else, and/or if you ever switch to a different quote service, since that will immediately break your code.
Posted by: Anatoly
2010-12-03 03:27:09
13 of 13
#2654
in reply to #2626

Reading one of your other replies ...

Anatoly wrote
I guess it's undocumented, but what happens is that the HistoryStart and HistoryEnd properties are wiped out after you call ticker.HistoryData, so that's why you're not getting the date (12:00 AM with no date is "zero date").


... it hit me!

My code sequence was:

ticker.SetProperty "HistoryStart", HistoryStartDate
ticker.SetProperty "HistoryEnd", HistoryEndDate

Set HistoryRecs = ticker.HistoryData
Continue = 1  ' Set a local flag (True/Yes)

If ( (HistoryRecs.Count > 0) And (HistoryRecs.Count < 3) ) Then
  Continue = 0
  HistoryRecNo = 0
  HistoryPrice = HistoryRecs.Item(HistoryRecNo).GetProperty("Close")

ElseIf HistoryRecs.Count = 0 Then
' ---------------< added these two lines >---------------
  ticker.SetProperty "HistoryStart", HistoryStartDate
  ticker.SetProperty "HistoryEnd", HistoryEndDate
' -------------------------------------------------------

  On Error Resume Next
    ticker.DownloadHistoryData HistoryStartDate, HistoryEndDate
  On Error Goto 0

  Set HistoryRecs = ticker.HistoryData
  MsgBox "HistoryRecs.Count: " & HistoryRecs.Count, vbOKOnly, "Test"

' ... other processing ...

BUT, since "Set HistoryRecs = ticker.HistoryData" CLEARS the Start and End Dates (like your reply stated), then these dates MUST be RESET before calling ticker.DownloadHistoryData!

When I added the two lines of code noted in the box above, the MsgBox HistoryRecs.Count: statement consistently returns 1, which is what I've been expecting all along.

Would you or Yermo please add this tidbit of VERY IMPORTANT information to the on-line documentation for ticker?

Thank you Anatoly.

Posted by: dgoyette