An abstract data flow framework for quantitative trading
Project description
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
EventLoopThe event loop object to use
The following code shows how to use compton in a non-coroutine environmennt
loop = asyncio.new_event_loop()
orchestrator = Orchestrator(
Reducers,
loop
)
orchestrator.add('US.TSLA')
loop.run_forever()
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 vectorreturns anVectorasync def init()method returns the initial datadef when_update(dispatch)registers the dispatcher which is a callable.
class MyProvider(Provider):
@property
def vector(self):
return (DataType.KLINE, TimeSpan.DAY)
def when_update(self, dispatch):
pass
async def init(self, symbol):
return {}
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, old, new):
# `old` might be `None`, if `new` is the initial data
if old is None:
# We could clean the initial data
return clean(new)
return {**old, **new}
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):
# `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):
# concurrency limit for method `process()`
# By default, `Consumer::concurrency` is `0` which means no limit
return 1
# 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file compton-3.0.0.tar.gz.
File metadata
- Download URL: compton-3.0.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a05ba9661d4489aabda2d8923ec6518c0a673e3193e57ba9d50c529c932839b
|
|
| MD5 |
bef18d508187061db138a37ff762057b
|
|
| BLAKE2b-256 |
8792de36ce6af41f1ceedfd17795f3b8ff2aab5cfb68702e08a04c9b15a25890
|
File details
Details for the file compton-3.0.0-py3-none-any.whl.
File metadata
- Download URL: compton-3.0.0-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e0f98677a99bf2b1e1aa45b659b1b62ba4b5659ef180ba201ceaedbd1c67f03
|
|
| MD5 |
33269ccd66bf51f5876d4606c18cd73e
|
|
| BLAKE2b-256 |
d80a7967fcd0874e069a3b49ca2c84d07cba85e687bfdc114f5cfbb6233c308b
|