LoginSignup
7
10

More than 5 years have passed since last update.

bitFlyerのMACDを算出する

Last updated at Posted at 2018-02-08

概要

注文アルゴリズムの指標としてMACDを使いたかったが、該当するリクエストが無かったので直接算出してみた。

参考にさせていただいたページ

ソースコード

macd.py
# -*- coding: utf-8 -*-

#http://www.bitbityen.com/entry/2017/11/22/080000

import json
import requests
import datetime

if __name__ == "__main__":
    #ローソク足の時間(秒)を指定
    periods = ["60"]

    prices = [[] for i in range(len(periods))]
    dates = [[] for i in range(len(periods))]

    #クエリパラメータを指定
    query = {"periods":','.join(periods)}

    #ローソク足取得
    res = json.loads(requests.get("https://api.cryptowat.ch/markets/bitflyer/btcfxjpy/ohlc",params=query).text)["result"]

    #表示[UNIX timestamp, 始値, 高値, 安値, 終値, 出来高]
    i = 0
    for period in periods:
        print("period = "+period)
        row = res[period]
        length = len(row)
        for column in row[:length-100:-1]:
            column[0] = datetime.datetime.fromtimestamp(column[0]).strftime( '%Y-%m-%d %H:%M:%S' )
            prices[i].insert(0,column[4])
            dates[i].insert(0,column[0])
            print (column)
        i += 1

import numpy as np
from matplotlib import pyplot as plt

#http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages
def ema_m(prices,minutes):
    results = []
    mul = 2/(minutes+1)
    sma = sum(prices[0:minutes])/minutes
    for i in range(len(prices)-minutes-1):
        if i == 0:
            ema = (prices[i+minutes+1] - sma)*mul + sma
        else:
            ema = (prices[i+minutes+1] - ema)*mul + ema
        results.insert(0,ema)
    #要素数[サンプル数-分足数]のEMAを0から新しい順に格納
    return results

#http://utisam.hateblo.jp/entry/2015/06/21/170006

if __name__ == "__main__":
    macd = []
    ema10 = np.array(ema_m(prices[0], 10))#90
    ema26 = np.array(ema_m(prices[0], 26))#74
    for i in range(len(ema26)):
        macd.append(ema10[i]-ema26[i])#74
    x = np.arange(0, len(dates[0]))
    plt.figure(figsize=(10, 4)) # サイズは適当に調整
    plt.xticks(x[0:len(signal)])
    #plt.plot(x[:len(signal)], ema10[len(signal)-1::-1])
    #plt.plot(x[:len(signal)], ema26[len(signal)-1::-1])
    plt.plot(x[:len(macd)], macd[::-1])
    plt.show()

備考

シグナルラインもemac(macd,9)で算出できるはずなので実装したかったが、上手くいかなかったので保留した。

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