LoginSignup
1
2

More than 1 year has passed since last update.

TA-Libのローソク足パターン認識を使う

Last updated at Posted at 2022-08-13

PythonのTA-Libの使用方法をウェブで検索すると、移動平均やボリンジャーバンドなどのOverlap Studiesの関数の記事が目立ちますが、Pattern Recognitionというカテゴリーもあります。

Pattern Recognitionはローソク足の特別なパターンを検出する関数群で、はらみ足や三羽烏、数多くのパターンを見つけてくれます。

この記事にたくさんの関数の概要と使い方の紹介がありますが、私も過去の記事で説明した方法でユーロドルの値動きから、いくつかのパターンを見つけていきたいと思います。

はらみ線

まずははらみ線です。
基本的なコードは以下の通りで、以降はtalibの関数を色々変えていきたいと思います。

import talib
from backtesting.test import EURUSD
from backtesting import Strategy, Backtest
from bokeh.io import save


class MyStrategy(Strategy):

    def init(self):
    	# 使いたい関数、関数の引数をインジケータ用のメソッドIの引数にする
        self.I(
                talib.CDLHARAMI,
                self.data.Open,
                self.data.High,
                self.data.Low,
                self.data.Close,
                )

    def next(self):
        pass


def main():
    bt = Backtest(EURUSD, MyStrategy)
    bt.run()
    # qiitaに載せやすいように調整
    fig = bt.plot(plot_volume=False, superimpose=False)
    fig.children[0].children[0][0].height = 200
    save(fig, filename='CDLchart.html')


if __name__ == '__main__':
    main()

Screenshot 2022-08-13 09.07.47.png

出力したはらみ線のフラグつきのチャートを拡大したものです。パターンが検出されると、100または-100を返すようですね。

よく見ると、2番めのフラグは当日の高値が前日の終値を超えていて、ネット情報とは挙動が異なります。talibのソースを確認すると、やはり少し実装が異なるようです。

   /* Proceed with the calculation for the requested range.
    * Must have:
    * - first candle: long white (black) real body
    * - second candle: short real body totally engulfed by the first
    * The meaning of "short" and "long" is specified with TA_SetCandleSettings
    * outInteger is positive (1 to 100) when bullish or negative (-1 to -100) when bearish; 
    * the user should consider that a harami is significant when it appears in a downtrend if bullish or 
    * in an uptrend when bearish, while this function does not consider the trend
    */
   outIdx = 0;
   do
   {
        if( TA_REALBODY(i-1) > TA_CANDLEAVERAGE( BodyLong, BodyLongPeriodTotal, i-1 ) &&         // 1st: long
            TA_REALBODY(i) <= TA_CANDLEAVERAGE( BodyShort, BodyShortPeriodTotal, i ) &&          // 2nd: short
            max( inClose[i], inOpen[i] ) < max( inClose[i-1], inOpen[i-1] ) &&                      //      engulfed by 1st
            min( inClose[i], inOpen[i] ) > min( inClose[i-1], inOpen[i-1] )
          )

	...

   } while( i <= endIdx );

確認せずに使うと、期待と異なる挙動で戸惑うところでした。TA-Libの挙動が期待とは異なることがあると心に留めておきましょう。

三羽烏

次は三羽烏です。以下の部分の変更だけでOKです。

        self.I(
		talib.CDS3BLACKCROWS,
                self.data.Open,
                self.data.High,
                self.data.Low,
                self.data.Close,
                )

Screenshot 2022-08-13 22.49.38.png

ついでにTA-Libの当該関数のコメント部分

/* Proceed with the calculation for the requested range.
 * Must have:
 * - three consecutive and declining black candlesticks
 * - each candle must have no or very short lower shadow
 * - each candle after the first must open within the prior candle's real body
 * - the first candle's close should be under the prior white candle's high
 * The meaning of "very short" is specified with TA_SetCandleSettings
 * outInteger is negative (-1 to -100): three black crows is always bearish; 
 * the user should consider that 3 black crows is significant when it appears after a mature advance or at high levels, 
 * while this function does not consider it
 */

各ローソク足のシャドウ(下ひげ?)が短くないといけないようです。TA-Libのローソク足パターン認識は少しクセがあります。

丸坊主

次は丸坊主です。関数名もCDLMARUBOZUです。

talib.CDLMARUBOZU

Screenshot 2022-08-13 23.16.45.png

こちらは狙い通りの挙動でした。

終わり

まだまだたくさんありますが、今回はこの辺で。

このようなローソク足パターンも、インアウトのタイミングとしては有用かもしれませんね。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2