Exemplo n.º 1
0
    def get_by_isin(cls, isin):
        """Gets an asset by ISIN

        :param isin: International Securities Identification Numbers
        """
        asset = cls.query.filter(cls.isin == isin).first()
        if asset is None:
            raise AssetNotFoundException(isin)
        else:
            return asset
Exemplo n.º 2
0
    def get_by_symbol(cls, symbol):
        """Gets an asset by symbol (e.g., AMZN, NVDA)

        NOTE: We may need to rename this method, when we find a more suitable
        name (rather than 'symbol').
        """
        asset = cls.query.filter(cls.code == symbol).first()
        if asset is None:
            raise AssetNotFoundException(symbol)
        else:
            return asset
Exemplo n.º 3
0
def get_asset_by_fund_code(code: str):
    """Gets an Asset instance mapped to the given fund code.

    :param code: A fund code
    """
    # NOTE: I know this looks really stupid, but we'll stick with this
    # temporary workaround until we figure out how to create an instance of
    # Asset model from a raw query result
    # (sqlalchemy.engine.result.RowProxy)
    query = "SELECT * FROM asset WHERE data->>'code' = :code LIMIT 1"
    raw_asset = db.session.execute(query, {'code': code}).first()
    if raw_asset is None:
        raise AssetNotFoundException(
            'Fund code {} is not mapped to any asset'.format(code))
    asset_id = raw_asset[0]
    return Asset.query.get(asset_id)
Exemplo n.º 4
0
def import_fund(code, from_date, to_date):
    """Imports fund data from KOFIA.

    :param from_date: e.g., 20160101
    :param to_date: e.g., 20160228
    """
    url = 'http://dis.kofia.or.kr/proframeWeb/XMLSERVICES/'
    headers = {
        'Origin':
        'http://dis.kofia.or.kr',
        'Accept-Encoding':
        'gzip, deflate',
        'Accept-Language':
        'en-US,en;q=0.8,ko;q=0.6',
        'User-Agent':
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) '
        'AppleWebKit/537.36 (KHTML, like Gecko) '
        'Chrome/48.0.2564.109 Safari/537.36',
        'Content-Type':
        'text/xml',
        'Accept':
        'text/xml',
        'Referer':
        'http://dis.kofia.or.kr/websquare/popup.html?w2xPath='
        '/wq/com/popup/DISComFundSmryInfo.xml&companyCd=20090602&'
        'standardCd=KR5223941018&standardDt=20160219&grntGb=S&'
        'search=&check=1&isMain=undefined&companyGb=A&uFundNm='
        '/v8ASwBCwqTQwLv4rW0AUwAmAFAANQAwADDHeLNxwqTJna2Mx5DSLMeQwu'
        'DQwQBbyPzC3QAt0wzA%0A3dYVAF0AQwAtAEU%3D&popupID=undefined&'
        'w2xHome=/wq/fundann/&w2xDocumentRoot=',
    }
    data = """<?xml version="1.0" encoding="utf-8"?>
        <message>
            <proframeHeader>
                <pfmAppName>FS-COM</pfmAppName>
                <pfmSvcName>COMFundPriceModSO</pfmSvcName>
                <pfmFnName>priceModSrch</pfmFnName>
            </proframeHeader>
            <systemHeader></systemHeader>
            <COMFundUnityInfoInputDTO>
                <standardCd>{code}</standardCd>
                <companyCd>A01031</companyCd>
                <vSrchTrmFrom>{from_date}</vSrchTrmFrom>
                <vSrchTrmTo>{to_date}</vSrchTrmTo>
                <vSrchStd>1</vSrchStd>
            </COMFundUnityInfoInputDTO>
        </message>
    """.format(code=code, from_date=from_date, to_date=to_date)
    resp = requests.post(url, headers=headers, data=data)

    app = create_app(__name__)
    with app.app_context():
        # NOTE: I know this looks really stupid, but we'll stick with this
        # temporary workaround until we figure out how to create an instance of
        # Asset model from a raw query result
        # (sqlalchemy.engine.result.RowProxy)
        query = "SELECT * FROM asset WHERE data->>'code' = :code LIMIT 1"
        raw_asset = db.session.execute(query, {'code': code}).first()
        if not raw_asset:
            raise AssetNotFoundException(
                'Fund code {} is not mapped to any asset'.format(code))
        asset_id = raw_asset[0]
        asset = Asset.query.get(asset_id)

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

        schema = AssetValueSchema()
        schema.load(resp.text)
        for date, unit_price, original_quantity in schema.get_data():
            log.info('Import data on {}', date)
            unit_price /= 1000.0
            try:
                AssetValue.create(asset=asset,
                                  target_asset=target_asset,
                                  evaluated_at=date,
                                  close=unit_price,
                                  granularity='1day')
            except IntegrityError:
                log.warn('Identical record has been found for {}. Skipping.',
                         date)
                db.session.rollback()