API Reference
Cointables provides the Chart class for extracting historical Open-High-Low-Close (OHLC) data for various cryptocurrencies from Binance.
- class cointables.Chart(client, coin='BTC', market='USDT', candles='30m')
- __init__(client, coin='BTC', market='USDT', candles='30m')
Chart class. Handler for the creation of financial charts for cryptocurrencies and analysis thereof
Parameters
- client<binance.client.Client object>
Binance API client object with API key and secret set
- coinstr
Ticker name of quote currency (default: ‘BTC’)
- marketstr
Ticker name of base currency (default: ‘USDT’)
- candlesstr
Time interval for candles (default: ‘30m’) Valid intervals are: ‘1m’, ‘3m’, ‘5m’, ‘15m’, ‘30m’, ‘1h’, ‘2h’, ‘4h’, ‘6h’, ‘8h’, ‘12h’, ‘1d’, ‘3d’, ‘1w’, ‘1M’
Attributes
- dataframe<pandas.DataFrame object>
A DataFrame object containing the financial data of price activity loaded from above information.
- messagestr
Any message from an external function to store on Chart class (for easy migration across modules).
- sampled_epochint or None
The epoch timestamp (either randomly sampled or set by user) see: get_data_random method.
- coinGET(time_diff=2419200000, num_candles=500)
Returns OHLC data of the quote cryptocurrency with the base currency (i.e., ‘market’). Note: The base currency for alts must be either USDT or BTC.
Parameters
- time_diffint, optional
An integer representing the time difference in seconds between the current time and the starting time of the historical data to retrieve. The default value is 2419200 seconds, which is equivalent to 28 days.
- num_candlesint, optional
An integer representing the number of historical candles to retrieve. The default value is 500. If this parameter is provided, it takes priority over the time_diff parameter.
- coinGET_custom(GET_METHOD)
Returns OHLC data of the quote cryptocurrency with the base currency (i.e., market) for a custom GET_METHOD. Note: The base currency for alts must be either USDT or BTC.
Parameters
- GET_METHODfunction
A custom GET_METHOD that returns the OHLC data for a specific cryptocurrency market.
Example
>>> def get_btc_data(): >>> # Some code that fetches and returns OHLC data for the BTC/USDT market. >>> return btc_data >>> >>> chart = Chart('BTC', 'USDT', '1d') >>> btc_df = chart.coinGET_custom(get_btc_data)
- coinGET_random(sampled_time=None, num_candles=500, time_diff=2419200000)
Returns OHLC data of the quote cryptocurrency with the base currency (i.e., ‘market’) in a randomly chosen time window with a chart time frame defined by the time difference between the sampled epoch timestamp and the end time, where the chart time frame is set to time_diff.
Parameters
- sampled_timefloat
Uses the given epoch timestamp to fetch data. The default value is np.random.uniform(1500000000, 1665308894). It samples a timestamp randomly between Thursday, July 13, 2017 8:26:40 AM GMT (1500000000) and Friday, September 9, 2022 8:54:54 AM GMT (1665308894).
- num_candlesint, optional
The number of candles to sample for the full time frame. Default: 500 candles (If None, samples the full time frame using the given time_diff value).
- time_diffint, optional
The size of the chart time frame between the specified or randomly sampled epoch timestamp and the end time, in milliseconds. The default value is 28 days (2419200000 milliseconds). time_diff will activate if num_candles is set to None
- get_data(time_diff=2419200000, num_candles=500)
Retrieve historical market data from the Binance API and return it as a Pandas DataFrame object.
Parameters
- time_diffint, optional
An integer representing the time difference in milliseconds between the current time and the starting time of the historical data to retrieve. The default value is 2419200 seconds, which is equivalent to 28 days.
- num_candlesint
An integer representing the number of historical candles to retrieve. The default value is 500. If this parameter is provided, it takes priority over the time_diff parameter.
- get_data_random(sampled_time=None, num_candles=500, time_diff=2419200000)
Fetches historical market data for a randomly sampled epoch timestamp or a specified timestamp within the range of Thursday, July 13, 2017 8:26:40 AM GMT (1500000000) and Friday, September 9, 2022 8:54:54 AM GMT (1665308894), with a chart time frame defined by the time difference between the sampled epoch timestamp and the end time.
Parameters:
- sampled_timefloat, optional
The epoch timestamp to use as the starting point for fetching data. If None, a random timestamp is sampled from the range between 1500000000 and 1665308894. Default is None.
- num_candlesint, optional
The number of candles to sample for the specified or randomly sampled epoch timestamp. If None, the full time frame using the given time_diff value will be used. Default is 500. NOTE: This parameter takes priority over time_diff
- time_diffint, optional
The size of the chart time frame between the specified or randomly sampled epoch timestamp and the end time, in milliseconds. Default is 2419200000 (28 days). NOTE: num_candles must be set to None to activate this parameter.
Returns:
- pandas.DataFrame
A dataframe containing the OHLCV data for the specified or randomly sampled epoch timestamp.