仮想通貨×想い

仮想通貨、プログラミングのこと、最近気になることなど。。。

仮想通貨取引所 Zaif API サンプルプログラム:テクニカル指標 EMA(指数平滑移動平均),MACDを算出(python編)

 pythonで、1分ごとにEMA、MACDを算出するプログラムを作ってみました。無限ループのため、[CTRL]+[c] で終了してください。

import requests
import json
from datetime import datetime
from time import sleep

def get_lastprice():
    response = requests.get("https://api.zaif.jp/api/1/last_price/xem_jpy")
    while response.status_code != 200:
        raise Exception('return status code is {}'.format(response.status_code))
        sleep(2)
        response = requests.get("https://api.zaif.jp/api/1/last_price/xem_jpy")        
    dic = json.loads(response.text)
    prc = dic["last_price"]
    return prc

nw = lp = jd = 0
twsx = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
mcdn = [0,0,0,0,0,0,0,0,0]

while lp != 99:    # loop out at this day

    nw       = datetime.now()
    lp       = nw.day
    jd       = nw.second
    i = j = k = l = m = n = o = p =0
    sum8 = sum11 = sum25 = summd = 0
    ema9 = ema12 = ema26 = macd = signal = 0

    if jd==0:
       
        twsx[25] = get_lastprice()        

        # cal. 9-ema      
        for i in range(17,25):
            sum8 += twsx[i]
        ema9  = (sum8 +twsx[25]*2) / (9+1)
        
        # cal. 12-ema
        for j in range(14,25):
            sum11 += twsx[j]
        ema12 = (sum11+twsx[25]*2) / (12+1)
        
        # cal. 26-ema
        for k in range(0,25):
            sum25 += twsx[k]
        ema26 = (sum25+twsx[25]*2) / (26+1)
        
        # cal. MACD(macd,signal)
        macd   = mcdn[8]  = ema12 - ema26
        for l in range(0,8):
            summd += mcdn[l]
        signal = (summd+mcdn[8]*2) / (9+1)
            
        # view list for check
        for o in range(0,26):
            print("twsx[",o,"]=",twsx[o])
        for p in range(0,9):
            print("mcdn[",p,"]=",mcdn[p])
        
        # view EMA,MACD
        print("---------------------------------------")
        print("NOW    =",nw)
        print("---------------------------------------")
        print(" 9_EMA =",ema9)
        print("12_EMA =",ema12)
        print("26_EMA =",ema26)
        print("---------------------------------------")
        print("MACD   =",macd)
        print("Signal =",signal)
        print("=======================================")
        
        # shift value
        for m in range(0,25):
            twsx[m] = twsx[m+1]
        for n in range(0,8):
            mcdn[n] = mcdn[n+1]
              
        sleep(60-5)

 実行結果は次のとおりです(9分経過後)。

D:python>python ema_macd_1m.py
twsx[ 0 ]= 0
twsx[ 1 ]= 0
twsx[ 2 ]= 0
twsx[ 3 ]= 0
twsx[ 4 ]= 0
twsx[ 5 ]= 0
twsx[ 6 ]= 0
twsx[ 7 ]= 0
twsx[ 8 ]= 0
twsx[ 9 ]= 0
twsx[ 10 ]= 0
twsx[ 11 ]= 0
twsx[ 12 ]= 0
twsx[ 13 ]= 0
twsx[ 14 ]= 0
twsx[ 15 ]= 0
twsx[ 16 ]= 0
twsx[ 17 ]= 37.8198
twsx[ 18 ]= 37.7504
twsx[ 19 ]= 37.75
twsx[ 20 ]= 37.7001
twsx[ 21 ]= 37.7003
twsx[ 22 ]= 37.7003
twsx[ 23 ]= 37.7003
twsx[ 24 ]= 37.7005
twsx[ 25 ]= 37.702
mcdn[ 0 ]= 3.0169641025641023
mcdn[ 1 ]= 4.519909971509971
mcdn[ 2 ]= 6.025592022792022
mcdn[ 3 ]= 7.527309401709401
mcdn[ 4 ]= 9.031033048433049
mcdn[ 5 ]= 10.534748717948718
mcdn[ 6 ]= 12.038464387464387
mcdn[ 7 ]= 13.54219601139601
mcdn[ 8 ]= 15.046039316239316
---------------------------------------
NOW    = 2018-03-11 14:44:00.000852
---------------------------------------
 9_EMA = 37.72257
12_EMA = 29.017361538461536
26_EMA = 13.97132222222222
---------------------------------------
MACD   = 15.046039316239316
Signal = 9.63282962962963
=======================================