Exemple #1
0
def insert_historical_candles(symbols, datestring, min_date=None, verbose=True):
    """
    Insert <datestring> historical candles for a symbol(s) beyond the oldest
    found candle in the database.

    Example:
        insert_historical_candles('BTCUSDT', '1M') will insert 1 month of
        historical data beyond what is found in the database.

    Parameters:
    ---------------
    symbols: string | list of strings
        Symbols of the cryptos to insert data for. Example: ['BTCUSDT', 'ETHBTC']

    datestring: string
        String designating how much data to collect. Format: integer followed by
        'D', 'M', or 'Y'.

        Examples:
        '10D' ---> 10 days
        '5M' ---> 5 months
        '1Y' ---> 1 year

    debug: boolean
        Setting to true will return the data that would have been inserted into
        the database.

    """

    if isinstance(symbols, str):
        symbols = [symbols]

    # Convert datestring to timedelta object
    dt = parse_datestring(datestring)

    # Oldest dates from the DB as a dict, with None values if nothing's there
    oldest_dates = get_oldest_dates(symbols=symbols)

    for i, symbol in enumerate(symbols):
        endTime = oldest_dates[symbol]

        if endTime:
            endTime = DateConvert(endTime).datetime
        else:
            endTime = datetime.utcnow()

        startTime = endTime-dt

        insert_hourly_candles(
            symbol, endTime=endTime, startTime=startTime, verbose=verbose
            )

        if i != len(symbols)-1:
            if verbose:
                print('Sleeping...')
            sleep(10)
Exemple #2
0
    def test_endTime_with_no_startTime(self):

        endTime = '2018-09-11 22:05:13'
        symbol = 'BTCUSDT'

        candles = insert_hourly_candles(symbol, endTime=endTime, debug=True)
        assert len(candles) == 1

        #-----------------------------------------------------------------------
        symbol = ['BTCUSDT', 'ETHUSDT']

        candles = insert_hourly_candles(symbol, endTime=endTime, debug=True)
        assert len(candles) == 2
Exemple #3
0
def update_candles(debug=False):
    """
    Insert candles from the most recent open_date in the database to
    the current time.
    """

    symbols = db.get_symbols()
    startTime = db.get_max_from_column(column='open_date')

    if debug:
        return insert_hourly_candles(symbols, startTime=startTime, debug=True)
    else:
        insert_hourly_candles(symbols,
                              startTime=startTime,
                              debug=False,
                              verbose=True)
Exemple #4
0
    def test_multi_chunk(self):
        startTime = datetime.utcnow() - timedelta(days=26)
        endTime = datetime.utcnow() - timedelta(days=1)
        symbol = ['BTCUSDT']

        candles = insert_hourly_candles(symbol,
                                        startTime=startTime,
                                        endTime=endTime,
                                        debug=True)

        hour_before = startTime - timedelta(hours=1)
        hour_after = startTime + timedelta(hours=1)
        assert hour_before < DateConvert(
            candles.open_date.min()).datetime < hour_after

        hour_before = endTime - timedelta(hours=1)
        hour_after = endTime + timedelta(hours=1)
        assert hour_before < DateConvert(
            candles.open_date.max()).datetime < hour_after

        dates = list(candles.open_date)
        min_date = min(dates)
        max_date = max(dates)
        complete_range = pd.date_range(min_date, max_date, freq='1H')
        complete_range = list(
            map(lambda x: DateConvert(x).date, complete_range))
        assert np.isin(complete_range, dates).all()
Exemple #5
0
    def test_no_endTime_and_no_startTime(self):
        symbol = 'BTCUSDT'

        candles = insert_hourly_candles(symbol, debug=True)

        now = datetime.utcnow()
        nearest_complete = now.replace(minute=0, second=0, microsecond=0)
        hour_ago = now - timedelta(hours=1)
        now, hour_ago = DateConvert(now).date, DateConvert(hour_ago).date
        assert len(candles) == 1
        assert hour_ago < candles.open_date.iloc[0] < now
        assert candles.open_date.iloc[0] == DateConvert(nearest_complete).date

        #-----------------------------------------------------------------------
        symbol = ['BTCUSDT', 'ETHUSDT']
        candles = insert_hourly_candles(symbol, debug=True)
        assert len(candles) == 2
Exemple #6
0
    def test_startTime_with_no_endTime(self):
        # A range of candles from startTime to most recent
        startTime = datetime.utcnow() - timedelta(hours=5)
        symbol = 'BTCUSDT'

        candles = insert_hourly_candles(symbol,
                                        startTime=startTime,
                                        debug=True)
        assert len(candles) == 5

        #-----------------------------------------------------------------------
        symbol = ['BTCUSDT', 'ETHUSDT']

        candles = insert_hourly_candles(symbol,
                                        startTime=startTime,
                                        debug=True)
        assert len(candles) == 10
Exemple #7
0
    def test_startTime_with_endTime(self):
        # Range of candles within interval StartTime-endTime

        startTime = '2018-09-09 22:05:13'
        endTime = '2018-09-10 22:05:13'
        symbol = 'BTCUSDT'

        candles = insert_hourly_candles(symbol,
                                        startTime=startTime,
                                        endTime=endTime,
                                        debug=True)
        assert len(candles) == 24

        #-----------------------------------------------------------------------
        symbol = ['BTCUSDT', 'ETHUSDT']

        candles = insert_hourly_candles(symbol,
                                        startTime=startTime,
                                        endTime=endTime,
                                        debug=True)
        assert len(candles) == 48
Exemple #8
0
    def test_expected_date(self):
        """Function should return the last complete candle."""

        endTime = '2018-09-11 22:05:13'
        symbol = 'BTCUSDT'

        candles = insert_hourly_candles(symbol, endTime=endTime, debug=True)
        assert len(candles) == 1

        #-----------------------------------------------------------------------
        symbol = ['BTCUSDT', 'ETHUSDT']

        candles = insert_hourly_candles(symbol, endTime=endTime, debug=True)
        assert candles.open_date.iloc[0] == '2018-09-11 22:00:00'

        #-----------------------------------------------------------------------

        endTime = '2018-09-11 22:00:00'
        symbol = 'BTCUSDT'

        candles = insert_hourly_candles(symbol, endTime=endTime, debug=True)
        assert len(candles) == 1

        #-----------------------------------------------------------------------
        symbol = ['BTCUSDT', 'ETHUSDT']

        candles = insert_hourly_candles(symbol, endTime=endTime, debug=True)
        assert candles.open_date.iloc[0] == '2018-09-11 22:00:00'

        #-----------------------------------------------------------------------
        endTime = '2018-09-11 21:59:00'
        symbol = 'BTCUSDT'

        candles = insert_hourly_candles(symbol, endTime=endTime, debug=True)
        assert len(candles) == 1

        #-----------------------------------------------------------------------
        symbol = ['BTCUSDT', 'ETHUSDT']

        candles = insert_hourly_candles(symbol, endTime=endTime, debug=True)
        assert candles.open_date.iloc[0] == '2018-09-11 21:00:00'