Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带pandas.io.data的Python ta库:烛台未打印,但其他图表正常_Python_Pandas_Candlestick Chart_Ta Lib - Fatal编程技术网

带pandas.io.data的Python ta库:烛台未打印,但其他图表正常

带pandas.io.data的Python ta库:烛台未打印,但其他图表正常,python,pandas,candlestick-chart,ta-lib,Python,Pandas,Candlestick Chart,Ta Lib,iPython 2.3.1,OS-X约塞米蒂10.10.2 Python打印(sys.version): 2.7.6(默认,2014年9月9日,15:04:36) [GCC 4.2.1兼容苹果LLVM 6.0(clang-600.0.39)] 以下代码适用于为美国股票数据提取的数据,例如,为英特尔设置安全id“INTC”。然而,当我访问欧洲股票的数据时,烛台功能失败,即使所有OHLC数据都在数据框中。我在这里输入了完整的代码,以显示其他技术分析图表对欧洲股市数据的绘制效果良好 import pa

iPython 2.3.1,OS-X约塞米蒂10.10.2
Python打印(sys.version):
2.7.6(默认,2014年9月9日,15:04:36)
[GCC 4.2.1兼容苹果LLVM 6.0(clang-600.0.39)]

以下代码适用于为美国股票数据提取的数据,例如,为英特尔设置安全id“INTC”。然而,当我访问欧洲股票的数据时,烛台功能失败,即使所有OHLC数据都在数据框中。我在这里输入了完整的代码,以显示其他技术分析图表对欧洲股市数据的绘制效果良好

import pandas.io.data as web
import pandas as pd
import numpy as np
import talib as ta
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.dates import date2num
from matplotlib.finance import candlestick
import datetime

ticker = 'DNO.L'

# Download sample data
sec_id = web.get_data_yahoo(ticker, '2014-06-01')

# Data for matplotlib finance plot
sec_id_ochl = np.array(pd.DataFrame({'0':date2num(sec_id.index),
                                  '1':sec_id.Open,
                                  '2':sec_id.Close,
                                  '3':sec_id.High,
                                  '4':sec_id.Low}))

# Technical Analysis
SMA_FAST = 50
SMA_SLOW = 200
RSI_PERIOD = 14
RSI_AVG_PERIOD = 15
MACD_FAST = 12
MACD_SLOW = 26
MACD_SIGNAL = 9
STOCH_K = 14
STOCH_D = 3
SIGNAL_TOL = 3
Y_AXIS_SIZE = 12

analysis = pd.DataFrame(index = sec_id.index)

analysis['sma_f'] = pd.rolling_mean(sec_id.Close, SMA_FAST)
analysis['sma_s'] = pd.rolling_mean(sec_id.Close, SMA_SLOW)
analysis['rsi'] = ta.RSI(sec_id.Close.as_matrix(), RSI_PERIOD)
analysis['sma_r'] = pd.rolling_mean(analysis.rsi, RSI_AVG_PERIOD) # check shift
analysis['macd'], analysis['macdSignal'], analysis['macdHist'] = \
    ta.MACD(sec_id.Close.as_matrix(), fastperiod=MACD_FAST, slowperiod=MACD_SLOW, signalperiod=MACD_SIGNAL)
analysis['stoch_k'], analysis['stoch_d'] = \
    ta.STOCH(sec_id.High.as_matrix(), sec_id.Low.as_matrix(), sec_id.Close.as_matrix(), slowk_period=STOCH_K, slowd_period=STOCH_D)

analysis['sma'] = np.where(analysis.sma_f > analysis.sma_s, 1, 0)
analysis['macd_test'] = np.where((analysis.macd > analysis.macdSignal), 1, 0)
analysis['stoch_k_test'] = np.where((analysis.stoch_k < 50) & (analysis.stoch_k > analysis.stoch_k.shift(1)), 1, 0)
analysis['rsi_test'] = np.where((analysis.rsi < 50) & (analysis.rsi > analysis.rsi.shift(1)), 1, 0)

# Prepare plot
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex=True)
ax1.set_ylabel(ticker, size=20)

#size plot
fig.set_size_inches(15,30)

# Plot candles
candlestick(ax1, sec_id_ochl, width=0.5, colorup='g', colordown='r', alpha=1)

# Draw Moving Averages
analysis.sma_f.plot(ax=ax1, c='r')
analysis.sma_s.plot(ax=ax1, c='g')

#RSI
ax2.set_ylabel('RSI', size=Y_AXIS_SIZE)
analysis.rsi.plot(ax = ax2, c='g', label = 'Period: ' + str(RSI_PERIOD))        
analysis.sma_r.plot(ax = ax2, c='r', label = 'MA: ' + str(RSI_AVG_PERIOD))
ax2.axhline(y=30, c='b')
ax2.axhline(y=50, c='black')
ax2.axhline(y=70, c='b')
ax2.set_ylim([0,100])
handles, labels = ax2.get_legend_handles_labels()
ax2.legend(handles, labels)

# Draw MACD computed with Talib
ax3.set_ylabel('MACD: '+ str(MACD_FAST) + ', ' + str(MACD_SLOW) + ', ' + str(MACD_SIGNAL), size=Y_AXIS_SIZE)
analysis.macd.plot(ax=ax3, color='b', label='Macd')
analysis.macdSignal.plot(ax=ax3, color='g', label='Signal')
analysis.macdHist.plot(ax=ax3, color='r', label='Hist')
ax3.axhline(0, lw=2, color='0')
handles, labels = ax3.get_legend_handles_labels()
ax3.legend(handles, labels)

# Stochastic plot
ax4.set_ylabel('Stoch (k,d)', size=Y_AXIS_SIZE)
analysis.stoch_k.plot(ax=ax4, label='stoch_k:'+ str(STOCH_K), color='r')
analysis.stoch_d.plot(ax=ax4, label='stoch_d:'+ str(STOCH_D), color='g')
handles, labels = ax4.get_legend_handles_labels()
ax4.legend(handles, labels)
ax4.axhline(y=20, c='b')
ax4.axhline(y=50, c='black')
ax4.axhline(y=80, c='b')

plt.show()
将pandas.io.data作为web导入
作为pd进口熊猫
将numpy作为np导入
将塔利布作为ta导入
将matplotlib.pyplot作为plt导入
将matplotlib.gridspec导入为gridspec
从matplotlib.dates导入date2num
从matplotlib.finance导入烛台
导入日期时间
股票代码='DNO.L'
#下载示例数据
sec_id=web.get_data_yahoo(股票代码'2014-06-01')
#matplotlib财务图的数据
sec_id_ochl=np.array(pd.DataFrame({'0':date2num(sec_id.index)),
“1”:sec_id.Open,
“2”:sec_id.Close,
“3”:sec_id.High,
'4':sec_id.Low})
#技术分析
SMA_FAST=50
SMA_慢速=200
RSI_周期=14
RSI平均周期=15
MACD_FAST=12
MACD_慢速=26
MACD_信号=9
STOCH_K=14
STOCH_D=3
信号_TOL=3
Y轴尺寸=12
分析=局部数据帧(索引=秒id.索引)
分析['sma_f']=pd.滚动平均值(秒id.Close,sma_FAST)
分析['SMAU s']=pd.滚动平均值(秒id关闭,sma慢)
分析['rsi']=ta.rsi(秒id.Close.as_矩阵(),rsi周期)
分析['sma_r']=pd.滚动平均值(分析.rsi,rsi_平均周期)#检查班次
分析['macd',分析['macdSignal',分析['macdHist']=\
ta.MACD(sec_id.Close.as_matrix(),fastperiod=MACD_FAST,slowperiod=MACD_SLOW,signalperiod=MACD_SIGNAL)
分析,分析\
ta.STOCH(sec_id.High.as_matrix()、sec_id.Low.as_matrix()、sec_id.Close.as_matrix()、slowk_period=STOCH_K、slowd_period=STOCH_D)
analysis['sma']=np.where(analysis.sma\u f>analysis.sma\u s,1,0)
分析['macd_测试']=np.where((analysis.macd>analysis.macdSignal),1,0)
分析['stoch_k_检验]=np.where((analysis.stoch_k<50)和(analysis.stoch_k>analysis.stoch_k.shift(1)),1,0)
分析['rsi_测试]=np.式中((analysis.rsi<50)和(analysis.rsi>analysis.rsi.shift(1)),1,0)
#整地
图(ax1、ax2、ax3、ax4)=plt.子批(4、1、sharex=True)
ax1.设置标签(股票代码,大小=20)
#尺寸图
图设置尺寸英寸(15,30)
#点蜡烛
烛台(ax1,秒id,宽度=0.5,上色=g,下色=r,阿尔法=1)
#画移动平均线
分析图(ax=ax1,c='r')
分析图(ax=ax1,c='g')
#RSI
ax2.设置Y轴标签('RSI',大小=Y轴大小)
analysis.rsi.plot(ax=ax2,c='g',label='Period:'+str(rsi_Period))
分析图(ax=ax2,c='r',label='MA:'+str(RSI平均周期))
ax2.axhline(y=30,c='b')
ax2.axhline(y=50,c='black')
ax2.axhline(y=70,c='b')
ax2.set_ylim([0100])
句柄,标签=ax2。获取\u图例\u句柄\u标签()
ax2.图例(手柄、标签)
#绘制用Talib计算的MACD
ax3.set_ylabel('MACD:'+str(MACD_FAST)+','+str(MACD_SLOW)+','+str(MACD_信号),size=Y_轴大小)
analysis.macd.plot(ax=ax3,color='b',label='macd')
analysis.macdSignal.plot(ax=ax3,color='g',label='Signal')
analysis.macdHist.plot(ax=ax3,color='r',label='Hist')
ax3.axhline(0,lw=2,color=0')
句柄,标签=ax3。获取\u图例\u句柄\u标签()
ax3.图例(手柄、标签)
#随机图
ax4.set_ylabel('Stoch(k,d'),大小=Y轴大小)
analysis.stoch_k.plot(ax=ax4,label='stoch_k:'+str(stoch_k),color='r')
analysis.stoch_d.plot(ax=ax4,label='stoch_d:'+str(stoch_d),color='g')
句柄,标签=ax4。获取\u图例\u句柄\u标签()
ax4.图例(手柄、标签)
ax4.axhline(y=20,c='b')
ax4.axhline(y=50,c='black')
ax4.axhline(y=80,c='b')
plt.show()

熊猫数据。索引需要转换日期类型

import matplotlib.dates as mdates

在代码中,在sec_id_ochl之前:

# Data for matplotlib finance plot
sec_id.index  = mdates.date2num(sec_id.index.to_pydatetime())

我复制了你的代码,遇到了与你相同的错误。我修好了,但需要几步。我在这里复制了固定的代码,以及我为使代码无误运行而执行的其他步骤

  • 修复由于Yahoo API中的更改而导致的pandas_datareader的问题。我从他那个里得到了一些解决方案

  • 我还使用上面@ndrw提交的答案修复了代码中的第18行

  • 我做了一些其他的改变,我已经包括在这篇文章里了。为了您的方便,我复制了下面的代码。我希望有帮助

    from pandas_datareader import data
    import pandas as pd
    import numpy as np
    import talib as ta
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    import matplotlib.gridspec as gridspec
    from matplotlib.dates import date2num
    from matplotlib.finance import candlestick_ohlc as candlestick
    import datetime
    
    ticker = 'OPK'
    
    # Download sample data
    sec_id = data.get_data_google(ticker, '2014-06-01')
    
    # Data for matplotlib finance plot
    sec_id_ochl = np.array(pd.DataFrame({'0':date2num(sec_id.index.to_pydatetime()),
            '1':sec_id.Open,
            '2':sec_id.Close,
            '3':sec_id.High,
            '4':sec_id.Low}))
    
    # Technical Analysis
    SMA_FAST = 50
    SMA_SLOW = 200
    RSI_PERIOD = 14
    RSI_AVG_PERIOD = 15
    MACD_FAST = 12
    MACD_SLOW = 26
    MACD_SIGNAL = 9
    STOCH_K = 14
    STOCH_D = 3
    SIGNAL_TOL = 3
    Y_AXIS_SIZE = 12
    
    analysis = pd.DataFrame(index = sec_id.index)
    
    analysis['sma_f'] = pd.rolling_mean(sec_id.Close, SMA_FAST)
    analysis['sma_s'] = pd.rolling_mean(sec_id.Close, SMA_SLOW)
    analysis['rsi'] = ta.RSI(sec_id.Close.as_matrix(), RSI_PERIOD)
    analysis['sma_r'] = pd.rolling_mean(analysis.rsi, RSI_AVG_PERIOD) # check shift
    analysis['macd'], analysis['macdSignal'], analysis['macdHist'] = ta.MACD(sec_id.Close.as_matrix(), fastperiod=MACD_FAST, slowperiod=MACD_SLOW, signalperiod=MACD_SIGNAL)
    analysis['stoch_k'], analysis['stoch_d'] = ta.STOCH(sec_id.High.as_matrix(), sec_id.Low.as_matrix(), sec_id.Close.as_matrix(), slowk_period=STOCH_K, slowd_period=STOCH_D)
    
    analysis['sma'] = np.where(analysis.sma_f > analysis.sma_s, 1, 0)
    analysis['macd_test'] = np.where((analysis.macd > analysis.macdSignal), 1, 0)
    analysis['stoch_k_test'] = np.where((analysis.stoch_k < 50) & (analysis.stoch_k > analysis.stoch_k.shift(1)), 1, 0)
    analysis['rsi_test'] = np.where((analysis.rsi < 50) & (analysis.rsi > analysis.rsi.shift(1)), 1, 0)
    
    # Prepare plot
    fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex=True)
    ax1.set_ylabel(ticker, size=20)
    
    #size plot
    fig.set_size_inches(15,30)
    
    # Plot candles
    candlestick(ax1, sec_id_ochl, width=0.5, colorup='g', colordown='r', alpha=1)
    
    # Draw Moving Averages
    analysis.sma_f.plot(ax=ax1, c='r')
    analysis.sma_s.plot(ax=ax1, c='g')
    
    #RSI
    ax2.set_ylabel('RSI', size=Y_AXIS_SIZE)
    analysis.rsi.plot(ax = ax2, c='g', label = 'Period: ' + str(RSI_PERIOD))
    analysis.sma_r.plot(ax = ax2, c='r', label = 'MA: ' + str(RSI_AVG_PERIOD))
    ax2.axhline(y=30, c='b')
    ax2.axhline(y=50, c='black')
    ax2.axhline(y=70, c='b')
    ax2.set_ylim([0,100])
    handles, labels = ax2.get_legend_handles_labels()
    ax2.legend(handles, labels)
    
    # Draw MACD computed with Talib
    ax3.set_ylabel('MACD: '+ str(MACD_FAST) + ', ' + str(MACD_SLOW) + ', ' + str(MACD_SIGNAL), size=Y_AXIS_SIZE)
    analysis.macd.plot(ax=ax3, color='b', label='Macd')
    analysis.macdSignal.plot(ax=ax3, color='g', label='Signal')
    analysis.macdHist.plot(ax=ax3, color='r', label='Hist')
    ax3.axhline(0, lw=2, color='0')
    handles, labels = ax3.get_legend_handles_labels()
    ax3.legend(handles, labels)
    
    # Stochastic plot
    ax4.set_ylabel('Stoch (k,d)', size=Y_AXIS_SIZE)
    analysis.stoch_k.plot(ax=ax4, label='stoch_k:'+ str(STOCH_K), color='r')
    analysis.stoch_d.plot(ax=ax4, label='stoch_d:'+ str(STOCH_D), color='g')
    handles, labels = ax4.get_legend_handles_labels()
    ax4.legend(handles, labels)
    ax4.axhline(y=20, c='b')
    ax4.axhline(y=50, c='black')
    ax4.axhline(y=80, c='b')
    
    plt.show()
    
    从pandas\u数据读取器导入数据
    作为pd进口熊猫
    将numpy作为np导入
    将塔利布作为ta导入
    将matplotlib.pyplot作为plt导入
    将matplotlib.dates导入为mdates
    将matplotlib.gridspec导入为gridspec
    从matplotlib.dates导入date2num
    从matplotlib.finance导入烛台作为烛台
    导入日期时间
    股票代码='OPK'
    #下载示例数据
    sec_id=data.get_data_google(股票代码“2014-06-01”)
    #matplotlib财务图的数据
    sec_id_ochl=np.array(pd.DataFrame({'0':date2num(sec_id.index.to_pydatetime()),
    “1”:sec_id.Open,
    “2”:sec_id.Close,
    “3”:sec_id.High,
    '4':sec_id.Low})
    #技术分析
    SMA_FAST=50
    SMA_慢速=200
    RSI_周期=14
    RSI平均周期=15
    MACD_FAST=12
    MACD_慢速=26
    MACD_信号=9
    STOCH_K=14
    STOCH_D=3
    信号_TOL=3
    Y轴尺寸=12
    分析=局部数据帧(索引=秒id.索引)
    分析['sma_f']=pd.滚动平均值(秒id.Close,sma_FAST)
    分析['SMAU s']=pd.滚动平均值(秒id关闭,sma慢)
    分析['rsi']=ta.rsi(秒id.Close.as_矩阵(),rsi周期)
    分析['sma_r']=pd.滚动平均值(分析.rsi,rsi_平均周期)#检查班次
    分析['macd']、分析['macdSignal']、分析['macdHist']=ta.macd(sec_id.Close.as_matrix(),fastperiod=macd_FAST,slowperiod=macd_SLOW,signalperiod=macd_SIGNAL)
    
    from pandas_datareader import data
    import pandas as pd
    import numpy as np
    import talib as ta
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    import matplotlib.gridspec as gridspec
    from matplotlib.dates import date2num
    from matplotlib.finance import candlestick_ohlc as candlestick
    import datetime
    
    ticker = 'OPK'
    
    # Download sample data
    sec_id = data.get_data_google(ticker, '2014-06-01')
    
    # Data for matplotlib finance plot
    sec_id_ochl = np.array(pd.DataFrame({'0':date2num(sec_id.index.to_pydatetime()),
            '1':sec_id.Open,
            '2':sec_id.Close,
            '3':sec_id.High,
            '4':sec_id.Low}))
    
    # Technical Analysis
    SMA_FAST = 50
    SMA_SLOW = 200
    RSI_PERIOD = 14
    RSI_AVG_PERIOD = 15
    MACD_FAST = 12
    MACD_SLOW = 26
    MACD_SIGNAL = 9
    STOCH_K = 14
    STOCH_D = 3
    SIGNAL_TOL = 3
    Y_AXIS_SIZE = 12
    
    analysis = pd.DataFrame(index = sec_id.index)
    
    analysis['sma_f'] = pd.rolling_mean(sec_id.Close, SMA_FAST)
    analysis['sma_s'] = pd.rolling_mean(sec_id.Close, SMA_SLOW)
    analysis['rsi'] = ta.RSI(sec_id.Close.as_matrix(), RSI_PERIOD)
    analysis['sma_r'] = pd.rolling_mean(analysis.rsi, RSI_AVG_PERIOD) # check shift
    analysis['macd'], analysis['macdSignal'], analysis['macdHist'] = ta.MACD(sec_id.Close.as_matrix(), fastperiod=MACD_FAST, slowperiod=MACD_SLOW, signalperiod=MACD_SIGNAL)
    analysis['stoch_k'], analysis['stoch_d'] = ta.STOCH(sec_id.High.as_matrix(), sec_id.Low.as_matrix(), sec_id.Close.as_matrix(), slowk_period=STOCH_K, slowd_period=STOCH_D)
    
    analysis['sma'] = np.where(analysis.sma_f > analysis.sma_s, 1, 0)
    analysis['macd_test'] = np.where((analysis.macd > analysis.macdSignal), 1, 0)
    analysis['stoch_k_test'] = np.where((analysis.stoch_k < 50) & (analysis.stoch_k > analysis.stoch_k.shift(1)), 1, 0)
    analysis['rsi_test'] = np.where((analysis.rsi < 50) & (analysis.rsi > analysis.rsi.shift(1)), 1, 0)
    
    # Prepare plot
    fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex=True)
    ax1.set_ylabel(ticker, size=20)
    
    #size plot
    fig.set_size_inches(15,30)
    
    # Plot candles
    candlestick(ax1, sec_id_ochl, width=0.5, colorup='g', colordown='r', alpha=1)
    
    # Draw Moving Averages
    analysis.sma_f.plot(ax=ax1, c='r')
    analysis.sma_s.plot(ax=ax1, c='g')
    
    #RSI
    ax2.set_ylabel('RSI', size=Y_AXIS_SIZE)
    analysis.rsi.plot(ax = ax2, c='g', label = 'Period: ' + str(RSI_PERIOD))
    analysis.sma_r.plot(ax = ax2, c='r', label = 'MA: ' + str(RSI_AVG_PERIOD))
    ax2.axhline(y=30, c='b')
    ax2.axhline(y=50, c='black')
    ax2.axhline(y=70, c='b')
    ax2.set_ylim([0,100])
    handles, labels = ax2.get_legend_handles_labels()
    ax2.legend(handles, labels)
    
    # Draw MACD computed with Talib
    ax3.set_ylabel('MACD: '+ str(MACD_FAST) + ', ' + str(MACD_SLOW) + ', ' + str(MACD_SIGNAL), size=Y_AXIS_SIZE)
    analysis.macd.plot(ax=ax3, color='b', label='Macd')
    analysis.macdSignal.plot(ax=ax3, color='g', label='Signal')
    analysis.macdHist.plot(ax=ax3, color='r', label='Hist')
    ax3.axhline(0, lw=2, color='0')
    handles, labels = ax3.get_legend_handles_labels()
    ax3.legend(handles, labels)
    
    # Stochastic plot
    ax4.set_ylabel('Stoch (k,d)', size=Y_AXIS_SIZE)
    analysis.stoch_k.plot(ax=ax4, label='stoch_k:'+ str(STOCH_K), color='r')
    analysis.stoch_d.plot(ax=ax4, label='stoch_d:'+ str(STOCH_D), color='g')
    handles, labels = ax4.get_legend_handles_labels()
    ax4.legend(handles, labels)
    ax4.axhline(y=20, c='b')
    ax4.axhline(y=50, c='black')
    ax4.axhline(y=80, c='b')
    
    plt.show()