Skip to content

Commit

Permalink
plot line
Browse files Browse the repository at this point in the history
  • Loading branch information
Rockyzsu committed Apr 23, 2018
1 parent 850a3c4 commit 8247077
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
33 changes: 22 additions & 11 deletions plot_line.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# -*-coding=utf-8-*-
import datetime
import os
import random
import sys
import time
from optparse import OptionParser

__author__ = 'Rocky'
Expand All @@ -21,27 +23,34 @@
mpl.rcParams['axes.unicode_minus'] = False

def plot_stock_line(code,name,table_name,start='2017-10-01',save=False):
api = ts.get_apis()
engine = get_engine('db_stock',local=True)
today =datetime.datetime.now().strftime('%Y-%m-%d')
title=u'{} {} {} {}'.format(today,code,name,table_name)
if os.path.exists(title+ '.png'):
return
engine = get_engine('db_stock',local=True)

fig = plt.figure(figsize=(10,8))
base_info = pd.read_sql('tb_basic_info',engine,index_col='index')


# fig,(ax,ax2)=plt.subplots(2,1,sharex=True,figsize=(16,10))
ax=fig.add_axes([0,0.3,1,0.55])
ax2=fig.add_axes([0,0.1,1,0.25])
if code is None and name is not None:
code = base_info[base_info['name']==name]['code'].values[0]
print code
df = None
for _ in range(4):
api = ts.get_apis()

try:
df = ts.bar(code, conn=api, start_date=start)
break
except Exception,e:
print e
ts.close_apis(api)
time.sleep(random.random()*3)
continue

if df is None:
return
df = df.sort_index()
if name is None:
name = base_info[base_info['code']==code]['name'].values[0]
Expand All @@ -54,21 +63,23 @@ def plot_stock_line(code,name,table_name,start='2017-10-01',save=False):
# # ax.set_xticklabels(df['date'][::5])
# ax.set_xticklabels(df['datetime'][::20])
candlestick2_ochl(ax,df['open'],df['close'],df['high'],df['low'],width=0.5,colorup='r',colordown='g',alpha=0.6)
title=u'{} {} {} {}'.format(today,code,name,table_name)
ax.grid(True)
ax.set_title(title)
ax.plot(sma5)
ax.plot(sma20)
plt.grid(True)
ax.plot(sma5,label='MA5')
ax.legend()
ax.plot(sma20,label='MA20')
ax.legend(loc=2)
ax.grid(True)
# df['vol'].plot(kind='bar')
volume_overlay(ax2,df['open'],df['close'],df['vol'],width=0.5,alpha=0.8,colordown='g',colorup='r')
ax2.set_xticks(range(0,len(df),1))
# ax.set_xticklabels(df['date'][::5])
ax2.set_xticklabels(df['datetime'][::1])
plt.setp(ax2.get_xticklabels(), rotation=30, horizontalalignment='right')
plt.grid(True)
ax2.grid(True)
# plt.subplots_adjust(hspace=0)
if save:
path = os.path.join(os.path.dirname(__file__),'data',today)
# path = os.path.join(os.path.dirname(__file__),'data',today)
fig.savefig(title+'.png')
else:
plt.show()
Expand Down
2 changes: 1 addition & 1 deletion recordMyChoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def update_daily(self):

add_cols = u'ALTER TABLE `{}` ADD `{}` FLOAT;'.format(self.table_name, self.today)
self._exe(add_cols)
self.conn.commit()
# self.conn.commit()
api = ts.get_apis()
cmd = 'SELECT * FROM `{}`'.format(self.table_name)
cur = self._exe(cmd)
Expand Down
11 changes: 8 additions & 3 deletions yesterday_zt_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,21 @@ def plot_yesterday_zt():
engine = get_engine('db_zdt')
table_name='zrzt'
table='{}{}'.format(datetime.datetime.now().strftime('%Y%m%d'),table_name)
df = pd.read_sql(table,engine)
# if df is None:
try:
df = pd.read_sql(table,engine)
except Exception,e:
print e
return

for i in range(len(df)):
code = df.iloc[i][u'代码']
name = df.iloc[i][u'名称']
plot_stock_line(code,name,table_name=table_name, start='2018-01-01',save=True)

if __name__ == '__main__':
path = os.path.join(os.path.dirname(__file__),'data')
current = datetime.datetime.now().strftime('%Y%m%d')
path = os.path.join(os.path.dirname(__file__),'data',current)
if not os.path.exists(path):
os.mkdir(path)
os.chdir(path)
plot_yesterday_zt()

0 comments on commit 8247077

Please sign in to comment.