ThreeWhiteSoldiers

Author

Takafumi Kubota

Published

July 8, 2024

Abstract

This R script analyzes and visualizes selected Nikkei 225 stocks to detect the Three White Soldiers (TWS) pattern, a bullish candlestick formation. Utilizing quantmod for data retrieval, TTR for technical analysis, and dplyr for data manipulation, it downloads one year of historical stock data from Yahoo Finance. The script identifies stocks with a TWS pattern on the most recent trading day and visualizes these patterns by adding vertical orange lines with 0.3 transparency to the charts. This automated approach aids analysts and investors in efficiently spotting bullish signals in stock data.

Keywords

Time Series, Three White Soldiers, Visualization

1 Detection and Visualization of Three White Soldiers Pattern in Nikkei 225 Stocks Using R

This R script performs time series analysis and visualization of selected Nikkei 225 stocks, focusing on detecting the Three White Soldiers (TWS) pattern, a bullish candlestick formation. The analysis starts by installing and loading the necessary libraries: quantmod for financial data retrieval and analysis, TTR for technical trading rules, and dplyr for efficient data manipulation.

The script sets the date range for analysis, defining the current date and the date one year prior. This ensures that the analysis covers exactly one year of trading days, crucial for detecting patterns accurately. The selected Nikkei 225 stocks are represented by their ticker symbols, and a custom operator %ni% is defined to exclude specific symbols that are not required for the analysis.

The getSymbols function from the quantmod package is used to download historical stock data from Yahoo Finance, spanning from the defined start date to the current date. A custom function detect_three_white_soldiers is defined to identify the TWS pattern within the stock data. This pattern is characterized by three consecutive long-bodied candlesticks that open within the previous candle’s body and close near the high of the day, indicating a potential bullish reversal.

Each stock’s data is evaluated for the TWS pattern using this function. Stocks with a TWS pattern on the most recent trading day are selected for further visualization. The script dynamically creates variables to store the downloaded data for these selected stocks.

For visualization, the chartSeries function is used to plot the stock data. The script adds vertical orange lines with 0.3 transparency to the charts at points where the TWS pattern is detected. This visual enhancement highlights the pattern’s occurrence, making it easier for analysts and traders to identify potential bullish signals.

This automated approach to pattern detection and visualization streamlines the process for financial analysts, researchers, and investors, reducing manual effort and increasing efficiency. The script’s modular design allows for easy modification and expansion to include additional stocks or indices, demonstrating the power of R programming in financial data analysis. By leveraging the capabilities of R and its libraries, the script provides a robust tool for detecting and visualizing key technical patterns in stock data.

Code
# 必要なライブラリのインストールと読み込み
#install.packages("quantmod")
#install.packages("TTR")
#install.packages("dplyr")
library(quantmod)
library(TTR)
library(dplyr)

# 日付範囲の設定
# 本日の日付
today <- Sys.Date()

# 1年前の日付
one_year_ago <- today - 365

# 日経225の先頭20件のティッカーシンボル
# (注:実際のティッカーシンボルは調べる必要があります。以下は例として一部を示しています)
symbols <- c("1332.T", "1333.T", "1605.T", "1721.T", "1801.T", "1802.T", "1803.T", "1808.T",
             "1925.T", "1928.T", "1963.T", "2002.T", "2269.T", "2282.T", "2413.T", "2432.T",
             "2768.T", "2801.T", "2802.T", "2871.T", "2914.T", "3086.T", "3099.T", "3101.T",
             "3103.T", "3105.T", "3116.T", "3141.T", "3401.T", "3402.T", "3405.T", "3407.T",
             "3436.T", "3606.T", "3632.T", "3659.T", "3738.T", "3861.T", "3863.T", "3865.T",
             "4183.T", "4188.T", "4208.T", "4272.T", "4324.T", "4502.T", "4503.T", "4506.T",
             "4507.T", "4519.T", "4523.T", "4568.T", "4689.T", "4704.T", "4751.T", "4755.T",
             "4901.T", "4902.T", "4911.T", "5019.T", "5020.T", "5101.T", "5108.T", "5201.T",
             "5202.T", "5232.T", "5233.T", "5301.T", "5332.T", "5333.T", "5401.T", "5406.T",
             "5411.T", "5703.T", "5706.T", "5707.T", "5711.T", "5713.T", "5714.T", "5801.T",
             "5802.T", "5803.T", "5901.T", "5902.T", "6103.T", "6113.T", "6301.T", "6302.T",
             "6305.T", "6326.T", "6361.T", "6366.T", "6367.T", "6417.T", "6471.T", "6472.T",
             "6473.T", "6479.T", "6501.T", "6503.T", "6504.T", "6506.T", "6645.T", "6674.T",
             "6701.T", "6702.T", "6703.T", "6724.T", "6752.T", "6758.T", "6762.T", "6770.T",
             "6841.T", "6856.T", "6902.T", "6952.T", "6954.T", "6971.T", "6976.T", "6981.T",
             "6988.T", "7003.T", "7004.T", "7011.T", "7012.T", "7013.T", "7186.T", "7201.T",
             "7202.T", "7203.T", "7261.T", "7267.T", "7269.T", "7270.T", "7272.T", "7731.T",
             "7733.T", "7735.T", "7741.T", "7751.T", "7752.T", "7762.T", "7832.T", "7911.T",
             "7912.T", "7936.T", "7951.T", "8001.T", "8002.T", "8015.T", "8031.T", "8035.T",
             "8053.T", "8058.T", "8233.T", "8252.T", "8253.T", "8267.T", "8303.T", "8304.T",
             "8316.T", "8331.T", "8354.T", "8355.T", "8369.T", "8377.T", "8410.T", "8411.T",
             "8601.T", "8604.T", "8628.T", "8630.T", "8697.T", "8725.T", "8750.T", "8766.T",
             "8795.T", "8801.T", "8802.T", "8804.T", "8830.T", "9001.T", "9005.T", "9007.T",
             "9008.T", "9009.T", "9020.T", "9021.T", "9022.T", "9062.T", "9064.T")

# %ni% 演算子を定義
`%ni%` <- Negate(`%in%`)
symbols <- symbols[symbols %ni% c("3606.T", "8303.T", "8355.T", "8369.T", "9062.T")]

# 株価データの取得
getSymbols(symbols, src = "yahoo", from = one_year_ago, to = today)
  [1] "1332.T" "1333.T" "1605.T" "1721.T" "1801.T" "1802.T" "1803.T" "1808.T"
  [9] "1925.T" "1928.T" "1963.T" "2002.T" "2269.T" "2282.T" "2413.T" "2432.T"
 [17] "2768.T" "2801.T" "2802.T" "2871.T" "2914.T" "3086.T" "3099.T" "3101.T"
 [25] "3103.T" "3105.T" "3116.T" "3141.T" "3401.T" "3402.T" "3405.T" "3407.T"
 [33] "3436.T" "3632.T" "3659.T" "3738.T" "3861.T" "3863.T" "3865.T" "4183.T"
 [41] "4188.T" "4208.T" "4272.T" "4324.T" "4502.T" "4503.T" "4506.T" "4507.T"
 [49] "4519.T" "4523.T" "4568.T" "4689.T" "4704.T" "4751.T" "4755.T" "4901.T"
 [57] "4902.T" "4911.T" "5019.T" "5020.T" "5101.T" "5108.T" "5201.T" "5202.T"
 [65] "5232.T" "5233.T" "5301.T" "5332.T" "5333.T" "5401.T" "5406.T" "5411.T"
 [73] "5703.T" "5706.T" "5707.T" "5711.T" "5713.T" "5714.T" "5801.T" "5802.T"
 [81] "5803.T" "5901.T" "5902.T" "6103.T" "6113.T" "6301.T" "6302.T" "6305.T"
 [89] "6326.T" "6361.T" "6366.T" "6367.T" "6417.T" "6471.T" "6472.T" "6473.T"
 [97] "6479.T" "6501.T" "6503.T" "6504.T" "6506.T" "6645.T" "6674.T" "6701.T"
[105] "6702.T" "6703.T" "6724.T" "6752.T" "6758.T" "6762.T" "6770.T" "6841.T"
[113] "6856.T" "6902.T" "6952.T" "6954.T" "6971.T" "6976.T" "6981.T" "6988.T"
[121] "7003.T" "7004.T" "7011.T" "7012.T" "7013.T" "7186.T" "7201.T" "7202.T"
[129] "7203.T" "7261.T" "7267.T" "7269.T" "7270.T" "7272.T" "7731.T" "7733.T"
[137] "7735.T" "7741.T" "7751.T" "7752.T" "7762.T" "7832.T" "7911.T" "7912.T"
[145] "7936.T" "7951.T" "8001.T" "8002.T" "8015.T" "8031.T" "8035.T" "8053.T"
[153] "8058.T" "8233.T" "8252.T" "8253.T" "8267.T" "8304.T" "8316.T" "8331.T"
[161] "8354.T" "8377.T" "8410.T" "8411.T" "8601.T" "8604.T" "8628.T" "8630.T"
[169] "8697.T" "8725.T" "8750.T" "8766.T" "8795.T" "8801.T" "8802.T" "8804.T"
[177] "8830.T" "9001.T" "9005.T" "9007.T" "9008.T" "9009.T" "9020.T" "9021.T"
[185] "9022.T" "9064.T"
Code
# 赤三兵(Three White Soldiers)を検出する関数の定義
detect_three_white_soldiers <- function(stock_data) {
  opens <- Op(stock_data)
  closes <- Cl(stock_data)
  
  # 赤三兵の検出条件
  three_white_soldiers <- (lag(closes, 3) < lag(opens, 3)) &      # 4日前が陰線
    (lag(closes, 2) > lag(opens, 2)) &      # 3日前が陽線
    (lag(closes, 1) > lag(opens, 1)) &      # 2日前が陽線
    (closes > opens) &                      # 前日が陽線
    (lag(closes, 2) > lag(closes, 3)) &     # 3日前の終値が4日前の終値より高い
    (lag(closes, 1) > lag(closes, 2)) &     # 2日前の終値が3日前の終値より高い
    (closes > lag(closes, 1))               # 前日の終値が2日前の終値より高い
  
  return(three_white_soldiers)
}

# 各銘柄に対して赤三兵を検出し、直前営業日に赤三兵がある銘柄をピックアップ
picked_symbols <- list()
for (symbol in symbols) {
  stock_data <- get(symbol)
  patterns <- detect_three_white_soldiers(stock_data)
  
  # 直前営業日に赤三兵があるかチェック
  if (patterns[length(patterns)]) {
    picked_symbols[[symbol]] <- stock_data
  }
}

# ピックアップされた銘柄の株価チャートを表示
for (symbol in names(picked_symbols)) {
  stock_data <- picked_symbols[[symbol]]
  chartSeries(stock_data, name = paste(symbol, "with Three White Soldiers Patterns"), theme = chartTheme("white"), volume = FALSE)
  addTA(detect_three_white_soldiers(stock_data), on = 1, col = adjustcolor("orange", alpha.f = 0.5))
}

1.1 コードの説明

1.1.1 必要なライブラリのインストールと読み込み

必要なライブラリquantmodTTR、およびdplyrをインストールし、読み込む。quantmodは金融データの取得と解析、TTRは技術的指標の計算、dplyrはデータ操作に使用される。

1.1.2 日付範囲の設定

現在の日付todayと1年前の日付one_year_agoを設定する。これにより、過去1年間の株価データを取得する範囲を決定する。

1.1.3 日経225の先頭20件のティッカーシンボルの設定

分析対象とする日経225の先頭20件のティッカーシンボルをsymbolsとして設定する。このシンボルリストは実際のティッカーシンボルに基づく。

1.1.4 %ni% 演算子の定義とフィルタリング

カスタム演算子%ni%を定義し、指定されたティッカーシンボルを除外するために使用する。この演算子は%in%演算子の否定を作成する。

1.1.5 株価データの取得

getSymbols関数を使用して、指定されたシンボルの株価データをYahoo Financeから取得する。取得範囲は設定した日付範囲に基づく。

1.1.6 赤三兵(Three White Soldiers)を検出する関数の定義

赤三兵(Three White Soldiers)パターンを検出するための関数detect_three_white_soldiersを定義する。これは、特定の条件に基づいて過去の株価データを評価する。

  • Op:株価データの始値を取得する関数。

  • Cl:株価データの終値を取得する関数。

  • lag:指定された日数分、時系列データをシフトする関数。

1.1.7 各銘柄に対して赤三兵を検出し、直前営業日に赤三兵がある銘柄をピックアップ

各ティッカーシンボルに対して、赤三兵パターンを検出し、直前の営業日に赤三兵パターンがあるかどうかを確認する。該当する銘柄をpicked_symbolsリストに追加する。

1.1.8 ピックアップされた銘柄の株価チャートを表示

ピックアップされた銘柄の株価チャートを表示し、赤三兵パターンが発生した場所に透明度0.3のオレンジ色の縦線を引く。

  • chartSeries:株価データのチャートをプロットする関数。

  • addTA:チャートにテクニカルインジケータやカスタム描画を追加する関数。

    • col:縦線の色をオレンジに設定。

    • alpha.f:透明度を0.3に設定。