Skip to content

Fetches crypto candles with cache. You won't hit the API twice!

Notifications You must be signed in to change notification settings

martincpt/cached-candles

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cached Crypto Candles for Python [cached_candles]

Requirement: Python >= 3.10

Fetches crypto candles with cache

This package checks against the local cache by symbol, interval, start and end date parameters, whether the requested candles been already fetched and returns them if they are available.

No more repetative queries

With this, you can save plenty of time and bandwidth, without having to download them over and over again or worry that you get temporary banned by reaching api rate limits.

No more API limits

This package also features a built-in api rate limiter which inserts some sleep time between independent api calls, in case you reach the limit defined in settings. This feature has been tested against the Bitfinex platform.

You don't even have to worry about if the requested timeframe contains more candle than a certain service can provide in a single API call. This library overcomes this issue and splits the query into several API calls and concatenate the results.

Ready for analysis

Results are returned within a Pandas DataFrame so they are ready to use for data analysis.

Available platforms (API)

No API keys needed!

Installation

pip install git+https://github.com/martincpt/cached-candles.git

Usage

from cached_candles import CachedCandles

bitfinex_cache = CachedCandles("bitfinex")

# this will store a csv file in the cache directory 
result = bitfinex_cache.candles("btcusd", "1h", start = "2021-05-08", end = "2021-05-15")

# next time you run the same query, it will simply return the local cache
cached = bitfinex_cache.candles("btcusd", "1h", start = "2021-05-08", end = "2021-05-15")

# however, the following query will result in new set of API call(s)
# even though overlapping data parts may be found in a different cache files
new_fetch = bitfinex_cache.candles("btcusd", "1h", start = "2021-05-08", end = "2021-05-16")

Choosing the platform

You can choose the platfrom upon initialization.

# will use bitfinex
bitfinex_cache = CachedCandles("bitfinex")

# will use binance
binance_cache = CachedCandles("binance")

Continuous Cache

One of the coolest thing in this library is continuous mode, which is by default if you haven't defined the end parameter.

Continuous mode can be also used by passing "now" literal as the end parameter.

# if end is not defined, continuous mode will be used
until_now = bitfinex_cache.candles("btcusd", "1h", start = "2021-05-08")

# the same applies for passing 'now'
until_now = bitfinex_cache.candles("btcusd", "1h", start = "2021-05-08", end = "now")

Continuous mode can pick up where it left off and capable to continuously build the cache file without performing unnecessary API calls for existing datapoints.

# calling the same query 24 hours later, will pick up the changes and / or any new candles only
pickup_changes = bitfinex_cache.candles("btcusd", "1h", start = "2021-05-08", end = "now")

Date types for start and end parameters

start and end parameters accepts datetime types or any parsable datetime string which the dateutil.parser.parse library can parse.

Only end accepts "now" literal and it will use continuous mode.

from datetime import datetime

start = datetime(2022, 7, 12)
end = datetime(2022, 7, 14)

# passing datetimes works too
candles = bitfinex_cache.candles("btcusd", "1h", start = start, end = end)

Specifying the cache directory

By default, the cache directory is in the same directory as this package. However, you can specify the root to the cache directory by passing the cache_root argument and set the directory name with cache_dir.

place_cache_dir_here = "/my/custom/path"

# this will create a directory called `cache` in the path above
cached_candles = CachedCandles("bitfinex", cache_root = place_cache_dir_here)

# more over, you can use a file path as well and it will use its current directory
cached_candles = CachedCandles("bitfinex", cache_root = __file__)

# if you are unstatisfied with the name `cache`, you can customize with `cache_dir`
cached_candles = CachedCandles("bitfinex", cache_dir = "datasets", cache_root = __file__)

Clearing the cache

For now, you can clear the cache by removing the cache directory or the query specific cache file by hand.

cached_candles = CachedCandles("bitfinex")

# this will return current cache directory
cache_path = cached_candles.cached_df.path

TODO

  • Add method(s) to remove cache(s)
  • Add ability to ignore cache (force refetch)

About

Fetches crypto candles with cache. You won't hit the API twice!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages