/
handledb.py
115 lines (109 loc) · 3.41 KB
/
handledb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#-*- coding: utf-8 -*-
import sqlite3
import pylab
import numpy
def getFlowOrder(date):
order = []
con = sqlite3.connect('moneyflow.db')
cur = con.cursor()
sql = 'select * from lxjlr where date=' + '"' + date + '" ' + 'order by masterAmount desc limit 50'
print sql
cur.execute(sql)
res = cur.fetchall()
cur.close()
con.close()
for line in res:
order.append([item for item in line])
return order
def getDatesInDB():
con = sqlite3.connect('moneyflow.db')
cur = con.cursor()
sql = 'SELECT distinct date from lxjlr'
cur.execute(sql)
datas = cur.fetchall()
cur.close()
con.close()
dates = []
for data in datas:
dates.append(data[0])
return dates
def getSymbolsInDB():
con = sqlite3.connect('moneyflow.db')
cur = con.cursor()
sql = 'SELECT distinct symbol from lxjlr'
cur.execute(sql)
datas = cur.fetchall()
cur.close()
con.close()
symbols = []
for data in datas:
symbols.append(data[0])
return symbols
def getFlowOrderSymbols(date):
symbols = []
con = sqlite3.connect('moneyflow.db')
cur = con.cursor()
sql = 'select symbol from lxjlr where date=' + '"' + date + '" ' + 'order by masterAmount desc limit 50'
cur.execute(sql)
res = cur.fetchall()
cur.close()
con.close()
for line in res:
symbols.append(line[0])
return symbols
def getSymbolData(symbol):
name = None
dates = []
trades = []
turnovers = []
amounts = []
masterAmounts = []
con = sqlite3.connect('moneyflow.db')
cur = con.cursor()
sql = 'select date, name, trade, turnover, amount, masterAmount from lxjlr where symbol=' + '"' + symbol + '"'
cur.execute(sql)
res = cur.fetchall()
cur.close()
con.close()
for line in res:
dates.append(line[0])
name = line[1]
trades.append(line[2])
turnovers.append(line[3])
amounts.append(line[4])
masterAmounts.append(line[5])
return (dates, symbol, name, trades, turnovers, amounts, masterAmounts)
pylab.mpl.rcParams['font.sans-serif'] = ['SimHei']
pylab.mpl.rcParams['axes.unicode_minus'] = False
def plotStock(dates, symbol, name, trades, turnovers, amounts, masterAmounts):
masterAmounts = numpy.array(masterAmounts)
amounts = numpy.array(amounts)
numpy.seterr(all='ignore')
masters = (masterAmounts / amounts) * 100
masters[numpy.isnan(masters)] = 0
masterAmounts = masterAmounts / 10000000
turnovers = pylab.array(turnovers)
turnovers = turnovers * 100
pylab.figure(figsize=(16,10), dpi=200)
pylab.subplot(3,1,1)
pylab.title(symbol + ' ' + name + ' (' + dates[-1] + ')')
pylab.plot(trades, color = 'black', label = u'收盘价格')
pylab.ylabel(u'价格')
pylab.legend(loc = 'upper left')
pylab.autoscale(True, 'both', None)
pylab.grid(True)
pylab.subplot(3,1,2)
pylab.plot(masterAmounts, color = 'red', label = u'主力流入')
pylab.ylabel(u'千万')
pylab.legend(loc = 'upper left')
pylab.autoscale(True, 'both', None)
pylab.grid(True)
pylab.subplot(3,1,3)
pylab.plot(masters, color = 'blue', label = u'主力占比')
# pylab.plot(turnovers, color = 'green', label = u'换手率')
pylab.xlabel(u'日期')
pylab.ylabel(u'百分比-100%')
pylab.legend(loc = 'upper left')
pylab.autoscale(True, 'both', None)
pylab.grid(True)
pylab.savefig(symbol, dpi=200)