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” ?
Good Day,
I receive from my Broker in the US a CSV file, which i can convert with Excel to be comma seperated, however I can’t upload it in Google Finance ( eiter way, means before or after converting it) . I ask my broker to use a CSV file format , which i can upload, however they dont have any other file available.
My question is how to convert a CSV file, so it can be actually used for Google Finance?
Any suggestions?
Txs, Mike
It’s easy to adapt the script above to convert to format that Google would accept. But I’ve never done it myself actually so learn Rebol it’s easy and then you can do it yourself
Maybe I would do that in the future if ever I need to myself. Then I would post the script.