Game Development Community

Take "Stock" in Torque

by Frank Carney · 03/10/2007 (11:14 pm) · 3 comments

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

About the author

I Started programming in HS and have never stopped. Now an 18 year vet of programming anything from assembler on a NES console to a nuclear waste processing system. If it can be programmed I may have tried to program it!


#1
03/11/2007 (12:20 am)
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!
#2
03/11/2007 (12:52 am)
You could make your game sunny and bright when the market is up and cloudy and rainy when your stocks are down.
#3
03/11/2007 (11:42 am)
And when stock's crash, you are forced to play as George W. Bush

Hahahaha...