-
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
23 changed files
with
321 additions
and
101 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
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/mexc/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 ${MEXC_COINS} --exchange mexc |
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,23 @@ | ||
from datetime import datetime | ||
from functools import cached_property | ||
|
||
from pydantic import Field, computed_field, condecimal | ||
|
||
from exchange_radar.producer.serializers.base import BaseSerializer | ||
|
||
|
||
class MexcTradeSerializer(BaseSerializer): | ||
symbol: str = Field(alias="s") | ||
price: condecimal(ge=0, decimal_places=8) = Field(alias="p") | ||
quantity: condecimal(ge=0, decimal_places=8) = Field(alias="v") | ||
trade_time: datetime = Field(alias="t") | ||
side: int = Field(alias="S", exclude=True) | ||
|
||
@computed_field | ||
def exchange(self) -> str: | ||
return "MEXC" | ||
|
||
@computed_field | ||
@cached_property | ||
def is_seller(self) -> bool: | ||
return True if self.side == 2 else False |
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,31 @@ | ||
import asyncio | ||
import logging | ||
|
||
from pymexc import spot | ||
|
||
from exchange_radar.producer.publisher import publish | ||
from exchange_radar.producer.serializers.mexc import MexcTradeSerializer | ||
from exchange_radar.producer.tasks.base import Task | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
ITER_SLEEP = 10.0 | ||
|
||
|
||
class MexcTradesTask(Task): | ||
async def process(self, symbol_or_symbols: str | tuple): | ||
def callback(message): | ||
try: | ||
for msg in message["d"]["deals"]: | ||
msg.update({"s": message["s"]}) | ||
data = MexcTradeSerializer(**msg) | ||
publish(data) | ||
except Exception as error: | ||
logger.error(f"ERROR: {error}") | ||
|
||
ws = spot.WebSocket() | ||
ws.deals_stream(callback, symbol_or_symbols) | ||
|
||
while True: | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
from exchange_radar.producer.serializers.coinbase import CoinbaseTradeSerializer | ||
from exchange_radar.producer.serializers.kraken import KrakenTradeSerializer | ||
from exchange_radar.producer.serializers.kucoin import KucoinTradeSerializer | ||
from exchange_radar.producer.serializers.mexc import MexcTradeSerializer | ||
from exchange_radar.producer.serializers.okx import OkxTradeSerializer | ||
|
||
|
||
|
@@ -310,3 +311,41 @@ def test_serializer_bitstamp(mock_redis): | |
"exchange": "Bitstamp", | ||
"is_seller": False, | ||
} | ||
|
||
|
||
@patch("exchange_radar.producer.models.redis") | ||
def test_serializer_mexc(mock_redis): | ||
mock_redis.hincrbyfloat.return_value = 3.7335 | ||
mock_redis.pipeline().__enter__().execute = MagicMock(return_value=[1.0, 100.0]) | ||
|
||
msg = { | ||
"c": "[email protected]@BTCUSDT", | ||
"d": { | ||
"deals": [{"p": "43469.99", "v": "0.002153", "S": 1, "t": 1706615234824}], | ||
"e": "[email protected]", | ||
}, | ||
"s": "BTCUSDT", | ||
"t": 1706615234826, | ||
} | ||
|
||
_msg = msg["d"]["deals"][0] | ||
_msg.update({"s": msg["s"]}) | ||
payload = MexcTradeSerializer(**_msg) | ||
|
||
assert payload.model_dump() == { | ||
"symbol": "BTCUSDT", | ||
"price": Decimal("43469.99"), | ||
"quantity": Decimal("0.002153"), | ||
"trade_time": datetime.datetime(2024, 1, 30, 11, 47, 14), | ||
"total": Decimal("93.59088847"), | ||
"currency": "USDT", | ||
"trade_symbol": "BTC", | ||
"volume": 3.7335, | ||
"volume_trades": (1.0, 100.0), | ||
"number_trades": (1, 100), | ||
"trade_time_ts": 1706615234, | ||
"message": "2024-01-30 11:47:14 | <span class='mexc'>MEXC </span> | 43469.99000000 USDT |" | ||
" 0.00215300 BTC | 93.59088847 USDT", | ||
"exchange": "MEXC", | ||
"is_seller": False, | ||
} |
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
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.