Forum Moderators: coopster

Message Too Old, No Replies

Yahoo stock feed and PHP

Auto updating stock symbols based on date

         

jspeed

4:43 pm on Nov 17, 2010 (gmt 0)

10+ Year Member



I have a small PHP code that fetches data from yahoos csv stock feed. It works great for a static stock symbol. (e.g. GOOG, YHOO)

But what I am wanting to do, is update the stock symbol based on what date it is. Specifically oil and gas futures. Since the "active" futures symbol changes based on what date it is.

The symbol looks like this: CLZ10.NYM

CL is for crude oil, Z is the month code, 10 is the year. Here are the other month codes.

F=January
G=February
H=March
J=April
K=May
M=June
N=July
Q=August
U=September
V=October
X=November
Z=December

The tricky thing is, the symbol doesn't change at the end of the month, it changes sometimes during the month, like the 3rd friday or something.

Any ideas or suggestions on how to accomplish what I'm talking about?

Here is the current code:

$stock = 'CLZ10.NYM';
if ($stock != "") {
$yahoo_file = fopen ("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=sl1d1t1c1ohgv&e=.csv","r");
$stock_info = fgetcsv ($yahoo_file, 1000, ",");
echo "<b>NYMEX</b> Crude Oil (bbl) $<b>$stock_info[1]</b>";
}
else {
echo"Not Working!";
}
//close the filehandle $fp
fclose ($yahoo_file);

coopster

8:28 pm on Nov 25, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you trying to build that stock name (acronym) dynamically then? The PHP date function will help you pick the correct day/month etc. Also, strtotime and strftime may be of use.

jspeed

5:53 pm on Nov 29, 2010 (gmt 0)

10+ Year Member



Yes, it would be building the stock symbol dynamically.

The "CL" would not change, but the month code and year would change. I could use strftime('%y') to get the two digit year, but how would I go about rotating the month every 30 days or so, but on the 3rd friday?