烛台图表使用Matplotlib跳过周末

时间:2017-12-08 18:05:30

标签: python csv numpy matplotlib candlestick-chart

我需要一些帮助。我在matplotlib和numpy之间学习。我只是使用我自己的csv文件从“使用Matplotlib的日内烛台图表”中再现一段代码,以便从该代码中学习。我的代码的不同部分与以下内容不同:

.component.ts

我收到错误消息

import numpy as np 
import matplotlib.pyplot as plt 
import datetime

from mpl_finance import candlestick_ohlc 
from matplotlib.dates import num2date

# data in a text file, 5 columns: time, opening, close, high, low
# note that I'm using the time you formated into an ordinal float data = 
np.loadtxt("/Users/paul/Documents/python/Quant/INTC.csv",       delimiter=",")

我甚至尝试使用此行并仍然向我提供相同的错误消息

ValueError: could not convert string to float: b'Date'.

这可能是我不理解的基本概念。对于一些指导非常感兴趣。

示例数据:

data = np.genfromtxt("/Users/paul/Documents/python/Quant/INTC.csv", delimiter=",", skip_header=1, usecols=[0,1,2,3,4], dtype=(dt, float,float,float, float))"

1 个答案:

答案 0 :(得分:1)

您可以导入"日期列' (字符串)通过将其转换为datetime对象。将它作为日期时间对象后,您可以过滤掉周末,如下所示。您可以在matplotlib中绘制过滤后的数据,因为matplotlib可以理解日期时间对象。希望有所帮助。

'''
data in csv
Date,Open,High,Low,Close,Volume,Adj Close 
2017-12-06,46.599998,46.740002,46.090000,46.700001,46.700001,34035000 
2017-12-07,46.700001,47.090000,46.389999,46.779999,46.779999,24461400
2017-12-08,46.619999,46.700001,46.279999,46.700001,46.700001,21565800
2017-12-09,46.049999,46.389999,45.650002,46.299999,46.299999,25570400 
2017-12-10,46.040001,46.090000,45.380001,45.580002,45.580002,24095400
2017-12-13,45.259998,45.939999,45.250000,45.750000,45.750000,18999000
'''

import numpy as np
from datetime import datetime

# use converter to convert a string object to datetime object. Note dtype is object for all columns
data = np.genfromtxt(r'stock.csv', delimiter = ',', names = True,
                    converters={0: lambda x: datetime.strptime(x, "%Y-%m-%d")}, dtype=object)

print data

'''
[ (datetime.datetime(2017, 12, 6, 0, 0), '46.599998', '46.740002', '46.090000', '46.700001', '46.700001', '34035000')
 (datetime.datetime(2017, 12, 7, 0, 0), '46.700001', '47.090000', '46.389999', '46.779999', '46.779999', '24461400')
 (datetime.datetime(2017, 12, 8, 0, 0), '46.619999', '46.700001', '46.279999', '46.700001', '46.700001', '21565800')
 (datetime.datetime(2017, 12, 9, 0, 0), '46.049999', '46.389999', '45.650002', '46.299999', '46.299999', '25570400')
 (datetime.datetime(2017, 12, 10, 0, 0), '46.040001', '46.090000', '45.380001', '45.580002', '45.580002', '24095400')
 (datetime.datetime(2017, 12, 13, 0, 0), '45.259998', '45.939999', '45.250000', '45.750000', '45.750000', '18999000')]
'''

# check if a day is a weekday or not
def check_weekday_or_not(datetime_object):
    if datetime_object.weekday() not in [5,6]:
        # datetime.weekday() returns 5 and 6 for saturday and Sunday
        return True
    else:
        return False

# create a function to apply on each row of the matrix
vfunc =np.vectorize(check_weekday_or_not)     
filter_mask = vfunc(data['Date'])
print filter_mask
#[ True  True  True False False  True]

# Apply the filter mask to obtain an array without weekends.
print data[filter_mask]
'''
array([ (datetime.datetime(2017, 12, 6, 0, 0), '46.599998', '46.740002', '46.090000', '46.700001', '46.700001', '34035000'),
       (datetime.datetime(2017, 12, 7, 0, 0), '46.700001', '47.090000', '46.389999', '46.779999', '46.779999', '24461400'),
       (datetime.datetime(2017, 12, 8, 0, 0), '46.619999', '46.700001', '46.279999', '46.700001', '46.700001', '21565800'),
       (datetime.datetime(2017, 12, 13, 0, 0), '45.259998', '45.939999', '45.250000', '45.750000', '45.750000', '18999000')], 
      dtype=[('Date', 'O'), ('Open', 'O'), ('High', 'O'), ('Low', 'O'), ('Close', 'O'), ('Volume', 'O'), ('Adj_Close', 'O')])
'''