DEV Community

lepinekong
lepinekong

Posted on • Edited on

Red for hopeless programmers - Part I

Part II is available.

So you wanna learn to code ? You tried hard to tackle C++, C#, Java or even Python or Javascript but soon realise you still can't do any practical stuff like reading historical quotes from Google Finance to analyse your favorite stocks or just creating a simple recipes database for your mom or teach your own kid how to program.

You hopelessly gave up, happily I'm a great soul and want to save you from despair thanks to a little gem : Red.

What is Red ?

Because you're in a hurry, I'll tell you shortly that Red (download it here) is a new programming language which is strongly compatible with a not so new programming language named Rebol. So if you're searching for some answers on Red, you may search for Rebol also.

The reasons why Rebol is so unknown are unfortunate, I could extend on these but another time maybe. Be assured that after reading this tutorial, you'd probably agree it deserves better recognition - though JSON's inventor, Douglas Crockford, made a great tribute to Carl Sassenrath [http://www.rebol.com/cgi-bin/blog.r?view=0423], Rebol's Creator (who'd also been Amiga's OS Architect).

What amazing stuffs you'll learn here

You'll learn these practical stuffs :

  1. How to download historical quotes from Google Finance (part I)

  2. How to easily do webscraping in a few lines instead of dozens in others languages (part II)

  3. How to create a simple to-do-list or recipes database app for your mom (part III)

  4. How to send keystrokes to automate your favorite windows's application (part IV)

Getting Started

To get started:

  1. Download Red Language Platform for Windows from [http://www.red-lang.org/p/download.html]. You can try other platforms like MacOSX or Linux on your own because I haven't tested them yet.

  2. Install Red

  3. Launch Red

Red Console

You'll then be in Red Console where you can directly type the program below.

How to download historical quotes from Google Finance

In plain english, the steps are :

  1. Read the url to get the quotes

  2. Memorize the quotes in a memory variable

  3. Write the quotes contained in the variable to a csv file

You can literally translate that into Red code by typing the code below in Red Console (if you're too lazy, you can also just copy and paste them into red console):

; 1. Read the url to get the quotes

Read http://finance.google.com/finance/historical?output=csv&q=AAPL 

; 2. Memorize the quotes in a memory variable

quotes: Read http://finance.google.com/finance/historical?output=csv&q=AAPL 

; 3. Write the quotes contained in the variable to a csv file

write %quotes.csv quotes

So a few remarks:

  • In Red, you can put a comment in the code with ";"

  • To assign a content or value to a variable, use ":" after the variable name

  • A file path must be prefixed with % for example %quotes.csv

  • Steps 1 & 2 can be done in one single step in Red so that the program is just:

; 1&2. Read the quotes url & Memorize the quotes in a memory variable

quotes: Read http://finance.google.com/finance/historical?output=csv&q=AAPL 

; 3. Write the quotes contained in the variable to a csv file

write %quotes.csv quotes

You can check in Red current directory that quotes.csv is there. To know that current directory, type

what-dir

How to choose the start date

You may want to download quotes only after some start date. In that case, you can ask that input to the user (when you use ask, you must paste only one line at a time, to erase console use "Ctrl L" while maintaining Ctrl Key):

url-base: http://finance.google.com/finance/historical?output=csv&q=AAPL

start-date: ask {Start date (YYYY-MM-DD): }

url: rejoin [url-base {&startdate=} start-date]

quotes: Read url

write %quotes.csv quotes

Ask-Date

Explanations:

  • Why did I use {} instead of "" to represent a string value ? Actually you could use "" in Red but {} is more powerfull as it can contain multiple lines string and embed both "" and {} inside it for example:
Explanation: {Why did I use {} instead of "" to represent a string value ? 
In fact you can use "" in Red but {} is more powerfull 
as it can contain multiple lines string and embed both "" and {} inside it}

In other programming languages you often have to use so-called "escape sequences" to include double quote " or single quote '.

  • To concatenate strings you can use rejoin like in the example:
url: rejoin [url-base {&startdate=} date]

Rejoin argument must be enclosed within a block delimited by []

Exercice: Ask user's inputs for Stock symbol, Start date and End date

You can now do a similar exercice with Symbol, Start-Date and End-Date as parameter:

Read http://finance.google.com/finance/historical?output=csv&q={stock-symbol}&startdate={start-date}&enddate={end-date}

How to show a Dialog Box for asking user's inputs

Instead of using "Ask" function in the console for user's inputs, you could use a Dialog Box.

The most basic dialog box in Red is just an empty block you can name whatever you like (avoid form though as it is a standard function [http://www.rebol.com/docs/words/wform.html]), for example win like window:

win: []

It won't shows up until you use view command:

view win

Empty Form

The reason being you can compose your form beforehand and only when it's complete, you'll show the form layout to the end user.

So let's compose our layout: we need

  • 1 stock symbol field

  • 2 dates fields (start date and end date)

  • 1 OK button

The simplest version of form is:

win: []

append win [field field field button]

view win

Basic Form

A nicer window with labels (with text keyword) and vertical alignment (with return keyword) is:

win: []

append win [text "stock symbol:"]

append win [field]

append win [return]

append win [text "start date:"]

append win [field]

append win [return]

append win [text "end date:"]

append win [field]

append win [return]

append win [button "OK"]

view win

Nicer Form

If you don't need to generate your window dynamically (which is very usefull in real application that's why I showed you that way first), you can just compose your form in compact static way:

win: [
    text "stock symbol:" field
    return
    text "start date:" field
    return
    text "end date:" field
    return
    button "OK"
]
view win

Nicer Form Compact

Now when clicking on the "OK" button, the form must close with "unview" so modify the code line by adding a block code like this :

button "OK" [unview]

so that the full code is now:

win: [
    text "stock symbol:" field
    return
    text "start date:" field
    return
    text "end date:" field
    return
    button "OK" [unview]
]
view win

When the windows closes, you have to get the text value of each field, so you have to name these fields first to be able to refer to them in button "OK" code block:

win: [
    text "stock symbol:" stock-symbol: field
    return
    text "start date:" start-date: field
    return
    text "end date:" end-date: field
    return
    button "OK" [
        unview 
        print [stock-symbol/text start-date/text end-date/text]
    ]
]
view win

Above for testing purpose, we're just printing the values in console with

    print [stock-symbol/text start-date/text end-date/text]

/text for a field means we're refering to the text property value of the field.

fields values

After clicking the OK button, the console should print the values of the fields:

test ok

We can now replace the print instruction with the code for downloading the quotes.

For that purpose we'll call a function named "download-quotes" by passing the stock-symbol, the start-date and the end-date:

;ask user's inputs:
win: [
    text "stock-symbol" stock-symbol: field
    return
    text "start-date" start-date: field
    return
    text "end-date" end-date: field
    return
    button "OK" [
        unview 
        quotes: download-quotes stock-symbol/text start-date/text end-date/text
        write %quotes.csv quotes
        print "quotes.csv"
    ]
]
view win

;downloading quotes:

download-quotes: function[stock-symbol start-date end-date][

    url-base: http://finance.google.com/finance/historical?output=csv

    url: rejoin [url-base {&q=} stock-symbol 
                {&startdate=} start-date 
                {&enddate=} end-date]

    quotes: Read url    
    return quotes
]

That's all Folks for part I. If you like this tutorial, I may start part II soon :)

References

Top comments (8)

Collapse
 
rgchris profile image
Chris Ross-Gill

COLLECT/KEEP is a nice way to manage multiple appends without even using additional words:

view collect [
    keep [text "stock symbol:"]
    keep [field]
    keep [return]
    keep [text "start date:"]
    keep [field]
    keep [return]
    keep [text "end date:"]
    keep [field]
    keep [return]
    keep [button "OK"]
]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
pungiish profile image
Jan

Nice, never would have thought I'm going to learn a new language today :D

Collapse
 
lepinekong profile image
lepinekong

Glad you discovered it :)

Collapse
 
red_adi profile image
red-adi • Edited

@lepinekong Thanks for this nice introduction! As it seems, historical data aren't available from Google Finance any longer. So I switched to Yahoo Finance.

Red []

stock-symbol: trim ask "Enter stock symbol: " 

; load is a work-around as the to-date conversion is not working, yet
; to-integer converts to Unix epoch time
start-date: to-integer load ask "Enter start date (YYYY-MM-DD): "
end-date: to-integer load ask "Enter end date (YYYY-MM-DD): "

url: to-url rejoin [
    "https://query1.finance.yahoo.com/v7/finance/download/" stock-symbol
    "?period1=" start-date 
    "&period2=" end-date
    "&interval=1d&events=history&includeAdjustedClose=true"
]

quotes: Read url

write %quotes.csv quotes
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jacobgood1 profile image
JacobGood1

"If you like this tutorial, I may start part II soon :)"

I like it, so go ahead with part 2 =)

Collapse
 
godwinburby profile image
Godwin Burby

Love this red language. I'm waiting for part iv where I can learn to automate windows apps with red. Currently i use Autohotkey.

Collapse
 
gltewalt profile image
Greg T • Edited

Very cool, thanks lepinkong :-)

Steps 1, 2, 3, can even be done all in one line.

Collapse
 
lepinekong profile image
lepinekong

You're welcome. Yes you're right and it's almost plain english :)