def insert_stock_trans(username, qname, price, amount, extra): data = {} data['user'] = username fee = extra.get('fee', 0) if amount > 0: data['act'] = 'buy' else: data['act'] = 'sell' data['ts'] = datetime.datetime.now() data['q'] = qname data['price'] = price data['fee'] = fee data['amount'] = amount if 'dt' in extra: data['dt'] = extra['dt'] else: data['dt'] = datetime.datetime.now() db, client = db_helper.get_db() tbl = db.transaction tbl.insert(data) cy = get_cy_by_qname(qname) if data['act'] == 'buy': money = abs(amount) * price + fee modify_cash(username, -money, cy, 'buy') else: #sell money = abs(amount) * price - fee modify_cash(username, money, cy, 'buy') modify_stock_holding(username, qname, price, amount, fee)
def get_holding(username): db, client = db_helper.get_db() tbl = db.holding arr = [] for q in tbl.find({'user': username}): arr.append(q) return arr
def get_holding(username): db, client = db_helper.get_db() tbl = db.holding arr = [] for q in tbl.find({'user':username}): arr.append(q) return arr
def update_quote(qname, sname, price): db, client = db_helper.get_db() tbl = db.quotes data = tbl.find_one({'q':qname}) if not data: data = {} data['q'] = qname data['sname'] = sname data['price'] = price data['ts'] = datetime.datetime.now() tbl.save(data)
def get_holding_qname(username, cate): db, client = db_helper.get_db() tbl = db.holding arr = [] for q in tbl.find({'user': username}): if cate == 'stock': if q['q'].find(".of") < 0: arr.append(q['q']) elif cate == 'fund': if q['q'].find('.of') > 0: arr.append(q['q']) return arr
def get_holding_qname(username, cate): db, client = db_helper.get_db() tbl = db.holding arr = [] for q in tbl.find({'user':username}): if cate == 'stock': if q['q'].find(".of") < 0: arr.append(q['q']) elif cate == 'fund': if q['q'].find('.of') > 0: arr.append(q['q']) return arr
def update_quote(qname, sname, price): db, client = db_helper.get_db() tbl = db.quotes data = tbl.find_one({'q': qname}) if not data: data = {} data['q'] = qname data['sname'] = sname data['price'] = price data['ts'] = datetime.datetime.now() tbl.save(data)
def update_security_info(qname, chname, cate, xchg): db, client = db_helper.get_db() tbl = db.security_info data = tbl.find_one({'q':qname}) if not data: data = {} data['q'] = qname data['chname'] = chname data['cate'] = cate data['xchg'] = xchg tbl.save(data)
def update_security_info(qname, chname, cate, xchg): db, client = db_helper.get_db() tbl = db.security_info data = tbl.find_one({'q': qname}) if not data: data = {} data['q'] = qname data['chname'] = chname data['cate'] = cate data['xchg'] = xchg tbl.save(data)
def modify_cash(username, money, cy, ev): db, client = db_helper.get_db() tb = db.cash_holding data = tb.find_one({'cy':cy, 'user':username}) if not data: data = {} data['user'] = username data['cy'] = cy data['am'] = 0 data['am'] = data['am'] + money tb.save(data) tb = db.cash_log tb.insert({'user': username, 'cy': cy, 'ev':ev, 'am': money, 'ts':datetime.datetime.now()})
def modify_cash(username, money, cy, ev): db, client = db_helper.get_db() tb = db.cash_holding data = tb.find_one({'cy': cy, 'user': username}) if not data: data = {} data['user'] = username data['cy'] = cy data['am'] = 0 data['am'] = data['am'] + money tb.save(data) tb = db.cash_log tb.insert({ 'user': username, 'cy': cy, 'ev': ev, 'am': money, 'ts': datetime.datetime.now() })
def modify_stock_holding(username, qname, price, amount, fee): db, client = db_helper.get_db() #update holding tbl_hold = db.holding data = tbl_hold.find_one({'user': username, 'q': qname}) if not data: data = {} data['user'] = username data['q'] = qname old_am = data.get('am', 0) old_price = data.get('price', 0) new_am = old_am + amount new_price = old_price if amount > 0: #average holding cost new_price = (old_price * old_am + price * amount + fee) / new_am data['price'] = round(new_price, 2) data['am'] = new_am tbl_hold.save(data)
def modify_stock_holding(username, qname, price, amount, fee): db, client = db_helper.get_db() #update holding tbl_hold = db.holding data = tbl_hold.find_one({'user':username, 'q':qname}) if not data: data = {} data['user'] = username data['q'] = qname old_am = data.get('am', 0) old_price = data.get('price', 0) new_am = old_am + amount new_price = old_price if amount > 0: #average holding cost new_price = (old_price * old_am + price * amount + fee) / new_am data['price'] = round(new_price, 2) data['am'] = new_am tbl_hold.save(data)
def test_create_db(self): self.assertTrue(isinstance(get_db(self.trade_class, DB_CONFIG_TESTS), TMSDataBase))
def setUp(self): """ request and clean the table before each test""" config = DB_CONFIG_TESTS self.database = get_db(self.trade_class, config) self.database.clean_table()
def get_price(qname): db, client = db_helper.get_db() tbl = db.quotes data = tbl.find_one({'q':qname}) if not data: return 0 return data['price']
def get_cash(username, cy): db, client = db_helper.get_db() tb = db.cash_holding data = tb.find_one({'cy': cy, 'user': username}) return data['am']
def get_sname(qname): db, client = db_helper.get_db() tbl = db.security_info data = tbl.find_one({'q': qname}) if not data: return '' return data['chname']
def get_sname(qname): db, client = db_helper.get_db() tbl = db.security_info data = tbl.find_one({'q':qname}) if not data: return '' return data['chname']
def get_cash(username, cy): db, client = db_helper.get_db() tb = db.cash_holding data = tb.find_one({'cy':cy, 'user':username}) return data['am']
def get_price(qname): db, client = db_helper.get_db() tbl = db.quotes data = tbl.find_one({'q': qname}) if not data: return 0 return data['price']