京橋のバイオインフォマティシャンの日常

南国のビーチパラソルの下で、Rプログラムを打ってる日常を求めて、、Daily Life of Bioinformatician in Kyobashi of Osaka

【R言語と仮想通貨】ビットコイン(BTC)の長期トレンド解析について

はじめに

2022年11月に入り、仮想通貨の代表格であるビットコイン(BTC)がかなり低迷しています。

おいおい、カエル君、、先日来、大手仮想通貨取引所のFTXショックで仮想通全体が暴落しておるぞ!!

そ、そうなんですか?師匠。。家にテレビがなくて、知りませんでした。

いやいや、カエル君、、テレビではやっておらんぞ。時代は、YouTubeじゃ。

前置きはさて置き、こんな時こそ、BTCの長期トレンドを解析して、 歴史的に、どれ位の値段位置にいるのかを確認したいと思い立ちました。

今回は、R言語で、ビットコイン(BTC)の各種のトレンド解析・可視化関数を実装してみました。

仕事も忙しかったのもあるけど、、2-3週間ほど、ずっと、ここ記事の関数群を作成していたら、暴落が開始しました、ホントに思い出深いプログラム実装になりましたね。。。

パッケージ準備

まずは、関連パッケージをインストールしていきます。 この記事の為に作成した、関数群はGistサイトから適時読み込みます。

#パッケージ・インストール
pack <- c("binancer", "lubridate", "TTR", "pdftools", "classInt")
install.packages(pack[!(pack %in% unique(rownames(installed.packages())))])

#ライブラリ準備
library(binancer)
library(lubridate)
library(TTR)
library(pdftools)
library(classInt)

また、pdf_png_save関数を使って、PDFファイルを画像(PNG)に変換したい場合には、 追加で、brewコマンドでpdftoolsを設定する必要があります。 詳しくは、過去の記事をご参照してくだだい。

skume.net

ビットコインの長期チャートデータについて

getBTC関数を使って、 2010年7月18日から今日(現在、2022年11月13日)までのBTCの日足データを取得します。

とりあえず、getBTC.Rを読み込みます。

それで、getBTC関数を実行して、取得したデータを表示してみます。

#getBTC.Rの読み込み
source("https://gist.githubusercontent.com/kumeS/c466e97d42f6facbb8ba5a22ed361bf4/raw/07728e5e4810e53aa8bdcc411ac51603943761df/getBTC.R")

#BTCのデータ取得
Dat <- getBTC()

#表示
head(Dat)
#        Date Close Month
#1 2010-07-18   0.1    07
#2 2010-07-19   0.1    07
#3 2010-07-20   0.1    07
#4 2010-07-21   0.1    07
#5 2010-07-22   0.1    07
#6 2010-07-23   0.1    07

tail(Dat)
#           Date    Close Month
#4496 2022-11-07 20591.13    11
#4497 2022-11-08 18547.23    11
#4498 2022-11-09 15922.81    11
#4499 2022-11-10 17601.15    11
#4500 2022-11-11 17070.31    11
#4501 2022-11-12 16899.61    11

データの取得元ですが、 https://www.investing.comのヒストリカルデータに加えて、バイナンスの日足データを使用しています。

長期の移動平均から、BTCの経験的な底値が分かるかも??

SMA_LogPlot関数を使って、BTCの値動きと長期移動平均(単純移動平均, SMA)との関係性を解析しました。 Y軸は、log表示です。

上値のラインは、底値に任意のファクターを掛け算して計算しています。

緑のライン・領域は売りゾーン(長期的にみて、値段が高い)、青のライン・領域は買いゾーン(長期的にみて、値段が安い)という判断になります。

それでは、これまでのBTC史のチャート推移を示してみます。

#SMA_LogPlot関数の準備
source("https://gist.githubusercontent.com/kumeS/cf479609eb312db4c7f3b3a002dda3df/raw/855d39904d48b1ba27ee7cf3c420b28440c5870c/SMA_LogPlot.R")

#pdf_png_save関数の準備
source("https://gist.githubusercontent.com/kumeS/bad9ace466970bd13b5afd15eed6f9b6/raw/8cd41b26c00696c59be98b4ce27a4f726ad27cae/pdf_png_save.R")

#(1) 2017年1月-2020年1月のチャート
SMA_LogPlot(Dat,
            term=c("2017-01-01", "2020-01-01"),
            xax = 0.008, yax = 0.9995, ycex=0.8)
#保存
pdf_png_save(FileName="SMA_LogPlot_01.pdf")

#(2) 2018年1月-2020年1月のチャート
SMA_LogPlot(Dat,
            term=c("2018-10-01", "2020-01-01"),
            xax = 0.025, yax = 0.9995, ycex=0.8)
pdf_png_save(FileName="SMA_LogPlot_02.pdf")

#(3) 2020年1月以降のチャート
SMA_LogPlot(Dat, 
            term=c("2020-01-01", as.character(lubridate::today())))
pdf_png_save(FileName="SMA_LogPlot_03.pdf")

#(4) 2022年1月以降のチャート
SMA_LogPlot(Dat, 
            term=c("2022-01-01", as.character(lubridate::today())),
            xax = 0.03, yax = 0.9997, ycex=0.8)
pdf_png_save(FileName="SMA_LogPlot_04.pdf")

以下に可視化結果を示します。また、pdf_png_save関数で結果を保存しています。

©️ 2022 京橋のバイオインフォマティシャンの日常 by skume

現在の2万ドル近辺でも、2019年1月あたりより、売られ過ぎになっってますね。 今回のコロナバブルの終わりですね。

長期の加重移動平均線(WMA)から、BTCの経験的な底値が分かるかも

次に、WMA_Plot関数を使って、BTCの値動きと長期移動平均(加重移動平均線, WMA)との関係性を解析します。

#WMA_Plot.Rの準備
source("https://gist.githubusercontent.com/kumeS/1f3f3556ddff20e0bc105ae1e8e5f443/raw/0eba9c4c2e3140ab1e2a748e030ecc8b918d6a52/WMA_Plot.R")

#pdf_png_save関数の準備
source("https://gist.githubusercontent.com/kumeS/bad9ace466970bd13b5afd15eed6f9b6/raw/8cd41b26c00696c59be98b4ce27a4f726ad27cae/pdf_png_save.R")

#(1) 2017年1月-2020年1月のチャート
WMA_Plot(Dat,
         term=c("2017-01-01", "2020-01-01"))
pdf_png_save(FileName="WMA_Plot_01.pdf")

#(2) 2020年1月以降のチャート
WMA_Plot(Dat, 
         term=c("2020-01-01", as.character(lubridate::today())))
pdf_png_save(FileName="WMA_Plot_02.pdf")

#(3) 2022年6月以降のチャート
WMA_Plot(Dat, 
         term=c("2022-06-01", as.character(lubridate::today())),
         lag=5, xax = 0.1, yax = 0.9999)
pdf_png_save(FileName="WMA_Plot_03.pdf")

以下に可視化結果を示します。

©️ 2022 京橋のバイオインフォマティシャンの日常 by skume

WMAの長期トレンドから見ても、2万円以下は安いのかもですね。。

WMAの長期トレンドを網掛け(ポリゴン)表示について

WMAの長期トレンドの網掛け表示をする場合は、 WMA_polygon_Plot関数で実行します。

Y軸のLog表示を切り替えられるようにしています。

#WMA_polygon_Plot.Rの読み込み
source("https://gist.githubusercontent.com/kumeS/00dfb4d5983904cbc2204d22ae156f01/raw/620c9dbe7bf694fa551c580efee31f9dbafd4644/WMA_polygon_Plot.R")

#Log表示
WMA_polygon_Plot(Dat, term=c("2020-01-01", as.character(lubridate::today())),
                lag=5, xax = 0.015, yax = 0.999, xcex=0.9, ycex=0.8, rou=0,
                M1=0.15, m1=0.9, line_col=T, LogY=T)
pdf_png_save(FileName="WMA_polygon_Plot_01.pdf")

#非Log表示
WMA_polygon_Plot(Dat, term=c("2020-01-01", as.character(lubridate::today())),
                lag=5, xax = 0.1, yax = 0.999, xcex=0.9, ycex=0.8, rou=0,
                M1=0.15, m1=0.8, line_col=T, LogY=F)
pdf_png_save(FileName="WMA_polygon_Plot_02.pdf")

以下に可視化結果を示します。

©️ 2022 京橋のバイオインフォマティシャンの日常 by skume

網掛け表示の方が、やや見易いような気がしますね。

SMAの長期トレンドを網掛け(ポリゴン)表示について: 2022年11月14日更新

SMAの長期トレンドの網掛け表示をする場合は、 SMA_polygon_Plot関数で実行します。

#getBTC.Rの読み込み
source("https://gist.githubusercontent.com/kumeS/c466e97d42f6facbb8ba5a22ed361bf4/raw/07728e5e4810e53aa8bdcc411ac51603943761df/getBTC.R")

#BTCのデータ取得
Dat <- getBTC()

#SMA_polygon_Plot.Rの読み込み
source("https://gist.githubusercontent.com/kumeS/ffde4bc3811f0b0549121173790c6ff5/raw/5d2f5b45beabe65cb35201b01c481ddafe324b36/SMA_polygon_Plot.R")
source("https://gist.githubusercontent.com/kumeS/bad9ace466970bd13b5afd15eed6f9b6/raw/8cd41b26c00696c59be98b4ce27a4f726ad27cae/pdf_png_save.R")

#(1) 2019-01-01以降のチャート
SMA_polygon_Plot(Dat, term=c("2019-01-01", as.character(lubridate::today())),
                 lag=5, xax = 0.06, yax = 0.999, xcex=0.9, ycex=0.8, rou=0,
                 M1=0.15, m1=0.9, line_col=T, LogY=F)
pdf_png_save(FileName="SMA_polygon_Plot_01.pdf")

#(2) 2022-01-01以降のチャート
SMA_polygon_Plot(Dat, term=c("2022-01-01", as.character(lubridate::today())),
                 lag=5, xax = 0.06, yax = 0.9998, xcex=0.9, ycex=0.8, rou=0,
                 M1=0.15, m1=0.6, line_col=T, LogY=F)
pdf_png_save(FileName="SMA_polygon_Plot_02.pdf")

©️ 2022 京橋のバイオインフォマティシャンの日常 by skume

多重の単純移動平均線(SMA)によるBTCのトレンド解析

単純移動平均線(SMA)が下値のサポートとして機能するか、上値のレジスタンスとなるか長期のトレンド解析をしてみました。

trends_SMA_plot関数にて実行します。

#trends_SMA_plot.Rの読み込み
source("https://gist.githubusercontent.com/kumeS/f4813db3049e95db9dd53d528afddf72/raw/a0a2b2bd4e06147ef1a57fbfa285738c9c469d75/trends_SMA_plot.R")

#(1) 2017年1月-2020年1月のチャート
trends_SMA_plot(Dat, 
            term=c("2017-01-01", "2020-01-01"),
            lag=5, xax = 0.1, yax = 0.9999)
pdf_png_save(FileName="trends_SMA_plot_01.pdf")

#(2) 2018年1月-2020年1月のチャート
trends_SMA_plot(Dat,
            term=c("2018-10-01", "2020-01-01"),
            M1=0.25, lag=5, xax = 0.1, yax = 0.99995)
pdf_png_save(FileName="trends_SMA_plot_02.pdf")

#(3) 2020年1月以降のチャート
trends_SMA_plot(Dat,
            term=c("2020-01-01", as.character(lubridate::today())),
            lag=5, xax = 0.1, yax = 0.99995)
pdf_png_save(FileName="trends_SMA_plot_03.pdf")

#(4) 2022年1月以降のチャート
trends_SMA_plot(Dat, 
            term=c("2022-01-01", as.character(lubridate::today())),
            lag=5, xax = 0.1, yax = 0.99995)
pdf_png_save(FileName="trends_SMA_plot_04.pdf")

以下に可視化結果を示します。

©️ 2022 京橋のバイオインフォマティシャンの日常 by skume

歴史的には、SMAの移動平均を5本目抜けないと強さが出ないですね。 今後も、SMA150日の移動平均を超える必要がありそうですね。

多重の加重移動平均線(WMA)によるBTCのトレンド解析

次に、WMA移動平均線が下値のサポートとして機能するか、上値のレジスタンスとなるか長期のトレンド解析をしてみました。

trends_WMA_plot関数にて実行します。

#trends_WMA_plot.Rの読み込み
source("https://gist.githubusercontent.com/kumeS/71cc142fe533d232fa51c985c2a345a7/raw/5461c64076a3cd3dfe889fcc05f6fdd8d0ccdd2e/trends_WMA_plot.R")

#(1) 2017年1月-2020年1月のチャート
trends_WMA_plot(Dat, 
                term=c("2017-01-01", "2020-01-01"),
                lag=5, xax = 0.1, yax = 0.999)
pdf_png_save(FileName="trends_WMA_plot_01.pdf")

#(2) 2018年1月-2020年1月のチャート
trends_WMA_plot(Dat,
                term=c("2018-10-01", "2020-01-01"),
                M1=0.2, lag=5, xax = 0.1, yax = 0.99995)
pdf_png_save(FileName="trends_WMA_plot_02.pdf")

#(3) 2020年1月以降のチャート
trends_WMA_plot(Dat,
                term=c("2020-01-01", as.character(lubridate::today())),
                lag=5, xax = 0.1, yax = 0.99995)
pdf_png_save(FileName="trends_WMA_plot_03.pdf")

#(4) 2022年1月以降のチャート
trends_WMA_plot(Dat, 
                term=c("2022-01-01", as.character(lubridate::today())),
                lag=5, xax = 0.1, yax = 0.99995)
pdf_png_save(FileName="trends_WMA_plot_04.pdf")

以下に可視化結果を示します。

©️ 2022 京橋のバイオインフォマティシャンの日常 by skume

WMAの方が値動きによりフィットしていて、図としても収まりが良いですね。 WMAだと、WMA200日線を超えるかどうかがポイントそうですね。

長期移動平均線(SMA)と価格の倍数から高値水準を測る

移動平均と比べて、何倍の値段で買われているかで判断する方法を紹介します。 SMA_Heatmap_Plot関数で実行します。

実際、これは、結構良い指標となりそうです。

#getBTC.Rの読み込み
source("https://gist.githubusercontent.com/kumeS/c466e97d42f6facbb8ba5a22ed361bf4/raw/07728e5e4810e53aa8bdcc411ac51603943761df/getBTC.R")

#BTCのデータ取得
Dat <- getBTC()

#SMA_Heatmap_Plot関数の読み込み
source("https://gist.githubusercontent.com/kumeS/a9aecb4914abba4b2d4d2d035b732154/raw/5024f37ff86c3fa1de06b2562519d3cd9a66e1bd/SMA_Heatmap_Plot.R")

#(1) 2017年1月-2020年1月のチャート
SMA_Heatmap_Plot(Dat,
                 term=c("2017-01-01", "2020-01-01"), SMA_periods=2000,
                 m1=0.25)
pdf_png_save(FileName="SMA_Heatmap_Plot_01.pdf")

#(2) 2018年1月-2020年1月のチャート
SMA_Heatmap_Plot(Dat,
                 term=c("2018-01-01", "2020-01-01"), SMA_periods=2000,
                 m1=0.25)
pdf_png_save(FileName="SMA_Heatmap_Plot_02.pdf")

#(3) 2020年1月以降のチャート
SMA_Heatmap_Plot(Dat,
                 term=c("2020-01-01", as.character(lubridate::today())))
pdf_png_save(FileName="SMA_Heatmap_Plot_03.pdf")

#(4) 2022年1月以降のチャート
SMA_Heatmap_Plot(Dat,
                 term=c("2022-01-01", as.character(lubridate::today())),
                 SMA_periods=2000)
pdf_png_save(FileName="SMA_Heatmap_Plot_04.pdf")

以下に可視化結果を示します。

©️ 2022 京橋のバイオインフォマティシャンの日常 by skume

歴史的には、少なくとも、倍率が1倍台にならないと、底値にはならないようですね。

今回、1を切るということは。。。どういう意味なんでしょうね。

まとめ

今回のビットコインの長期トレンド解析については、 実際、Look into bitcoin のチャートを眺めながら、アイディアを貰いました。感謝です。

また、Rプログラムを作ってるときに、大手仮想通貨取引所のFTX騒動が勃発して、FTXが破産申請、引き続き、仮想通貨暴落中。。。

仮想通貨は、いつも我々の想像を超えてきますね。。やってはいけないことを繰り返しやる、それが現代の金融システムですね。

補足

BTCの長期チャートのdygraphs表示

折角なので、dygraph関数で、BTCの長期チャートをインタラクティブに可視化してみました。

#パッケージ・インストール
pack <- c("dygraphs", "xts")
install.packages(pack[!(pack %in% unique(rownames(installed.packages())))])

#ライブラリ準備
library(dygraphs)
library(xts)

#データ読み込み
Dat <- getBTC()
rownames(Dat) <- Dat$Date
Dat.xts <- xts::as.xts(Dat)

#可視化
Dat.xts[,2] %>%
  dygraphs::dygraph(main = "BTC") %>% 
  dygraphs::dySeries("Close", label = "BTC") %>%
  dygraphs::dyRangeSelector(height = 40)  %>%
  htmltools::save_html(file="BTC_x.html")

https://kumes.github.io/Blog/R_BTC/BTC_x.html

イーサリアム(ETH)の長期チャートデータについて

ビットコインだけでなく、イーサリアム(ETH)の長期チャート用のデータを取得する関数も作成しました。

以下のように、getETH関数を使って、ETHの日足長期データを取得します。

#getETH.Rの読み込み
source("https://gist.githubusercontent.com/kumeS/3260853bb9278061a4a245a4e11092b2/raw/ea2fe720e04894456abff6c6c4e7e1ade687a85d/getETH.R")

#ETHのデータ取得
Dat <- getETH()

#表示
head(Dat)
#        Date Close Month
#1 2015-08-07  3.00    08
#2 2015-08-08  1.20    08
#3 2015-08-09  1.20    08
#4 2015-08-10  1.20    08
#5 2015-08-11  0.99    08
#6 2015-08-12  1.29    08

tail(Dat)
#           Date    Close Month
#2649 2022-11-07 1567.748    11
#2650 2022-11-08 1334.770    11
#2651 2022-11-09 1102.730    11
#2652 2022-11-10 1299.280    11
#2653 2022-11-11 1289.280    11
#2654 2022-11-12 1268.150    11

データの取得元は、ethereumprice.orgとバイナンスのデータです。

参考資料

www.statology.org

stackoverflow.com

tips-r.blogspot.com

stackoverflow.com

【Rのジミ〜な小技シリーズ】意外と分からない、ファイルを別ディレクトリに移動させる操作関数篇: filesstringsパッケージ

はじめに

過去に、R環境から行う、 フォルダの基本操作(作成・削除・コピー・名前変更・移動)について纏め記事を書きました。

skume.net

この続編的な内容で、ファイルを別ディレクトリに移動させる話となります。

Baseパッケージには適当な関数*1が用意されていないので、 便利なファイル・文字列操作ツールである、filesstringsパッケージ内の関数を使っています。

このfilesstringsパッケージは、 当初、ファイルや文字列を操作するためのパッケージとしてスタートしました。 その後、fsファイル操作パッケージとstrex文字列操作パッケージが出現し これらパッケージが提供していた機能も提供するようになっています。

そのため、これらのパッケージはfilesstringsパッケージの機能をほとんど消滅に追いやったと言われますが、 このパッケージはユニークで便利なファイル操作関数をいくつか内在しています。

今回、filesstringsパッケージのファイル操作関数を紹介します。

とあるディレクトリとファイル構成について

まずは、例題として扱う、ディレクトリとファイル構成を概説します。

#テストディレクトリ移動
setwd("./test")

#フォルダ・ファイルの表示
list.files(recursive = F)
#[1] "test_A"     "test_B.txt" "test_C.txt" "test_D.txt"

架空のフォルダ・ファイル構成として、 testというディレクトリに入ると、そこには、 test_Aというフォルダ、またtest_B.txtなどのファイルがあると仮定します。

例題として、test_Aというあるフォルダに、 test_Btest_C.txtとかのファイルを移動させます。

 

まずは、filesstringsパッケージをインストールする

install.packages関数で、パッケージのインストールを行います。

#filesstringsのインストール
install.packages("filesstrings")
library(filesstrings)

ファイルを移動させる関数群

ファイルを移動させる関数*2には、move_files関数とfile.move関数があります。

以下の通り、move_filesとfile.moveは、全く同じ使い方です。

#move_filesを使う場合
filesstrings::move_files(files="test_B.txt", 
                         destinations="test_A",
                         overwrite = FALSE)
#1 files moved. 0 failed.

#file.moveを使う場合
filesstrings::file.move(files="test_C.txt", 
                        destinations="test_A",
                        overwrite = FALSE)
#1 files moved. 0 failed.

この関数では、

  • files: 移動するファイル/フォルダの文字ベクトル(相対パスまたは絶対パス)。
  • destinations: ファイルを移動させる先のディレクトリの文字ベクトル.
  • overwrite: ファイルの上書きを許可するか?デフォルトは「FALSE」。

が引数として指定できます。

同時に、N個のものを移動させる場合

#両ファイルをベクトルの変数に入れる
Files <- c("test_B.txt", "test_C.txt")
#OR dir関数を使う
#Files <- dir("test_A", pattern = "txt")

#file.moveを使う場合
filesstrings::file.move(files=paste0("./test_A/", Files), 
                        destinations="../test",
                        overwrite = FALSE)
#2 files moved. 0 failed.

 

これを実行すると、元のファイル構成に戻っています。

つまりは、filesの引数に文字列ベクトルを与えることで、複数のファイルを同時に移動させられます。

まとめ

filesstringsパッケージは時々重宝します。

参考資料

github.com

*1:リネイムとかはあるが

*2:つまりは、指定したファイルを指定したディレクトリに移動する関数である。

Prospects for wide area imaging and comprehensive morphological analysis of biological tissues using electron microscopy : Introduction

The above illustration is reprinted from : Omics - Wikipedia

Introduction

Biological tissues contain countless amounts of extremely fine and complex morphological information. In order to understand such complex morphological information, visualization techniques such as microscopy, imaging techniques and tissue staining methods have been developed so far.

In particular, electron microscopy, the highest resolution imaging method, is used to observe the ultra-fine morphological structures in biological tissues. The morphological information obtained by electron microscopy includes nano-scale information on the tissues and cells, such as the shapes of cell nuclei, the state of chromatin DNA, the morphology of intracellular organelles such as mitochondria, Golgi apparatus, and endoplasmic reticulum (ER), as well as the neural networks and synaptic structure between neurons in the central nervous system of brain.

Biological morphome

The term "Morphome" (also "biological morphome") is used to refer to the aggregate of morphological characteristics in biological tissues and cells. The morphome such as the morphological information of tissues and cells is expressed as the sum of molecular dynamics such as DNA information (genome) and RNA information (transcriptome), and it is also regarded as morphological phenotype information. Most of the morphological data are image data, which are seemingly very different from the sequence data and numerical data mainly used in the conventional omics data (genomics, transcriptomics and metabolomics) of molecular biology.

Morphomics

Morphomics, a research field to handle a large-scale of morphological data, is now emerging through the application of the latest imaging and information processing technologies. This means that the quantitative information on morphological images is treated as new omics information by combining comprehensive morphological information and bioimage informatics.

In this article, as one of the imaging techniques for comprehensively acquiring morphological information of biological tissues, I introduce a scanning electron microscope (SEM) for the wide-area imaging of tissue sections, combined with an array tomography method for producing continuous ultra-thin sections. For bioimaging and informatics research, it is also important to try to identify various morphological information from the large-scale image information "image big data", and to analyze the shape and 3D volume of cells quantitatively. As one of the technologies contributing to the realization of such image processing and analysis, I outline deep learning (DL) and its applications.