Skip to content

Commit 24b3e60

Browse files
Fix "Futures re-subscribe to authenticated feed doesn't work" (#180)
1 parent 2e430bb commit 24b3e60

17 files changed

Lines changed: 298 additions & 137 deletions

.github/workflows/_codecov.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
run: python -m pip install --upgrade pip
5555

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

5959
- name: Generate coverage report
6060
env:

.github/workflows/_test_futures_private.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ jobs:
4343
with:
4444
python-version: ${{ inputs.python-version }}
4545

46-
- name: Install dependencies
46+
- name: Update Pip
4747
run: |
4848
python -m pip install --upgrade pip
49-
python -m pip install pytest
5049
5150
- name: Install package
52-
run: python -m pip install .
51+
run: python -m pip install ".[test]"
5352

5453
## Unit tests of the private Futures REST clients and endpoints
5554
##

.github/workflows/_test_futures_public.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ jobs:
3333
with:
3434
python-version: ${{ inputs.python-version }}
3535

36-
- name: Install dependencies
36+
- name: Update Pip
3737
run: |
3838
python -m pip install --upgrade pip
39-
python -m pip install pytest
4039
4140
- name: Install package
42-
run: python -m pip install .
41+
run: python -m pip install ".[test]"
4342

4443
## Unit tests of the public Futures REST clients and endpoints
4544
##

.github/workflows/_test_spot_private.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ jobs:
4040
with:
4141
python-version: ${{ inputs.python-version }}
4242

43-
- name: Install dependencies
43+
- name: Update Pip
4444
run: |
4545
python -m pip install --upgrade pip
46-
python -m pip install pytest
4746
4847
- name: Install package
49-
run: python -m pip install .
48+
run: python -m pip install ".[test]"
5049

5150
## Unit tests of private Spot REST clients and endpoints
5251
##

.github/workflows/_test_spot_public.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ jobs:
3333
with:
3434
python-version: ${{ inputs.python-version }}
3535

36-
- name: Install dependencies
36+
- name: Update Pip
3737
run: |
3838
python -m pip install --upgrade pip
39-
python -m pip install pytest
4039
4140
- name: Install package
42-
run: python -m pip install .
41+
run: python -m pip install ".[test]"
4342

4443
## Unit tests of the public Spot REST clients and endpoints
4544
##

.pre-commit-config.yaml

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,43 @@
44
#
55

66
repos:
7+
- repo: https://github.com/astral-sh/ruff-pre-commit
8+
rev: v0.1.7
9+
hooks:
10+
- id: ruff
11+
args:
12+
- --preview
13+
- --fix
14+
- --exit-non-zero-on-fix
15+
- repo: https://github.com/pycqa/flake8
16+
rev: 6.1.0
17+
hooks:
18+
- id: flake8
19+
args:
20+
- --select=E9,F63,F7,F82
21+
- --show-source
22+
- --statistics
23+
- repo: https://github.com/pycqa/pylint
24+
rev: v3.0.1
25+
hooks:
26+
- id: pylint
27+
name: pylint
28+
types: [python]
29+
exclude: ^examples/|^tests/|^setup.py$
30+
args:
31+
- --rcfile=pyproject.toml
32+
- -d=R0801 # ignore duplicate code
33+
- -j=4
34+
- repo: https://github.com/pre-commit/mirrors-mypy
35+
rev: v1.7.1
36+
hooks:
37+
- id: mypy
38+
name: mypy
39+
additional_dependencies: ["types-requests"]
40+
pass_filenames: false
41+
args:
42+
- --config-file=pyproject.toml
43+
- kraken
744
- repo: https://github.com/pre-commit/pre-commit-hooks
845
rev: v4.5.0
946
hooks:
@@ -51,39 +88,3 @@ repos:
5188
- id: isort
5289
args:
5390
- --profile=black
54-
- repo: https://github.com/astral-sh/ruff-pre-commit
55-
rev: v0.1.7
56-
hooks:
57-
- id: ruff
58-
args:
59-
- --preview
60-
- --fix
61-
- --exit-non-zero-on-fix
62-
- repo: https://github.com/pycqa/flake8
63-
rev: 6.1.0
64-
hooks:
65-
- id: flake8
66-
args:
67-
- --select=E9,F63,F7,F82
68-
- --show-source
69-
- --statistics
70-
- repo: https://github.com/pycqa/pylint
71-
rev: v3.0.1
72-
hooks:
73-
- id: pylint
74-
name: pylint
75-
types: [python]
76-
exclude: ^examples/|^tests/|^setup.py$
77-
args:
78-
- --rcfile=pyproject.toml
79-
- -d=R0801 # ignore duplicate code
80-
- repo: https://github.com/pre-commit/mirrors-mypy
81-
rev: v1.7.1
82-
hooks:
83-
- id: mypy
84-
name: mypy
85-
additional_dependencies: ["types-requests"]
86-
pass_filenames: false
87-
args:
88-
- --config-file=pyproject.toml
89-
- kraken

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Changelog
22

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

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

77
**Implemented enhancements:**
88

99
- Add `start`, `end`, and `cursor` parameters to `kraken.spot.Funding.get_recent_withdraw_status` [\#176](https://github.com/btschwertfeger/python-kraken-sdk/issues/176)
1010
- Add `withdraw_methods` and `withdraw_addresses` to `kraken.spot.Funding` [\#174](https://github.com/btschwertfeger/python-kraken-sdk/issues/174)
11-
- 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))
11+
- 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))
1212
- 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))
1313

1414
## [v2.0.0](https://github.com/btschwertfeger/python-kraken-sdk/tree/v2.0.0) (2023-10-22)

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
44
# GitHub: https://github.com/btschwertfeger
55

6-
PYTHON := python
6+
PYTHON := venv/bin/python
77
PYTEST := $(PYTHON) -m pytest
88
PYTEST_OPTS := -vv --junit-xml=pytest.xml
99
PYTEST_COV_OPTS := $(PYTEST_OPTS) --cov --cov-report=xml:coverage.xml --cov-report=term
@@ -40,7 +40,7 @@ install:
4040
##
4141
.PHONY: dev
4242
dev:
43-
$(PYTHON) -m pip install -e ".[dev]"
43+
$(PYTHON) -m pip install -e ".[dev,test]"
4444

4545
## ======= T E S T I N G =======
4646
## test Run the unit tests
@@ -53,10 +53,10 @@ test:
5353
.PHONY: tests
5454
tests: test
5555

56-
## test-wip Run tests marked as 'wip'
56+
## wip Run tests marked as 'wip'
5757
##
58-
.PHONY: test-wip
59-
test-wip:
58+
.PHONY: wip
59+
wip:
6060
@rm *.log || true
6161
$(PYTEST) -m "wip" -vv $(TEST_DIR)
6262

examples/futures_trading_bot_template.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
import os
1818
import sys
1919
import traceback
20-
from typing import Optional, Union
20+
from typing import Optional
2121

2222
import requests
2323
import urllib3
2424

25-
from kraken.exceptions import KrakenException
25+
from kraken.exceptions import KrakenAuthenticationError
2626
from kraken.futures import Funding, KrakenFuturesWSClient, Market, Trade, User
2727

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

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

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

8989
# Add more functions to customize the trading strategy …
9090

91-
def save_exit(self: TradingBot, reason: Optional[str] = "") -> None:
91+
def save_exit(self: TradingBot, reason: str = "") -> None:
9292
"""Controlled shutdown of the strategy"""
93-
logging.warning(
94-
"Save exit triggered, reason: {reason}",
95-
extra={"reason": reason},
96-
)
93+
logging.warning("Save exit triggered, reason: %s", reason)
9794
# some ideas:
9895
# * save the bots data
9996
# * maybe close trades
@@ -188,7 +185,7 @@ def __check_credentials(self: ManagedBot) -> bool:
188185
except requests.exceptions.ConnectionError:
189186
logging.error("ConnectionError, Kraken not available.")
190187
return False
191-
except KrakenException.KrakenAuthenticationError:
188+
except KrakenAuthenticationError:
192189
logging.error("Invalid credentials!")
193190
return False
194191

kraken/futures/websocket/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ConnectFuturesWebsocket:
3939
:type callback: function
4040
"""
4141

42-
MAX_RECONNECT_NUM: int = 2
42+
MAX_RECONNECT_NUM: int = 3
4343

4444
def __init__(
4545
self: ConnectFuturesWebsocket,
@@ -130,14 +130,18 @@ async def __run_forever(self: ConnectFuturesWebsocket) -> None:
130130
finally:
131131
self.__client.exception_occur = True
132132

133+
async def close_connection(self: ConnectFuturesWebsocket) -> None:
134+
"""Closes the connection -/ will force reconnect"""
135+
await self.__socket.close()
136+
133137
async def __reconnect(self: ConnectFuturesWebsocket) -> None:
134138
logging.info("Websocket start connect/reconnect")
135139

136140
self.__reconnect_num += 1
137141
if self.__reconnect_num >= self.MAX_RECONNECT_NUM:
138142
raise MaxReconnectError
139143

140-
reconnect_wait: float = self.__get_reconnect_wait(self.__reconnect_num)
144+
reconnect_wait: float = self.__get_reconnect_wait(attempts=self.__reconnect_num)
141145
logging.debug(
142146
"asyncio sleep reconnect_wait=%f s reconnect_num=%d",
143147
reconnect_wait,
@@ -163,6 +167,7 @@ async def __reconnect(self: ConnectFuturesWebsocket) -> None:
163167
for task in finished:
164168
if task.exception():
165169
exception_occur = True
170+
self.__challenge_ready = False
166171
traceback.print_stack()
167172
message = f"{task} got an exception {task.exception()}\n {task.get_stack()}"
168173
logging.warning(message)
@@ -176,14 +181,14 @@ async def __reconnect(self: ConnectFuturesWebsocket) -> None:
176181
await self.__callback({"error": message})
177182
if exception_occur:
178183
break
179-
logging.warning("reconnect over")
184+
logging.warning("Connection closed")
180185

181186
async def __recover_subscription_req_msg(
182187
self: ConnectFuturesWebsocket,
183188
event: asyncio.Event,
184189
) -> None:
185190
logging.info(
186-
"Recover subscriptions %s waiting.",
191+
"Recover subscriptions %s: waiting",
187192
self.__subscriptions,
188193
)
189194
await event.wait()
@@ -196,7 +201,7 @@ async def __recover_subscription_req_msg(
196201
logging.info("%s: OK", sub)
197202

198203
logging.info(
199-
"Recover subscriptions %s done.",
204+
"Recover subscriptions %s: done",
200205
self.__subscriptions,
201206
)
202207

0 commit comments

Comments
 (0)