def get(self): portfolios = Portfolio.query().fetch() self.tv["portfolios"] = [] for this_data in portfolios: self.tv["portfolios"].append(this_data.to_object()) self.tv["current_page"] = "EDIT PORTFOLIO" self.render('frontend/portfolio-edit.html')
def check_user(view): c_user = users.get_current_user() if c_user: log = users.create_logout_url(url_for('index')) else: if view[0] in ('security'): log = users.create_login_url(url_for(view[0], pos=view[1])) elif view[0] in ('sec_info', 'edit_security'): log = users.create_login_url(url_for(view[0], pos=view[1], sec_id=view[2])) else: log = users.create_login_url(url_for(view[0])) # Create a new portfolio if new user if c_user: ptfs = Portfolio.query(Portfolio.user == c_user) ptfs = list(ptfs) if len(ptfs) == 0: ptf = Portfolio( user = c_user, points = START_POINTS ) ptf.put() ptf_id = ptf.key.id() flash(u'Portfolio %s successfully created.' % ptf_id, 'success') return [c_user, log]
def get(self): if self.user: self.redirect(self.uri_for('www-dashboard-admin')) return portfolios = Portfolio.query().fetch() self.tv["portfolios"] = [] for this_data in portfolios: self.tv["portfolios"].append(this_data.to_object()) self.tv["current_page"] = "PORTFOLIO" self.render('frontend/portfolio.html')
def match_orders(sec, buysell): """Match orders in cross""" # Get buy and sell lists b = Order.query(Order.security == sec, Order.buysell == 'Buy', Order.active == True, ancestor=sec.key).order(-Order.price, Order.timestamp) s = Order.query(Order.security == sec, Order.buysell == 'Sell', Order.active == True, ancestor=sec.key).order(Order.price, Order.timestamp) b = list(b) s = list(s) # Match orders until market uncrosses bn = 0 sn = 0 while(1): if bn + 1 > len(b): break if sn + 1 > len(s): break if b[bn].price >= s[sn].price: t = Trade() t.timestamp = datetime.utcnow() t.buy_user = b[bn].user t.sell_user = s[sn].user t.security = b[bn].security if buysell == "Buy": t.price = s[sn].price else: t.price = b[bn].price b[bn] = b[bn].key.get() s[sn] = s[sn].key.get() b_ptf = Portfolio.query(Portfolio.user == b[bn].user).get() s_ptf = Portfolio.query(Portfolio.user == s[sn].user).get() if b[bn].volume > s[sn].volume: t.volume = s[sn].volume b[bn].volume += -s[sn].volume s[sn].active = False b_ptf.points += s[sn].volume s_ptf.points += s[sn].volume b[bn].put() s[sn].put() sn += 1 elif b[bn].volume < s[sn].volume: t.volume = b[bn].volume s[sn].volume += -b[bn].volume b[bn].active = False b_ptf.points += b[bn].volume s_ptf.points += b[bn].volume b[bn].put() s[sn].put() bn += 1 elif b[bn].volume == s[sn].volume: t.volume = b[bn].volume b[bn].active = False s[sn].active = False b_ptf.points += b[bn].volume s_ptf.points += b[bn].volume b[bn].put() s[sn].put() bn += 1 sn += 1 b_ptf.put() s_ptf.put() t.put() flash(u'Trade %s successfully completed.' % t.key.id(), 'success') continue break