Corfucinas/crypto-candlesticks

Possibility of period overflow

Opened this issue · 2 comments

Hi - this library is very interesting and perusing and running the code I noticed that there's a possibility of the download period calculation going out of bounds, which throws an error if it steps into the future (i.e., in general there is no guarantee that ((end_time - start_time) % step_size) == 0, except when step_size is set to one day and the interval is 1d ). Here's your code:

    while start_time <= end_time:
        period = start_time + step_size
        candlestick = exchange.get_candles(
            ticker=ticker,
            time_interval=interval,
            start_time=start_time,
            end_time=period,
        )
        candle_data.extend(candlestick)
        write_to_console(
            ticker,
            interval,
            candlestick,
            live,
            setup_table(),
        )
        live.refresh()
        start_time = period
        time.sleep(_RATE_LIMIT)

... whereas I suggest it should be (period has been renamed to period_end):

    while start_time < end_time:
        period_end = start_time + step_size
        if period_end > end_time:
            period_end = end_time
        candlestick = exchange.get_candles(
            ticker=ticker,
            time_interval=interval,
            start_time=start_time,
            end_time=period_end,
        )
        candle_data.extend(candlestick)
        write_to_console(
            ticker,
            interval,
            candlestick,
            live,
            setup_table(),
        )
        live.refresh()
        start_time = period_end
        time.sleep(_RATE_LIMIT)

(step_size should probably also be adjusted based on the interval, though I haven't shown that.)

Hope that makes sense.

Thanks,
Arvindra

Hi @asehmi , yes, you're correct in your observations.

I'll take a look and update it when I have a bit more time on my hands. Thanks for opening this issue