Exchange coingekko for coinmarketcap
This commit is contained in:
@@ -7,7 +7,7 @@ import logging
|
||||
import time
|
||||
from typing import Dict, List
|
||||
|
||||
from coinmarketcap import Market
|
||||
from pycoingecko import CoinGeckoAPI
|
||||
|
||||
from freqtrade.constants import SUPPORTED_FIAT
|
||||
|
||||
@@ -38,8 +38,8 @@ class CryptoFiat:
|
||||
# Private attributes
|
||||
self._expiration = 0.0
|
||||
|
||||
self.crypto_symbol = crypto_symbol.upper()
|
||||
self.fiat_symbol = fiat_symbol.upper()
|
||||
self.crypto_symbol = crypto_symbol.lower()
|
||||
self.fiat_symbol = fiat_symbol.lower()
|
||||
self.set_price(price=price)
|
||||
|
||||
def set_price(self, price: float) -> None:
|
||||
@@ -67,17 +67,20 @@ class CryptoToFiatConverter:
|
||||
This object is also a Singleton
|
||||
"""
|
||||
__instance = None
|
||||
_coinmarketcap: Market = None
|
||||
_coingekko: CoinGeckoAPI = None
|
||||
|
||||
_cryptomap: Dict = {}
|
||||
|
||||
def __new__(cls):
|
||||
"""
|
||||
This class is a singleton - should not be instanciated twice.
|
||||
"""
|
||||
if CryptoToFiatConverter.__instance is None:
|
||||
CryptoToFiatConverter.__instance = object.__new__(cls)
|
||||
try:
|
||||
CryptoToFiatConverter._coinmarketcap = Market()
|
||||
CryptoToFiatConverter._coingekko = CoinGeckoAPI()
|
||||
except BaseException:
|
||||
CryptoToFiatConverter._coinmarketcap = None
|
||||
CryptoToFiatConverter._coingekko = None
|
||||
return CryptoToFiatConverter.__instance
|
||||
|
||||
def __init__(self) -> None:
|
||||
@@ -86,14 +89,12 @@ class CryptoToFiatConverter:
|
||||
|
||||
def _load_cryptomap(self) -> None:
|
||||
try:
|
||||
coinlistings = self._coinmarketcap.listings()
|
||||
self._cryptomap = dict(map(lambda coin: (coin["symbol"], str(coin["id"])),
|
||||
coinlistings["data"]))
|
||||
except (BaseException) as exception:
|
||||
coinlistings = self._coingekko.get_coins_list()
|
||||
# Create mapping table from synbol to coingekko_id
|
||||
self._cryptomap = {x['symbol']: x['id'] for x in coinlistings}
|
||||
except (Exception) as exception:
|
||||
logger.error(
|
||||
"Could not load FIAT Cryptocurrency map for the following problem: %s",
|
||||
type(exception).__name__
|
||||
)
|
||||
f"Could not load FIAT Cryptocurrency map for the following problem: {exception}")
|
||||
|
||||
def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float:
|
||||
"""
|
||||
@@ -115,8 +116,8 @@ class CryptoToFiatConverter:
|
||||
:param fiat_symbol: FIAT currency you want to convert to (e.g USD)
|
||||
:return: Price in FIAT
|
||||
"""
|
||||
crypto_symbol = crypto_symbol.upper()
|
||||
fiat_symbol = fiat_symbol.upper()
|
||||
crypto_symbol = crypto_symbol.lower()
|
||||
fiat_symbol = fiat_symbol.lower()
|
||||
|
||||
# Check if the fiat convertion you want is supported
|
||||
if not self._is_supported_fiat(fiat=fiat_symbol):
|
||||
@@ -170,15 +171,13 @@ class CryptoToFiatConverter:
|
||||
:return: bool, True supported, False not supported
|
||||
"""
|
||||
|
||||
fiat = fiat.upper()
|
||||
|
||||
return fiat in SUPPORTED_FIAT
|
||||
return fiat.upper() in SUPPORTED_FIAT
|
||||
|
||||
def _find_price(self, crypto_symbol: str, fiat_symbol: str) -> float:
|
||||
"""
|
||||
Call CoinMarketCap API to retrieve the price in the FIAT
|
||||
:param crypto_symbol: Crypto-currency you want to convert (e.g BTC)
|
||||
:param fiat_symbol: FIAT currency you want to convert to (e.g USD)
|
||||
Call CoinGekko API to retrieve the price in the FIAT
|
||||
:param crypto_symbol: Crypto-currency you want to convert (e.g btc)
|
||||
:param fiat_symbol: FIAT currency you want to convert to (e.g usd)
|
||||
:return: float, price of the crypto-currency in Fiat
|
||||
"""
|
||||
# Check if the fiat convertion you want is supported
|
||||
@@ -195,12 +194,13 @@ class CryptoToFiatConverter:
|
||||
return 0.0
|
||||
|
||||
try:
|
||||
_gekko_id = self._cryptomap[crypto_symbol]
|
||||
return float(
|
||||
self._coinmarketcap.ticker(
|
||||
currency=self._cryptomap[crypto_symbol],
|
||||
convert=fiat_symbol
|
||||
)['data']['quotes'][fiat_symbol.upper()]['price']
|
||||
self._coingekko.get_price(
|
||||
ids=_gekko_id,
|
||||
vs_currencies=fiat_symbol
|
||||
)[_gekko_id][fiat_symbol]
|
||||
)
|
||||
except BaseException as exception:
|
||||
except Exception as exception:
|
||||
logger.error("Error in _find_price: %s", exception)
|
||||
return 0.0
|
||||
|
Reference in New Issue
Block a user