Esempio n. 1
0
    def get_candles(self,
                    freq,
                    assets,
                    bar_count=1,
                    start_dt=None,
                    end_dt=None):
        is_single = (isinstance(assets, TradingPair))
        if is_single:
            assets = [assets]

        symbols = self.get_symbols(assets)
        timeframe = CCXT.get_timeframe(freq)

        if timeframe not in self.api.timeframes:
            freqs = [CCXT.get_frequency(t) for t in self.api.timeframes]
            raise UnsupportedHistoryFrequencyError(
                exchange=self.name,
                freq=freq,
                freqs=freqs,
            )

        if start_dt is not None and end_dt is not None:
            raise ValueError(
                'Please provide either start_dt or end_dt, not both.')

        if start_dt is None:
            if end_dt is None:
                end_dt = pd.Timestamp.utcnow()

            dt_range = get_periods_range(
                end_dt=end_dt,
                periods=bar_count,
                freq=freq,
            )
            start_dt = dt_range[0]

        delta = start_dt - get_epoch()
        since = int(delta.total_seconds()) * 1000

        candles = dict()
        for index, asset in enumerate(assets):
            ohlcvs = self.api.fetch_ohlcv(symbol=symbols[index],
                                          timeframe=timeframe,
                                          since=since,
                                          limit=bar_count,
                                          params={})

            candles[asset] = []
            for ohlcv in ohlcvs:
                candles[asset].append(
                    dict(last_traded=pd.to_datetime(ohlcv[0],
                                                    unit='ms',
                                                    utc=True),
                         open=ohlcv[1],
                         high=ohlcv[2],
                         low=ohlcv[3],
                         close=ohlcv[4],
                         volume=ohlcv[5]))
            candles[asset] = sorted(candles[asset],
                                    key=lambda c: c['last_traded'])

        if is_single:
            return six.next(six.itervalues(candles))

        else:
            return candles
Esempio n. 2
0
    def get_candles(self, freq, assets, bar_count=1, start_dt=None,
                    end_dt=None):
        is_single = (isinstance(assets, TradingPair))
        if is_single:
            assets = [assets]

        symbols = self.get_symbols(assets)
        timeframe = CCXT.get_timeframe(freq)

        if timeframe not in self.api.timeframes:
            freqs = [CCXT.get_frequency(t) for t in self.api.timeframes]
            raise UnsupportedHistoryFrequencyError(
                exchange=self.name,
                freq=freq,
                freqs=freqs,
            )

        if start_dt is not None and end_dt is not None:
            raise ValueError(
                'Please provide either start_dt or end_dt, not both.'
            )

        elif end_dt is not None:
            # Make sure that end_dt really wants data in the past
            # if it's close to now, we skip the 'since' parameters to
            # lower the probability of error
            bars_to_now = pd.date_range(
                end_dt, pd.Timestamp.utcnow(), freq=freq
            )
            # See: https://github.com/ccxt/ccxt/issues/1360
            if len(bars_to_now) > 1 or self.name in ['poloniex']:
                dt_range = get_periods_range(
                    end_dt=end_dt,
                    periods=bar_count,
                    freq=freq,
                )
                start_dt = dt_range[0]

        since = None
        if start_dt is not None:
            delta = start_dt - get_epoch()
            since = int(delta.total_seconds()) * 1000

        candles = dict()
        for index, asset in enumerate(assets):
            ohlcvs = self.api.fetch_ohlcv(
                symbol=symbols[index],
                timeframe=timeframe,
                since=since,
                limit=bar_count,
                params={}
            )

            candles[asset] = []
            for ohlcv in ohlcvs:
                candles[asset].append(dict(
                    last_traded=pd.to_datetime(
                        ohlcv[0], unit='ms', utc=True
                    ),
                    open=ohlcv[1],
                    high=ohlcv[2],
                    low=ohlcv[3],
                    close=ohlcv[4],
                    volume=ohlcv[5]
                ))
            candles[asset] = sorted(
                candles[asset], key=lambda c: c['last_traded']
            )

        if is_single:
            return six.next(six.itervalues(candles))

        else:
            return candles
Esempio n. 3
0
    def get_candles(self, freq, assets, bar_count=1, start_dt=None,
                    end_dt=None):
        is_single = (isinstance(assets, TradingPair))
        if is_single:
            assets = [assets]

        symbols = self.get_symbols(assets)
        timeframe = CCXT.get_timeframe(freq)

        if timeframe not in self.api.timeframes:
            freqs = [CCXT.get_frequency(t) for t in self.api.timeframes]
            raise UnsupportedHistoryFrequencyError(
                exchange=self.name,
                freq=freq,
                freqs=freqs,
            )

        if start_dt is not None and end_dt is not None:
            raise ValueError(
                'Please provide either start_dt or end_dt, not both.'
            )

        if start_dt is None:
            # TODO: determine why binance is failing
            if end_dt is None and self.name not in ['binance']:
                end_dt = pd.Timestamp.utcnow()

            if end_dt is not None:
                dt_range = get_periods_range(
                    end_dt=end_dt,
                    periods=bar_count,
                    freq=freq,
                )
                start_dt = dt_range[0]

        if start_dt is not None:
            # Convert out start date to a UNIX timestamp, then translate to
            # milliseconds
            delta = start_dt - get_epoch()
            since = int(delta.total_seconds()) * 1000
        else:
            since = None

        candles = dict()
        for index, asset in enumerate(assets):
            ohlcvs = self.api.fetch_ohlcv(
                symbol=symbols[index],
                timeframe=timeframe,
                since=since,
                limit=bar_count,
                params={}
            )

            candles[asset] = []
            for ohlcv in ohlcvs:
                candles[asset].append(dict(
                    last_traded=pd.to_datetime(
                        ohlcv[0], unit='ms', utc=True
                    ),
                    open=ohlcv[1],
                    high=ohlcv[2],
                    low=ohlcv[3],
                    close=ohlcv[4],
                    volume=ohlcv[5]
                ))
            candles[asset] = sorted(
                candles[asset], key=lambda c: c['last_traded']
            )

        if is_single:
            return six.next(six.itervalues(candles))

        else:
            return candles