diff --git a/.gitignore b/.gitignore index 0a77a8d..47e20bc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .coverage .venv -**/__pycache__/* \ No newline at end of file +**/__pycache__/* +**/*.log \ No newline at end of file diff --git a/oanda_client/oanda.py b/oanda_client/oanda.py index 11b65e9..8add8eb 100644 --- a/oanda_client/oanda.py +++ b/oanda_client/oanda.py @@ -1,6 +1,5 @@ import asyncio import os -import sys from abc import abstractmethod from datetime import datetime, timezone from typing import Callable, Tuple @@ -15,6 +14,7 @@ ) from pytrade.interfaces.account import IAccount from pytrade.interfaces.client import IClient +from pytrade.logging import get_logger from pytrade.models import Order from v20.account import Account from v20.instrument import Candlestick as v20Candlestick @@ -71,6 +71,7 @@ def __init__(self, config_path: str = os.environ.get("V20_CONFIG", DEFAULT_PATH) dict() ) self._stream_tasks: list[asyncio.Task] = [] + self.logger = get_logger() @property def account(self) -> IAccount: @@ -248,30 +249,32 @@ async def _stream_candles( granularity: Granularity, callback: Callable[[Candlestick], None], ): - try: - interval = 60 * MINUTES_MAP[granularity] - previous_candle = None - candle = self.get_candle(instrument, granularity) - previous_candle = candle - initial_delay = (2 * interval) - ( - (datetime.now(timezone.utc) - candle.timestamp).seconds - ) - callback(candle) - await asyncio.sleep(initial_delay) - while True: + while True: + try: + interval = 60 * MINUTES_MAP[granularity] + previous_candle = None candle = self.get_candle(instrument, granularity) - - # Handle clase where we grab previous candle again - if candle.timestamp == previous_candle.timestamp: - await asyncio.sleep(1) - continue - previous_candle = candle + initial_delay = (2 * interval) - ( + (datetime.now(timezone.utc) - candle.timestamp).seconds + ) callback(candle) - await asyncio.sleep(interval) - - except asyncio.CancelledError: - pass - except Exception as err: - print(err) - sys.exit(1) + await asyncio.sleep(initial_delay) + while True: + candle = self.get_candle(instrument, granularity) + + # Handle clase where we grab previous candle again + if candle.timestamp == previous_candle.timestamp: + await asyncio.sleep(1) + continue + + previous_candle = candle + callback(candle) + await asyncio.sleep(interval) + + except asyncio.CancelledError: + pass + except Exception as err: + self.logger.error( + "Exception encountered streaming candles", exc_info=err + )