def get_context_data(self, **kwargs): context = super(StockKLView, self).get_context_data(**kwargs) if not self.sid: return context KLdatas = [] hisData = ReadLocalData() rows = hisData.getLocalData(self.sid) for row in rows: KLdata = [row[DATE_COL_DATE].strftime('%Y%m%d'),0,row[DATE_COL_OPEN],row[DATE_COL_HIGH],row[DATE_COL_LOW],row[DATE_COL_CLOSE],\ row[DATE_COL_VOL], float(row[DATE_COL_CLOSE])*int(row[DATE_COL_VOL])] KLdatas.append(KLdata) #print 'KLdata',KLdata context['KLdatas'] = KLdatas return context
def get_context_data(self, **kwargs): try: context = super(EarningsOverView, self).get_context_data(**kwargs) kdataproc = ReadLocalData() shDapanRows = kdataproc.getLocalData(DPshzhongzhi) shDapanDict = {} print 'shDapanRows=',len(shDapanRows) for row in shDapanRows: shDapanDict[row[DATE_COL_DATE]]=row allOperRows = Operations.objects.all() ownerDict = {} sinaIds = [] for row in allOperRows: if ownerDict.has_key(row.owner): if ownerDict[row.owner].has_key(row.stockId): ownerDict[row.owner][row.stockId].append(row) else: ownerDict[row.owner][row.stockId] = [row] sinaIds.append(stockIDforSina(row.stockId)) else: ownerDict[row.owner] = {row.stockId:[row]} sinaIds.append(stockIDforSina(row.stockId)) sinadata = getCurDataFromSina.sinaStockAPI() resultmap = sinadata.getCurPriFromSina(sinaIds) realSinaData = {} for i in range(len(sinaIds)): realSinaData[sinaIds[i]] = resultmap[i] allOwnerInfo = [] idManager = getAllIdFromSina.sinaIdManage() idManager.initLocalData() for owner, stockrows in ownerDict.items(): curCostTotal, curEarningTotal, historyEarningTotal = 0,0,0 i = 0 listTableDatas = [] alreadyHolderDatas = [] for sid, onestock in stockrows.items(): if not sid in idManager.allstockmap: continue detailinfo = idManager.allstockmap[sid] sortdata = sorted(onestock,key = lambda x:x.time,reverse = False) #print onestock,'--',sortdata curCost, curEarning, historyEarning = self.analysisTrade(sortdata, realSinaData[stockIDforSina(sid)], listTableDatas, alreadyHolderDatas, detailinfo, shDapanDict) curCostTotal += curCost curEarningTotal += curEarning historyEarningTotal += historyEarning print owner,curCostTotal, curEarningTotal, historyEarningTotal allOwnerInfo.append([owner, listTableDatas,alreadyHolderDatas,curCostTotal, curEarningTotal, historyEarningTotal]) context['allOwnerInfo'] = allOwnerInfo except Exception as e: logger.exception(u'加载基本信息出错[%s]!', e) return context
def analysisTrade(self, alltrade, currdata, tabledata, alreadyHolderDatas, detailinfo, shDapanDict): historyEarning, curCost,curHoldCount, curEarning = 0, 0, 0, 0 stockId = alltrade[0].stockId name = currdata[COL('股票名称')] KLurl = '/stockKL?s='+stockId sinaUrl = "http://vip.stock.finance.sina.com.cn/moneyflow/#!ssfx!"+stockIDforSina(stockId) nextTime = datetime.datetime.strptime(currdata[COL('日期')],'%Y-%m-%d') op = '无操作' kdataproc = ReadLocalData() hisrows = kdataproc.getLocalData(stockId, -1, alltrade[0].time) print 'hisrows=',len(hisrows) newPrice = float(currdata[COL('当前价格')]) if newPrice == 0.0: if len(hisrows) > 1: newPrice = float(hisrows[-1][DATE_COL_CLOSE]) else: print name,"can't find new price and history record" return (0,0,0) nextPrice = newPrice totalStockNum = 10000*detailinfo[getAllIdFromSina.SCOL_MKTCAP]/newPrice currDayInfo = self.showCurDayInfo(currdata, totalStockNum) for i in range(len(alltrade)): trade = alltrade[i] curTime = trade.time curPri = float(trade.costPrice) stockCount = trade.stockCount cc = trade.costPrice*trade.stockCount moneyIncrease = 0 if trade.operation == 0: #买入 op = "买%d"%(trade.stockCount) curCost += cc curHoldCount += stockCount elif trade.operation == 1: #卖出 op = "卖%d"%(trade.stockCount) curCost -= cc curHoldCount -= stockCount stockCount=-stockCount else: print "error",trade.operation print name, curCost,curHoldCount, curEarning, trade.stockCount,trade.operation if i == len(alltrade)-1: nextTime = datetime.datetime.strptime(currdata[COL('日期')],'%Y-%m-%d').date() nextPrice = newPrice else: nextTime = alltrade[i+1].time nextPrice = float(alltrade[i+1].costPrice) priIncreasePercent = 100*(nextPrice-curPri)/curPri moneyIncrease = stockCount*(nextPrice-curPri) curEarningInfo = '' curEarn = 0 if curHoldCount == 0: curEarningInfo = "若仍然持有,那么将盈利%d%%"%(priIncreasePercent) else: curEarn = moneyIncrease curEarningInfo = "盈利%d%%"%(priIncreasePercent) #print stockId,curTime,"curPri=",curPri,'nextPrice=',nextPrice,nextTime hisHighPri,hisHighInfo = nextPrice,currDayInfo hisHighVol,hisHighVolInfo = int(currdata[COL('成交量')]),currDayInfo hisLowPri,hisLowInfo = nextPrice,currDayInfo dayCount = 0 volSum = 0 nextDayInfo = currDayInfo thisDayInfo = '' dapanStrong,dapanWeak = 0,0 for i in range(len(hisrows)): hisrow = hisrows[i] histime = hisrow[DATE_COL_DATE] if histime == nextTime: nextDayInfo = self.showDayInfo(hisrow, totalStockNum) elif histime == curTime: thisDayInfo = self.showDayInfo(hisrow, totalStockNum) if histime > curTime and histime < nextTime: dayCount+=1 hisclosepri = float(hisrow[DATE_COL_CLOSE]) hisopenpri = float(hisrow[DATE_COL_OPEN]) hisvol = int(hisrow[DATE_COL_VOL]) volSum+=hisvol if hisclosepri > hisHighPri: hisHighPri = hisclosepri hisHighInfo = self.showDayInfo(hisrow, totalStockNum) if hisclosepri < hisLowPri: hisLowPri = hisclosepri hisLowInfo = self.showDayInfo(hisrow, totalStockNum) if hisvol > hisHighVol: hisHighVol = hisvol hisHighVolInfo = self.showDayInfo(hisrow, totalStockNum) lastClosePri = float(hisrows[i-1][DATE_COL_CLOSE]) #print hisrows[i-1][DATE_COL_DATE] if False== shDapanDict.has_key(hisrows[i-1][DATE_COL_DATE]): #print "dapan not find",hisrows[i-1][DATE_COL_DATE] continue if False== shDapanDict.has_key(hisrows[i][DATE_COL_DATE]): #print "dapan not find",hisrows[i][DATE_COL_DATE] continue lastSHClose = float(shDapanDict[hisrows[i-1][DATE_COL_DATE]][DATE_COL_CLOSE]) thisSHClose = float(shDapanDict[hisrows[i][DATE_COL_DATE]][DATE_COL_CLOSE]) if (thisSHClose-lastSHClose)/lastSHClose > (hisclosepri-lastClosePri)/lastClosePri: dapanStrong+=1 else: dapanWeak+=1 avgVol = 0 avgTurnover = 0 if dayCount > 0: avgVol = float(volSum)/dayCount avgTurnover = 100*avgVol/totalStockNum hisHighVolInfo+=("\n日均换手:%.2f"%(avgTurnover)) else: print "get nothing:",stockId tableLine = [name, sinaUrl, self.showFormat(curTime), thisDayInfo,\ trade.costPrice, op, "金额:%d"%(trade.costPrice*trade.stockCount),\ curHoldCount, "持有金额:%d"%(trade.costPrice*curHoldCount), nextDayInfo,\ nextPrice, curEarn, curEarningInfo, \ "%.2f"%(hisHighPri), hisHighInfo,\ "%.2f"%(hisLowPri), hisLowInfo,\ "%.2f"%(100*hisHighVol/totalStockNum), hisHighVolInfo,\ "%d/%d"%(dapanWeak,dapanStrong), trade.info] tabledata.append(tableLine) print name, curCost,curHoldCount, curEarning, historyEarning if curHoldCount != 0: curEarning = newPrice*curHoldCount - curCost alreadyHolderDatas.append([stockId, KLurl, name, sinaUrl, \ curHoldCount, curCost, curEarning, newPrice]) else: historyEarning = -curCost curEarning = 0 curCost = 0 print name, curCost,curHoldCount, curEarning, historyEarning return (curCost, curEarning, historyEarning)