예제 #1
0
def get_ohlcv(exchange: ccxt.Exchange, symbol: str,
              timeframe: str) -> pd.DataFrame:
    # Check if fetching of OHLC Data is supported
    if not exchange.has["fetchOHLCV"]:
        raise ValueError(
            '{} does not support fetching OHLC data. Please use another exchange'
            .format(exchange))

    # Check requested timeframe is available. If not return a helpful error.
    if timeframe not in exchange.timeframes:
        print('-' * 36, ' ERROR ', '-' * 35)
        print('The requested timeframe ({}) is not available from {}\n'.format(
            args.timeframe, args.exchange))
        print('Available timeframes are:')
        for key in exchange.timeframes.keys():
            print('  - ' + key)
        raise ValueError

    # Check if the symbol is available on the Exchange
    exchange.load_markets()
    if symbol not in exchange.symbols:
        print('-' * 36, ' ERROR ', '-' * 35)
        print('The requested symbol ({}) is not available from {}\n'.format(
            symbol, exchange))
        print('Available symbols are:')
        for key in exchange.symbols:
            print('  - ' + key)
        print('-' * 80)
        raise ValueError

    # Get data
    data = exchange.fetch_ohlcv(symbol, timeframe)
    header = ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume']
    return pd.DataFrame(data, columns=header)
예제 #2
0
 def fetch_ohlcv(self, exchange: ccxt.Exchange, symbol, since):
     while True:
         try:
             candles = exchange.fetch_ohlcv(symbol,
                                            self.dataframe,
                                            since=since,
                                            limit=60 * 12)
             return candles
         except (ccxt.errors.DDoSProtection,
                 ccxt.errors.RequestTimeout) as e:
             self.logger.debug(error_class=e.__class__.__name__,
                               exchange=exchange.id)
             time.sleep(3)
         except ccxt.errors.BaseError as e:
             self.logger.debug(error_class=e.__class__.__name__)
             self.logger.exception()
             time.sleep(5)