def createPortfolio(pfGroup, portfolioName, symbolArray): #market type TW inlculde OTC market. stockArray=[]; if (symbolArray != []): #print(symbolArray) for i in range(0,len(symbolArray)): stockSymbol=symbolArray[i].strip(); #print(stockSymbol) #the followings copy from addSymbol stockSymbol1, marketType = stockUtil.parseInputSymbol(pfGroup, stockSymbol); #If stockSymbol=xxxx__xx, stockSymbol1=xxxxx stockName = stockUtil.getStockNameFromCSV(marketType ,stockSymbol1); #Assume input is stockId. if input not in CSV, stockName is set None stockId = stockUtil.getStockIdFromCSV(marketType, stockSymbol1); #Assume input is stockName. if input not in CSV, stockId is set None if stockId==None and stockName!=None: stockId=stockSymbol1; #input is stockId if stockId!=None and stockName==None: stockName=stockSymbol1; #input is stockName if stockId==None and stockName==None: stockId=stockSymbol1; #input can't found in csv Table stockName=stockId; #input can't found in csv Table #print("stockId:" + stockUtil.cvNone(stockId)) #print("stockName:" + stockUtil.cvNone(stockName)) #print("marketType:" + stockUtil.cvNone(marketType)) #print("stockSymbol1:" + stockUtil.cvNone(stockSymbol1)) stockArray.append( {"stockId":stockId , "stockName":stockName, "marketType":marketType}); #print(stockArray); pfCount=len(Portfolio.select().where(Portfolio.group == pfGroup)) #q=Portfolio.insert({"group":pfGroup, "index":pfCount, "name": portfolioName ,"stock_array":stockArray}) q=Portfolio.insert(group=pfGroup, index=pfCount, name=portfolioName ,stock_array=stockArray) #q=Portfolio.insert({Portfolio.group:pfGroup, Portfolio.index:pfCount, Portfolio.name: portfolioName ,Portfolio.stock_array:stockArray}) q.execute()
def pfGroup_pfIndex_addSymbol_vf(request,pfGroup,pfIndex): #form post pfIndexNo=int(pfIndex.replace("pf","")) if request.method == "POST": stockSymbol = request.POST["symbol"] if stockSymbol != "": stockSymbol1, marketType = stockUtil.parseInputSymbol(pfGroup, stockSymbol); #If stockSymbol=xxxx__xx, stockSymbol1=xxxxx stockName = stockUtil.getStockNameFromCSV(marketType ,stockSymbol1); #Assume input is stockId. if input not in CSV, stockName is set None stockId = stockUtil.getStockIdFromCSV(marketType, stockSymbol1); #Assume input is stockName. if input not in CSV, stockId is set None if stockId==None and stockName!=None: stockId=stockSymbol1; #input is stockId if stockId!=None and stockName==None: stockName=stockSymbol1; #input is stockName if stockId==None and stockName==None: stockId=stockSymbol1; #input can't found in csv Table stockName=stockId; #input can't found in csv Table #print("stockId:" + stockUtil.cvNone(stockId)) #print("stockName:" + stockUtil.cvNone(stockName)) #print("marketType:" + stockUtil.cvNone(marketType)) #print("stockSymbol1:" + stockUtil.cvNone(stockSymbol1)) currentPortfolio = Portfolio.select().where(Portfolio.group == pfGroup, Portfolio.index == pfIndexNo).dicts().first() if currentPortfolio != None: stockArray=stockUtil.evalTextArray(currentPortfolio["stock_array"]) #stock_array is converted form string type to array stockArray.append( {"stockId":stockId.upper() , "stockName":stockName, "marketType":marketType}); q = Portfolio.update(stock_array=stockArray).where(Portfolio.id == currentPortfolio["id"]) q.execute() #print(currentPortfolio) #if no portfolio in pfGroup, addSymbol do nothing. User must add symbol by create portfolio menju. else: from django.contrib import messages messages.add_message(request, messages.INFO, 'add a symbol must within portfolio.') return HttpResponseRedirect("/portfolio/" + pfGroup + "/"+ pfIndex)
def pfGroup_pfIndex_getWatchList_vf(request,pfGroup,pfIndex): pfIndexNo=int(pfIndex.replace("pf","")) currentPortfolio = Portfolio.select().where(Portfolio.group == pfGroup, Portfolio.index == pfIndexNo).dicts().first() stockArray = stockUtil.evalTextArray(currentPortfolio["stock_array"]) #stock_array is converted form string type to array currentPortfolio["stock_array"] = stockArray #print(currentPortfolio) return JsonResponse(currentPortfolio, safe=False)
def pfGroup_deletePortfolioList_vf(request,pfGroup): if request.method == "POST": body_unicode = request.body.decode('utf-8') delIdStr = json.loads(body_unicode) delIdArray=delIdStr.split(",") for Id in delIdArray: #print(Id) q=Portfolio.delete().where(Portfolio.id == Id) q.execute() qry = Portfolio.select().where(Portfolio.group == pfGroup).order_by(Portfolio.index) qry.count for index, item in enumerate(qry): #print(item._data) q = Portfolio.update(index=index).where(Portfolio.id == item.id) q.execute() return HttpResponseRedirect("/portfolio/" + pfGroup + "/pf0")
def pfGroup_export_vf(request,pfGroup): import itertools downloadName = "portfolio" + pfGroup + ".csv" portfolios = Portfolio.select().where(Portfolio.group == pfGroup) portfolioCount=len(portfolios) array=[[] for i in range(0,portfolioCount)] #generate [[],[],[],[],[]] for i, portfolio in enumerate(portfolios): #print(portfolio.name) array[portfolio.index].append(portfolio.name) stockArray=stockUtil.evalTextArray(portfolio.stock_array) #print(stockArray) for stock in stockArray: if pfGroup == stock["marketType"]: if pfGroup=='HK': cell = stock["stockId"] + stock["stockName"] else: cell=stock["stockName"] else: cell = stock["stockName"] + "__" + stock["marketType"] array[portfolio.index].append(cell) #print(array) result=list(itertools.zip_longest(*array)) #print(result) data="" for row in result: newRow = [x if x!=None else "" for x in row] data = data + ",".join(newRow) + "\n" #print(data) response = HttpResponse(data,content_type='text/csv') response['Content-Disposition'] = "attachment; filename=%s" % downloadName return response
def pfGroup_portfolioList_vf(request,pfGroup): data=[] if request.method == "POST": qrys = Portfolio.select(Portfolio.id, Portfolio.group, Portfolio.index, Portfolio.name).where(Portfolio.group == pfGroup) for qry in qrys: data.append({"id":qry.id, "group":qry.group, "index":qry.index, "name":qry.name} ) #print(data) return JsonResponse(data, safe=False)
def cleanData(): pfGroupArray=stockUtil.evalTextArray(stockUtil.read_config("portfolio","pfGroupArray")) allPfArray=[] #collect all portfolio to allPfArray for pfGroup in pfGroupArray: qry = Portfolio.select().where(Portfolio.group == pfGroup).order_by(Portfolio.index) for item in qry: stockArray = stockUtil.evalTextArray(item.stock_array) for stock in stockArray: allPfArray.append(stock) #print(allPfArray) #Delete the history data according to stockid not in portfolio. for pfGroup in pfGroupArray: setattr(HistoryData._meta, "db_table", "history_" + pfGroup.lower()) data1=HistoryData.select(HistoryData.stockid).distinct().dicts() for x in data1: #pick up stockid of historydata table founded=0 #print(x["stockid"]) for y in allPfArray: #look up portfolio if pfGroup == y["marketType"] and x["stockid"] == y["stockId"] : founded=1 break; if founded==0: q = HistoryData.delete().where(HistoryData.stockid == x["stockid"]) q.execute() #Delete the stockinfo data according to stockid not in portfolio. data1=StockInfo.select().dicts() #data1=HistoryData.select(HistoryData.stockid).distinct().dicts() for x in data1: #pick up stockid of historydata table founded=0 #print(x) for y in allPfArray: #look up portfolio if x["markettype"] == y["marketType"] and x["stockid"] == y["stockId"] : founded=1 break; if founded==0: q = StockInfo.delete().where(StockInfo.id == x["id"]) q.execute() #Delete old data outside date of range year now=datetime.datetime.now() currentYear=now.strftime("%Y") keepYears = stockUtil.read_config("stockData.history","keepYears") fromYear = str(int(currentYear) - int(keepYears) + 1) for pfGroup in pfGroupArray: setattr(HistoryData._meta, "db_table", "history_" + pfGroup.lower()) data1=HistoryData.select(HistoryData.stockid).distinct().dicts() q = HistoryData.delete().where(HistoryData.date < fromYear + "-01-01") q.execute() #update DoneYear in StockInfo table qry=StockInfo.select() for item in qry: historyInfoDict=stockUtil.evalTextDict(item.history_info) historyDoneYearAry=historyInfoDict["DoneYear"] array1=[] for i in historyDoneYearAry: if int(i) >= int(fromYear): array1.append(str(i)) q=StockInfo.update(history_info={'DoneYear':array1}).where(StockInfo.id==item.id) q.execute()
def pfGroup_pfIndex_updateWatchList_vf(request,pfGroup,pfIndex): #ajax POST. pfIndexNo=int(pfIndex.replace("pf","")) if request.method == "POST": body_unicode = request.body.decode('utf-8') updatePortfolio = json.loads(body_unicode) #print(updatePortfolio) #print(updatePortfolio["stock_array"]) q=Portfolio.update(stock_array=updatePortfolio["stock_array"]).where(Portfolio.group == pfGroup, Portfolio.index == pfIndexNo) q.execute() return HttpResponseRedirect("/portfolio/" + pfGroup + "/"+ pfIndex)
def pfGroup_updatePortfolioList_vf(request,pfGroup): #ajax post json if request.method == "POST": body_unicode = request.body.decode('utf-8') updateArray = json.loads(body_unicode) #print(updateArray) for item in updateArray: #print(item) q=Portfolio.update(index=item["index"],name=item["name"]).where(Portfolio.id == item["id"]) #Portfolio is a class, item is a dictionary. q.execute() return HttpResponseRedirect("/portfolio/" + pfGroup + "/pf0")
def pfGroup_pfIndex_vf(request,pfGroup,pfIndex): pfGroupArray=stockUtil.evalTextArray(stockUtil.read_config("portfolio","pfGroupArray")) pfNameArray=[] #print("pfGroup:" + pfGroup) #print("pfIndex:" + pfIndex) pfIndexNo=int(pfIndex.replace("pf","")) currentPortfolio = Portfolio.select().where(Portfolio.group == pfGroup, Portfolio.index == pfIndexNo).dicts().first() #print(currentPortfolio) if currentPortfolio != None: stockArray=stockUtil.evalTextArray(currentPortfolio["stock_array"]) #stock_array is converted form string type to array currentPortfolio["stock_array"]=stockArray if currentPortfolio == None: createPortfolio(pfGroup, "1", []) return HttpResponseRedirect("/portfolio/" + pfGroup + "/pf0") qry = Portfolio.select().where(Portfolio.group == pfGroup).order_by(Portfolio.index) for i in qry: pfNameArray.append({"group":i.group,"index":i.index,"name":i.name}) #print(currentPortfolio) #print(pfNameArray) return render(request,"portfolio.html",{"pfGroupArray":pfGroupArray, "pfGroup": pfGroup, "pfIndex": pfIndex, "pfNameArray": pfNameArray, "currentPortfolio": currentPortfolio})
def downloadData(pfGroup): qry = Portfolio.select().where(Portfolio.group == pfGroup).order_by(Portfolio.index) for item in qry: stockArray = stockUtil.evalTextArray(item.stock_array) for stock in stockArray: for i in range(0,2): try: downloadDataHistory(stock["stockId"], stock["marketType"]) #downloadDataFinancial(stock["stockId"], stock["marketType"]) except Exception as e: print("Unexpected error:", sys.exc_info()[0]) #print error type print(str(e))#print error message continue #skip "break" instruction #raise break #let stock be donload only 1 time if no exception. pollMsgQueue.append("History data download complete.")
def pfGroup_pfIndex_chart_vf(request,pfGroup,pfIndex): #href link. render a new page. portfolioCount = Portfolio.select().where(Portfolio.group==pfGroup).count() #print(request.GET) return render(request, "portfolioWatchList_chart.html", { "stockObj":{"stockId":request.GET.get("stockId"), "marketType":request.GET.get("marketType"), "stockName":request.GET.get("stockName")}, "index":request.GET.get("index"), "period":request.GET.get("period"), "portfolioCount":portfolioCount})