__author__ = 'weijie' from EmQuantAPI import * from datetime import timedelta, datetime import time import traceback import json from StockRestPojo import StockRestPojo from dbOperation import dbOperation from threading import Thread import db_config def csqCallback(quantdata): current_time = datetime.strftime(datetime.now(),"%H:%M:%S") time1 = "09:15:00" time2 = "11:30:00" time3 = "12:59:00" time4 = "15:00:00" if (current_time > time1 and current_time < time2) or (current_time > time3 and current_time < time4): try: fileName = "/www/wwwroot/csqdata/stock/" + str(round(time.time() * 1000000)) + ".txt" with open(fileName, 'w', encoding="utf-8") as file: file.write(str(quantdata.Data)) except Exception as e: print(str(e)) elif current_time > time4: try: fileName = "/www/wwwroot/csqdata/stock/after/" + str(round(time.time() * 1000000)) + ".txt" with open(fileName, 'w', encoding="utf-8") as file: file.write(str(quantdata.Data)) except Exception as e: print(str(e)) elif current_time < time1: # 盘前数据 try: fileName = "/www/wwwroot/csqdata/stock/InFront/" + str(round(time.time() * 1000000)) + ".txt" with open(fileName, 'w', encoding="utf-8") as file: file.write(str(quantdata.Data)) except Exception as e: print(str(e)) class StockRestData: # 股票数据抓取 def toGet(self): try: db = dbOperation(db_config.db_gupiao) 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") # 拼接证券代码 codesStr = "" i = 0 for stockPojo in stockPojoList: i = i + 1 codesStr = codesStr + stockPojo['code'] + ',' if 0 == i%600 or str(i) == str(len(stockPojoList)): #实时行情订阅使用范例 codesStr = codesStr[:-1] 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) print(data) codesStr = "" db.close() except Exception as ee: print("error >>>",ee) traceback.print_exc()