class UpdateNowpriceHandler(object): ''' classdocs ''' def __init__(self, twseDict, gtsmDict): ''' Constructor ''' self.dbHandler = MySQLHandler() self.twseDict = twseDict self.gtsmDict = gtsmDict def main(self): date = datetime.datetime.now().date() MysqlDatas = self.dbHandler.getAllCanUpdatePrice(date) print MysqlDatas for MysqlData in MysqlDatas: print MysqlData code = MysqlData['predict_targetcode'] if (self.twseDict.get(str(code)) != None): twseHandler = TwseHandler() MysqlData['now_price'] = twseHandler.getNowPrice( MysqlData['predict_time'], code) if (self.gtsmDict.get(str(code)) != None): gtsmHandler = GtsmHandler() MysqlData['now_price'] = gtsmHandler.getNowPrice( MysqlData['predict_time'], code) if MysqlData['now_price'] != 0 and MysqlData['now_price'] != None: self.dbHandler.updateOracletNowprice(MysqlData)
def __init__(self, twseDict, gtsmDict): ''' Constructor ''' self.dbHandler = MySQLHandler() self.twseDict = twseDict self.gtsmDict = gtsmDict
def pttBuild(self, d): dbHandler = MySQLHandler() mysqlData = dbHandler.getOnePttSource(d['b_author'], d['d_date'])[0] oracletDict = {} print mysqlData['title'] print type(mysqlData['title']) codeInfo = self.newFindCode(mysqlData['title'], mysqlData['content']) if codeInfo != False: oracletDict['name'] = mysqlData['author'].split(u'(')[0] oracletDict['date'] = mysqlData['date'].date() oracletDict['code'] = codeInfo[0] oracletDict['targetName'] = codeInfo[1] oracletDict['type'] = codeInfo[2] oracletDict['event'] = self.newgetEvent(mysqlData['content'], mysqlData['title']) print 'event:', oracletDict['event'] oracletDict['occurTime'] = self.getOccurTime( mysqlData['content'], oracletDict['date']) oracletDict['nowprice'] = None # if oracletDict['type']==5: # oracletDict['nowprice']=TwseHandler().getNowPrice(oracletDict['date'],oracletDict['code']) # elif oracletDict['type']==6: # oracletDict['nowprice']= GtsmHandler().getNowPrice(oracletDict['date'],oracletDict['code']) # if oracletDict['name']==u'bbs3379': # print 'test' # 4/5問題集1,目前先用==None代替 # if oracletDict['nowprice']==False or oracletDict['nowprice'] == 0 \ # or oracletDict['nowprice'] ==None or oracletDict['event']==False: # return False if oracletDict['event'] == False: return False else: # 需判斷有沒有預測人啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if dbHandler.getPredictPeople(oracletDict['name']) == False: dbHandler.setOnePredictPeople(oracletDict['name'], '2') pass dbHandler.setOneOraclet(oracletDict) return oracletDict else: return False pass
def updatePttAndMessage(self, pttSource, d): messagestr = d['g_message'].replace('\\', '') message = json.loads(messagestr) dbHandler = MySQLHandler() # 把是否有oraclet的判斷移到外面 # self.newFindCode(mysqlData['title'],mysqlData['content']) codeInfo = self.newFindCode(pttSource[0]['title'], pttSource[0]['content']) print codeInfo if codeInfo != False: try: oracletNumber = dbHandler.getOneOraclet( pttSource[0]['author'].split(u'(')[0].encode('utf-8'), pttSource[0]['date'].date().strftime("%Y-%m-%d"), str(codeInfo[0]), self.getEvent( pttSource[0]['content']).encode('utf-8'))[0]['number'] except IndexError as e: logging.exception("updatePtt") return False # 如果標的文沒有event的話會跳到這,也就是此文章沒有立卦的意思 except AttributeError as e: print 'updatePttAndMessage AttributeError---------------------------------------------' logging.exception("updatePttGetEvent") return False dbMessage = json.loads(pttSource[0]['message']) updateName = set() # 如果新撈到的留言筆數比原資料庫中的筆數多 if len(dbMessage) < len(message): # 需更新pttsource資料庫!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! dbHandler.updatePttsource(pttSource[0]['author'], pttSource[0]['date'], messagestr) for temp in message: # 不確定能不能用,需確認 if temp not in dbMessage: updateName.add(temp['id']) message = SetOracletHandler.normalizeMessage(messagestr) print message for temp in message: if temp['id'] in updateName: # 需要測試!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! dbHandler.updateOrInsetMessage( temp['id'], pttSource[0]['date'], temp['time'], temp['content'].replace(' ', ''), oracletNumber, temp['status'])
#-*- coding:utf-8 -*- ''' Created on 2016年5月2日 @author: ben ''' import json from MySQLHandler.MySQLHandler import MySQLHandler from datetime import date,datetime if __name__ == '__main__': dbHandler=MySQLHandler() # mySQLData=dbHandler.getAllOraclets() # for SQLData in mySQLData: # print SQLData['results'] # if SQLData['results']!=None: # dbHandler.updateOracletResultStatus(SQLData['number'],SQLData['results']) # # break sql="SELECT * FROM new_results" mySQLData=dbHandler.getSQL(sql) for SQLData in mySQLData: print SQLData priceList=json.loads(SQLData['price_list']) dateList=json.loads(SQLData['date_list']) for idx, date in enumerate(dateList): temp=date.split('/') temp[0]=int(temp[0])+1911 tempDate=datetime.strptime(str(temp[0])+temp[1]+temp[2], '%Y%m%d').date() dateList[idx]=tempDate pass if len(dateList)==len(dateList): for idx, date in enumerate(dateList):
class NewVerifyHandler(object): ''' classdocs ''' def __init__(self, twseDict, gtsmDict): ''' Constructor ''' self.dbHandler = MySQLHandler() self.twseDict = twseDict self.gtsmDict = gtsmDict def determineAuthenticationSource(self, code): if (self.twseDict.get(str(code)) != None): return u'上市' if (self.gtsmDict.get(str(code)) != None): return u'上櫃' return False def calculateROI(self, priceList, nowPrice, eventContent): ROI = 0 for price in priceList: price = float(price) # print type(price) # print type(self.nowPrice) ROI += (price - nowPrice) / nowPrice ROI /= len(priceList) if eventContent == u"賣出": ROI = ROI * (-1) roi = ROI return roi # 回傳oraclet,roi及result長度為4 def shorTermROI(self, priceList, oraclet): while len(priceList) != 0: weekpriceList = [] weekpriceList = priceList[:5] del priceList[0:5] ROI = 0 for price in weekpriceList: price = float(price) # print type(price) # print type(self.nowPrice) ROI += (price - oraclet.nowPrice) / oraclet.nowPrice ROI /= len(weekpriceList) if oraclet.eventContent == u"賣出": ROI = ROI * (-1) oraclet.roi.append(ROI) return oraclet def rankVerify(self, oraclet): AuthenType = self.determineAuthenticationSource( oraclet.predictTargetcode) if AuthenType == False: return if AuthenType == u'上市': getPricrHandler = TwseHandler() elif AuthenType == u'上櫃': getPricrHandler = GtsmHandler() priceList = getPricrHandler.getAuthenticationSource( oraclet.predictTargetcode, oraclet.startDate, oraclet.endDate, oraclet.period) while len(priceList) > oraclet.listLen: priceList.pop() if u'--' in priceList: logging.warning(str(oraclet.number) + ',reason:--') return False print priceList print len(priceList) if len(priceList) == oraclet.listLen: if oraclet.listLen == 20: oraclet = self.shorTermROI(priceList, oraclet) for roi in oraclet.roi: if roi > 0: oraclet.results.append(True) else: oraclet.results.append(False) else: oraclet.roi.append( self.calculateROI(priceList, oraclet.nowPrice, oraclet.eventContent)) if oraclet.roi != None: if oraclet.roi[0] > 0: oraclet.results.append(True) else: oraclet.results.append(False) return oraclet else: return False def OneOracletVerify(self, oraclet): self.dbHandler.updateOracletResults(oraclet, 0) if oraclet.eventContent == u'買進' or oraclet.eventContent == u'賣出': oraclet = self.rankVerify(oraclet) # 外資目標價驗證,還沒做~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ else: pass # update到卜卦庫,還沒做~~ if oraclet != False: print oraclet.roi print oraclet.results self.dbHandler.verifyUpdateToDb(oraclet) self.dbHandler.updateOracletResults(oraclet, 1) pass def main(self): # 從資料庫撈取results等於null,且發生日期小於今天 MysqlData = self.dbHandler.getAllCanVerifyOraclets() x = 0 for record in MysqlData: print 'record:', record oraclet = Oraclet(record) self.OneOracletVerify(oraclet) pass # x+=1 # if x==10: # break pass
def dataToMYsql(self, d): dbHandler = MySQLHandler() pttSource = dbHandler.getOnePttSource(d['b_author'], d['d_date']) if pttSource == False: # 新增ptt_source dbHandler.setOnePttSource(d) # 卜卦立案 oracletDict = self.pttBuild(d) if oracletDict != False: # 毛盾卜卦找尋集更新、更新留言庫 # 3_23 留言資料庫更新coding完成,需要測試~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ dbHandler.setOneContradiction(oracletDict) messagestr = d['g_message'].replace('\\', '') message = SetOracletHandler.normalizeMessage(messagestr) dbHandler.setNewMessage(message, oracletDict) else: # 拿取oracelt 沒拿到代表不用做更新 dbHandler.updatePttsource(d['b_author'], d['d_date'], d['g_message']) self.updatePttAndMessage(pttSource, d) # 檢查是否需要更新ptt_source、更新留言庫 pass pass
class NewVerifyHandler(object): ''' classdocs ''' def __init__(self, twseDict, gtsmDict): ''' Constructor ''' self.dbHandler = MySQLHandler() self.getDbHandler = GetDbHandler() self.setDbHandler = SetDbHandler() self.twseDict = twseDict self.gtsmDict = gtsmDict def determineAuthenticationSource(self, code): if (self.twseDict.get(str(code)) != None): return u'上市' if (self.gtsmDict.get(str(code)) != None): return u'上櫃' return False def calculateROI(self, priceList, nowPrice, eventContent): ROI = 0 # 代表是長期投資,每20筆取一筆即可 tempPriceList = [] if len(priceList) > 100: tempPriceList.append(priceList[0]) for i in range(1, 7, 1): tempPriceList.append(priceList[(20 * i) - 1]) priceList = tempPriceList print 'tempPriceList:', priceList for price in priceList: price = float(price) # print type(price) # print type(self.nowPrice) ROI += (price - nowPrice) / nowPrice ROI /= len(priceList) if eventContent == u"賣出": ROI = ROI * (-1) roi = ROI return roi # 回傳oraclet,roi及result長度為4 def shorTermROI(self, priceList, oraclet): while len(priceList) != 0: weekpriceList = [] weekpriceList = priceList[:5] del priceList[0:5] ROI = 0 for price in weekpriceList: price = float(price) # print type(price) # print type(self.nowPrice) ROI += (price - oraclet.nowPrice) / oraclet.nowPrice ROI /= len(weekpriceList) if oraclet.eventContent == u"賣出": ROI = ROI * (-1) oraclet.roi.append(ROI) return oraclet def rankVerify(self, oraclet): AuthenType = self.determineAuthenticationSource( oraclet.predictTargetcode) if AuthenType == False: return False if AuthenType == u'上市': getPricrHandler = TwseHandler() elif AuthenType == u'上櫃': getPricrHandler = GtsmHandler() priceDict = getPricrHandler.getAuthenticationSource2( oraclet.predictTargetcode, oraclet.startDate, oraclet.period, oraclet.listLen) if priceDict == False: return False else: oraclet.roi = self.calculateROI(priceDict['price'], oraclet.nowPrice, oraclet.eventContent) if oraclet.roi != None: if oraclet.roi > 0: oraclet.results = True else: oraclet.results = False oraclet.priceList = priceDict['price'] oraclet.dateList = priceDict['date'] return oraclet else: return False def foreignTargetPriceVerify(self, oraclet): AuthenType = self.determineAuthenticationSource( oraclet.predictTargetcode) if AuthenType == False: print 'AuthenType False' return False if AuthenType == u'上市': getPricrHandler = TwseHandler() elif AuthenType == u'上櫃': getPricrHandler = GtsmHandler() priceDict = getPricrHandler.getAuthenticationSource2( oraclet.predictTargetcode, oraclet.startDate, oraclet.period, oraclet.listLen) if priceDict == False: print 'priceDict False' return False else: oraclet.priceList = priceDict['price'] oraclet.dateList = priceDict['date'] oraclet.results = False # 5/22修改 要看有沒有錯!!!!!!! if float(oraclet.eventContent) > float(oraclet.nowPrice): for price in priceDict['price']: if float(price) > float(oraclet.eventContent): oraclet.results = True break else: for price in priceDict['price']: if float(price) < float(oraclet.eventContent): oraclet.results = True break return oraclet def OneOracletVerify(self, oraclet): self.dbHandler.updateOracletResultStatus(oraclet.number, 0) if oraclet.eventContent == u'買進' or oraclet.eventContent == u'賣出': oraclet = self.rankVerify(oraclet) # 外資目標價驗證,還沒做~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ else: oraclet = self.foreignTargetPriceVerify(oraclet) pass # setresultRecord 一次要更新兩個 if oraclet != False: for idx, date in enumerate(oraclet.dateList): temp = date.split('/') temp[0] = int(temp[0]) + 1911 tempDate = datetime.datetime.strptime( str(temp[0]) + temp[1] + temp[2], '%Y%m%d').date() self.dbHandler.setresultRecord(oraclet.number, tempDate, oraclet.priceList[idx]) if self.dbHandler.newVerifyUpdateToDb(oraclet): self.dbHandler.updateOracletResults(oraclet, oraclet.results) self.dbHandler.updateOracletResultStatus(oraclet.number, 1) # 更新預測人預測準確率(未驗證) # predictorData=self.getDbHandler.getOnePredictorAccuracy(oraclet.predictPeople)[0] # OracletCount=self.getDbHandler.getPredictorOracletCount(oraclet.predictPeople)[0] # self.setDbHandler.updatePredictorStatus(predictorData['predict_people'], OracletCount[0]['count'], # predictorData['count'], predictorData['sum'], # predictorData['accuracy']) print "true---------------------------" pass def main(self): # 從資料庫撈取results等於null,且發生日期小於今天 MysqlData = self.dbHandler.getAllCanVerifyOraclets() x = 0 for record in MysqlData: print 'record:', record oraclet = Oraclet(record) self.OneOracletVerify(oraclet) pass x += 1 if x == 1000: break