PyAlgoTradeの日本語解説ブログ

PyAlgoTrade の勝手に日本語解説ブログ。日本語の内容に関して保証はいたしておりません。必ず本家のサイトをご確認ください。

2015年9月21日月曜日

PyAlgoTrade MACD RSI StochasticOscillator RateOfChange モメンタム指標を使う



モメンタム(慣性)ってなんだっけ

モメンタム投資とは、現在の株価のトレンドが継続することを想定した投資ストラテジーです

15分でわかるモメンタム投資より

 ということで、昨日まで上がっていたものは上がるし下がっていたものは下がる。毎日変わるような相場は休め。という戦略らしい。

PyAlgoTradeで使えるモメンタムに関する指標は以下の通り

  • MACD
  • RSI
  • StochasticOscillator
  • RateOfChange

早速プログラムで使ってみる。まずはMACD

#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
from pyalgotrade.technical import macd
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed

logging.basicConfig(filename='/tmp/exec.log')

class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        strategy.BacktestingStrategy.__init__(self, feed)
        firstEMA=12
        slowEMA=26
        signalEMA=9
        self.__macd = macd.MACD(feed[instrument].getCloseDataSeries(),firstEMA, slowEMA, signalEMA)
        self.__macdHistgram=self.__macd.getHistogram()
        self.__macdSignal=self.__macd.getSignal()
        self.__instrument = instrument

    def onBars(self, bars):
        bar = bars[self.__instrument]
        self.info("%f MACD:%s Histgram:%s Signal:%s " % (bar.getClose(),self.__macd[-1],self.__macdHistgram[-1],self.__macdSignal[-1]))


# Load the yahoo feed from the CSV file
feed = yahoofeed.Feed()
feed.addBarsFromCSV("orcl", "orcl-2000.csv")

# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "orcl")
myStrategy.run()

MACDの3つの追加パラメーターについて補足説明。 長期の移動平均と短期の移動平均の差を求めて計算するためそれぞれ指定する、またMACDの移動平均となるシグナル線の期間も指定する。標準は26,12,9が使われる。別のソフトだけど以下の動画の説明が詳しい。


次はRSI、買われすぎや売られすぎの指標として補助的に移動平均と一緒に使われることがあ多いが、ライブラリもまた移動平均と同様にデータ列と期間を与えることで使用することができる。

#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
from pyalgotrade.technical import rsi
from pyalgotrade.technical import ma
from pyalgotrade.technical import vwap
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed

logging.basicConfig(filename='/tmp/exec.log')

class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        strategy.BacktestingStrategy.__init__(self, feed)
        self.__rsi = rsi.RSI(feed[instrument].getCloseDataSeries(), 15)
        self.__instrument = instrument

    def onBars(self, bars):
        bar = bars[self.__instrument]
        self.info("%f RSI:%s " % (bar.getClose(),self.__rsi[-1]))


# Load the yahoo feed from the CSV file
feed = yahoofeed.Feed()
feed.addBarsFromCSV("orcl", "orcl-2000.csv")

# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "orcl")
myStrategy.run()



こちらもMACD同様に動画の説明が詳しい
Stochastic oscillator (ストキャスティクス)と ROCもRSI同様に計算できる。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
from pyalgotrade.technical import rsi
from pyalgotrade.technical import stoch
from pyalgotrade.technical import roc
from pyalgotrade.technical import ma
from pyalgotrade.technical import vwap
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed

logging.basicConfig(filename='/tmp/exec.log')

class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        strategy.BacktestingStrategy.__init__(self, feed)
        self.__rsi = rsi.RSI(feed[instrument].getCloseDataSeries(), 15)
        self.__so = stoch.StochasticOscillator(feed[instrument], 15)
        self.__roc =roc.RateOfChange(feed[instrument].getCloseDataSeries(), 15)
        self.__instrument = instrument

    def onBars(self, bars):
        bar = bars[self.__instrument]
        self.info("%f RSI:%s StochasticOscillator:%s RateOfChange:%s " % (bar.getClose(),self.__rsi[-1],self.__so[-1],self.__roc[-1]))


# Load the yahoo feed from the CSV file
feed = yahoofeed.Feed()
feed.addBarsFromCSV("orcl", "orcl-2000.csv")

# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "orcl")
myStrategy.run()

動画の説明はわかりやすくていいな。でもROCの説明動画は見つかりませんでした





文章での説明は、証券会社各社で提供されているがカブドッドコム証券のテクニカル分析ABCがわかりやすかった。
 テクニカル分析ABC http://kabu.com/investment/guide/technical/default.html


0 件のコメント:

コメントを投稿