使用烛台图表_OHLC [英] Charting with Candlestick_OHLC

查看:38
本文介绍了使用烛台图表_OHLC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import pandas as pd 
import numpy as np
from matplotlib.finance import candlestick_ohlc
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
import io
import datetime
import urllib
import urllib.request
%matplotlib notebook


urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/GOOG/chartdata;
              type=quote;range=1y/csv'

with urllib.request.urlopen(urlToVisit) as response:
    sourcePage = response.read().decode('utf-8')


df = pd.read_csv(io.StringIO(sourcePage), skiprows=18, header=None, sep=",", 
                 names=['date','closeP','highP','lowP','openP','volume'],
                 index_col= 0, parse_dates= True)

if 'volume' not in df:
        df['volume'] = np.zeros(len(df))

DATA = df[['openP', 'highP', 'lowP', 'closeP','volume']].values

f1 = plt.subplot2grid((6,4), (1,0), rowspan=6, colspan=4, axisbg='#07000d')
candlestick_ohlc(f1, DATA, width=.6, colorup='#53c156', colordown='#ff1717')

f1.grid('on')
f1.xaxis.set_major_locator(mticker.MaxNLocator(15))
f1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.subplots_adjust(left=.09, bottom=.14, right=.94, top=.95, wspace=.20, hspace=0)
plt.xlabel('Date')
plt.ylabel('Stock Price')
plt.show()

问题来了,当我尝试绘制candlestick_ohlc"时,它只绘制了成交量条形图!(为什么会发生这种情况?)我在想问题可能与我的约会有关吗?顺便说一句,我正在使用 iPython Notebook.我的 来源来自 - 雅虎财经.如果你注意到了,我跳过了前 18 行,这样我就可以直接看到实际数据本身,它看起来像:

So here's the problem, when I try to plot the 'candlestick_ohlc' but it only plots the volume bar chart! (Why is this happening?) I'm thinking that maybe the problem has to do with my dates? I'm using iPython Notebook btw. My source is from - Yahoo Finance. If you notice, I skipped the first 18 lines so that I can get straight to the actual data itself and it looks like:

20150302,569.7757,570.5834,557.2202,558.9953,2129600
20150303,572.0694,573.8146,564.9689,568.8881,1704700
20150304,571.8001,575.5299,566.4548,570.3043,1876800
20150305,573.7548,576.3277,571.8400,573.4456,1389600
20150306,566.1307,575.1011,565.2082,573.3060,1659100
20150309,567.2925,568.7086,561.9921,565.3079,1062100

日期、收盘价、最高价、最低价、开盘价、成交量

date,close,high,low,open,volume

有什么想法吗?将不胜感激任何帮助!

Any ideas? Would appreciate any help!!

推荐答案

所以在@DSM 的帮助下,

So with the help of @DSM,

DATA = df[['openP', 'highP', 'lowP', 'closeP','volume']]
DATA = DATA.reset_index()
DATA["date"]  = DATA["date"].apply(mdates.date2num)

f1 = plt.subplot2grid((6,4), (1,0), rowspan=6, colspan=4, axisbg='#07000d')
candlestick_ohlc(f1, DATA.values, width=.6, colorup='#53c156', colordown='#ff1717')

解决了问题!归功于他.

fixed the problem! Credits to him.

这篇关于使用烛台图表_OHLC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆