使用 Matplotlib 的烛台图表跳过周末

candlestick charts using Matplotlib skip weekends

我需要一些帮助。我正在 matplotlib 和 numpy 之间学习。我只是用我自己的 csv 文件从 "Intraday candlestick charts using Matplotlib" 复制一段代码,以从该代码中学习。我的代码的不同部分如下:

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))"

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

示例数据:

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

您可以通过将“日期列”(字符串)转换为日期时间对象来导入它。将其作为日期时间对象后,您可以过滤掉周末,如下所示。您可以在 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')])
'''