本次版本增加如下功能:
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)
需要代码的同学可以直接向我要,要不我还是建一个git库,将代码共享上去大家都可以直接下载,不过暂时先这样吧,改天再上传上去了。
最后,还是那句话,请了解股票的朋友们多多提建议,看能否实现更多功能帮助大家分析股票。