前提・実現したいこと
https://qiita.com/shionhonda/items/bd2a7aaf143eff4972c4
上記サイトをみつけ、おもしろそうだとおもったので、
勉強がてらソースをまねて書いています。
自分はbitbankなので、一部サイトと違う部分があります。
(サイトはbitflyer)
初めてPythonを使っての自動取引botを作成したいと思っています。
発生している問題・エラーメッセージ
TypeError Traceback (most recent call last) <ipython-input-2-7172f38f0892> in <module>() 52 # プロット用データの更新 53 raws = np.append(raws, [ltps1[itr1-1]]) ---> 54 sma1 = np.append(sma1, [ltps1.mean()]) 55 sma2.append(ltps2.mean()) 56 sgm1 = np.append(sgm1, [ltps1.std()]) D:\Anaconda3\lib\site-packages\numpy\core\_methods.py in _mean(a, axis, dtype, out, keepdims) 68 is_float16_result = True 69 ---> 70 ret = umr_sum(arr, axis, dtype, out, keepdims) 71 if isinstance(ret, mu.ndarray): 72 ret = um.true_divide( TypeError: cannot perform reduce with flexible type
該当のソースコード
#bitbankインストールPATH import sys sys.path.append("ハードコーディング") # グラフ描画に使う import numpy as np import pandas as pd import matplotlib.pyplot as plt # bitbankにアクセスするのに使う import python_bitbankcc # 時間の管理に使う import time from datetime import datetime # リアルタイムプロットに必要 from ipywidgets import FloatProgress from IPython.display import display, clear_output # Jupyter環境でグラフを表示するのに必要 %matplotlib inline # 最終取引価格、移動平均、標準偏差を格納する配列 raws = [] sma1, sma2 = [], [] sgm1, sgm2 = [], [] # 移動平均を取る幅 itr1 = 15 # 15 mins itr2 = 60 # 60 mins # 60分間の最終取引価格の配列 pub = python_bitbankcc.public() tick = pub.get_ticker( 'xrp_jpy' ) current_price = tick['last'] ltps2 = float(current_price)*np.ones(itr2) plt.ion() fig = plt.figure(figsize=(16,5)) axe = fig.add_subplot(111) while True: # 60秒ごとに稼働 if datetime.now().strftime('%S') [0:2]== '00': clear_output(wait = True) pub = python_bitbankcc.public() tick = pub.get_ticker( 'xrp_jpy' ) raws = np.append(raws, tick['last']) # 最終取引価格の更新 ltps2 = np.hstack((ltps2[1:itr2], tick['last'])) ltps1 = ltps2[itr2-itr1:itr2] # プロット用データの更新 raws = np.append(raws, [ltps1[itr1-1]]) sma1 = np.append(sma1, [ltps1.mean()]) sma2.append(ltps2.mean()) sgm1 = np.append(sgm1, [ltps1.std()]) sgm2 = np.append(sgm2, [ltps2.std()]) # プロット axe.plot(raws, "black", linewidth=2, label="Raw price") axe.plot(sma1, "r", linewidth=1, label="15min SMA") axe.plot(sma2, "g", linewidth=1, label="60min SMA") axe.plot(sma1+2*sgm1, "r", linewidth=1, linestyle="dashed", label="15min 2sigma") axe.plot(sma1-2*sgm1, "r", linewidth=1, linestyle="dashed") axe.plot(sma2+2*sgm2, "g", linewidth=1, linestyle="dashed", label="60min 2sigma") axe.plot(sma2-2*sgm2, "g", linewidth=1, linestyle="dashed") axe.legend(loc='upper left') axe.set_title("SMA and Bollinger band") display(fig) # 次の00秒まで休憩 time.sleep(57) axe.cla()
試したこと
Googleでエラーについて調べて、型を変えてみたりしたくらい。
そもそも、見当違いのことをしているのだろうけど・・・。
補足情報(FW/ツールのバージョンなど)
pythonは3.6 IDEはAnacondaのJupyter Notebookです。
エラーが出ている箇所で、どんな引数が渡っているかprintしたりして確認してみてください。sma1, [ltps1.mean()]をそれぞれprintして見れば良いです