Python实现股票行情接收V010
来源:互联网 发布:java编写菱形图案 编辑:程序博客网 时间:2024/06/09 22:00
本次版本增加如下功能:
1、增加一个新的行情源:QQ行情源:http://qt.gtimg.cn
2、修改主循环,新增一个线程来获取行情
3、增加股票跟踪功能,当跟踪股票最新价格高于止盈比例或是止损比例的时候,播放声音提醒
1、增加一个新的行情源, 例如:http://qt.gtimg.cn/r=0.9392363841179758&q=sh600006,获取的数据与sina的行情有区别,如下:
v_sh600006="1~东风汽车~600006~3.04~3.07~3.07~53132~20602~32530~3.04~127~3.03~2306~3.02~1304~3.01~2929~3.00~4115~3.05~1817~3.06~822~3.07~3135~3.08~2828~3.09~2197~14:59:54/3.04/40/B/12160/16286|14:59:49/3.03/1/S/336/16280|14:59:34/3.04/46/B/13984/16264|14:59:29/3.04/10/B/3040/16252|14:59:09/3.03/64/S/19396/16231|14:59:04/3.05/49/B/14934/16224~20140403150300~-0.03~-0.98~3.08~3.01~3.04/53132/16195550~53132~1620~0.27~121.16~~3.08~3.01~2.28~60.80~60.80~1.02~3.38~2.76~";该方法是通过腾讯行情网站抓http报文找到的,其中r=0.9392363841179758不知道什么意思,改成其他值也可以查询,请知道的朋友告诉一声。另外该数据中部分数据还没有解析完事什么意义,因此只是简单的解析了几个主要的字段,不多说了,获取行情数据代码如下:
class QuoteSourceQtimg(QuoteSource): def queryStock(self): host="http://qt.gtimg.cn/r=0.9392363841179758&q=" url = host + self.stocks req = urllib2.Request(url) res_data = urllib2.urlopen(req) res = res_data.read() d = QuoteData() for line in res.split(';'): #print line if len(line) < 50 : continue info = line[12: 100] #print info vargs = info.split('~') #print vargs d.stockName = vargs[1] d.stockid = vargs[2] d.lastPrice = vargs[3] d.preClose = vargs[4] d.openPrice = vargs[5] d.lowPrice = '' d.highPrice = '' d.avgPrice = '' self.notifyListeners(d);
2、修改主循环,新增一个线程来获取行情,这个不用多说,只是新建一个线程子类,然后再run中写循环查询即可:
class QuoteThread(threading.Thread): def __init__(self, source, intervalSecs = 3): self.source = source self.interval = intervalSecs self.threadRunning = False threading.Thread.__init__(self) def run(self): print 'StockQuote run' self.threadRunning = True; while self.threadRunning: self.source.queryStock() sleep(self.interval) def stop(self): print 'StockQuote stop' self.threadRunning = False;
3、增加股票跟踪功能,当跟踪股票最新价格高于止盈比例或是止损比例的时候,播放声音提醒
实现一个类QuoteInformer直接从QuoteListener派生,这样不用改获取行情部分的代码了,然后判断是否超出止损和止盈值:
class QuoteInformer(QuoteListener): def __init__(self, stock, cmpPrice = 0.0, lowper = -2, highper = 2): QuoteListener.__init__(self, 'Inform') self.stockid = stock self.lowper = lowper self.highper = highper self.cmpPrice = cmpPrice self.preClose = 0.0 self.lastPrice = 0.0 self.playThread = 0 def OnQuoteRecv(self, quoteData): if cmp(self.stockid, quoteData.stockid) == 0 : #self.printInfo(0.0) if self.cmpPrice == 0.0 : self.cmpPrice = string.atof(quoteData.lastPrice) self.preClose = string.atof(quoteData.preClose) self.lastPrice = string.atof(quoteData.lastPrice) curper = (self.lastPrice - self.cmpPrice) / self.cmpPrice * 100 self.printInfo(curper) if curper <= self.lowper : self.playLowSound() elif curper >= self.highper : self.playHighSound() def playLowSound(self): self.playThread = PlayThread(3) self.playThread.playSound("F:\KuGou\wav\level_up.wav") def playHighSound(self): self.playThread = PlayThread(3) self.playThread.playSound("F:\KuGou\wav\msg.wav") def printInfo(self, curper) : print "stockid curper lastPrice cmpPrice lowper highper preClose" print "%s %f %f %f %f %f %f" % (self.stockid, curper, self.lastPrice, self.cmpPrice, self.lowper, self.highper, self.preClose)
播放声音,这里直接使用winsound来播放声音,不过遗憾的是这个只能播放wav格式的音频文件,暂时也就这样吧,以后有需要再增加代码播放其他音频文件。
class PlayThread(threading.Thread):
def __init__(self, count): self.wavfile = "" self.loopcount = count threading.Thread.__init__(self) def playSound(self, wavfile): self.wavfile = wavfile self.start() def run(self): count = self.loopcount while count > 0 : count = count - 1 self.playSoundOnce() def playSoundOnce(self): winsound.PlaySound(self.wavfile,winsound.SND_FILENAME)
最后,还是那句话,请了解股票的朋友们多多提建议,看能否实现更多功能帮助大家分析股票。
0 0
- Python实现股票行情接收V010
- Python实现股票行情接收
- Python实现股票行情接收V001
- Python实现股票行情接收V013
- 股票行情接收头文件(转)
- flex实现股票行情走势图
- 利用散列表实现股票行情的查询
- 使用 Android ListView 来实现股票行情
- python实现jms客户端(发送接收消息)
- Java中利用散列表实现股票行情的查询
- amCharts实现股票行情分时线及日K线
- 股票行情查询
- Raspberr pi串口发送接收的Python实现
- 用Python实现电子邮件接收程序(POP3)
- 如何用C#写一个实现像股票行情的波动曲线图
- python接收邮件
- python邮件发送接收
- python 接收邮件
- SDUT 1252 - 进制转换
- 今天
- android学习点点滴滴(二)
- 线性表、栈、队列、单链队列、循环队列 内存分配介绍
- hdu的题目分类
- Python实现股票行情接收V010
- 一步步学习微软InfoPath2010和SP2010--第十四章节--高级选项(4)--浏览器和Filler按钮
- SiteMesh3.0的下载,简介与使用
- 动态代理
- LED显示模块
- 构造函数
- PHP学习笔记
- 第四周作业——图的表示
- Hibernate 一对多 多对一 实例 及一些 问题(2)字典项的使用