StockRestData.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. __author__ = 'weijie'
  2. from EmQuantAPI import *
  3. from datetime import timedelta, datetime
  4. import time
  5. import traceback
  6. import json
  7. from StockRestPojo import StockRestPojo
  8. from dbOperation import dbOperation
  9. from threading import Thread
  10. import db_config
  11. def csqCallback(quantdata):
  12. current_time = datetime.strftime(datetime.now(),"%H:%M:%S")
  13. time1 = "09:15:00"
  14. time2 = "11:30:00"
  15. time3 = "12:59:00"
  16. time4 = "15:00:00"
  17. if (current_time > time1 and current_time < time2) or (current_time > time3 and current_time < time4):
  18. try:
  19. fileName = "/www/wwwroot/csqdata/stock/" + str(round(time.time() * 1000000)) + ".txt"
  20. with open(fileName, 'w', encoding="utf-8") as file:
  21. file.write(str(quantdata.Data))
  22. except Exception as e:
  23. print(str(e))
  24. elif current_time > time4:
  25. try:
  26. fileName = "/www/wwwroot/csqdata/stock/after/" + str(round(time.time() * 1000000)) + ".txt"
  27. with open(fileName, 'w', encoding="utf-8") as file:
  28. file.write(str(quantdata.Data))
  29. except Exception as e:
  30. print(str(e))
  31. elif current_time < time1:
  32. # 盘前数据
  33. try:
  34. fileName = "/www/wwwroot/csqdata/stock/InFront/" + str(round(time.time() * 1000000)) + ".txt"
  35. with open(fileName, 'w', encoding="utf-8") as file:
  36. file.write(str(quantdata.Data))
  37. except Exception as e:
  38. print(str(e))
  39. class StockRestData:
  40. # 股票数据抓取
  41. def toGet(self):
  42. try:
  43. db = dbOperation(db_config.db_gupiao)
  44. stockPojoList = db.query_list("select id, code, name, list_date listDate, seralid from t_stock_base_info where is_rest_add = 0 order by id asc")
  45. # 拼接证券代码
  46. codesStr = ""
  47. i = 0
  48. for stockPojo in stockPojoList:
  49. i = i + 1
  50. codesStr = codesStr + stockPojo['code'] + ','
  51. if 0 == i%600 or str(i) == str(len(stockPojoList)):
  52. #实时行情订阅使用范例
  53. codesStr = codesStr[:-1]
  54. data = c.csq(codesStr, 'date,time,now,high,low,open,preclose,roundlot,change,pctchange,volume,amount,volumeratio,commissionratio,commissiondiff,tradestatus,outvolume,involume,highlimit,lowlimit,speed,averageprice,buyprice1,buyprice2,buyprice3,buyprice4,buyprice5,buyvolume1,buyvolume2,buyvolume3,buyvolume4,buyvolume5,sellprice1,sellprice2,sellprice3,sellprice4,sellprice5,sellvolume1,sellvolume2,sellvolume3,sellvolume4,sellvolume5,closedtime,closedvolume,closedamount','Pushtype=2',csqCallback)
  55. print(data)
  56. codesStr = ""
  57. db.close()
  58. except Exception as ee:
  59. print("error >>>",ee)
  60. traceback.print_exc()