今天从cme网站和下一个功能计划拉动ES未来的OHLC创建一天的酒吧直播情节。有时y轴出现时带有当前价格值,有时会出现单位数值,即1,2,3,4,5 ax_setylim修复了这个问题。也没有使用另一个烛台绘图功能。
import bleach
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick2_ohlc
import matplotlib.animation as animation
from matplotlib import style
def get_data():
req=requests.get('http://www.cmegroup.com/trading/
equity-index/us- index/e-mini-sandp500.html')
html=req.text
htmlBS=BeautifulSoup(html,'lxml')
Open=(htmlBS.find('td',
id="quotesFuturesProductTable1_ESZ7_open")).contents
Last=(htmlBS.find('td',
id="quotesFuturesProductTable1_ESZ7_last")).contents
High=(htmlBS.find('td',
id="quotesFuturesProductTable1_ESZ7_high")).contents
Low=(htmlBS.find('td',
id="quotesFuturesProductTable1_ESZ7_low")).contents
#use bleach module to clean strong tag from Low
Open=bleach.clean(Open,tags=[],strip=True)
Last=bleach.clean(Last,tags=[],strip=True)
High=bleach.clean(High,tags=[],strip=True)
Low=bleach.clean(Low,tags=[],strip=True)
def clean_ohlc(x):
x=x.replace('[','')
x=x.replace(']','')
return x
Open=clean_ohlc(Open)
High=clean_ohlc(High)
Low=clean_ohlc(Low)
Last=clean_ohlc(Last)
OHLC=[Open,High,Low,Last]
OHLC=[x.strip("'").strip('"') for x in OHLC]
OHLC=[float(x) for x in OHLC]
O=[]
H=[]
L=[]
C=[]
O.append(OHLC[0])
H.append(OHLC[1])
L.append(OHLC[2])
C.append(OHLC[3])
return O,H,L,C
style.use('ggplot')
fig=plt.figure()
ax= fig.add_subplot(111)
def update(dummy):
O,H,L,C= get_data()
ax=plt.gca()
ax.clear()
ax.set_xlim([-.5,.5])
candlestick2_ohlc(ax,O,H,L,C,width=.2,
colorup='g',colordown='r',alpha=1.0)
anime=animation.FuncAnimation(fig,update,interval=25)
plt.show()