LoginSignup
2
2

More than 5 years have passed since last update.

QuantXでHLバンドを実装してみた

Posted at

今回やったこと

投資アルゴリズム開発プラットフォーム「QuantX」で、HLバンドを実装してみました。

HLバンドとは

HLバンドは、直近の高値、安値のブレイクを捉えるテクニカル指標です。
Hバンド:前日から遡ってn日間の高値
Lバンド:前日から遡ってn日間の安値
中心線:HバンドとLバンドの平均
の3本線で構成されます。

今回はシンプルに、Hバンドを上向きにブレイクしたポイントを買い、Lバンドを下向きにブレイクしたポイントを売りとしてシグナルを生成してみます。

アルゴリズム

今回作ったアルゴリズムはこちら

import pandas as pd
import numpy as np 

def initialize(ctx):
    # 設定
    ctx.logger.debug("initialize() called")
    ctx.configure(
      channels={          # 利用チャンネル
        "jp.stock": {
          "symbols": [
            "jp.stock.1305",
            "jp.stock.9984",
            "jp.stock.9983",
            "jp.stock.7201",
            "jp.stock.9201",
            "jp.stock.9202",
            "jp.stock.7203"
          ],
          "columns": [
            "high_price_adj",    # 高値(株式分割調整後)
            "low_price_adj",     # 安値(株式分割調整後)
            "close_price_adj",    # 終値(株式分割調整後) 
          ]
        }
      }
    )

    def _my_signal(data):
      cp = data['close_price_adj'].fillna(method = 'ffill')
      hp = data['high_price_adj'].fillna(method = 'ffill')
      lp = data['low_price_adj'].fillna(method = 'ffill')
      hband = hp.rolling(20).max().shift(1)
      lband = lp.rolling(20).min().shift(1)
      hl_mean = (hband + lband)/2

      buy_sig = cp[(cp > hband) & (cp.shift(1) < hband.shift(1))]
      sell_sig = cp[(cp < lband) & (cp.shift(1) > lband.shift(1))]

      return {
        'buy:sig':buy_sig,
        'sell:sig':sell_sig,
        'hband':hband,
        'lband':lband,
      }

    # シグナル登録
    ctx.regist_signal("my_signal", _my_signal)

def handle_signals(ctx, date, current):
    '''
    current: pd.DataFrame
    '''

    done_syms = set([])
    for (sym,val) in ctx.portfolio.positions.items():
        returns = val["returns"]
        if returns < -0.03:
          sec = ctx.getSecurity(sym)
          sec.order(-val["amount"], comment="損切り(%f)" % returns)
          done_syms.add(sym)
        elif returns > 0.05:
          sec = ctx.getSecurity(sym)
          sec.order(-val["amount"], comment="利益確定売(%f)" % returns)
          done_syms.add(sym)


    buy = current["buy:sig"].dropna()
    for (sym,val) in buy.items():
        if sym in done_syms:
          continue

        sec = ctx.getSecurity(sym)
        sec.order(sec.unit() * 1, comment="SIGNAL BUY")
        ctx.logger.debug("BUY: %s,  %f" % (sec.code(), val))
        pass

    sell = current["sell:sig"].dropna()
    for (sym,val) in sell.items():
        if sym in done_syms:
          continue

        sec = ctx.getSecurity(sym)
        sec.order(sec.unit() * -1, comment="SIGNAL SELL")
        ctx.logger.debug("SELL: %s,  %f" % (sec.code(), val))
        pass

結果

スクリーンショット 2019-01-31 15.57.17.png

直近3年でこのくらいの数字。
HLバンドは上がり続ける大相場の時にこそ効果を発揮するもので、上昇局面にあった20年ほど前に人気があった手法であったそうですが、今日そこまでの大相場を形成することは珍しいので、シンプルな戦略は現在あまり効果がないようです。

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