def __init__(self, inRate, terms, ulInvest, dayCount, id_plan, taxRate): threading.Thread.__init__(self) self.__flag = threading.Event() # 用于暂停线程的标识 self.__flag.set() # 设置为True self.__running = threading.Event() # 用于停止线程的标识 self.__running.set() # 将running设置为True self.inRate = inRate self.terms = terms self.ulInvest = ulInvest self.dayCount = dayCount self.id_plan = id_plan self.taxRate = taxRate self.Isflag = 1 #确定投资哪只股票 reRate = 0 # 股票的收益率 i = 0 # 循环指针 j = 0 # 选择的股票脚标 for stock in SD.stock: res = ml.MaLearning(stock, dayCount) # 返回值是列表 res = res[0] # 收益增长率 if res > reRate: reRate = res j = i i = i + 1 self.stock = SD.stock[j] self.stock_id = SD.stock_id[j]
def calculate(inRate, terms, ulInvest, dayCount, stock, id_plan, stock_id, taxRate): #计算价值目标,参数:投资增长率,总期数,总额度,时间间隔,股指,对应投资计划的id,股指基金代码 #输出的时候数据要转化成字符串 print "获取的数据 inRate:" + str(inRate) + " terms:" + str( terms) + " ulInvest:" + str(ulInvest) + " dayCount:" + str( dayCount) + " stock:" + str(stock) + " id_plan:" + str( id_plan) + " stock_id:" + str(stock_id) reRate = ml.MaLearning(stock, dayCount)[0] #是个数组 inRate = float(inRate) #投资增长率,注意获取的数据需要转化成可用的形式 terms = int(terms) #总期数 ulInvest = float(ulInvest) #总额度 taxRate = float(taxRate) if reRate > 0: data = DA.Data(id_plan) alInvest = float(data[0][0]) #已经投资的总额 alVolume = float(data[0][1]) #已经投资的份数 alterms = int(data[0][2]) #已经投资的次数 term = alterms + 1 #本次是第几次投资 tempDate = time.strftime('%Y-%m-%d', time.localtime(time.time())) print "ccc " + str(alInvest) + " " + str(alInvest) + " " + str( alVolume) if alterms >= terms: return "over" else: R = (reRate + inRate) / 2 A = pow((1 + R), terms) Invest = ulInvest / (terms * A) # 每期投资的价值目标C TInvest = Invest * term * pow((1 + R), term) #账户此时应该拥有的总价值 fd = ts.get_realtime_quotes(stock_id) price = float(fd['price']) #每股的价格 NeedInvest = TInvest - alVolume * price #本次投资的金额 NeedVolume = NeedInvest / price #本次投资的份额 #只对卖出所得的实际收入进行收税 #if NeedInvest<0: # NeedInvest=NeedInvest*(1-taxRate) alVolume = alVolume + NeedVolume #在计划表中重新计算已经投资的总份额 alInvest = alInvest + NeedInvest #在计划表中重新记录已经投资的总金额 n = 0 m = 0 print "投资数据 NeedInvest:" + str( NeedInvest) + " NeedVolume:" + str( NeedVolume) + " alInvest:" + str( alInvest) + " alVolume:" + str(alVolume) try: conn = MySQLdb.Connect(host='localhost', user='******', passwd='888212', db='economic', port=3306, charset='utf8') cur = conn.cursor() sql1 = "insert into record (date,term,invest,volume,id_plan,stock) values (%s,%s,%s,%s,%s,%s)" n = cur.execute( sql1, (tempDate, term, NeedInvest, NeedVolume, id_plan, stock)) conn.commit() sql2 = "update valueplan set alInvest=%s,alterms=%s,volume=%s where id=%s" m = cur.execute(sql2, (alInvest, term, alVolume, id_plan)) conn.commit() cur.close() conn.close() except: print '投资记录 MySQL connect fail...' print m, n if (n == 1) & (m == 1): return "ok" else: return "error" else: return "not proper"
def change_AFinal(request): if request.POST: ulInvest = request.POST['FulInvest'] #最终价值目标 alInvest = request.POST['FalInvest'] #账户已有值 terms = request.POST['Fterms'] #将要总期数 inRate = request.POST['FinRate'] #投资增长率 taxRate = request.POST['FtaxRate'] #税率 dayCount = request.POST['FdayCount'] #时间间隔 id_user = request.session['userid'] #用户id stock_id = request.POST['Fstock'] inRate = float(inRate) terms = int(terms) dayCount = int(dayCount) taxRate = float(taxRate) ulInvest = float(ulInvest) alInvest = float(alInvest) stock_id = int(stock_id) stock = SD.stock[stock_id] stock_id = SD.stock_id[stock_id] reRate = ml.MaLearning(stock, dayCount)[0] # 股票的收益率,返回值是列表 R = (reRate + inRate) / 2 n = terms #目前和最终之间的期数差 A = pow((1 + R), n) B = alInvest / ulInvest T = n / (1 - B * A) t = T - n #目前的时间 fd = ts.get_realtime_quotes(stock_id) price = float(fd['price']) # 每股的价格 #price=random.uniform(0.9,1.2)#模拟计算的 alVolume = alInvest / price tempTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) #暂时存储一些默认值 tempClass = 2 tempAlterms = 0 tempVolume = alVolume tempAlInvest = alInvest #已经投资的金额 tempIV = alInvest tempover = 1 n = 0 try: conn = MySQLdb.Connect(host='localhost', user='******', passwd='888212', db='economic', port=3306, charset='utf8') cur = conn.cursor() #插入到计划表中 sql1 = "insert into valueplan (ultimateInvest,rateInvest,terms,dayCount,id_user,class,time,alterms,volume,alInvest,over,rateTax,IVInvest) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" n = cur.execute(sql1, (ulInvest, inRate, terms, dayCount, id_user, tempClass, tempTime, tempAlterms, tempVolume, tempAlInvest, tempover, taxRate, tempIV)) conn.commit() #获取计划的id sql2 = "select id from valueplan where time = %s" cur.execute(sql2, (tempTime)) data = cur.fetchall() id_plan = data[0][0] cur.close() conn.close() print id_plan threadRE = threadRecord(inRate, T, t, ulInvest, dayCount, id_plan, taxRate, stock, stock_id) # 调用子线程来执行定时任务,定投 threadRE.start() except: print '制定投资计划 MySQL connect fail...' if n != 1: print "插入错误" return HttpResponse("false") else: return HttpResponse("true") else: print "未获取到数据" return HttpResponse("no data")
def change_initial(request): if request.POST: dayCount = request.POST['FdayCount'] #时间间隔 inInvest = request.POST['FinInvest'] #最终价值目标 terms = request.POST['Fterms'] #总期数 inRate = request.POST['FinRate'] #投资增长率 taxRate = request.POST['FtaxRate'] #税率 id_user = request.session['userid'] #用户id inRate = float(inRate) terms = int(terms) dayCount = int(dayCount) taxRate = float(taxRate) inInvest = float(inInvest) # 确定投资哪只股票 reRate = 0 # 股票的收益率 i = 0 # 循环指针 j = 0 # 选择的股票脚标 for stock in SD.stock: print dayCount res = ml.MaLearning(stock, dayCount) # 返回值是列表 res = res[0] # 收益增长率 if res > reRate: reRate = res j = i i = i + 1 stock = SD.stock[j] stock_id = SD.stock_id[j] R = (reRate + inRate) / 2 A = pow((1 + R), terms) ulInvest = inInvest * (terms * A) # 最终投资价值目标 print taxRate tempTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) #暂时存储一些默认值 tempClass = 0 tempAlterms = 0 tempVolume = 0 tempAlInvest = 0 tempover = 1 n = 0 try: conn = MySQLdb.Connect(host='localhost', user='******', passwd='888212', db='economic', port=3306, charset='utf8') cur = conn.cursor() #插入到计划表中 sql1 = "insert into valueplan (initiateInvest,ultimateInvest,rateInvest,terms,dayCount,id_user,class,time,alterms,volume,alInvest,over,rateTax) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" n = cur.execute(sql1, (inInvest, ulInvest, inRate, terms, dayCount, id_user, tempClass, tempTime, tempAlterms, tempVolume, tempAlInvest, tempover, taxRate)) conn.commit() #获取计划的id sql2 = "select id from valueplan where time = %s" cur.execute(sql2, (tempTime)) data = cur.fetchall() id_plan = data[0][0] cur.close() conn.close() print id_plan threadRE = threadRecord(inRate, terms, ulInvest, dayCount, id_plan, taxRate, stock, stock_id) # 调用子线程来执行定时任务,定投 threadRE.start() except: print '制定投资计划 MySQL connect fail...' if n != 1: print "插入错误" return HttpResponse("false") else: return HttpResponse("true") else: print "未获取到数据" return HttpResponse("no data")