Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update of finance.py to (O,H,L,C) instead of (O,C,H,L) #1783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions lib/matplotlib/finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
('day', np.int8),
('d', np.float), # mpl datenum
('open', np.float),
('close', np.float),
('high', np.float),
('low', np.float),
('close', np.float),
('volume', np.float),
('aclose', np.float)])

Expand All @@ -49,7 +49,7 @@ def parse_yahoo_historical(fh, adjusted=True, asobject=False):
Parse the historical data in file handle fh from yahoo finance.

*adjusted*
If True (default) replace open, close, high, and low prices with
If True (default) replace open, high, low, close prices with
their adjusted values. The adjustment is by a scale factor, S =
adjusted_close/close. Adjusted prices are actual prices
multiplied by S.
Expand All @@ -64,14 +64,14 @@ def parse_yahoo_historical(fh, adjusted=True, asobject=False):
If False (default for compatibility with earlier versions)
return a list of tuples containing

d, open, close, high, low, volume
d, open, high, low, close, volume

If None (preferred alternative to False), return
a 2-D ndarray corresponding to the list of tuples.

Otherwise return a numpy recarray with

date, year, month, day, d, open, close, high, low,
date, year, month, day, d, open, high, low, close,
volume, adjusted_close

where d is a floating poing representation of date,
Expand Down Expand Up @@ -107,25 +107,25 @@ def parse_yahoo_historical(fh, adjusted=True, asobject=False):
aclose = float(vals[6])

results.append((dt, dt.year, dt.month, dt.day,
dnum, open, close, high, low, volume, aclose))
dnum, open, high, low, close, volume, aclose))
results.reverse()
d = np.array(results, dtype=stock_dt)
if adjusted:
scale = d['aclose'] / d['close']
scale[np.isinf(scale)] = np.nan
d['open'] *= scale
d['close'] *= scale
d['high'] *= scale
d['low'] *= scale
d['close'] *= scale

if not asobject:
# 2-D sequence; formerly list of tuples, now ndarray
ret = np.zeros((len(d), 6), dtype=np.float)
ret[:,0] = d['d']
ret[:,1] = d['open']
ret[:,2] = d['close']
ret[:,3] = d['high']
ret[:,4] = d['low']
ret[:,2] = d['close']
ret[:,5] = d['volume']
if asobject is None:
return ret
Expand Down Expand Up @@ -241,9 +241,9 @@ def plot_day_summary(ax, quotes, ticksize=3,
colorup='k', colordown='r',
):
"""
quotes is a sequence of (time, open, close, high, low, ...) sequences
quotes is a sequence of (time, open, high, low, close, ...) sequences

Represent the time, open, close, high, low as a vertical line
Represent the time, open, high, low, close as a vertical line
ranging from low to high. The left tick is the open and the right
tick is the close.

Expand All @@ -259,7 +259,7 @@ def plot_day_summary(ax, quotes, ticksize=3,
lines = []
for q in quotes:

t, open, close, high, low = q[:5]
t, open, high, low, close = q[:5]

if close>=open : color = colorup
else : color = colordown
Expand Down Expand Up @@ -301,13 +301,13 @@ def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',

"""

quotes is a sequence of (time, open, close, high, low, ...) sequences.
quotes is a sequence of (time, open, high, low, close, ...) sequences.
As long as the first 5 elements are these values,
the record can be as long as you want (eg it may store volume).

time must be in float days format - see date2num

Plot the time, open, close, high, low as a vertical line ranging
Plot the time, open, high, low, close as a vertical line ranging
from low to high. Use a rectangular bar to represent the
open-close span. If close >= open, use colorup to color the bar,
otherwise use colordown
Expand All @@ -328,7 +328,7 @@ def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
lines = []
patches = []
for q in quotes:
t, open, close, high, low = q[:5]
t, open, high, low, close = q[:5]

if close>=open :
color = colorup
Expand Down Expand Up @@ -365,12 +365,12 @@ def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
return lines, patches


def plot_day_summary2(ax, opens, closes, highs, lows, ticksize=4,
def plot_day_summary2(ax, opens, highs, lows, closes ticksize=4,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is the biggest problem - we have no way of detecting if the use is using the old interface or the new one. This kind of change is very hard for a user to track down, and is probably something that a user wouldn't even notice had changed. I don't dispute your assertion about the expected order, but it is because of this line that I am 👎 for this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do

def plot_day_summary2(ax, opens, closes, highs, lows, **kwargs):
    plot_day_summary3(ax, opens, highs, lows, closes, **kwargs)

and rename the function with the new call order plot_day_summary2 -> plot_day_summary3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, better yet, add plot_day_summary_ochl as an alias for plot_day_summary2 and then add plot_day_summary_ohlc? Then the intention is right in the name of the function, rather than just with a numerical suffix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdboom - nice suggestion - I'd be in favour of that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pelson - You are absolutely right in that it must be clear to end-users which interface they are using. In fact, it shouldn' t be that hard to check which one they are using. You could simply check whether the fourth provided list (highs in the old ochl-interface, lows in the new ohlc-interface) is always larger or equal to the fifth list (lows in the old ochl-interface, closes in the new ohlc-interface). If this argument is true, they are using the old interface and we could raise an error. If this argument is false then they are using the new interface and all is good.

@mdboom - Indeed also a very good suggestion. I would be enthousiastic about this, although I still think that the ochl-flavour should eventually be removed to avoid confusion and simplify the interface by reducing the amount of functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer @mdboom's suggestion here. I am very in favour of verbose method names.

I would be enthousiastic about this, although I still think that the ochl-flavour should eventually be removed to avoid confusion and simplify the interface by reducing the amount of functions.

I agree here, but I'd prefer to see at least a deprecation warning before this is removed. That way our users are warned about a potential future alteration that requires a change to their code.

colorup='k', colordown='r',
):
"""

Represent the time, open, close, high, low as a vertical line
Represent the time, open, high, low, close as a vertical line
ranging from low to high. The left tick is the open and the right
tick is the close.

Expand All @@ -382,7 +382,7 @@ def plot_day_summary2(ax, opens, closes, highs, lows, ticksize=4,
return value is a list of lines added
"""

# note this code assumes if any value open, close, low, high is
# note this code assumes if any value open, low, high, close is
# missing they all are missing

rangeSegments = [ ((i, low), (i, high)) for i, low, high in zip(xrange(len(lows)), lows, highs) if low != -1 ]
Expand Down Expand Up @@ -458,7 +458,7 @@ def plot_day_summary2(ax, opens, closes, highs, lows, ticksize=4,
return rangeCollection, openCollection, closeCollection


def candlestick2(ax, opens, closes, highs, lows, width=4,
def candlestick2(ax, opens, highs, lows, closes, width=4,
colorup='k', colordown='r',
alpha=0.75,
):
Expand All @@ -477,7 +477,7 @@ def candlestick2(ax, opens, closes, highs, lows, width=4,
return value is lineCollection, barCollection
"""

# note this code assumes if any value open, close, low, high is
# note this code assumes if any value open, low, high, close is
# missing they all are missing

delta = width/2.
Expand Down Expand Up @@ -599,7 +599,7 @@ def volume_overlay3(ax, quotes,
width=4, alpha=1.0):
"""
Add a volume overlay to the current axes. quotes is a list of (d,
open, close, high, low, volume) and close-open is used to
open, high, low, close, volume) and close-open is used to
determine the color of the bar

kwarg
Expand All @@ -619,22 +619,22 @@ def volume_overlay3(ax, quotes,
False : colordown,
}

dates, opens, closes, highs, lows, volumes = zip(*quotes)
dates, opens, highs, lows, closes, volumes = zip(*quotes)
colors = [colord[close1>=close0] for close0, close1 in zip(closes[:-1], closes[1:]) if close0!=-1 and close1 !=-1]
colors.insert(0,colord[closes[0]>=opens[0]])

right = width/2.0
left = -width/2.0


bars = [ ( (left, 0), (left, volume), (right, volume), (right, 0)) for d, open, close, high, low, volume in quotes]
bars = [ ( (left, 0), (left, volume), (right, volume), (right, 0)) for d, open, high, low, close, volume in quotes]

sx = ax.figure.dpi * (1.0/72.0) # scale for points
sy = ax.bbox.height / ax.viewLim.height

barTransform = Affine2D().scale(sx,sy)

dates = [d for d, open, close, high, low, volume in quotes]
dates = [d for d, open, high, low, close, volume in quotes]
offsetsBars = [(d, 0) for d in dates]

useAA = 0, # use tuple here
Expand All @@ -656,7 +656,7 @@ def volume_overlay3(ax, quotes,

minpy, maxx = (min(dates), max(dates))
miny = 0
maxy = max([volume for d, open, close, high, low, volume in quotes])
maxy = max([volume for d, open, high, low, close, volume in quotes])
corners = (minpy, miny), (maxx, maxy)
ax.update_datalim(corners)
#print 'datalim', ax.dataLim.bounds
Expand Down