Skip to content

Commit

Permalink
3.0.1rc7
Browse files Browse the repository at this point in the history
  • Loading branch information
DogsTailFarmer committed Mar 26, 2024
1 parent 1237cac commit 9749cc0
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 172 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 3.0.1rc6 - 2024-03-23
## 3.0.1rc7 - 2024-03-26
### Update
* Refactoring processing periodically events
* Up requirements for exchanges-wrapper==2.1.5

## 3.0.1rc3 - 2024-03-21
### Added for new features
Expand Down
2 changes: 1 addition & 1 deletion martin_binance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__author__ = "Jerry Fedorenko"
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
__license__ = "MIT"
__version__ = "3.0.1rc6"
__version__ = "3.0.1rc7"
__maintainer__ = "Jerry Fedorenko"
__contact__ = "https://github.com/DogsTailFarmer"

Expand Down
30 changes: 15 additions & 15 deletions martin_binance/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__author__ = "Jerry Fedorenko"
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
__license__ = "MIT"
__version__ = "3.0.1rc3"
__version__ = "3.0.1rc7"
__maintainer__ = "Jerry Fedorenko"
__contact__ = "https://github.com/DogsTailFarmer"

Expand All @@ -16,6 +16,7 @@
import grpclib.exceptions
import shortuuid

from martin_binance import ORDER_TIMEOUT
from exchanges_wrapper import martin as mr, Channel, Status, GRPCError

logger = logging.getLogger('logger.client')
Expand All @@ -27,8 +28,8 @@

class Trade:
def __init__(self, account_name, rate_limiter, symbol):
self.channel = Channel('127.0.0.1', 50051)
self.stub = mr.MartinStub(self.channel)
self.channel = None
self.stub = None
self.account_name = account_name
self.rate_limiter = rate_limiter
self.symbol = symbol
Expand All @@ -42,10 +43,14 @@ async def get_client(self):
self.wait_connection = True
client = None
while client is None:
self.channel = Channel('127.0.0.1', 50051)
self.stub = mr.MartinStub(self.channel)
try:
client = await self.connect()
except UserWarning:
client = None
self.channel.close()
await asyncio.sleep(random.randint(5, 30))
else:
self.client = client
self.wait_connection = False
Expand All @@ -64,18 +69,12 @@ async def connect(self):
except asyncio.CancelledError:
pass # Task cancellation should not be logged as an error.
except ConnectionRefusedError as ex:
logger.error(f"{ex}, reconnect...")
await asyncio.sleep(random.randint(5, 30))
raise UserWarning from ex
raise UserWarning(f"{ex}, reconnect...") from None
except GRPCError as ex:
status_code = ex.status
logger.error(f"Exception on register client: {status_code.name}, {ex.message}")
if status_code == Status.FAILED_PRECONDITION:
raise SystemExit(1) from ex
logger.warning('Restart gRPC client session')
await asyncio.sleep(random.randint(5, 30))
raise UserWarning from ex

raise UserWarning(f"Exception on register client: {status_code.name}, {ex.message}") from None
else:
logger.info(f"gRPC session started for client_id: {_client.client_id}\n"
f"trade_id: {self.trade_id}")
Expand All @@ -87,20 +86,21 @@ async def send_request(self, _request, _request_type, **kwargs):
kwargs['client_id'] = self.client.client_id
kwargs['trade_id'] = self.trade_id
try:
res = await _request(_request_type(**kwargs))
res = await asyncio.wait_for(_request(_request_type(**kwargs)), ORDER_TIMEOUT)
except asyncio.CancelledError:
pass # Task cancellation should not be logged as an error
except grpclib.exceptions.StreamTerminatedError:
raise UserWarning("Have not connection to gRPC server")
except ConnectionRefusedError:
raise UserWarning("Connection to gRPC server broken")
except asyncio.TimeoutError:
self.channel.close()
raise UserWarning("gRCP request timeout error")
except GRPCError as ex:
status_code = ex.status
if status_code == Status.UNAVAILABLE:
self.client = None
raise UserWarning(
"Connection to gRPC server failed, wait connection..."
) from ex
raise UserWarning("Wait connection to gRPC server") from None
logger.debug(f"Send request {_request}: {status_code.name}, {ex.message}")
raise
except Exception as ex:
Expand Down
305 changes: 162 additions & 143 deletions martin_binance/executor.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions martin_binance/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
LOG_LEVEL = logging.DEBUG # Default level for console output
HOLD_TP_ORDER_TIMEOUT = 30
COLLECT_ASSETS = bool()
GRID_ONLY_DELAY = 30 # sec delay before try restart GRID_ONLY cycle
#
ADAPTIVE_TRADE_CONDITION = bool()
BB_CANDLE_SIZE_IN_MINUTES = int()
Expand Down
14 changes: 4 additions & 10 deletions martin_binance/strategy_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__author__ = "Jerry Fedorenko"
__copyright__ = "Copyright © 2021 Jerry Fedorenko aka VM"
__license__ = "MIT"
__version__ = "3.0.1rc6"
__version__ = "3.0.1rc7"
__maintainer__ = "Jerry Fedorenko"
__contact__ = "https://github.com/DogsTailFarmer"

Expand Down Expand Up @@ -87,7 +87,6 @@ def __init__(self):
self.client = None
self.exchange = str()
self.symbol = str()
self.channel = None
self.stub = mr.MartinStub
self.client_id = int()
self.info_symbol = {}
Expand Down Expand Up @@ -175,7 +174,6 @@ def reset_vars(self):
def update_vars(self, _session):
self.client = _session.client
self.stub = _session.stub
self.channel = _session.channel
self.client_id = _session.client.client_id if _session.client else None
self.exchange = _session.client.exchange if _session.client else None
self.send_request = _session.send_request
Expand Down Expand Up @@ -651,6 +649,7 @@ async def ask_exit(self):
if prm.MODE in ('T', 'TC'):
try:
await self.send_request(self.stub.stop_stream, mr.MarketRequest, symbol=self.symbol)
self.session.channel.close()
except Exception as ex:
self.message_log(f"ask_exit: {ex}", log_level=logging.WARNING)

Expand All @@ -661,21 +660,16 @@ async def ask_exit(self):
self.start_collect = False
self.session_data_handler()

if self.channel:
self.channel.close()
if prm.LAST_STATE_FILE.exists():
print(f"Current state saved into {prm.LAST_STATE_FILE}")

tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
[task.cancel() for task in tasks]
await asyncio.gather(*tasks, return_exceptions=True)
if prm.LOGGING:
print(f"Cancelling {len(tasks)} outstanding tasks")
if prm.LAST_STATE_FILE.exists():
print(f"Current state saved into {prm.LAST_STATE_FILE}")

try:
self.stop()
except Exception as _err:
print(f"ask_exit.strategy.stop: {_err}")

async def fetch_order(self, _id: int, _client_order_id: str = None, _filled_update_call=False):
try:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dynamic = ["version", "description"]
requires-python = ">=3.9"

dependencies = [
"exchanges-wrapper==2.1.3",
"exchanges-wrapper==2.1.5",
"jsonpickle==3.0.2",
"psutil==5.9.6",
"requests==2.31.0",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exchanges-wrapper==2.1.3
exchanges-wrapper==2.1.5
jsonpickle==3.0.2
psutil==5.9.6
requests==2.31.0
Expand Down

0 comments on commit 9749cc0

Please sign in to comment.