2012-10-29 10 views
7

Ich mache ein Candlestick-Diagramm mit zwei Datensätzen: [offen, hoch, niedrig, nahe] und Volumen. Ich versuche, die Bände am unteren Rand des Diagramms so zu überlagern:Matplotlib - Finanzvolumen-Overlay

finviz.com

Ich volume_overlay3 Aufruf aber statt Bars füllt die ganze Fläche der Parzelle. Was mache ich falsch?

volume_overlay3

Meine andere Option ist() zu verwenden .bar, die nicht die oben hat und nach unten Farben aber funktionieren würde, wenn ich die Skala richtig machen könnte:

enter image description here

fig = plt.figure() 
ax = fig.add_subplot(1,1,1) 

candlestick(ax, candlesticks) 

ax2 = ax.twinx() 

volume_overlay3(ax2, quotes) 

ax2.xaxis_date() 

ax2.set_xlim(candlesticks[0][0], candlesticks[-1][0]) 

ax.yaxis.set_label_position("right") 
ax.yaxis.tick_right() 

ax2.yaxis.set_label_position("left") 
ax2.yaxis.tick_left() 
+0

haupt nicht in Ökonomie - so wie ist Sie up/down-Funktion angegeben (ich denke, dass Sie jedem Balken eine Farbauswahl zwischen zwei Werten in Abhängigkeit von der/Abwärts Regel geben könnte ... zB durch Faltung des Signals mit und [-1 1] Kernel oder etwas ähnliches ... wenn es nicht nur die "plt.hold (true)" war, die fehlte – deinonychusaur

Antwort

22

Das Volume_overlay3 hat nicht für mich funktioniert. Also habe ich deine Idee ausprobiert, ein Bar-Plot zum Candlestick-Plot hinzuzufügen.

Nach dem Erstellen einer Doppelachse für das Volumen diese Achse neu positionieren (kurz machen) und den Bereich der Candlestick Y-Daten ändern, um Kollisionen zu vermeiden.

import numpy as np 
import matplotlib 
import matplotlib.pyplot as plt 
# from matplotlib.finance import candlestick 
# from matplotlib.finance import volume_overlay3 
# finance module is no longer part of matplotlib 
# see: https://github.com/matplotlib/mpl_finance 
from mpl_finance import candlestick_ochl as candlestick 
from mpl_finance import volume_overlay3 
from matplotlib.dates import num2date 
from matplotlib.dates import date2num 
import matplotlib.mlab as mlab 
import datetime 

datafile = 'data.csv' 
r = mlab.csv2rec(datafile, delimiter=';') 

# the dates in my example file-set are very sparse (and annoying) change the dates to be sequential 
for i in range(len(r)-1): 
    r['date'][i+1] = r['date'][i] + datetime.timedelta(days=1) 

candlesticks = zip(date2num(r['date']),r['open'],r['close'],r['max'],r['min'],r['volume']) 

fig = plt.figure() 
ax = fig.add_subplot(1,1,1) 

ax.set_ylabel('Quote ($)', size=20) 
candlestick(ax, candlesticks,width=1,colorup='g', colordown='r') 

# shift y-limits of the candlestick plot so that there is space at the bottom for the volume bar chart 
pad = 0.25 
yl = ax.get_ylim() 
ax.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1]) 

# create the second axis for the volume bar-plot 
ax2 = ax.twinx() 


# set the position of ax2 so that it is short (y2=0.32) but otherwise the same size as ax 
ax2.set_position(matplotlib.transforms.Bbox([[0.125,0.1],[0.9,0.32]])) 

# get data from candlesticks for a bar plot 
dates = [x[0] for x in candlesticks] 
dates = np.asarray(dates) 
volume = [x[5] for x in candlesticks] 
volume = np.asarray(volume) 

# make bar plots and color differently depending on up/down for the day 
pos = r['open']-r['close']<0 
neg = r['open']-r['close']>0 
ax2.bar(dates[pos],volume[pos],color='green',width=1,align='center') 
ax2.bar(dates[neg],volume[neg],color='red',width=1,align='center') 

#scale the x-axis tight 
ax2.set_xlim(min(dates),max(dates)) 
# the y-ticks for the bar were too dense, keep only every third one 
yticks = ax2.get_yticks() 
ax2.set_yticks(yticks[::3]) 

ax2.yaxis.set_label_position("right") 
ax2.set_ylabel('Volume', size=20) 

# format the x-ticks with a human-readable date. 
xt = ax.get_xticks() 
new_xticks = [datetime.date.isoformat(num2date(d)) for d in xt] 
ax.set_xticklabels(new_xticks,rotation=45, horizontalalignment='right') 

plt.ion() 
plt.show() 

plot

data.csv ist hier oben: http://pastebin.com/5dwzUM6e

+0

Danke! Das ist großartig. Ich konnte nicht denken heraus, wie man den Bereich des Volumendiagramms justiert.Dies funktioniert. – nathancahill

+0

super klare, hilfreiche Kommentare darüber, was die Nummern/Funktionen tun und warum - das war genau das, was ich brauchte. Danke! – dwanderson

0

Wenn Sie Diagramme auf aufeinander (dh zeichnen sie auf der gleichen Achse) stapeln verwenden:

plt.hold(True) 
1

Siehe Antwort here. Anscheinend ein Bug und es wird behoben werden.

Jetzt müssen Sie die zurückgegebene Sammlung aus dem volume_overlay3-Aufruf einer Variablen zuweisen und diese dem Diagramm hinzufügen.

vc = volume_overlay3(ax2, quotes) 
ax2.add_collection(vc) 
+0

Ich habe das versucht, aber nicht bekommen die Lautstärke-Überlagerung wie in der gewählten Antwort gezeigt.Haben Sie die gleiche Lautstärke-Überlagerung wie in der gewählten Antwort? – phan