f1.pysql
# -*- coding: utf-8 -*- import sqlalchemy import tushare import pandas import socket import struct import matplotlib.pyplot as Plot from matplotlib.finance import candlestick_ohlc as Drawk from matplotlib.pylab import mpl ######################################################################## class Myclass: """""" #---------------------------------------------------------------------- def __init__(self, User = 'sa', Password = '123456', Dsn = 'XiTongDSN', Host = '192.168.1.3', Port = 12345): """ sql server """ self.User = User self.Password = Password self.Dsn = Dsn self.Engine = sqlalchemy.create_engine('mssql+pyodbc://'+self.User+':'+self.Password+'@'+self.Dsn) """ socket """ self.Host = Host self.Port = Port #---------------------------------------------------------------------- def Get_url_data(self): """ codelist """ Codelist = pandas.DataFrame((tushare.get_today_all())['code']) Codelist = Codelist.sort(columns = 'code', ascending = True) Codelist.to_sql('codelist', self.Engine, if_exists = 'replace', index = False) """ stockdata """ for i in range(0, len(Codelist)): Stockdata = tushare.get_h_data(Codelist['code'][i]) Index = list(Stockdata['open'].index) Stockdata['date'] = pandas.Series(Index, Index) Stockdata = Stockdata.sort_values(by = 'date', ascending = True) Stockdata.to_sql(Codelist['code'][i], self.Engine, if_exists = 'replace', index = False) #---------------------------------------------------------------------- def Get_sql_data(self, Code = '603989'): """ sql server """ Connection = self.Engine.connect() Stockdata = pandas.read_sql_table(Code, Connection) Connection.close() return Code, Stockdata #---------------------------------------------------------------------- def Run_socket_server(self): """ socket server """ Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) Socket.bind((self.Host, self.Port)) Socket.listen(5) Code, Stockdata = self.Get_sql_data() I = list(Stockdata['date'].index) O = Stockdata['open'] H = Stockdata['high'] L = Stockdata['low'] C = Stockdata['close'] V = Stockdata['volume'] i = 0 while True: Connection, Address = Socket.accept() if Connection.recv(1024) == b'connect' and i < len(I): Connection.send(struct.pack('6si5f', Code.encode('utf-8'), I[i], O[i], H[i], L[i], C[i], V[i])) i += 1 else: Connection.close() Socket.close() #---------------------------------------------------------------------- def Run_socket_client(self, Message = b'connect'): """ socket client """ Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) Socket.connect((self.Host, self.Port)) Socket.send(Message) Code, I, O, H, L, C, V = struct.unpack('6si5f', Socket.recv(1024)[:32]) Socket.close() return Code.decode('utf-8'), I, O, H, L, C, V #---------------------------------------------------------------------- def Plot_figure(self): """ plot figure """ mpl.rcParams['font.sans-serif'] = ['SimHei'] Figure = Plot.figure(u'由 DengChaohai 製做') f1 = Figure.add_subplot(3, 4, (1, 7), xlim = [0, 100], xlabel = u'天數', ylabel = u'成交價') Plot.grid(True) f2 = Figure.add_subplot(3, 4, (9, 11), xlim = [0, 100], xlabel = u'天數', ylabel = u'成交量') Plot.grid(True) f3 = Figure.add_subplot(30, 4, 8, ylim = [0, 0], xticklabels = [], yticklabels = []) Plot.grid(True) f4 = Figure.add_subplot(30, 4, 16, ylim = [0, 0], xticklabels = [], yticklabels = []) Plot.grid(True) f5 = Figure.add_subplot(30, 4, 24, ylim = [0, 0], xticklabels = [], yticklabels = []) Plot.grid(True) f6 = Figure.add_subplot(30, 4, 32, ylim = [0, 0], xticklabels = [], yticklabels = []) Plot.grid(True) f7 = Figure.add_subplot(30, 4, (40, 120)) Plot.grid(True) Quotes = [] Index = [] Price = [] TRUE = True while TRUE: Code, I, O, H, L, C, V = self.Run_socket_client() Index.append(I) Price.append((C + H + L + C) / 4) Quotes.append((I, O, H, L, C, V)) Drawk(f1, Quotes, width = 0.5, colorup = 'g', colordown = 'r') f1.plot(Index, Price, 'c') f1.set_title(u'股票代碼 @ ' + Code, loc = 'left', color = 'b') f2.bar(I, V, width = 0.5, color = 'c', edgecolor = 'c') f2.set_title(u'當前成交量 @ ' + str(V), loc = 'left', color = 'b') f3.set_title(u'當前開盤價 @ ' + str(O), loc = 'left', color = 'b') f4.set_title(u'當前最高價 @ ' + str(H), loc = 'left', color = 'b') f5.set_title(u'當前最低價 @ ' + str(L), loc = 'left', color = 'b') f6.set_title(u'當前收盤價 @ ' + str(C), loc = 'left', color = 'b') f7.plot(Index, Price, 'r') f7.set_title(u'自動化交易 @ ' + str(Price[I]), loc = 'left', color = 'r') Plot.pause(0.01) if I > 100: TRUE = False self.Run_socket_client(b'disconnect')
f2.pyapp
# -*- coding: utf-8 -*- import f1 F1 = f1.Myclass() F1.Run_socket_server()
f3.pysocket
# -*- coding: utf-8 -*- import f1 F1 = f1.Myclass() F1.Plot_figure()