LoginSignup
16
16

More than 3 years have passed since last update.

金融予測ラベリング応用編: トリプルバリア戦略

Posted at

どうも、KOです。
今回はこちらの本に紹介されているラベリング手法トリプルバリア法を実装していきたいと思います。

トリプルバリア法とは

Horizon based barrier.png

取引開始時刻を$t$としたときに、3つの条件を決めます。

  • 利益確定幅
  • 損切幅
  • 最大保有期間

上の図のようになりますが、3つのバリアが出来ることから、トリプルバリアという名前がつきました。

データセット

1949年からの日経平均株価の終値を使用します。

close
DATE
1949-05-16 176.21
1949-05-17 174.80
1949-05-18 172.53
1949-05-19 171.34
1949-05-20 169.20

実装

今回は利益確定を2%,損失を1%として、最大保有期間を5日とします。
もし5日間ほとんど値段が動かなかった場合は、5日目の終値で決済することにします。

take_profit = 0.02
stop_loss = 0.01
max_holding_period = 5

prices = df["close"].values
label = [0]*len(prices)

for n in range(len(prices)-max_holding_period):
    outcome = 0
    for m in range(1, max_holding_period):
        if prices[n+m]/prices[n] > 1+take_profit:
            outcome = take_profit
            break
        if prices[n+m]/prices[n] < 1-stop_loss:
            outcome = -stop_loss
            break
    if outcome == 0:
        outcome = prices[n+m]/prices[n]-1
    label[n] = outcome
labels = pd.DataFrame(label, index=df.index, columns=["bin"])
labels
bin
DATE
1949-05-16 -0.01
1949-05-17 -0.01
1949-05-18 -0.01
1949-05-19 -0.01
1949-05-20 0.02

対インデックス比較

triple_barrier_label.png

今回は複利で行う。また、戦略としては毎日資産の20%を賭け、前章の戦略で清算する。
戦後からバブル自体までの上昇トレンドでもそうだが、バブル崩壊でもほとんど損をせずに勝ち続けている。
もちろん、これは取引コストやスリッページを無視しているため、実際に運用する際のパフォーマンス自体はこれよりは悪くなるのだが、バブル崩壊後の局面でも損をせずに利益を出し続けているのはすごいことだと思う。
基本的に3パラメーターを最適化することによって、より良い戦略にすることも出来ると思う。

結果

今回はトリプルバリア法に関する紹介をしたが、皆さんもパラメーターを色々と弄ったり、最適化してみてはいかがでしょうか?

16
16
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
16
16