Previous Blog Next Blog
Prev/Next Blog
by date

Take "Stock" in Torque

Take "Stock" in Torque
Name:Frank Carney
Date Posted:Mar 11, 2007
Rating:4.0 out of 5
Public:YES
Comments:YES
RSS Feed:GarageGames Blog feedor Subscribe with .
Profile Page:View profile page for Frank Carney

Blog post
Okay, now this blog is going to be something completely different...

Get stock quotes and history in Torque!

Yahoo has a wonderful way to allow programmers to get stock quotes using http requests. I wanted to know how to get these into Torque so I figured it out. Here are some reference documents first:
search.cpan.org/src/MSISK/Finance-QuoteHist-1.09/lib/Finance/QuoteHist/Yahoo.pm History data info
gummy-stuff.org/Yahoo-data.htm Single Quote data info

So, what do you do with this information:

$info::stock::echo = true;

function GetStock(%symbol)
{
//$url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=sl1c1" ,$symbol);

%obj = new TCPObject(GetStock);
//%host = "finance.yahoo.com:80";
//%host = "download.finance.yahoo.com:80";
%host = "ichart.finance.yahoo.com:80";
%obj.connect(%host);

// remember
%obj.symbol = %symbol;
%obj.host = %host;
%obj.commands = new SimSet();
}

function GetStock::onConnected(%this)
{
echo("sending");

// yahoo path
// symbol(s), last trade price(l1), change(c1)
//%uri = "/d/quotes.csv?s="@ %this.symbol @"&f=sl1c1";
// symbol(s), last trade price & time (l)
//%uri = "/d/quotes.csv?s="@ %this.symbol @"&f=sl";
// symbol(s),last date(d1),last time(t1),last price(l1)
// w = 52 week range
//%uri = "/d/quotes.csv?s="@ %this.symbol @"&f=snd1t1l1w&e=.csv";
%uri = "/table.csv?s="@ %this.symbol @"&d=0&e=00&f=2008&g=d&a=00&b=00&c=1900&ignore=.csv";

//"download.finance.yahoo.com/d/quotes.csv?s=MSFT&f=sl1d1t1c1ohgv&e=.csv"

//"download.finance.yahoo.com/d/quotes.csv?s=RHT&f=sl1d1t1c1ohgv&e=.csv"
// s = symbol
// l1 = last price
// d1 = last date
// t1 = last time
// c1 = change
// o = open
// h = day's high
// g = day's low
// &e=.csv = ?

//ichart.finance.yahoo.com/table.csv?s=RHT&d=2&e=11&f=2007&g=d&a=7&b=11&c=1999&ignore=.csv

%getstr = "GET" SPC %uri;

// What is this crap for?
//SPC "HTTP/1.0\nHost:" SPC %this.host SPC "\n";

echo(%getstr);

%this.send(%getstr SPC "\r\n");

echo("sent");
}

function GetStock::onConnectFailed(%this)
{
echo("Connection Failed");
}

function GetStock::onLine(%this, %line)
{
if(%line !$= "")
{
// parse return data
if($info::stock::echo)
echo(%line);

// run any commands put in the queue
while(%this.commands.getCount())
{
%command = %this.commands.getObject(0);
eval(%command.text);
%command.delete();
}
}
}

function GetStock::onDisconnect(%this)
{
%this.commands.delete();
%this.delete();
}


Here are a couple of usage examples:

getStock("RHT");
getStock("MSFT");


Well, you do what I did above. Right now the code is set to pull up all historical data. You will have to figure out how to play with this on your own. This has been fun, now I have to get back to work so to speak.

Some possible uses:
1. Ticker data in game (not sure why)
2. Historical data for novel processing techniques (I know why, but I ain't tellin' why)
3. It is just damn cool and completely in script (another cool reason)

Have fun and don't get ulcers,
Frank

Recent Blog Posts
List:12/25/07 - Milestone Reached: Mission Launched from Database
12/24/07 - Creating Objects Without Creating Them
12/24/07 - Another Sqlite Blog
09/03/07 - SQLite is Fun!
08/20/07 - Playing House
07/05/07 - Getting burned out and coming back, again...
04/07/07 - Inputs, Inputs everywhere...
04/01/07 - RPG, what does it mean?

Submit ResourceSubmit your own resources!

Gary Patterson   (Mar 11, 2007 at 08:20 GMT)   Resource Rating: 5
Hmm... I wonder if there's room for a solid stock market simulator game running on Torque using this technique. Very interesting though, and could be the basis for all sorts of things.

Torque doesn't have to be about games. It could be used as the basis for a novel interface into the stock market. All the slick graphics are made easy, and this code makes getting the data straightforward.

Hmm... ideas are forming...

Great code, Frank!

Mark McCoy   (Mar 11, 2007 at 08:52 GMT)
You could make your game sunny and bright when the market is up and cloudy and rainy when your stocks are down.

Maxwell Marsh   (Mar 11, 2007 at 18:42 GMT)
And when stock's crash, you are forced to play as George W. Bush

Hahahaha...

You must be a member and be logged in to either append comments or rate this resource.