-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
261 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ OKX=ETH,BTC,LINK,FTM | |
BYBIT=ETH,BTC,LINK | ||
BITSTAMP=BTC,ETH,LINK | ||
MEXC=BTC,ETH,LINK | ||
HTX=BTC,ETH,LINK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
ARG PYTHON_VERSION=3.11.3-slim-bullseye | ||
|
||
FROM python:${PYTHON_VERSION} as python | ||
|
||
ENV PYTHONUNBUFFERED=1 \ | ||
PYTHONDONTWRITEBYTECODE=1 \ | ||
PIP_NO_CACHE_DIR=off \ | ||
PIP_DISABLE_PIP_VERSION_CHECK=on \ | ||
PIP_DEFAULT_TIMEOUT=100 \ | ||
POETRY_HOME="/opt/poetry" \ | ||
POETRY_VIRTUALENVS_IN_PROJECT=true \ | ||
POETRY_NO_INTERACTION=1 \ | ||
PYSETUP_PATH="/app" \ | ||
VENV_PATH="/app/.venv" | ||
|
||
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" | ||
|
||
|
||
FROM python as python-build-stage | ||
|
||
RUN apt-get update && apt-get install --no-install-recommends -y \ | ||
curl \ | ||
build-essential | ||
|
||
ENV POETRY_VERSION=1.7.1 | ||
RUN curl -sSL https://install.python-poetry.org | python | ||
|
||
WORKDIR $PYSETUP_PATH | ||
|
||
COPY ./poetry.lock ./pyproject.toml ./ | ||
RUN poetry install --only main --no-root | ||
|
||
|
||
FROM python as python-run-stage | ||
|
||
COPY --from=python-build-stage $POETRY_HOME $POETRY_HOME | ||
COPY --from=python-build-stage $PYSETUP_PATH $PYSETUP_PATH | ||
|
||
COPY ./compose/producer/entrypoint /entrypoint | ||
RUN sed -i 's/\r$//g' /entrypoint | ||
RUN chmod +x /entrypoint | ||
|
||
COPY ./compose/producer/htx/start /start | ||
RUN sed -i 's/\r$//g' /start | ||
RUN chmod +x /start | ||
|
||
WORKDIR $PYSETUP_PATH | ||
|
||
COPY . . | ||
|
||
RUN poetry install | ||
|
||
ENTRYPOINT ["/entrypoint"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
set -o errexit | ||
set -o pipefail | ||
set -o nounset | ||
|
||
exec poetry run producer ${HTX_COINS} --exchange htx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from datetime import datetime | ||
from functools import cached_property | ||
|
||
from pydantic import Field, computed_field, condecimal, field_validator | ||
|
||
from exchange_radar.producer.serializers.base import BaseSerializer | ||
|
||
|
||
class HtxTradeSerializer(BaseSerializer): | ||
symbol: str = Field(alias="channel") | ||
price: condecimal(ge=0, decimal_places=8) | ||
quantity: condecimal(ge=0, decimal_places=8) = Field(alias="amount") | ||
trade_time: datetime = Field(alias="ts") | ||
direction: str = Field(exclude=True) | ||
|
||
def __init__(self, *args, **kwargs): | ||
kwargs["price"] = str(kwargs["price"]) | ||
kwargs["amount"] = str(kwargs["amount"]) | ||
super().__init__(*args, **kwargs) | ||
|
||
@field_validator("symbol") # noqa | ||
@classmethod | ||
def symbol_normalization(cls, v) -> str: | ||
return "".join(v.split(".")[1]).upper() | ||
|
||
@computed_field | ||
@cached_property | ||
def is_seller(self) -> bool: | ||
return self.direction == "sell" | ||
|
||
@computed_field | ||
def exchange(self) -> str: | ||
return "HTX" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import asyncio | ||
import gzip | ||
import json | ||
import logging | ||
|
||
import websockets | ||
|
||
from exchange_radar.producer.publisher import publish | ||
from exchange_radar.producer.serializers.htx import HtxTradeSerializer | ||
from exchange_radar.producer.tasks.base import Task | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
ITER_SLEEP = 10.0 | ||
|
||
|
||
class HtxTradesTask(Task): | ||
async def process(self, symbol_or_symbols: str | tuple): | ||
uri = "wss://api.huobi.pro/ws" | ||
message = {"sub": f"market.{symbol_or_symbols.lower()}.trade.detail"} | ||
|
||
while True: | ||
try: | ||
async with websockets.connect(uri) as ws: | ||
await ws.send(json.dumps(message)) | ||
while True: | ||
try: | ||
response = json.loads(gzip.decompress(await ws.recv()).decode("utf-8")) | ||
if "ping" in response: | ||
await ws.send(json.dumps({"pong": response["ping"]})) | ||
continue | ||
for msg in response["tick"]["data"]: | ||
msg["channel"] = response["ch"] | ||
data = HtxTradeSerializer(**msg) | ||
publish(data) | ||
except Exception as error: | ||
logger.error(f"ERROR: {error}") | ||
except Exception as error2: | ||
logger.error(f"GENERAL ERROR: {error2}") | ||
finally: | ||
logger.error(f"Trying again in {ITER_SLEEP} seconds...") | ||
await asyncio.sleep(ITER_SLEEP) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.