このページでは,日本円,米国ドル,ユーロ,オーストラリアドルなどの外国為替の変動データ(時系列データ)の情報源を紹介します
さらに、SQLite3 のテーブル quote を生成します。
◆ 作成するSQLite3 データベース: quotedb
※ データ源の URL: http://www.mizuhobank.co.jp/rate/market/historical.html
免責事項:このページでは,外国為替のデータを扱いますが,あくまでも入門演習での教材データを作る手順である.
Web ブラウザで, http://www.mizuhobank.co.jp/rate/market/historical.html を開く
表示されたCSV 形式のデータを,コピー&ペースト操作で,マイクロソフト・エクセルに貼り付ける.
◆ bash プログラム
#!/bin/bash cd /tmp nkf -w quote.csv > quote2.csv libreoffice quote2.csv
「cat -n a.csv | sed 's/\t/,/g' > /tmp/Book1.csv」で
このページでは,データベースの作成を行うので, 作成するデータベースのデータベース名を決めておくこと. このページでは,次のように書く.
データベース名は,自由に決めてよいが,半角文字(つまり英字と英記号)を使い,スペースを含まないこと,
◆ bash プログラム
#!/bin/bash
rm -f /tmp/quotedb
#
cat >/tmp/a.$$.sql <<-SQL
drop table quote;
SQL
cat /tmp/a.$$.sql | sqlite3 /tmp/quotedb
#
cat >/tmp/a.$$.sql <<-SQL
create table quote (
seq integer primary key not null,
at datetime,
USD real,
GBP real,
EUR real,
CAD real,
CHF real,
SEK real,
DKK real,
NOK real,
AUD real,
NZD real,
ZAR real,
BHD real,
IDR100 real,
CNY real,
HKD real,
INR real,
MYR real,
PHP real,
SGD real,
KRW100 real,
THB real,
KWD real,
SAR real,
AED real,
MXN real,
PGK real,
HUF real,
CZK real,
PLN real,
RUB real,
TRY real,
a01 real,
IDR100b real,
CNYb real,
MYRb real,
KRW100b real,
TWD real
);
SQL
cat /tmp/a.$$.sql | sqlite3 /tmp/quotedb
◆ bash プログラム
#!/bin/bash
cat >/tmp/a.$$.sql <<-SQL
.mode csv
.import /tmp/a.$$.csv quote
SQL
#
tail -n +2 /tmp/Book1.csv > /tmp/a.$$.csv
cat /tmp/a.$$.sql | sqlite3 /tmp/quotedb
#
echo 'select * from quote limit 10;' | sqlite3 /tmp/quotedb
SQLiteman で確認
※ 「プログラム 'r' はまだインストールされていません。 」と表示されるときは「sudo apt -y install littler」を実行する
◆ ggplot() を使用 (R プログラム)
library(ggplot2) library(RSQLite) library(xts) library(chron) fetch_table <- function(sqlite3dbname, tablename) { drv <- dbDriver("SQLite", max.con = 1) conn <- dbConnect(drv, dbname=sqlite3dbname) rs <- dbSendQuery( conn, paste("SELECT * from", tablename, ";") ) T <- fetch(rs, n = -1) return(T) } T <- fetch_table("/tmp/quotedb", "quote") # 今回のデータでは 1 列目が不要なので「-1」 T <- T[,-1] # table_to_melt <- function(T, tsvec, tsformat) { # 先に xts 型に変換しておく D <- as.xts( read.zoo( T, tsformat ) ) # melt(Date, AttrNum, Value). Date <- rep(strptime(tsvec, tsformat, tz=""), ncol(D)) AttrNum <- rep(1:ncol(D), each=nrow(D)) Value <- as.numeric(as.vector(D)) return( data.frame(Date=Date, AttrNum=AttrNum, Value=Value) ) } M <- table_to_melt(T, T$at, "%Y/%m/%d") # ggplot(M, aes(x=Date, y=Value, colour=factor(AttrNum))) + geom_point(size=1); # ggplot(subset(M, AttrNum==3), aes(x=Date, y=Value, colour=factor(AttrNum))) + geom_point(size=1);
◆ plot.zoo() を使用 (bash プログラム)
#!/bin/bash cat > /tmp/a.r <<-COMMAND library(RSQLite) library(xts) library(chron) drv <- dbDriver("SQLite", max.con = 1) conn <- dbConnect(drv, dbname="/tmp/quotedb") rs <- dbSendQuery( conn, "SELECT * from quote;" ) T <- fetch(rs, n = -1) # 今回のデータでは 1 列目が不要なので「-1」 D <- as.xts( read.zoo( T[,-1] ) ) png("/tmp/a.png"); plot.zoo(D, col=1:100, plot.type="single", ylim=c(0,500)) dev.off() COMMAND cat /tmp/a.r | r display /tmp/a.png