Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.coverage
.venv
**/__pycache__/*
**/__pycache__/*
**/*.log
53 changes: 28 additions & 25 deletions oanda_client/oanda.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import os
import sys
from abc import abstractmethod
from datetime import datetime, timezone
from typing import Callable, Tuple
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
)