def stock_assets(): with open('tests/samples/stocks.csv') as fin: reader = csv.reader(fin, delimiter=',') for row in reader: isin, code, name = row if isin.startswith('#'): continue Asset.create(type=AssetType.stock, isin=isin, code=code, name=name)
def stock_assets(): with open('tests/samples/stocks.csv') as fin: reader = csv.reader(fin, delimiter=',') for row in reader: isin, code, name = row if isin.startswith('#'): continue Asset.create( type=AssetType.stock, isin=isin, code=code, name=name)
def asset_sp500(request, db): asset = Asset.create(type='security', name='KB Star S&P500', description='', data={'code': 'KR5223941018'}) request.addfinalizer(partial(teardown, db=db, record=asset)) return asset
def import_8percent_data(parsed_data, account_checking, account_8p, asset_krw): from finance.models import Asset, AssetValue, Record, Transaction assert account_checking assert account_8p assert asset_krw asset_8p = Asset.create(name=parsed_data['name']) remaining_value = parsed_data['amount'] started_at = parsed_data['started_at'] with Transaction.create() as t: Record.create( created_at=started_at, transaction=t, account=account_checking, asset=asset_krw, quantity=-remaining_value) Record.create( created_at=started_at, transaction=t, account=account_8p, asset=asset_8p, quantity=1) AssetValue.create( evaluated_at=started_at, asset=asset_8p, target_asset=asset_krw, granularity='1day', close=remaining_value) for record in parsed_data['records']: date, principle, interest, tax, fees = record returned = principle + interest - (tax + fees) remaining_value -= principle with Transaction.create() as t: Record.create( created_at=date, transaction=t, account=account_checking, asset=asset_krw, quantity=returned) AssetValue.create( evaluated_at=date, asset=asset_8p, target_asset=asset_krw, granularity='1day', close=remaining_value)
def asset_stock_nvda(request, db): asset = Asset.create(type='stock', name='NVDA', code='NVDA', description='NVIDIA Corporation') request.addfinalizer(partial(teardown, db=db, record=asset)) return asset
def asset_stock_ncsoft(request, db): asset = Asset.create(type='stock', name='NCsoft Corporation', code='036570.KS', description='NCsoft Corporation') request.addfinalizer(partial(teardown, db=db, record=asset)) return asset
def insert_asset(row, data=None): """Parses a comma separated values to fill in an Asset object. (type, name, description) :param row: comma separated values """ from finance.models import Asset type, name, description = [x.strip() for x in row.split(',')] return Asset.create( type=type, name=name, description=description, data=data)
def insert_asset(row, data=None): """Parses a comma separated values to fill in an Asset object. (type, name, description) :param row: comma separated values """ from finance.models import Asset type, name, description = [x.strip() for x in row.split(',')] return Asset.create(type=type, name=name, description=description, data=data)
def import_8percent_data(parsed_data, account_checking, account_8p, asset_krw): from finance.models import Asset, AssetValue, Record, Transaction assert account_checking assert account_8p assert asset_krw asset_8p = Asset.create(name=parsed_data['name']) remaining_value = parsed_data['amount'] started_at = parsed_data['started_at'] with Transaction.create() as t: Record.create(created_at=started_at, transaction=t, account=account_checking, asset=asset_krw, quantity=-remaining_value) Record.create(created_at=started_at, transaction=t, account=account_8p, asset=asset_8p, quantity=1) AssetValue.create(evaluated_at=started_at, asset=asset_8p, target_asset=asset_krw, granularity='1day', close=remaining_value) for record in parsed_data['records']: date, principle, interest, tax, fees = record returned = principle + interest - (tax + fees) remaining_value -= principle with Transaction.create() as t: Record.create(created_at=date, transaction=t, account=account_checking, asset=asset_krw, quantity=returned) AssetValue.create(evaluated_at=date, asset=asset_8p, target_asset=asset_krw, granularity='1day', close=remaining_value)
def fetch_asset_values(code, start_time, end_time): try: asset = Asset.get_by_symbol(code) except AssetNotFoundException: log.info('Asset {0} does not exist. Creating an Asset record...', code) asset = Asset.create(name=code, code=code, type=AssetType.stock) rows = fetch_stock_values(code, start_time, end_time, Granularity.min) for date, open_, high, low, close_, volume, source in rows: insert_asset_value(asset, date, Granularity.min, open_, high, low, close_, volume, source) try: db.session.commit() except (IntegrityError, InvalidRequestError): log.exception('Something went wrong') db.session.rollback() else: log.info('Asset values for {0} have been imported', code)
def insert_stock_assets(): """NOTE: This is a temporary workaround. All stock informaion shall be fetched automatically on the fly. """ rows = [ ('036570.KS', 'NCsoft Corporation'), ('145210.KS', 'SAEHWA IMC'), ('069080.KQ', 'Webzen'), ('053800.KQ', 'Ahnlab Inc.'), ('017670.KS', 'SK Telecom Co. Ltd.'), ('005380.KS', 'Hyundai Motor Company'), ('056080.KQ', 'Yujin Robot Co., Ltd.'), ('069500.KS', 'KODEX 200'), ('009830.KS', '한화케미칼'), ] for code, description in rows: log.info('Inserting {} ({})...', code, description) yield Asset.create(type='stock', code=code, description=description, ignore_if_exists=True)
def insert_stock_assets(): """NOTE: This is a temporary workaround. All stock informaion shall be fetched automatically on the fly. """ rows = [ ('036570.KS', 'NCsoft Corporation'), ('145210.KS', 'SAEHWA IMC'), ('069080.KQ', 'Webzen'), ('053800.KQ', 'Ahnlab Inc.'), ('017670.KS', 'SK Telecom Co. Ltd.'), ('005380.KS', 'Hyundai Motor Company'), ('056080.KQ', 'Yujin Robot Co., Ltd.'), ('069500.KS', 'KODEX 200'), ('009830.KS', '한화케미칼'), ] for code, description in rows: log.info('Inserting {} ({})...', code, description) yield Asset.create(type='stock', code=code, description=description, ignore_if_exists=True)
def insert_stock_assets(): """NOTE: This is a temporary workaround. All stock informaion shall be fetched automatically on the fly. """ rows = [ ("036570.KS", "NCsoft Corporation"), ("145210.KS", "SAEHWA IMC"), ("069080.KQ", "Webzen"), ("053800.KQ", "Ahnlab Inc."), ("017670.KS", "SK Telecom Co. Ltd."), ("005380.KS", "Hyundai Motor Company"), ("056080.KQ", "Yujin Robot Co., Ltd."), ("069500.KS", "KODEX 200"), ("009830.KS", "한화케미칼"), ] for code, description in rows: log.info("Inserting {} ({})...", code, description) yield Asset.create(type="stock", code=code, description=description, ignore_if_exists=True)
def test_insert_stock_record(db, account_stock, account_checking): data = { 'date': parse_date('2016-06-30'), 'sequence': 1, 'category1': '장내매수', 'category2': '매수', 'code': '005380', 'name': '현대차', 'unit_price': 136000, 'quantity': 10, 'subtotal': 1360000, 'interest': 0, 'fees': 200, 'late_fees': 0, 'channel': '', 'final_amount': 1670200, } asset = Asset.create(type='stock', code='005380.KS', description='현대차') record = insert_stock_record(data, account_stock, account_checking) # TODO: Automate this process db.session.delete(record) db.session.delete(asset) db.session.commit()
def test_insert_stock_record(db, account_stock, account_checking): data = { "date": parse_date("2016-06-30"), "sequence": 1, "category1": "장내매수", "category2": "매수", "code": "005380", "name": "현대차", "unit_price": 136000, "quantity": 10, "subtotal": 1360000, "interest": 0, "fees": 200, "late_fees": 0, "channel": "", "final_amount": 1670200, } asset = Asset.create(type="stock", code="005380.KS", description="현대차") record = insert_stock_record(data, account_stock, account_checking) # TODO: Automate this process db.session.delete(record) db.session.delete(asset) db.session.commit()
def asset_sp500(request, db): asset = Asset.create( type='security', name='S&P 500', description='') request.addfinalizer(partial(teardown, db=db, record=asset)) return asset
def asset_krw(request, db): asset = Asset.create( type='currency', name='KRW', description='Korean Won') request.addfinalizer(partial(teardown, db=db, record=asset)) return asset
def asset_hf1(request, db): asset = Asset.create( type='bond', name='포트폴리오 투자상품 1호') request.addfinalizer(partial(teardown, db=db, record=asset)) return asset
def asset_usd(request, db): asset = Asset.create(type='currency', name='USD', description='United States Dollar') request.addfinalizer(partial(teardown, db=db, record=asset)) return asset
def create_asset(type_: AssetType, code: str, description: str): return Asset.create(type=type_, code=code, description=description, ignore_if_exists=True)
def create_asset(type_: AssetType, code: str, description: str): return Asset.create( type=type_, code=code, description=description, ignore_if_exists=True)