Interface for performing queries against exchange API's
| 11 | from tenacity import retry, retry_if_exception_type, stop_after_attempt |
| 12 | |
| 13 | class ExchangeInterface(): |
| 14 | """Interface for performing queries against exchange API's |
| 15 | """ |
| 16 | |
| 17 | def __init__(self, exchange_config): |
| 18 | """Initializes ExchangeInterface class |
| 19 | |
| 20 | Args: |
| 21 | exchange_config (dict): A dictionary containing configuration for the exchanges. |
| 22 | """ |
| 23 | |
| 24 | self.logger = structlog.get_logger() |
| 25 | self.exchanges = dict() |
| 26 | |
| 27 | # Loads the exchanges using ccxt. |
| 28 | for exchange in exchange_config: |
| 29 | if exchange_config[exchange]['required']['enabled']: |
| 30 | new_exchange = getattr(ccxt, exchange)({ |
| 31 | "enableRateLimit": True |
| 32 | }) |
| 33 | |
| 34 | # sets up api permissions for user if given |
| 35 | if new_exchange: |
| 36 | self.exchanges[new_exchange.id] = new_exchange |
| 37 | else: |
| 38 | self.logger.error("Unable to load exchange %s", new_exchange) |
| 39 | |
| 40 | |
| 41 | @retry(retry=retry_if_exception_type(ccxt.NetworkError), stop=stop_after_attempt(3)) |
| 42 | def get_historical_data(self, market_pair, exchange, time_unit, start_date=None, max_periods=100): |
| 43 | """Get historical OHLCV for a symbol pair |
| 44 | |
| 45 | Decorators: |
| 46 | retry |
| 47 | |
| 48 | Args: |
| 49 | market_pair (str): Contains the symbol pair to operate on i.e. BURST/BTC |
| 50 | exchange (str): Contains the exchange to fetch the historical data from. |
| 51 | time_unit (str): A string specifying the ccxt time unit i.e. 5m or 1d. |
| 52 | start_date (int, optional): Timestamp in milliseconds. |
| 53 | max_periods (int, optional): Defaults to 100. Maximum number of time periods |
| 54 | back to fetch data for. |
| 55 | |
| 56 | Returns: |
| 57 | list: Contains a list of lists which contain timestamp, open, high, low, close, volume. |
| 58 | """ |
| 59 | |
| 60 | try: |
| 61 | if time_unit not in self.exchanges[exchange].timeframes: |
| 62 | raise ValueError( |
| 63 | "{} does not support {} timeframe for OHLCV data. Possible values are: {}".format( |
| 64 | exchange, |
| 65 | time_unit, |
| 66 | list(self.exchanges[exchange].timeframes) |
| 67 | ) |
| 68 | ) |
| 69 | except AttributeError: |
| 70 | self.logger.error( |