ใƒ•ใ‚กใ‚ฆใ‚นใƒˆใƒใƒƒใ‚ฏใ‚ฐใƒฉใ‚ฆใƒณใƒ‰ใ‚ฟใ‚นใ‚ฏใƒ‘ใƒผใƒˆII๏ผšใ‚จใƒผใ‚ธใ‚งใƒณใƒˆใจใƒใƒผใƒ 

็›ฎๆฌก

  1. ใƒ‘ใƒผใƒˆI๏ผšใฏใ˜ใ‚ใซ

  2. ใƒ‘ใƒผใƒˆII๏ผšใ‚จใƒผใ‚ธใ‚งใƒณใƒˆใจใƒใƒผใƒ 

ใ“ใ“ใงไฝ•ใ‚’ใ—ใฆใ„ใ‚‹ใฎ๏ผŸ

ใ ใ‹ใ‚‰ใ€2็•ช็›ฎใฎ้ƒจๅˆ†ใ€‚ไปฅๅ‰ใซๆ›ธใ„ใŸใ‚ˆใ†ใซใ€ใใฎไธญใงๆฌกใฎใ“ใจใ‚’่กŒใ„ใพใ™ใ€‚

  1. ๅฟ…่ฆใชใ‚จใƒณใƒ‰ใƒใ‚คใƒณใƒˆใฎใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’ไฝฟ็”จใ—ใฆใ€aiohttpใงalphavantage็”จใฎๅฐใ•ใชใ‚ฏใƒฉใ‚คใ‚ขใƒณใƒˆใ‚’ไฝœๆˆใ—ใพใ—ใ‚‡ใ†ใ€‚

  2. ่จผๅˆธใซ้–ขใ™ใ‚‹ใƒ‡ใƒผใ‚ฟใจใใฎใƒกใ‚ฟๆƒ…ๅ ฑใ‚’ๅŽ้›†ใ™ใ‚‹ใ‚จใƒผใ‚ธใ‚งใƒณใƒˆใ‚’ไฝœๆˆใ—ใพใ™ใ€‚

, , , faust , , kafka, ( click), - , .

AlphaVantage

, aiohttp alphavantage.

alphavantage.py

Spoiler
import urllib.parse as urlparse
from io import StringIO
from typing import Any, Dict, List, Union

import aiohttp
import pandas as pd
import stringcase
from loguru import logger

from horton.config import API_ENDPOINT


class AlphaVantageClient:
    def __init__(
        self,
        session: aiohttp.ClientSession,
        api_key: str,
        api_endpoint: str = API_ENDPOINT,
    ):
        self._query_params = {"datatype": "json", "apikey": api_key}
        self._api_endpoint = api_endpoint
        self._session = session

    @logger.catch
    def _format_fields(self, data: Dict[str, Any]) -> Dict[str, Any]:
        formatted_data = {}

        for field, item in data.items():
            formatted_data[stringcase.snakecase(field)] = item

        return formatted_data

    @logger.catch
    async def _construct_query(
        self, function: str, to_json: bool = True, **kwargs
    ) -> Union[Dict[str, Any], str]:
        path = "query/"

        async with self._session.get(
            urlparse.urljoin(self._api_endpoint, path),
            params={"function": function, **kwargs, **self._query_params},
        ) as response:
            data = (await response.json()) if to_json else (await response.text())

            if to_json:
                data = self._format_fields(data)

        return data

    @logger.catch
    async def get_securities(self, state: str = "active") -> List[Dict[str, str]]:
        data = await self._construct_query("LISTING_STATUS", state=state, to_json=False)

        data = pd.read_csv(StringIO(data))

        securities = data.to_dict("records")

        for index, security in enumerate(securities):
            security = self._format_fields(security)
            security["_type"] = "physical"

            securities[index] = security

        return securities

    @logger.catch
    async def get_security_overview(self, symbol: str) -> Dict[str, str]:
        return await self._construct_query("OVERVIEW", symbol=symbol)

    @logger.catch
    async def get_historical_data(self, symbol: str) -> Dict[str, Any]:
        return await self._construct_query(
            "TIME_SERIES_DAILY_ADJUSTED", symbol=symbol, outputsize="full"
        )

    @logger.catch
    async def get_last_price_data(self, symbol: str) -> Dict[str, Any]:
        return await self._construct_query("GLOBAL_QUOTE", symbol=symbol)

    @logger.catch
    async def get_indicator_data(
        self, symbol: str, indicator: str, **indicator_options
    ) -> Dict[str, Any]:
        return await self._construct_query(
            indicator, symbol=symbol, **indicator_options
        )

:

  1. API AlphaVantage , construct_query http .

  2. snake_case .

  3. logger.catch .

P.S. alphavantage config.yml, HORTON_SERVICE_APIKEY. .

CRUD-

securities .

database/security.py

- , .

get_app()

app.py

Spoiler
import faust

from horton.config import KAFKA_BROKERS


def get_app():
    return faust.App("horton", broker=KAFKA_BROKERS)

, , , , App-. settings , .

app = get_app()

collect_securities_topic = app.topic("collect_securities", internal=True)

@app.agent(collect_securities_topic)
async def collect_securities(stream: StreamT[None]) -> AsyncIterable[bool]:
	pass

, faust- - . , ... , , internal -.

  1. kafka, , . , , :)

  2. internal, faust, , , , faust, : retention, retention policy (- delete, compact), - (partitions, , , faust).

  3. , , , . , (, - retention policy) .

    :

app = get_app()

@app.agent()
async def collect_securities(stream: StreamT[None]) -> AsyncIterable[bool]:
	pass

, , :)

app = get_app()

collect_securities_topic = app.topic("collect_securities", internal=True)

@app.agent(collect_securities_topic)
async def collect_securities(stream: StreamT[None]) -> AsyncIterable[bool]:
    async with aiohttp.ClientSession() as session:
        async for _ in stream:
            logger.info("Start collect securities")

            client = AlphaVantageClient(session, API_KEY)

            securities = await client.get_securities()

            for security in securities:
                await SecurityCRUD.update_one(
                    {"symbol": security["symbol"], "exchange": security["exchange"]}, security, upsert=True
                )

            yield True

, aiohttp . , , , - , ( , concurrency ).

, ( _, , , ) , (offset), , . , , (get_securities - active, . ) , , , , () .

!

> docker-compose up -d
...   ...
> faust -A horton.agents worker --without-web -l info

P.S. - faust , .

faust', ( ) info. :

Spoiler
โ”Œฦ’aยตSโ€  v1.10.4โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ id          โ”‚ horton                                            โ”‚
โ”‚ transport   โ”‚ [URL('kafka://localhost:9092')]                   โ”‚
โ”‚ store       โ”‚ memory:                                           โ”‚
โ”‚ log         โ”‚ -stderr- (info)                                   โ”‚
โ”‚ pid         โ”‚ 1271262                                           โ”‚
โ”‚ hostname    โ”‚ host-name                                         โ”‚
โ”‚ platform    โ”‚ CPython 3.8.2 (Linux x86_64)                      โ”‚
โ”‚ drivers     โ”‚                                                   โ”‚
โ”‚   transport โ”‚ aiokafka=1.1.6                                    โ”‚
โ”‚   web       โ”‚ aiohttp=3.6.2                                     โ”‚
โ”‚ datadir     โ”‚ /path/to/project/horton-data                      โ”‚
โ”‚ appdir      โ”‚ /path/to/project/horton-data/v1                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
... , ,  ...

โ”ŒTopic Partition Setโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ topic                      โ”‚ partitions โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ collect_securities         โ”‚ {0-7}      โ”‚
โ”‚ horton-__assignor-__leader โ”‚ {0}        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 

!!!

partition set. , , , - (8, topic_partitions - ), ( partitions) . 8 , , .

, :

> faust -A horton.agents send @collect_securities
{"topic": "collect_securities", "partition": 6, "topic_partition": ["collect_securities", 6], "offset": 0, "timestamp": ..., "timestamp_type": 0}

P.S. @ , "collect_securities".

, 6 - , kafdrop localhost:9000

, , loguru:

2020-09-23 00:26:37.304 | INFO     | horton.agents:collect_securities:40 - Start collect securities

, mongo ( Robo3T Studio3T) , :

, , .

- :)

, !

, , 1/3 , , , .

, , :

collect_security_overview_topic = app.topic("collect_security_overview", internal=True)


@app.agent(collect_security_overview_topic)
async def collect_security_overview(
    stream: StreamT[?],
) -> AsyncIterable[bool]:
    async with aiohttp.ClientSession() as session:
        async for event in stream:
            ...

security, (symbol) . faust Records - , .

records.py , :

import faust


class CollectSecurityOverview(faust.Record):
    symbol: str
    exchange: str

, faust python, , - 3.6.

, :

collect_security_overview_topic = app.topic(
    "collect_security_overview", internal=True, value_type=CollectSecurityOverview
)


@app.agent(collect_security_overview_topic)
async def collect_security_overview(
    stream: StreamT[CollectSecurityOverview],
) -> AsyncIterable[bool]:
    async with aiohttp.ClientSession() as session:
        async for event in stream:
            logger.info(
                "Start collect security [{symbol}] overview", symbol=event.symbol
            )

            client = AlphaVantageClient(session, API_KEY)

            security_overview = await client.get_security_overview(event.symbol)

            await SecurityCRUD.update_one({"symbol": event.symbol, "exchange": event.exchange}, security_overview)

            yield True

, - value_type. , , - .

, - collect_securitites :

....
for security in securities:
    await SecurityCRUD.update_one({
            "symbol": security["symbol"],
            "exchange": security["exchange"]
        },
        security,
        upsert = True,
    )

    await collect_security_overview.cast(
        CollectSecurityOverview(symbol = security["symbol"], exchange = security["exchange"])
    )
....

. , .cast, , , :

  1. cast - , . .

  2. send - , . .

  3. ask - . .

, !

, - . , faust - click. faust -A

agents.py app.command, cast collect_securitites:

@app.command()
async def start_collect_securities():
    """Collect securities and overview."""

    await collect_securities.cast()

, , :

> faust -A horton.agents --help

....
Commands:
  agents                    List agents.
  clean-versions            Delete old version directories.
  completion                Output shell completion to be evaluated by the...
  livecheck                 Manage LiveCheck instances.
  model                     Show model detail.
  models                    List all available models as a tabulated list.
  reset                     Delete local table state.
  send                      Send message to agent/topic.
  start-collect-securities  Collect securities and overview.
  tables                    List available tables.
  worker                    Start worker instance for given app.

, , faust :

> faust -A horton.agents start-collect-securities

?

, , , sink cron- .

! :)

ใ“ใฎ้ƒจๅˆ†ใฎใ‚ณใƒผใƒ‰

PSๆœ€ๅพŒใฎ้ƒจๅˆ†ใงใ€็งใฏใƒ•ใ‚กใ‚ฆใ‚นใƒˆใจใ‚ณใƒณใƒ•ใƒซใ‚จใƒณใƒˆใชใ‚ซใƒ•ใ‚ซ๏ผˆใ‚ณใƒณใƒ•ใƒซใ‚จใƒณใƒˆใชๆฉŸ่ƒฝใ‚’ๅ‚™ใˆใฆใ„ใพใ™๏ผ‰ใซใคใ„ใฆๅฐ‹ใญใ‚‰ใ‚Œใพใ—ใŸใ€‚ใ‚ณใƒณใƒ•ใƒซใ‚จใƒณใƒˆใฎๆ–นใŒใฏใ‚‹ใ‹ใซๆฉŸ่ƒฝ็š„ใงใ‚ใ‚‹ใ‚ˆใ†ใซ่ฆ‹ใˆใพใ™ใŒใ€่ฆ็‚นใฏใ€ใƒ•ใ‚กใ‚ฆใ‚นใƒˆใŒใ‚ณใƒณใƒ•ใƒซใ‚จใƒณใƒˆใ‚’ๅฎŒๅ…จใซใ‚ฏใƒฉใ‚คใ‚ขใƒณใƒˆใงใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใชใ„ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใฏใ€ใƒ‰ใƒƒใ‚ฏใงใฎใ‚ฏใƒฉใ‚คใ‚ขใƒณใƒˆๅˆถ้™ใฎ่ชฌๆ˜ŽใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚




All Articles