def saveStockTech(): date = datetime.now().strftime('%Y-%m-%d') weekday = datetime.today().weekday() if weekday == 5 or weekday == 6: return tomorrow = ( datetime.now() + timedelta(days=(3 if weekday == 4 else 1))).strftime('%Y-%m-%d') stockList = select( unicode("SELECT code,name from s_stock_info") ) # loadJsonConfig(os.path.abspath(os.path.join(os.getcwd(), "../config/goodStockList.json"))) i = 0 while i < len(stockList): code = stockList[i][0] checkExistSql = unicode( "select count(*) from s_stock_tech where code='{0}' and date='{1}'" ).format(code, date) count = select(checkExistSql, False)[0] if count > 0: i = i + 1 continue try: info = getTechParamter(code) saveStockTechToDb(info[0], date, count > 0) saveStockTechForecastToDb(info[1], info[2], tomorrow) i = i + 1 except Exception, e: writeErrorLog( unicode("getTechFailed, code: {0}, e: {1}").format(code, e)) time.sleep(1)
def getWR_CCI_OBV(date): fileName = unicode("沪深A股{0}").format(date) fileName = os.path.abspath( os.path.join(os.getcwd(), "../config/import/" + fileName + "_4.txt")) if not os.path.exists(fileName): return None i = -1 dict = {} for line in open(fileName): i = i + 1 if i < 2: continue line = line.strip() if (line == ''): continue lineInfo = line.split('\t') if (len(lineInfo) <= 2): continue data = {} data['code'] = lineInfo[0] try: data['WR1'] = lineInfo[9] data['WR2'] = -1 if len(lineInfo) < 11 else lineInfo[10] data['CCI'] = -1 if len(lineInfo) < 12 else lineInfo[11] data['OBV'] = -1 if len(lineInfo) < 13 else lineInfo[12] dict[lineInfo[0]] = data except Exception, e: writeErrorLog( unicode("[getWR_CCI_OBVfailed]code:{0}, e:{1}").format( lineInfo, str(e)))
def saveStockInfo(): date = datetime.now() weekday = datetime.today().weekday() diff = 0 if weekday < 5 else weekday - 4 date = (date - timedelta(days=diff)).strftime('%Y-%m-%d') stockList = select( unicode("SELECT code,name from s_stock_info") ) # loadJsonConfig(os.path.abspath(os.path.join(os.getcwd(), "../config/goodStockList.json"))) i = 0 while i < len(stockList): code = stockList[i][0] try: info = getStockInfo(code) if info is None: i = i + 1 continue saveStockInfoToDb(info, date) i = i + 1 except Exception, e: writeErrorLog( unicode("getStockInfoFailed, code:{0}, e:{1}").format( code, str(e))) time.sleep(0.2)
def updateEXPMA(): sql = unicode( "select code,date from s_stock_tech where EXPMA1=0 and EXPMA2=0 ORDER by code,date asc") data = select(sql) if len(data) <= 0: writeLog(unicode("没有获取到无效EXPMA指标数据")) return for item in data: code = item[0] date = item[1] try: data = getEXPMA(code, date) if data is None: continue updateSql = unicode( "update s_stock_tech set EXPMA1={0},EXPMA2={1} where code='{2}' and date='{3}'").format( data[0], data[1], code, date) execute(updateSql) except Exception, e: writeErrorLog(unicode("更新EXPMA指标数据异常,code:{0}, date:{1}, e:{2}").format(code, date, str(e)))
def updateAllMACD(): sql = unicode("select code from s_stock_info order by code asc") stockList = select(sql) if stockList is None: return for stock in stockList: sql = unicode( "select code,date,macd,dif,dea from s_stock_tech where code='{0}' ORDER by code,date asc limit 1,60000").format( stock[0]) data = select(sql) if len(data) <= 0: # writeLog(unicode("没有获取到无效MACD指标数据,code:{0}").format(stock[0])) continue for item in data: code = item[0] date = item[1] try: data = getMACD(code, date) if data is None: continue updateSql = unicode( "update s_stock_tech set macd={0},dif={1},dea={2} where code='{3}' and date='{4}'").format( data[0], data[1], data[2], code, date) execute(updateSql) except Exception, e: writeErrorLog(unicode("更新MACD指标数据异常,code:{0}, date:{1}, e:{2}").format(code, date, str(e)))
def handleSpecifiedStock(codeList): result = [] result.append( unicode("{0},{1},{2},{3},{4},{5},{6},{7}\n").format( "代码", "名称", "PE", "PEG", "预测PEG", "No1(代码:名称:PEG)", "No2(代码:名称:PEG)", "No3(代码:名称:PEG)").encode('gbk')) i = 0 while i < len(codeList): try: code = codeList[i] peInfo = getPEByWC(code) pegData = getPEGByWC(code) rankData = getRankPeg(code) print( unicode("获取指定股票数据, code: {0} , i:{1}, info: {2}").format( code, i, peInfo)) result.append( unicode("{0},{1},{2},{3},{4},{5},{6},{7}\n").format( code, peInfo[0], peInfo[1], pegData[0], pegData[1], rankData[0], rankData[1], rankData[2]).encode('gbk')) i = i + 1 except Exception, e: writeErrorLog( unicode("getPEGFailed, code: {0} ,e: {1}").format( code, str(e))) if (i % 50 == 0): time.sleep(300) else: time.sleep(random.randint(15, 20))
def updateMACD(): sql = unicode( "select code,date,macd,dif,dea from s_stock_tech where MACD=0 and dif=0 and dea=0 ORDER by code,date asc") data = select(sql) if len(data) <= 0: writeLog(unicode("没有获取到无效MACD指标数据")) return for item in data: code = item[0] date = item[1] try: data = getMACD(code, date) if data is None: continue updateSql = unicode( "update s_stock_tech set macd={0},dif={1},dea={2} where code='{3}' and date='{4}'").format( data[0], data[1], data[2], code, date) execute(updateSql) except Exception, e: writeErrorLog(unicode("更新MACD指标数据异常,code:{0}, date:{1}, e:{2}").format(code, date, str(e)))
def handleListData(listData, avgPE, result, dicDuplicate): i = 0 while i < len(listData): writeDebugLog(unicode("获取股票数据, {0}").format(listData[i])) info = listData[i].strip().split(",") if (len(info) < 25): i = i + 1 continue code = info[1] name = info[2] pe = info[24] writeDebugLog( unicode( "获取股票数据, code: {0} , name:{1}, pe: {2}, avgPE: {3}").format( code, name, pe, avgPE)) try: if (float(pe) > float(avgPE)): return -1 # 如果已经大于当前平均PE,则不再继续获取 pegData = getPEG(code) # print pegData[0],pegData[1] if not dicDuplicate.has_key(code): result.append( unicode("{0},{1},{2},{3},{4},{5},{6},{7},{8}\n").format( code, name, pe, pegData[3], pegData[4], pegData[5], pegData[2], pegData[0], pegData[1]).encode('gbk')) i = i + 1 except Exception, e: writeErrorLog( unicode("getPEGFailed, code: {0} ,e: {1}").format( code, str(e))) time.sleep(0.2)
def sendMessage(receiver, message): try: global SENDER if SENDER is None: initWeChatSender() SENDER.sendWeChatMessage(receiver, message) except Exception, e: writeErrorLog(unicode("send message error: {0}").format(str(e)))
def select(self, sql, fetchall=True): try: cursor = self.connection.cursor() cursor.execute(sql) return cursor.fetchall() if fetchall else cursor.fetchone() except Exception as e: writeErrorLog( unicode('selectFailed, sql:{0}, error:{1}').format( sql, str(e))) self.connection.rollback() finally: cursor.close()
def batchInsert(self, sql, parameters): try: cursor = self.connection.cursor() cursor.executemany(sql, parameters) self.connection.commit() except Exception as e: writeErrorLog( unicode('batchInsertFailed, sql:{0},parameters:{2}, error:{1}' ).format(sql, str(e), parameters)) self.connection.rollback() finally: cursor.close()
def sendWeChatMessage(self, receiver, message): try: if self.senders.has_key(receiver): self.senders.get(receiver).send(message) else: writeWarningLog( 'sender is not exist: receiver: {0}, message: {1}'.format( receiver, message)) except Exception, e: writeErrorLog( 'send we chat failed: receiver: {0}, message: {1}, e: {2}'. format(receiver, message, str(e)))
def isMACDGoldFork(data): try: for i in range(0, len(data)): MACD = data[0][1] yesterdayMACD = data[1][1] # 黄金叉:1. 前一天macd<=0,今天macd>=0 2. macd指标在0轴上方(此条件暂时忽略) if (yesterdayMACD <= 0 and MACD >= 0): return True return False except Exception, e: writeErrorLog(unicode("[买入卖出信号异常] data:{0}, e:{1}").format(data, e)) return False
def updateStockChangePercent(code, data): for i in range(1, len(data)): try: changeAmount = data[i][1] - data[i - 1][1] changePercent = round(changeAmount * 100 / data[i - 1][1], 2) updateSql = unicode( "update s_stock set changePercent={0},changeAmount={1} where code='{2}' and date='{3}'" ).format(changePercent, changeAmount, code, data[i][0]) execute(updateSql) except Exception, e: writeErrorLog( unicode("更新涨幅数据失败: code:{0}, i:{1}, date:{2}, closePrice:{3}"). format(code, i, data[i][0], data[i][1]))
def startServer(): try: bot = Bot('bot.pkl', console_qr=True) master = ensure_one(bot.friends().search(unicode('踏雪'))) log_group = ensure_one(bot.friends().search(unicode('期待'))) token = '295cc3d8-c977-11e6-a341-0090f5f61084' writeLog(unicode("receiver: {0}, {1}").format(master, log_group)) listen(bot, [master, log_group], token=token, port=9090, status_report=True, status_receiver=master) except Exception, e: writeErrorLog(unicode("receive message error: {0}").format(str(e)))
def execute(self, sql, parameter=None): try: cursor = self.connection.cursor() cursor.execute( sql, parameter) if parameter is not None else cursor.execute(sql) self.connection.commit() except Exception as e: writeErrorLog( unicode('executeFailed, sql:{0}, error:{1}').format( sql, str(e))) self.connection.rollback() finally: cursor.close()
def updateKDJ(): sql = unicode("SELECT code,date,k,d,j from s_stock_tech where K=0 and d=0 and j=0 ORDER by code,date asc") data = select(sql) if len(data) <= 0: writeLog(unicode("没有获取到无效KDJ指标数据")) return for item in data: code = item[0] date = item[1] try: rsv = getRSV(code, date) if rsv is None: continue techSql = unicode( "SELECT k,d,j from s_stock_tech where code='{0}' and date<'{1}' ORDER by date desc limit 1").format( code, date) data = select(techSql) yesterdayK = 50 yesterdayD = 50 if len(data) > 0: yesterdayK = data[0][0] yesterdayD = data[0][1] todayK = getFloat((rsv + 2 * float(yesterdayK)) / 3) todayD = getFloat((todayK + 2 * float(yesterdayD)) / 3) todayJ = getFloat(3 * todayK - 2 * todayD) writeLog(unicode("更新KDJ指标数据,code:{0},date:{1},todayK:{2},todayD:{3},todayJ:{4},rsv:{5}").format(code, date, todayK, todayD, todayJ, rsv)) print code, date, todayK, todayD, todayJ, rsv updateSql = unicode("update s_stock_tech set k={0},d={1},j={2} where code='{3}' and date='{4}'").format( todayK, todayD, todayJ, code, date) execute(updateSql) except Exception, e: writeErrorLog(unicode("更新KDJ指标数据异常,code:{0}, date:{1}, e:{2}").format(code, date, str(e)))
def handleListData(listData, avgPE, result, dicDuplicate): i = 0 while i < len(listData): # writeLog(unicode("获取股票数据, {0}").format(listData[i])) info = listData[i] if info is None: i = i + 1 continue code = info.get('code') name = info.get('name') pe = info.get('pe') if pe == '--': i = i + 1 continue pe = float(pe) print( unicode( "获取股票数据, code: {0} , name:{1}, pe: {2}, avgPE: {3}").format( code, name, pe, avgPE)) try: if (pe > float(avgPE)): return -1 # 如果已经大于当前平均PE,则不再继续获取 pegData = getPEGByWC(code) print pegData rankData = getRankPeg(code) print rankData if not dicDuplicate.has_key(code): result.append( unicode("{0},{1},{2},{3},{4},{5},{6},{7}\n").format( code, name, pe, pegData[0], pegData[1], rankData[0], rankData[1], rankData[2]).encode('gbk')) i = i + 1 except Exception, e: writeErrorLog( unicode("getPEGFailed, code: {0} ,e: {1}").format( code, str(e))) if (i % 50 == 0): time.sleep(300) else: time.sleep(random.randint(10, 15))
def getMACD_KDJ_DMI_EXPMA(date): fileName = unicode("沪深A股{0}").format(date) fileName = os.path.abspath( os.path.join(os.getcwd(), "../config/import/" + fileName + ".txt")) if not os.path.exists(fileName): return None i = -1 dict = {} for line in open(fileName): i = i + 1 if i < 2: continue line = line.strip() if (line == ''): continue lineInfo = line.split('\t') if (len(lineInfo) <= 2): continue data = {} data['code'] = lineInfo[0] try: data['DIF'] = lineInfo[6] data['DEA'] = lineInfo[7] data['MACD'] = lineInfo[8] data['K'] = lineInfo[9] data['D'] = lineInfo[10] data['J'] = lineInfo[11] data['PDI'] = lineInfo[12] data['MDI'] = lineInfo[13] data['ADX'] = lineInfo[14] data['ADXR'] = lineInfo[15] data['EXP1'] = lineInfo[16] data['EXP2'] = lineInfo[17] dict[lineInfo[0]] = data except Exception, e: writeErrorLog( unicode("[getMACD_KDJ_DMI_EXPMAfailed]code:{0}, e:{1}").format( lineInfo, str(e)))
def getStockInfos(code, year): try: url = "http://d.10jqka.com.cn/v2/line/hs_{0}/01/{1}.js".format( code, year) res = httpGet(url).decode("utf-8") index = res.find("(") if (index < 0): writeErrorLog( unicode("解析行情失败: code:{0}, year:{1}, res:{2}").format( code, year, res)) return [] res = res[index + 1:-1] writeLog(unicode("获取股票历史行情: code: {0}, year:{1}").format(code, year)) jo = json.loads(res) dataInfo = jo['data'].split(';') result = {} for item in dataInfo: infos = item.split(',') dic = {} dic['open'] = infos[1] dic['high'] = infos[2] dic['low'] = infos[3] dic['close'] = infos[4] dic['volume'] = infos[5] dic['amount'] = "{0}亿".format(round( float(infos[6]) / 100000000, 1)) result[datetime.strptime(infos[0], '%Y%m%d').strftime('%Y-%m-%d')] = dic return result except Exception, e: writeErrorLog( unicode("解析行情失败: code:{0}, year:{1}, e:{2}").format( code, year, str(e))) if "404" in str(e): return [] else: return None
def getDMA_BOLL_BIAS(date): fileName = unicode("沪深A股{0}").format(date) fileName = os.path.abspath( os.path.join(os.getcwd(), "../config/import/" + fileName + "_5.txt")) if not os.path.exists(fileName): return None i = -1 dict = {} for line in open(fileName): i = i + 1 if i < 2: continue line = line.strip() if (line == ''): continue lineInfo = line.split('\t') if (len(lineInfo) <= 2): continue data = {} data['code'] = lineInfo[0] try: data['DMA'] = -1 if len(lineInfo) < 10 else lineInfo[9] data['AMA'] = -1 if len(lineInfo) < 11 else lineInfo[10] data['MID'] = -1 if len(lineInfo) < 12 else lineInfo[11] data['UPP'] = -1 if len(lineInfo) < 13 else lineInfo[12] data['LOW'] = -1 if len(lineInfo) < 14 else lineInfo[13] data['BIAS1'] = -1 if len(lineInfo) < 15 else lineInfo[14] data['BIAS2'] = -1 if len(lineInfo) < 16 else lineInfo[15] data['BIAS3'] = -1 if len(lineInfo) < 17 else lineInfo[16] dict[lineInfo[0]] = data except Exception, e: writeErrorLog( unicode("[getDMA_BOLL_BIASfailed]code:{0}, e:{1}").format( lineInfo, str(e)))
def getPSY_RSI_MTM(date): fileName = unicode("沪深A股{0}").format(date) fileName = os.path.abspath( os.path.join(os.getcwd(), "../config/import/" + fileName + "_3.txt")) if not os.path.exists(fileName): return None i = -1 dict = {} for line in open(fileName): i = i + 1 if i < 2: continue line = line.strip() if (line == ''): continue lineInfo = line.split('\t') if (len(lineInfo) <= 2): continue data = {} data['code'] = lineInfo[0] try: data['PSY'] = lineInfo[9] data['RSI1'] = -1 if len(lineInfo) < 12 else lineInfo[11] data['RSI2'] = -1 if len(lineInfo) < 13 else lineInfo[12] data['RSI3'] = -1 if len(lineInfo) < 14 else lineInfo[13] data['MTM'] = -1 if len(lineInfo) < 15 else lineInfo[14] data['MTMMA'] = -1 if len(lineInfo) < 16 else lineInfo[15] dict[lineInfo[0]] = data except Exception, e: writeErrorLog( unicode("[getPSY_RSI_MTMfailed]code:{0}, e:{1}").format( lineInfo, str(e)))
import sys import os sys.path.insert(0, os.path.abspath(os.path.join(os.getcwd(), ".."))) from wxpy import * from wechat_sender import * from common.LoggerHelper import writeLog, writeErrorLog reload(sys) sys.setdefaultencoding('utf-8') try: bot = Bot('bot.pkl', console_qr=True) master = ensure_one(bot.friends().search(unicode('踏雪'))) log_group = ensure_one(bot.friends().search(unicode('期待'))) token = '295cc3d8-c977-11e6-a341-0090f5f61084' writeLog(unicode("receiver: {0}, {1}").format(master, log_group)) listen(bot, [master, log_group], token=token, port=9090, status_report=True, status_receiver=master) except Exception, e: writeErrorLog(unicode("receive message error: {0}").format(str(e)))
def checkAvgLine(fileName, stockList, needSaveToDb=True): result = [] result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format("代码", "类别", "昨日最高价", "今日最高价", "MaN", "名称").encode('gbk')) i = 0 weekday = datetime.today().weekday() datediff = 3 if weekday == 0 else (weekday - 4 if weekday == 6 else 1) date = (datetime.now() - timedelta(days=datediff)).strftime('%Y-%m-%d') while i < len(stockList): code = stockList[i][0] name = stockList[i][1] try: sql = unicode( "SELECT code,date,MA5,MA10,MA20,MA30,MA60,MA120,MA250 from s_stock where code='{0}' and date='{1}'" ).format(code, date) data = select(sql) if (len(data) <= 0): writeLog( unicode("没有获取到均值数据, code: {0}, date: {1}").format( code, date)) i = i + 1 continue ma5 = data[0][2] ma10 = data[0][3] ma30 = data[0][5] ma60 = data[0][6] ma120 = data[0][7] ma250 = data[0][8] yesterdayPrice = StockInfo.getNPrice(code, 1, 'high') realTimePrice = float(getRealTimeData(code)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice <= ma5 and realTimePrice >= ma5): # 击穿5日均线,并上涨 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '5up', yesterdayPrice, realTimePrice, ma5, name).encode('gbk')) writeLog( unicode( "[5up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma5: {3}" ).format(code, yesterdayPrice, realTimePrice, ma5)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice <= ma10 and realTimePrice >= ma10): # 击穿10日均线,并上涨 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '10up', yesterdayPrice, realTimePrice, ma10, name).encode('gbk')) writeLog( unicode( "[10up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma10: {3}" ).format(code, yesterdayPrice, realTimePrice, ma10)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice <= ma30 and realTimePrice >= ma30): # 击穿30日均线,并上涨 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '30up', yesterdayPrice, realTimePrice, ma30, name).encode('gbk')) writeLog( unicode( "[30up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma30: {3}" ).format(code, yesterdayPrice, realTimePrice, ma30)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice <= ma60 and realTimePrice >= ma60): # 击穿60日均线,并上涨 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '60up', yesterdayPrice, realTimePrice, ma60, name).encode('gbk')) writeLog( unicode( "[60up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma60: {3}" ).format(code, yesterdayPrice, realTimePrice, ma60)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice <= ma120 and realTimePrice >= ma120): # 击穿120日均线,并上涨 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '120up', yesterdayPrice, realTimePrice, ma120, name).encode('gbk')) writeLog( unicode( "[120up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma120: {3}" ).format(code, yesterdayPrice, realTimePrice, ma120)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice <= ma250 and realTimePrice >= ma250): # 击穿250日均线,并上涨 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '250up', yesterdayPrice, realTimePrice, ma250, name).encode('gbk')) writeLog( unicode( "[250up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma250: {3}" ).format(code, yesterdayPrice, realTimePrice, ma250)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice >= ma5 and realTimePrice <= ma5): # 击穿5日均线,并下跌 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '5down', yesterdayPrice, realTimePrice, ma5, name).encode('gbk')) writeLog( unicode( "[5down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma5: {3}" ).format(code, yesterdayPrice, realTimePrice, ma5)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice >= ma10 and realTimePrice <= ma10): # 击穿10日均线,并下跌 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '10down', yesterdayPrice, realTimePrice, ma10, name).encode('gbk')) writeLog( unicode( "[10down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma10: {3}" ).format(code, yesterdayPrice, realTimePrice, ma10)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice >= ma30 and realTimePrice <= ma30): # 击穿30日均线,并下跌 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '30down', yesterdayPrice, realTimePrice, ma30, name).encode('gbk')) writeLog( unicode( "[30down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma30: {3}" ).format(code, yesterdayPrice, realTimePrice, ma30)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice >= ma60 and realTimePrice <= ma60): # 击穿60日均线,并下跌 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '60down', yesterdayPrice, realTimePrice, ma60, name).encode('gbk')) writeLog( unicode( "[60down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma60: {3}" ).format(code, yesterdayPrice, realTimePrice, ma60)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice >= ma120 and realTimePrice <= ma120): # 击穿120日均线,并下跌 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '120down', yesterdayPrice, realTimePrice, ma120, name).encode('gbk')) writeLog( unicode( "[120down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma120: {3}" ).format(code, yesterdayPrice, realTimePrice, ma120)) if (yesterdayPrice != 0 and realTimePrice != 0 and yesterdayPrice >= ma250 and realTimePrice <= ma250): # 击穿250日均线,并下跌 result.append( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '250down', yesterdayPrice, realTimePrice, ma250, name).encode('gbk')) writeLog( unicode( "[250down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma250: {3}" ).format(code, yesterdayPrice, realTimePrice, ma250)) i = i + 1 except Exception, e: writeErrorLog( unicode("checkAvgLineFailed, code:{0}, i:{1}, e:{2}").format( code, i, str(e))) i = i + 1 time.sleep(0.5)
def checkAvgline(stockList): date = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d') dictLastRemindPrice = {} while 1 == 1: if datetime.now().hour >= 16: # 今日提醒程序结束 return for stock in stockList: code = stock[0] name = stock[1] try: sql = unicode( "SELECT code,date,MA5,MA10,MA20,MA30,MA60,MA120,MA250 from s_stock where code='{0}' and date='{1}'" ).format(code, date) data = select(sql) if (len(data) <= 0): writeLog( unicode("没有获取到均值数据, code: {0}, date: {1}").format( code, date)) continue ma5 = data[0][2] ma10 = data[0][3] ma30 = data[0][5] ma60 = data[0][6] ma120 = data[0][7] ma250 = data[0][8] lastPrice = getNPrice( code, 1, 'high') if not dictLastRemindPrice.has_key( code) else dictLastRemindPrice.get(code) realTimePrice = float(getRealTimeData(code)) print name, lastPrice, realTimePrice if (lastPrice != 0 and realTimePrice != 0 and lastPrice <= ma5 and realTimePrice >= ma5): # 击穿5日均线,并上涨 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '5up', lastPrice, realTimePrice, ma5, name)) writeLog( unicode( "[remind_5up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma5: {3}" ).format(code, lastPrice, realTimePrice, ma5)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice <= ma10 and lastPrice >= ma10): # 击穿10日均线,并上涨 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '10up', lastPrice, realTimePrice, ma10, name)) writeLog( unicode( "[remind_10up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma10: {3}" ).format(code, lastPrice, realTimePrice, ma10)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice <= ma30 and realTimePrice >= ma30): # 击穿30日均线,并上涨 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '30up', lastPrice, realTimePrice, ma30, name)) writeLog( unicode( "[remind_30up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma30: {3}" ).format(code, lastPrice, realTimePrice, ma30)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice <= ma60 and realTimePrice >= ma60): # 击穿60日均线,并上涨 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '60up', lastPrice, realTimePrice, ma60, name)) writeLog( unicode( "[remind_60up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma60: {3}" ).format(code, lastPrice, realTimePrice, ma60)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice <= ma120 and realTimePrice >= ma120): # 击穿120日均线,并上涨 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '120up', lastPrice, realTimePrice, ma120, name)) writeLog( unicode( "[remind_120up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma120: {3}" ).format(code, lastPrice, realTimePrice, ma120)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice <= ma250 and realTimePrice >= ma250): # 击穿250日均线,并上涨 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '250up', lastPrice, realTimePrice, ma250, name)) writeLog( unicode( "[remind_250up] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma250: {3}" ).format(code, lastPrice, realTimePrice, ma250)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice >= ma5 and realTimePrice <= ma5): # 击穿5日均线,并下跌 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '5down', lastPrice, realTimePrice, ma5, name)) writeLog( unicode( "[remind_5down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma5: {3}" ).format(code, lastPrice, realTimePrice, ma5)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice >= ma10 and realTimePrice <= ma10): # 击穿10日均线,并下跌 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '10down', lastPrice, realTimePrice, ma10, name)) writeLog( unicode( "[remind_10down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma10: {3}" ).format(code, lastPrice, realTimePrice, ma10)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice >= ma30 and realTimePrice <= ma30): # 击穿30日均线,并下跌 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '30down', lastPrice, realTimePrice, ma30, name)) writeLog( unicode( "[remind_30down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma30: {3}" ).format(code, lastPrice, realTimePrice, ma30)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice >= ma60 and realTimePrice <= ma60): # 击穿60日均线,并下跌 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '60down', lastPrice, realTimePrice, ma60, name)) writeLog( unicode( "[remind_60down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma60: {3}" ).format(code, lastPrice, realTimePrice, ma60)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice >= ma120 and realTimePrice <= ma120): # 击穿120日均线,并下跌 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '120down', lastPrice, realTimePrice, ma120, name)) writeLog( unicode( "[remind_120down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma120: {3}" ).format(code, lastPrice, realTimePrice, ma120)) if (lastPrice != 0 and realTimePrice != 0 and lastPrice >= ma250 and realTimePrice <= ma250): # 击穿250日均线,并下跌 sendMessageToMySelf( unicode("{0},{5},{1},{2},{3},{4}\n").format( code, '250down', lastPrice, realTimePrice, ma250, name)) writeLog( unicode( "[remind_250down] code:{0}, yesterdayPrice: {1}, realTimePrice: {2}, ma250: {3}" ).format(code, lastPrice, realTimePrice, ma250)) dictLastRemindPrice[code] = realTimePrice except Exception, e: writeErrorLog( unicode( "remind_checkAvgLineFailed, code:{0}, e:{1}").format( code, str(e))) time.sleep(1)