Skip to content

Commit

Permalink
Adjust Start Time to Match Granularity
Browse files Browse the repository at this point in the history
  • Loading branch information
Neal Chambers committed Dec 16, 2024
1 parent 9532227 commit 786f1a1
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/dali/plugin/pair_converter/coinbase_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,19 @@ def optimize(self, transaction_manifest: TransactionManifest) -> None:
def get_historic_bar_from_native_source(self, timestamp: datetime, from_asset: str, to_asset: str, exchange: str) -> Optional[HistoricalBar]:
result: Optional[HistoricalBar] = None
utc_timestamp = timestamp.astimezone(timezone.utc)
start = utc_timestamp.replace(second=0)
end = start
start = utc_timestamp
retry_count: int = 0

while retry_count < len(TIME_GRANULARITY):
try:
granularity = list(TIME_GRANULARITY.keys())[retry_count]
start_epoch = int(start.timestamp())
start_epoch = start_epoch - (start_epoch % TIME_GRANULARITY[granularity])
end_epoch = start_epoch
if self._authorized:
candle = self.client.get_candles(f"{from_asset}-{to_asset}", str(int(start.timestamp())), str(int(end.timestamp())), granularity).to_dict()[
"candles"
][0]
candle = self.client.get_candles(f"{from_asset}-{to_asset}", str(start_epoch), str(end_epoch), granularity).to_dict()["candles"][0]
else:
candle = self.client.get_public_candles(f"{from_asset}-{to_asset}", str(int(start.timestamp())), str(int(end.timestamp())), granularity).to_dict()[
"candles"
][0]
candle = self.client.get_public_candles(f"{from_asset}-{to_asset}", str(start_epoch), str(end_epoch), granularity).to_dict()["candles"][0]
candle_start = datetime.fromtimestamp(int(candle["start"]), timezone.utc)
result = HistoricalBar(
duration=timedelta(seconds=TIME_GRANULARITY[granularity]),
Expand All @@ -89,5 +87,8 @@ def get_historic_bar_from_native_source(self, timestamp: datetime, from_asset: s
)
except ValueError:
retry_count += 1
if result:
break
retry_count += 1

return result

0 comments on commit 786f1a1

Please sign in to comment.