分享

Python实现股票行情接收V010

 imelee 2019-06-12

本次版本增加如下功能:

1、增加一个新的行情源:QQ行情源:http://qt.

2、修改主循环,新增一个线程来获取行情

3、增加股票跟踪功能,当跟踪股票最新价格高于止盈比例或是止损比例的时候,播放声音提醒

1、增加一个新的行情源, 例如:http://qt./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不知道什么意思,改成其他值也可以查询,请知道的朋友告诉一声。另外该数据中部分数据还没有解析完事什么意义,因此只是简单的解析了几个主要的字段,不多说了,获取行情数据代码如下:
  1. class QuoteSourceQtimg(QuoteSource):
  2. def queryStock(self):
  3. host="http://qt./r=0.9392363841179758&q="
  4. url = host + self.stocks
  5. req = urllib2.Request(url)
  6. res_data = urllib2.urlopen(req)
  7. res = res_data.read()
  8. d = QuoteData()
  9. for line in res.split(';'):
  10. #print line
  11. if len(line) < 50 :
  12. continue
  13. info = line[12: 100]
  14. #print info
  15. vargs = info.split('~')
  16. #print vargs
  17. d.stockName = vargs[1]
  18. d.stockid = vargs[2]
  19. d.lastPrice = vargs[3]
  20. d.preClose = vargs[4]
  21. d.openPrice = vargs[5]
  22. d.lowPrice = ''
  23. d.highPrice = ''
  24. d.avgPrice = ''
  25. self.notifyListeners(d);

2、修改主循环,新增一个线程来获取行情,这个不用多说,只是新建一个线程子类,然后再run中写循环查询即可:
  1. class QuoteThread(threading.Thread):
  2. def __init__(self, source, intervalSecs = 3):
  3. self.source = source
  4. self.interval = intervalSecs
  5. self.threadRunning = False
  6. threading.Thread.__init__(self)
  7. def run(self):
  8. print 'StockQuote run'
  9. self.threadRunning = True;
  10. while self.threadRunning:
  11. self.source.queryStock()
  12. sleep(self.interval)
  13. def stop(self):
  14. print 'StockQuote stop'
  15. self.threadRunning = False;


3、增加股票跟踪功能,当跟踪股票最新价格高于止盈比例或是止损比例的时候,播放声音提醒

实现一个类QuoteInformer直接从QuoteListener派生,这样不用改获取行情部分的代码了,然后判断是否超出止损和止盈值:

  1. class QuoteInformer(QuoteListener):
  2. def __init__(self, stock, cmpPrice = 0.0, lowper = -2, highper = 2):
  3. QuoteListener.__init__(self, 'Inform')
  4. self.stockid = stock
  5. self.lowper = lowper
  6. self.highper = highper
  7. self.cmpPrice = cmpPrice
  8. self.preClose = 0.0
  9. self.lastPrice = 0.0
  10. self.playThread = 0
  11. def OnQuoteRecv(self, quoteData):
  12. if cmp(self.stockid, quoteData.stockid) == 0 :
  13. #self.printInfo(0.0)
  14. if self.cmpPrice == 0.0 :
  15. self.cmpPrice = string.atof(quoteData.lastPrice)
  16. self.preClose = string.atof(quoteData.preClose)
  17. self.lastPrice = string.atof(quoteData.lastPrice)
  18. curper = (self.lastPrice - self.cmpPrice) / self.cmpPrice * 100
  19. self.printInfo(curper)
  20. if curper <= self.lowper :
  21. self.playLowSound()
  22. elif curper >= self.highper :
  23. self.playHighSound()
  24. def playLowSound(self):
  25. self.playThread = PlayThread(3)
  26. self.playThread.playSound("F:\KuGou\wav\level_up.wav")
  27. def playHighSound(self):
  28. self.playThread = PlayThread(3)
  29. self.playThread.playSound("F:\KuGou\wav\msg.wav")
  30. def printInfo(self, curper) :
  31. print "stockid curper lastPrice cmpPrice lowper highper preClose"
  32. 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):

  1. def __init__(self, count):
  2. self.wavfile = ""
  3. self.loopcount = count
  4. threading.Thread.__init__(self)
  5. def playSound(self, wavfile):
  6. self.wavfile = wavfile
  7. self.start()
  8. def run(self):
  9. count = self.loopcount
  10. while count > 0 :
  11. count = count - 1
  12. self.playSoundOnce()
  13. def playSoundOnce(self):
  14. winsound.PlaySound(self.wavfile,winsound.SND_FILENAME)
需要代码的同学可以直接向我要,要不我还是建一个git库,将代码共享上去大家都可以直接下载,不过暂时先这样吧,改天再上传上去了。

最后,还是那句话,请了解股票的朋友们多多提建议,看能否实现更多功能帮助大家分析股票。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多