Skip to main content

Test Coverage

python-compton

An abstract data-flow framework for quantitative trading, which decouples data initialization, data composition and data processing.

Install

pip install compton

Usage

from compton import (
  Orchestrator,
  Provider,
  Reducer,
  Consumer
)

Vector

We call a tuple of hashable parameters as a vector which is used to identify a certain kind of data.

from enum import Enum

class DataType(Enum):
    KLINE = 1
    ORDER_BOOK = 2

class TimeSpan(Enum):
    DAY = 1
    WEEK = 2

vector = (DataType.KLINE, TimeSpan.DAY)

Orchestrator(reducers, loop=None)

  • reducers List[Reducer] reducers to compose data
  • loop? Optional[EventLoop] The event loop object to use. In most cases, you should NOT pass this argument, unless you exact know what you are doing.
Orchestrator(
    reducers
).connect(
    provider
).subscribe(
    consumer
).add('TSLA')

orchestrator.connect(provider: Provider) -> self

Connects to a data provider

orchestrator.subscribe(consumer: Consumer) -> self

Subscribes the consumer to orchestrator.

orchestrator.add(symbol: str) -> self

Adds a new symbol to orchestrator, and start the data flow for symbol

Provider

Provider is an abstract class which provides initial data and data updates.

A provider should be implemented to support many symbols

We must inherit class Provider and implement some abstract method before use.

  • @property vector returns an Vector
  • async def init() method returns the initial data
  • There is an protected method self.dispatch(symbol, payload) to set the payload updated, which should only be called in a coroutine, or a RuntimeError is raised.
class MyProvider(Provider):
    @property
    def vector(self) -> Vector:
        return (DataType.KLINE, TimeSpan.DAY)

    # @abstractmethod
    async def init(self, symbol):
        return {}

    # Optional
    def when_update(self, dispatch: Dispatcher):
        # Then save the `dispatch` method for further data updating
        # by default, it saves as `self.dispatch`

Reducer

Another abstract class which handles data composition.

The reducer.vector could be a generic vector which applies partial match to other vectors

class MyReducer(Reducer):
    @property
    def vector(self):
        # So, MyReducer support both
        # - (DataType.KLINE, TimeSpan.DAY)
        # - and (DataType.KLINE, TimeSpan.WEEK)
        return (DataType.KLINE,)

    def merge(self, previous, payload):
        # `previous` might be `None`, if `payload` is the initial data
        if previous is None:
            # We could clean the initial data
            return clean(payload)

        return {**previous, **payload}

Consumer

A consumer could subscribes to more than one kind of data types

class MyConsumer(Consumer):
    @property
    def vectors(self):
        # Subscribe to two kinds of data types
        return [
            (DataType.KLINE, TimeSpan.DAY),
            (DataType.KLINE, TimeSpan.WEEK)
        ]

    @property
    def all(self) -> bool:
        """
        `True` indicates that the consumer will only go processing
        if both of the data corresponds with the two vectors have changes

        And by default, `Consumer::all` is False
        """
        return True

    @property
    def concurrency(self) -> int:
        """
        Concurrency limit for method `process()`

        By default, `Consumer::concurrency` is `0` which means no limit
        """
        return 1

    def should_process(self, *payloads) -> bool:
        """
        If this method returns `False`, then the data update will not be processed
        """
        return True

    # Then there will be
    # both `kline_day` and `kline_week` passed into method `process`
    async def process(self, symbol, kline_day, kline_week):
        await doSomething(symbol, kline_day, kline_week)

License

MIT

Metadata

Release files for compton 5.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for compton 5.0.1
File Size Uploaded Provenance
compton-5.0.1.tar.gz 13.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for compton 5.0.1
File Interpreter ABI Platform
compton-5.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / compton-5.0.1.tar.gz

Download URL compton-5.0.1.tar.gz
Size 13.1 kB
Tags Source
SHA-256 checksum
How to use checksums
68d430d31ae86a3a20829382e1149f004d2ce1e73a94e4a4cb06c56688c91ddc
BLAKE2b-256 checksum
How to use checksums
2956afceeeb95b01d0a15ddffd5a13612fddd44c3be4ae7ad14fafb841602fa8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / compton-5.0.1-py3-none-any.whl

Download URL compton-5.0.1-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d33ebf367cbbc29ac692bf203e93a15099473b53f1f0fbda6884bb46c235b648
BLAKE2b-256 checksum
How to use checksums
5ae3c601087c740e7e2d1b846311f7ffbf7f47292d95a80d73866a2bbb409a7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for compton 5.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

5.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page