Exemple #1
0
def import_fund(code, from_date, to_date):
    """Imports fund data from KOFIA.

    :param code: e.g., KR5223941018
    :param from_date: e.g., 2016-01-01
    :param to_date: e.g., 2016-02-28
    """
    provider = Kofia()

    app = create_app(__name__)
    with app.app_context():
        asset = get_asset_by_fund_code(code)

        # FIXME: Target asset should also be determined by asset.data.code
        base_asset = Asset.query.filter_by(name="KRW").first()

        data = provider.fetch_data(code, parse_date(from_date),
                                   parse_date(to_date))
        for date, unit_price, quantity in data:
            log.info("Import data on {}", date)
            unit_price /= 1000.0
            try:
                AssetValue.create(
                    asset=asset,
                    base_asset=base_asset,
                    evaluated_at=date,
                    close=unit_price,
                    granularity=Granularity.day,
                    source="kofia",
                )
            except IntegrityError:
                log.warn("Identical record has been found for {}. Skipping.",
                         date)
                db.session.rollback()
Exemple #2
0
def insert_test_data():
    """Inserts some sample data for testing."""
    app = create_app(__name__)
    with app.app_context():
        user = User.create(
            family_name="Byeon",
            given_name="Sumin",
            email="*****@*****.**",
            ignore_if_exists=True,
        )

        account_checking = create_account(AccountType.checking, "Shinhan",
                                          "checking", user)
        account_stock = create_account(AccountType.investment, "Mirae Asset",
                                       "stock", user)

        asset_krw = create_asset(AssetType.currency, "KRW", "Korean Won")
        create_asset(AssetType.currency, "USD", "United States Dollar")

        for _ in insert_stock_assets():
            pass

        create_asset(AssetType.security, "KR5223941018", "KB S&P500")
        create_asset(AssetType.security, "KR5229221225", "이스트스프링차이나")

        portfolio = Portfolio()
        portfolio.base_asset = asset_krw
        portfolio.add_accounts(account_checking, account_stock)
Exemple #3
0
def import_stock_records(filename):
    """Parses exported data from the Shinhan HTS."""
    app = create_app(__name__)
    with app.app_context():
        account_bank = Account.query.filter(Account.name == "신한 입출금").first()
        account_stock = Account.query.filter(Account.name == "신한 주식").first()
        with open(filename) as fin:
            for parsed in parse_stock_records(fin):
                insert_stock_record(parsed, account_stock, account_bank)
Exemple #4
0
def import_sp500_records():
    """Import S&P500 fund sample data. Expects a tab seprated value document.
    """
    app = create_app(__name__)
    app.app_context().push()

    account_checking = Account.get(id=1001)
    account_sp500 = Account.get(id=7001)
    asset_krw = Asset.query.filter_by(name="KRW").first()
    asset_sp500 = Asset.query.filter_by(name="KB S&P500").first()

    # Expected number of columns
    expected_col_count = 6

    with open("sample-data/sp500.csv") as fin:
        # Skip the first row (headers)
        headers = next(fin)
        col_count = len(headers.split())
        if col_count != expected_col_count:
            raise Exception("Expected number of columns = {}, "
                            "actual number of columns = {}".format(
                                expected_col_count, col_count))

        for line in fin:
            cols = line.split("\t")
            if len(cols) != expected_col_count:
                continue
            date = parse_date(cols[0], "%Y.%m.%d")
            _type = cols[1]
            quantity_krw, quantity_sp500 = [
                int(extract_numbers(v)) for v in cols[3:5]
            ]

            log.info(", ".join([c.strip() for c in cols]))

            if not (_type == "일반입금" or _type == "일반신규"):
                log.info("Record type '{}' will be ignored", _type)
                continue

            with Transaction.create() as t:
                # NOTE: The actual deposit date and the buying date generally
                # differ by a few days. Need to figure out how to parse this
                # properly from the raw data.
                try:
                    deposit(account_checking, asset_krw, -quantity_krw, date,
                            t)
                except IntegrityError:
                    log.warn("Identical record exists")
                    db.session.rollback()

                try:
                    deposit(account_sp500, asset_sp500, quantity_sp500, date,
                            t)
                except IntegrityError:
                    log.warn("Identical record exists")
                    db.session.rollback()
Exemple #5
0
def import_stock_values(code):
    """Import stock price information."""
    app = create_app(__name__)
    with app.app_context():
        # NOTE: We assume all Asset records are already in the database, but
        # this is a temporary workaround. We should implement some mechanism to
        # automatically insert an Asset record when it is not found.

        stdin = click.get_text_stream("stdin")
        for _ in import_stock_values_(stdin, code):
            pass
Exemple #6
0
def app(request):
    """Session-wide test `Flask` application."""
    settings_override = {
        "TESTING": True,
    }
    settings_override["SQLALCHEMY_DATABASE_URI"] = os.environ["TEST_DB_URL"]
    app = create_app(__name__, config=settings_override)

    # Establish an application context before running the tests.
    # ctx = app.app_context()
    # ctx.push()

    # def teardown():
    #     ctx.pop()

    # request.addfinalizer(teardown)
    return app
Exemple #7
0
def import_dart(fin):
    """Import DART (전자공시) data."""

    try:
        data = json.loads(fin.read())
    except json.decoder.JSONDecodeError as e:
        log.error("Valid JSON data expected: {}", e)

    app = create_app(__name__)
    with app.app_context():
        for row in data:
            try:
                report = DartReport.create(**row)
            except IntegrityError:
                log.info("DartReport-{} already exists", row["id"])
                db.session.rollback()
            else:
                log.info("Fetched report: {}", report)
Exemple #8
0
def drop_all():
    """Drops all database tables."""
    app = create_app(__name__)
    with app.app_context():
        db.drop_all()
Exemple #9
0
def create_all():
    """Creates necessary database tables."""
    app = create_app(__name__)
    with app.app_context():
        db.create_all()
Exemple #10
0
import os

from finance.web import create_app

if __name__ == "__main__":
    application = create_app()
    host = os.environ.get("HOST", "0.0.0.0")
    port = int(os.environ.get("PORT", 8002))
    debug = bool(os.environ.get("DEBUG", False))

    application.run(host=host, port=port, debug=debug)