Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Coinbase Advanced API to replace Historic Crypto Plugin #264

Open
macanudo527 opened this issue Oct 22, 2024 · 4 comments
Open

Comments

@macanudo527
Copy link
Collaborator

The current Historic Cypto Plugin Silently exits due to an incorrect API call. The Historic Crypto module appears to be abandoned, so we need to implement a better solution. The Coinbase Advanced API should offer a better long term solution.

@ndopencode
Copy link
Contributor

I noticed this issue recently. Is there a work around while Historic Crypto is no longer working?

@macanudo527
Copy link
Collaborator Author

Not really. It should be pretty simple to implement the Coinbase Advanced API if you want to submit something though.

@orientalperil
Copy link
Contributor

orientalperil commented Dec 7, 2024

I made something for my use that does the same thing as Historic Crypto but uses Coinbase Advanced API. I'm using AutoConfig from python-decouple to read the api key from an .env file.

from datetime import datetime
from datetime import timedelta
from datetime import timezone
from typing import Optional

from coinbase.rest import RESTClient
from dali.abstract_pair_converter_plugin import AbstractPairConverterPlugin
from dali.historical_bar import HistoricalBar
from dali.transaction_manifest import TransactionManifest
from rp2.rp2_decimal import RP2Decimal

from dali_dali.settings import config


class PairConverterPlugin(AbstractPairConverterPlugin):
    def __init__(self, historical_price_type: str) -> None:
        super().__init__(historical_price_type)
        self.client = RESTClient(api_key=config('COINBASE_API_KEY'), api_secret=config('COINBASE_PRIVATE_KEY'))

    def name(self) -> str:
        return "dali_dali_coinbase"

    def cache_key(self) -> str:
        return self.name()

    def optimize(self, transaction_manifest: TransactionManifest) -> None:
        pass

    def get_historic_bar_from_native_source(self, timestamp: datetime, from_asset: str, to_asset: str, exchange: str) -> Optional[HistoricalBar]:
        time_granularity = 'ONE_MINUTE'
        utc_timestamp = timestamp.astimezone(timezone.utc)
        start = utc_timestamp.replace(second=0)
        end = start

        candle = self.client.get_candles(f'{from_asset}-{to_asset}', int(start.timestamp()), int(end.timestamp()), time_granularity).to_dict()['candles'][0]
        result = HistoricalBar(
            duration=timedelta(seconds=60),
            timestamp=start,
            open=RP2Decimal(candle['open']),
            high=RP2Decimal(candle['high']),
            low=RP2Decimal(candle['low']),
            close=RP2Decimal(candle['close']),
            volume=RP2Decimal(candle['volume']),
        )

        return result

@macanudo527
Copy link
Collaborator Author

macanudo527 commented Dec 13, 2024

@orientalperil I refactored your code and created a plugin with it. If you have the time can you test it out? You can find it in #271

Thanks for contributing the code sample!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants