Skip to content

Commit

Permalink
Modified 'candlestick' function in response to request matplotlib#2546
Browse files Browse the repository at this point in the history
1. Added ability for the candlestick function to use a "light" and "dark" theme
2. Fill color of candlestick bodies can be user definite and is based upon stock price movement on each given period
3. In the light theme (which uses a white background) candlestick shadows are dark; ie: Follows traditional candlestick format
4. In dark theme (which uses black background) candlestick shadows match box colors
  • Loading branch information
jdgd1 committed Apr 28, 2014
1 parent db02533 commit 360a6b0
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/matplotlib/finance.py
Expand Up @@ -306,7 +306,7 @@ def plot_day_summary(ax, quotes, ticksize=3,


def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
alpha=1.0):
alpha=1.0, coloredge='k', theme='light'):

"""
Expand All @@ -326,6 +326,8 @@ def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
colorup : the color of the rectangle where close >= open
colordown : the color of the rectangle where close < open
alpha : the rectangle alpha level
coloredge : the color of the rectangle border
theme : specify if using light theme or dark theme (light theme uses a white grid bg, dark uses black grid bg)
return value is lines, patches where lines is a list of lines
added and patches is a list of the rectangle patches added
Expand All @@ -334,33 +336,43 @@ def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',

OFFSET = width/2.0

# Adjust candlestick body edges based upon theme
if theme == 'light':
coloredge = 'k'
coloredgeup = 'k'
coloredgedown = 'k'
else:
coloredgeup = colorup
coloredgedown = colordown

lines = []
patches = []
for q in quotes:
t, open, close, high, low = q[:5]

# Make candlestick bodies fill based upon stock price movement for given period
if close>=open :
color = colorup
lower = open
height = close-open
vline = Line2D(xdata=(t, t), ydata=(low, high), color=coloredgeup, linewidth=0.5, antialiased=True, zorder=0)
else :
color = colordown
lower = close
height = open-close
vline = Line2D(xdata=(t, t), ydata=(low, high), color=coloredgedown, linewidth=0.5, antialiased=True, zorder=0)

vline = Line2D(
xdata=(t, t), ydata=(low, high),
color='k',
linewidth=0.5,
antialiased=True,
)
# Should look for a better way to do this
if theme != 'light':
coloredge = color

rect = Rectangle(
xy = (t-OFFSET, lower),
width = width,
height = height,
facecolor = color,
edgecolor = color,
edgecolor = coloredge,
zorder=1,
)
rect.set_alpha(alpha)

Expand Down

0 comments on commit 360a6b0

Please sign in to comment.