Skip to content

Commit

Permalink
Fix "Futures re-subscribe to authenticated feed doesn't work" (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger authored Dec 18, 2023
1 parent 2e430bb commit 24b3e60
Show file tree
Hide file tree
Showing 17 changed files with 298 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/_codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
run: python -m pip install --upgrade pip

- name: Install package
run: python -m pip install ".[dev]"
run: python -m pip install ".[test]"

- name: Generate coverage report
env:
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/_test_futures_private.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ jobs:
with:
python-version: ${{ inputs.python-version }}

- name: Install dependencies
- name: Update Pip
run: |
python -m pip install --upgrade pip
python -m pip install pytest
- name: Install package
run: python -m pip install .
run: python -m pip install ".[test]"

## Unit tests of the private Futures REST clients and endpoints
##
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/_test_futures_public.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ jobs:
with:
python-version: ${{ inputs.python-version }}

- name: Install dependencies
- name: Update Pip
run: |
python -m pip install --upgrade pip
python -m pip install pytest
- name: Install package
run: python -m pip install .
run: python -m pip install ".[test]"

## Unit tests of the public Futures REST clients and endpoints
##
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/_test_spot_private.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ jobs:
with:
python-version: ${{ inputs.python-version }}

- name: Install dependencies
- name: Update Pip
run: |
python -m pip install --upgrade pip
python -m pip install pytest
- name: Install package
run: python -m pip install .
run: python -m pip install ".[test]"

## Unit tests of private Spot REST clients and endpoints
##
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/_test_spot_public.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ jobs:
with:
python-version: ${{ inputs.python-version }}

- name: Install dependencies
- name: Update Pip
run: |
python -m pip install --upgrade pip
python -m pip install pytest
- name: Install package
run: python -m pip install .
run: python -m pip install ".[test]"

## Unit tests of the public Spot REST clients and endpoints
##
Expand Down
73 changes: 37 additions & 36 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,43 @@
#

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
hooks:
- id: ruff
args:
- --preview
- --fix
- --exit-non-zero-on-fix
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
args:
- --select=E9,F63,F7,F82
- --show-source
- --statistics
- repo: https://github.com/pycqa/pylint
rev: v3.0.1
hooks:
- id: pylint
name: pylint
types: [python]
exclude: ^examples/|^tests/|^setup.py$
args:
- --rcfile=pyproject.toml
- -d=R0801 # ignore duplicate code
- -j=4
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
hooks:
- id: mypy
name: mypy
additional_dependencies: ["types-requests"]
pass_filenames: false
args:
- --config-file=pyproject.toml
- kraken
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand Down Expand Up @@ -51,39 +88,3 @@ repos:
- id: isort
args:
- --profile=black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
hooks:
- id: ruff
args:
- --preview
- --fix
- --exit-non-zero-on-fix
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
args:
- --select=E9,F63,F7,F82
- --show-source
- --statistics
- repo: https://github.com/pycqa/pylint
rev: v3.0.1
hooks:
- id: pylint
name: pylint
types: [python]
exclude: ^examples/|^tests/|^setup.py$
args:
- --rcfile=pyproject.toml
- -d=R0801 # ignore duplicate code
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
hooks:
- id: mypy
name: mypy
additional_dependencies: ["types-requests"]
pass_filenames: false
args:
- --config-file=pyproject.toml
- kraken
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Changelog

## [Unreleased](https://github.com/btschwertfeger/python-kraken-sdk/tree/HEAD)
## [v2.1.0](https://github.com/btschwertfeger/python-kraken-sdk/tree/v2.1.0) (2023-12-07)

[Full Changelog](https://github.com/btschwertfeger/python-kraken-sdk/compare/v2.0.0...HEAD)
[Full Changelog](https://github.com/btschwertfeger/python-kraken-sdk/compare/v2.0.0...v2.1.0)

**Implemented enhancements:**

- Add `start`, `end`, and `cursor` parameters to `kraken.spot.Funding.get_recent_withdraw_status` [\#176](https://github.com/btschwertfeger/python-kraken-sdk/issues/176)
- Add `withdraw_methods` and `withdraw_addresses` to `kraken.spot.Funding` [\#174](https://github.com/btschwertfeger/python-kraken-sdk/issues/174)
- Add `start`, `end`, and `cursor` parameters to `kraken.spot.Funding.get_recent_withdraw_status` [\#177](https://github.com/btschwertfeger/python-kraken-sdk/pull/177) ([btschwertfeger](https://github.com/btschwertfeger))
- Resolve "Add `start`, `end`, and `cursor` parameters to `kraken.spot.Funding.get_recent_withdraw_status`" [\#177](https://github.com/btschwertfeger/python-kraken-sdk/pull/177) ([btschwertfeger](https://github.com/btschwertfeger))
- Resolve "Add `withdraw_methods` and `withdraw_addresses` to `kraken.spot.Funding`" [\#175](https://github.com/btschwertfeger/python-kraken-sdk/pull/175) ([btschwertfeger](https://github.com/btschwertfeger))

## [v2.0.0](https://github.com/btschwertfeger/python-kraken-sdk/tree/v2.0.0) (2023-10-22)
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# GitHub: https://github.com/btschwertfeger

PYTHON := python
PYTHON := venv/bin/python
PYTEST := $(PYTHON) -m pytest
PYTEST_OPTS := -vv --junit-xml=pytest.xml
PYTEST_COV_OPTS := $(PYTEST_OPTS) --cov --cov-report=xml:coverage.xml --cov-report=term
Expand Down Expand Up @@ -40,7 +40,7 @@ install:
##
.PHONY: dev
dev:
$(PYTHON) -m pip install -e ".[dev]"
$(PYTHON) -m pip install -e ".[dev,test]"

## ======= T E S T I N G =======
## test Run the unit tests
Expand All @@ -53,10 +53,10 @@ test:
.PHONY: tests
tests: test

## test-wip Run tests marked as 'wip'
## wip Run tests marked as 'wip'
##
.PHONY: test-wip
test-wip:
.PHONY: wip
wip:
@rm *.log || true
$(PYTEST) -m "wip" -vv $(TEST_DIR)

Expand Down
15 changes: 6 additions & 9 deletions examples/futures_trading_bot_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import os
import sys
import traceback
from typing import Optional, Union
from typing import Optional

import requests
import urllib3

from kraken.exceptions import KrakenException
from kraken.exceptions import KrakenAuthenticationError
from kraken.futures import Funding, KrakenFuturesWSClient, Market, Trade, User

logging.basicConfig(
Expand Down Expand Up @@ -64,7 +64,7 @@ def __init__(self: TradingBot, config: dict) -> None:
self.__market: Market = Market(key=config["key"], secret=config["secret"])
self.__funding: Funding = Funding(key=config["key"], secret=config["secret"])

async def on_message(self: TradingBot, message: Union[list, dict]) -> None:
async def on_message(self: TradingBot, message: list | dict) -> None:
"""Receives all messages that came form the websocket feed(s)"""
logging.info(message)

Expand All @@ -88,12 +88,9 @@ async def on_message(self: TradingBot, message: Union[list, dict]) -> None:

# Add more functions to customize the trading strategy …

def save_exit(self: TradingBot, reason: Optional[str] = "") -> None:
def save_exit(self: TradingBot, reason: str = "") -> None:
"""Controlled shutdown of the strategy"""
logging.warning(
"Save exit triggered, reason: {reason}",
extra={"reason": reason},
)
logging.warning("Save exit triggered, reason: %s", reason)
# some ideas:
# * save the bots data
# * maybe close trades
Expand Down Expand Up @@ -188,7 +185,7 @@ def __check_credentials(self: ManagedBot) -> bool:
except requests.exceptions.ConnectionError:
logging.error("ConnectionError, Kraken not available.")
return False
except KrakenException.KrakenAuthenticationError:
except KrakenAuthenticationError:
logging.error("Invalid credentials!")
return False

Expand Down
15 changes: 10 additions & 5 deletions kraken/futures/websocket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ConnectFuturesWebsocket:
:type callback: function
"""

MAX_RECONNECT_NUM: int = 2
MAX_RECONNECT_NUM: int = 3

def __init__(
self: ConnectFuturesWebsocket,
Expand Down Expand Up @@ -130,14 +130,18 @@ async def __run_forever(self: ConnectFuturesWebsocket) -> None:
finally:
self.__client.exception_occur = True

async def close_connection(self: ConnectFuturesWebsocket) -> None:
"""Closes the connection -/ will force reconnect"""
await self.__socket.close()

async def __reconnect(self: ConnectFuturesWebsocket) -> None:
logging.info("Websocket start connect/reconnect")

self.__reconnect_num += 1
if self.__reconnect_num >= self.MAX_RECONNECT_NUM:
raise MaxReconnectError

reconnect_wait: float = self.__get_reconnect_wait(self.__reconnect_num)
reconnect_wait: float = self.__get_reconnect_wait(attempts=self.__reconnect_num)
logging.debug(
"asyncio sleep reconnect_wait=%f s reconnect_num=%d",
reconnect_wait,
Expand All @@ -163,6 +167,7 @@ async def __reconnect(self: ConnectFuturesWebsocket) -> None:
for task in finished:
if task.exception():
exception_occur = True
self.__challenge_ready = False
traceback.print_stack()
message = f"{task} got an exception {task.exception()}\n {task.get_stack()}"
logging.warning(message)
Expand All @@ -176,14 +181,14 @@ async def __reconnect(self: ConnectFuturesWebsocket) -> None:
await self.__callback({"error": message})
if exception_occur:
break
logging.warning("reconnect over")
logging.warning("Connection closed")

async def __recover_subscription_req_msg(
self: ConnectFuturesWebsocket,
event: asyncio.Event,
) -> None:
logging.info(
"Recover subscriptions %s waiting.",
"Recover subscriptions %s: waiting",
self.__subscriptions,
)
await event.wait()
Expand All @@ -196,7 +201,7 @@ async def __recover_subscription_req_msg(
logging.info("%s: OK", sub)

logging.info(
"Recover subscriptions %s done.",
"Recover subscriptions %s: done",
self.__subscriptions,
)

Expand Down
Loading

0 comments on commit 24b3e60

Please sign in to comment.