Skip to content

Instantly share code, notes, and snippets.

@dockimbel
Last active January 5, 2019 01:57
Show Gist options
  • Save dockimbel/468f669234d6887911e7664d1cfcc2c7 to your computer and use it in GitHub Desktop.
Save dockimbel/468f669234d6887911e7664d1cfcc2c7 to your computer and use it in GitHub Desktop.
Simple currencies converter in Red
Red [
Title: "Currency converter"
Author: ["Mark Summerfield" "Nenad Rakocevic"]
License: ["Apache 2.0" http://www.apache.org/licenses/LICENSE-2.0]
Version: 1.0.0
Needs: 'View
]
currencies: rates: usd: gbp: from: target: value: none
initialize: func [/local list name code pos e][
list: parse read http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv [
collect some [
#"#" thru lf ;-- skip comments
| "Date" thru lf ;-- skip dates
| copy name to comma skip opt #" " ;-- extract currency's name
copy code to ["_NOON" | comma] thru comma ;-- extract currency's code
keep (append name reduce [" (" code ")"]) ;-- store "name (code)" label
some [pos: thru [comma | e: lf keep (load copy/part pos e) break]] ;-- extract & store last quote
]
]
list: sort/skip append list ["Canadian dollar (CAD)" 1.0] 2 ;-- add CAD, then sort by pairs
currencies: extract list 2
rates: extract next list 2
parse currencies [some pos: into [some ["USD" (usd: index? pos) | "GBP" (gbp: index? pos) | skip]]]
]
initialize
view [
title "Currency"
below
panel [
text "&From"
from: drop-list data currencies select usd
value: field "1.00"
]
panel [
text "&To"
target: drop-list data currencies select gbp
text "1.00" react [
result: rates/(from/selected) / rates/(target/selected) * load value/text
face/text: form round/to result 0.01
]
]
]
@ne1uno
Copy link

ne1uno commented Jan 5, 2019

www.bankofcanada.ca/en/markets/csv/exchange_eng.csv
used in the currency converter has changed

https://www.bankofcanada.ca/search/?esearch=Exchange%20rates%20csv&esorder=relevance

the daily exchange rates page has csv & json links with start dates
maybe their API docs have a better link or csv was from some other page?
https://www.bankofcanada.ca/valet/docs

https://www.bankofcanada.ca/valet/observations/group/FX_RATES_DAILY/csv?start_date=2017-01-03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment