Advertisements

用R分析虚拟币大盘


授人以渔不如教人编程 – “老子”

惊觉好久没有炫耀最牛的R语言了,貌似没有继续学习的样子。。。其实我一直都在暗中学习哦。虚拟币大盘这么跌宕起伏这么乱,当然要R才行哦。今天就简单介绍几行小程序,让你乱中取静,心情自然凉,凉飕飕又一夏,夏日的啥么茶。。。

那同学们我们开始吧。
首先简单装两个函数包,“BatchGetSymbols” 和 “TTR”,前者是从雅虎财经直接抓股票价格数据的,后者是算统计平均之流的。这两个包都有若干个相依存的函数包,让它们装就是了。

install.packages(c(“BatchGetSymbols”,”TTR”))

嗯,先抓个比特币的价格来看看,然后画个两个星期的价格平滑曲线:

“# set ticker”
“# 莱特币是 c(‘LTC-USD’), 然后 以太币是 c(‘ETH-USD’)”

ticker.symb <- c(‘BTC-USD’)

“# 抓个一年的美刀价格吧”
N.day <- 365

first.date <- Sys.Date() – N.day
last.date <- Sys.Date()

l.out <- BatchGetSymbols(tickers = ticker.symb,
first.date = first.date, last.date = last.date,
cache.folder = file.path(tempdir(), ‘BGS_Cache’) ) # cache in tempdir()

date.vec <- as.Date(l.out$df.tickers$ref.date)
avg.price.vec <- (l.out$df.tickers$price.open + l.out$df.tickers$price.close)/2

“# 两星期平均线, SMA = smoothing moving average”

MA.2wk.avg.vec <- SMA(avg.price.vec, n = 2*7)

plot(date.vec, avg.price.vec, type = “l”,
xlab = “”, ylab = “price”, xaxt = “n”, log = “y”, main = ticker.symb)
axis.Date(1, at = seq(min(date.vec),
max(date.vec)+2, “months”),
las = 2, format = “%m-%d/%y”)
lines(date.vec, MA.2wk.avg.vec, col = “red”, lwd = 2)
legend(“topleft”,c(“price”,”2-week MA”),
col = c(“black”, “red”), lwd = c(1, 2))

 

BTC_R

只改一个变量,重跑一遍以太币

ticker.symb <- c(‘ETH-USD’)

ETH_R

接着看看莱特币

ticker.symb <- c(‘LTC-USD’)

LTC_R

当当当~ 有没有觉得一下子虚拟币的大局都清晰了呢?
(不用谢)

Advertisements
Categories: 虚拟币Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: