Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
def downloadData_vf(request):
  pfGroupArray=stockUtil.evalTextArray(stockUtil.read_config("portfolio","pfGroupArray"))
  if request.method == "GET":
    return render(request, "downloadData.html", {"pfGroupArray":pfGroupArray})
  else:
    for pfGroup in pfGroupArray:
      if request.POST.get("select" + pfGroup) == 'on':
        downloadData(pfGroup)
  return HttpResponse()
Ejemplo n.º 6
0
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})
Ejemplo n.º 7
0
def deleteData_vf(request):
  pfGroupArray=stockUtil.evalTextArray(stockUtil.read_config("portfolio","pfGroupArray"))
  if request.method == "GET":
    return render(request, "deleteData.html", {"pfGroupArray":pfGroupArray})
  else:
    for pfGroup in pfGroupArray:
      if request.POST.get("select" + pfGroup) == 'on':
        setattr(HistoryData._meta, "db_table", "history_" + pfGroup.lower())
        q = HistoryData.delete()
        q.execute()
    q = StockInfo.delete()
    q.execute()
    q = ProjectInfo.delete()
    q.execute()
    return HttpResponse("Delete data OK.")
Ejemplo n.º 8
0
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.")
Ejemplo n.º 9
0
      database = db
      db_table = 'history'
      primary_key = CompositeKey('date', 'stockid')

class StockInfo(Model):
    stockid = TextField()
    markettype = TextField()
    history_info = TextField()
    finance_info = TextField()
    class Meta:
      database = db
      db_table = 'stockinfo'

class ProjectInfo(Model):
    download_access = TextField() #TRUE if access right is lock. Let download job is only one at the same time.
    download_date = TextField()
    download_time = TextField()
    class Meta:
      database = db
      db_table = 'projectinfo'

db.connect()
Portfolio.create_table(True)
pfGroupArray=stockUtil.evalTextArray(stockUtil.read_config("portfolio","pfGroupArray"))
for pfGroup in pfGroupArray:
  setattr(HistoryData._meta, "db_table", "history_" + pfGroup.lower())
  HistoryData.create_table(True) #argument=True: create table if table not exist.
StockInfo.create_table(True)
ProjectInfo.create_table(True)
db.close()
Ejemplo n.º 10
0
def root_vf(request): #root of portfolio
  pfGroupArray=stockUtil.evalTextArray(stockUtil.read_config("portfolio","pfGroupArray"))
  #print(pfGroupArray)
  return HttpResponseRedirect("/portfolio/" + pfGroupArray[0])