예제 #1
0
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)
예제 #2
0
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)
예제 #3
0
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
예제 #4
0
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
예제 #5
0
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)
예제 #6
0
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
예제 #7
0
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
예제 #8
0
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)
예제 #9
0
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)
예제 #10
0
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)
예제 #11
0
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()})
예제 #12
0
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()
    })
예제 #13
0
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)
예제 #14
0
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)
예제 #15
0
 def test_create_db(self):
     self.assertTrue(isinstance(get_db(self.trade_class, DB_CONFIG_TESTS),
                                 TMSDataBase))
예제 #16
0
 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()
예제 #17
0
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']
예제 #18
0
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']
예제 #19
0
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']
예제 #20
0
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']
예제 #21
0
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']
예제 #22
0
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']