Live stream data visualization HELP!
in
Programming Questions
•
2 years ago
Hi my class is working on visualizing data and I thought it would be really cool to do a live feed of a stock price. I am having trouble grabbing the live data part. Does anyone know how to help me extract data from yahoo finance? The graph is working but I dont know how to get the price off of the website.
http://finance.yahoo.com/q?s=cmg&ql=1
// Want to grab both price and time from yahoo finance
PFont f;
//Timer timer = new Timer (10000); --timer insteat
String[] ticker = {"CMG"}; //instead of ticker should i use "time"?
int counter = 0;
//int timer;
//initialize stock quote grabber to extract the stock price automatically from Yahoo finance webpage
StockQuoteGrabber SQ;
void setup() {
size(500,300);
//make StockQuoteGrabber object
SQ = new StockQuoteGrabber(ticker[counter]);
//Tell it to request CMG quote
SQ.requestStockQuote();
timer.start();
f = createFont("Helectiva", 16, true);
}
void draw() {
background(255);
textFont(f);
fill(0);
/* if (timer.isFinished()) {
htmlRequest.makeRequest();
println( "Making request!");
timer.start(); */
// Get the values to display
String stockQuote = SQ.getStockQuote();
int time = SQ.getTime(); //Get time of the stock price
//Display everything
//text(time[counter],10,160);
//text("Click to display chart.", 10,180);
//Draw graph based on CMG quote
stroke(0);
fill(175);
rect(20,20,460,260);
line(30,30,30,270);
line(30,270,470,270);
//point(time, stockQuote); //stockQuote not applicable?
}
void mousePressed() {
//Increment the counter and get the stock quote (price) at the next point in time.
counter = (counter + 1) % ticker.length;
SQ.setTicker(ticker[counter]);
//The data is requested again with a new point in time every time the mouse is pressed.
SQ.requestStockQuote();
}
//Here is my class:
//A StockQuoteGrabber class
class StockQuoteGrabber {
int time = 0;
String stockQuote = "";
String ticker;
StockQuoteGrabber(String timeTicker) {
ticker = timeTicker;
}
// Set a new time
void setTicker(String timeTicker) {
ticker = timeTicker;
}
// Get the time
int getTime() {
return time;
}
// Get the Stock quote
String getStockQuote() {
return stockQuote;
}
// Make the XML request
void requestStockQuote() {
//Put XML source code into an array of strings
String url = "http://finance.yahoo.com/q?s=cmg&ql=1";
String[] lines = loadStrings(url);
//Turn array into one long String
String xml = join(lines, ""); //--> is it xml or html?
//Searching for Stock quote (price)
String lookfor = "Last Trade:</th><td class=";
String end = "/";
stockQuote = (giveMeTextBetween(xml, lookfor, end));
// Searching for time
lookfor = "Trade Time:</th><td class=";
time = int(giveMeTextBetween (xml, lookfor, end));
}
// A function that returns a substring between two substrings
String giveMeTextBetween(String s, String before, String after) {
String found = "";
int start = s.indexOf(before); // Find index of the beginning tag
if (start == - 1) return""; // If we don't find anything, send back a blank String
start += before.length(); // Move to the end of the beginning tag
int end = s.indexOf(after, start); // Find the index of the end tag
if (end == - 1) return""; // If we don't find the end tag, send back a blank string
return s.substring(start, end);
}
}
1