Do you need to retrieve stock market historical data from Google Finance (for example http://www.google.com/finance/historical?q=goog&output=csv) to do some calculation (for example calculate the average of last 20 days) ?
So, in this lesson, you will learn how to:
1. Parse a CSV file to a Rebol’s block (similar to other’s programming languages’ array)
2. Manipulate Rebol’s block’s item
We will use the parse function we already encountered (how to extract html webpage without using Regular Expressions) but in a different form.
Start Rebol’s Console and type:
stock-data: read http://www.google.com/finance/historical?q=goog&output=csv
type? stock-data

Note that the type is string.
Do it again by adding the /lines refinement to the read function (you can use the up arrow twice to retrieve the previous command and use the left arrow to go to the beginning of the line).
stock-data: read/lines http://www.google.com/finance/historical?q=goog&output=csv
type? stock-data

Note that the type is block.
You can now retrieve each element of the block with:
stock-data/1
stock-data/2
stock-data/3

Let’s parse last quote (stock-data/2) and retrieve the close (5th column):
last-quote: parse stock-data/2 ","
close: pick last-quote 5

To parse the whole stock-data we have to iterate through it :
close-block: []
foreach element stock-data [
last-quote: parse/all element ","
append close-block pick last-quote 5
]

You can now calculate the 20 days moving average:
cum: 0
for i 2 21 1 [cum: cum + to-decimal close-block/:i]
average: cum / 20

This is the whole program for any stock symbol
Rebol[
title: "Parse google finance csv file"
author: "http://reboltutorial.com/blog/parse-csvparse-csv/"
version: 1.0.0
]
symbol: ask "symbol: "
url: rejoin [http://www.google.com/finance/historical?q= symbol "&output=csv"]
stock-data: read/lines url
close-block: []
foreach element stock-data [
last-quote: parse/all element ","
append close-block pick last-quote 5
]
cum: 0
for i 2 21 1 [cum: cum + to-decimal close-block/:i]
average: cum / 20
References:
1. http://www.rebol.net/cookbook/recipes/0018.html
2. http://snippets.dzone.com/posts/show/1281















Hello.
I like your site and wanted to know if you would be interested in exchanging blogroll links.
Thanks in advance
Thanks for this simple example. Saw this link in Dzone.com. Not many programmers know about REBOL, so thanks for promoting this nice, productive language.
@Edoc, you’re welcome, don’t forget to vote on dzone
http://www.dzone.com/links/search.html?query=rebol
@Suzan, hi suzan, my blog is not all about finance but about Rebol, so I don’t think a link exchange is suitable, but I have accepted your comment which already points to your blog from this page.
Are there any details anywhere about the google interface used by you? I have searched on the web but no luck so far.
What do you mean by “google interface” ?