A Python package for fetching price data from Binance and Oanda with caching capabilities
Project description
Kline Package
A Python package for fetching price data from Binance and Oanda with flexible caching capabilities.
Features
-
🚀 Fetch Price Data from multiple sources:
- Binance (cryptocurrency exchange) - direct API integration
- Oanda (forex/CFD broker) - requires API key and account ID
-
� Real-time Streaming:
- Binance WebSocket (klines, trades, tickers, order book)
- Oanda price streaming
-
�💾 Flexible Caching:
- Parquet format (efficient, compressed)
- CSV format (human-readable)
-
🔧 Universal Format: Same symbol and timeframe format for all data sources
- Symbols:
BTCUSDT,EURUSD,XAUUSD(auto-converts for each API) - Timeframes:
1m,5m,1h,4h,1d(auto-converts for each API)
- Symbols:
Installation
pip install kline-package
Development Installation
git clone https://github.com/yourusername/kline-package.git
cd kline-package
pip install -e ".[dev]"
Quick Start
Fetching Binance Data
from kline_package.fetchers import BinanceFetcher
from kline_package.cache import ParquetCache
# Initialize fetcher
fetcher = BinanceFetcher()
# Fetch data (universal format)
data = fetcher.fetch(symbol="BTCUSDT", interval="1h", limit=100)
# Cache to parquet
cache = ParquetCache(cache_dir="./data")
cache.save(data, filename="btc_prices")
Fetching Oanda Data
from kline_package.fetchers import OandaFetcher
from kline_package.cache import CSVCache
# Initialize with credentials
fetcher = OandaFetcher(
api_key="your-api-key",
account_id="your-account-id"
)
# Fetch data (universal format - auto-converts EURUSD to EUR_USD, 1h to H1)
data = fetcher.fetch(symbol="EURUSD", interval="1h", count=100)
# Cache to CSV
cache = CSVCache(cache_dir="./data")
cache.save(data, filename="eurusd_prices")
Universal Format
Both fetchers use the same format for symbols and timeframes:
Timeframes
| Universal | Binance | Oanda |
|---|---|---|
1m |
1m | M1 |
5m |
5m | M5 |
15m |
15m | M15 |
1h |
1h | H1 |
4h |
4h | H4 |
1d |
1d | D |
1w |
1w | W |
Symbols
| Universal | Binance | Oanda |
|---|---|---|
BTCUSDT |
BTCUSDT | - |
EURUSD |
- | EUR_USD |
XAUUSD |
- | XAU_USD |
Requirements
- Python >= 3.8
- pandas >= 1.3.0
- requests >= 2.26.0
- pyarrow >= 6.0.0 (for Parquet support)
API Documentation
Fetchers
BinanceFetcher
Fetches price data from Binance cryptocurrency exchange.
Methods:
fetch(symbol, interval, limit, start_time, end_time)- Fetch kline/candlestick data
OandaFetcher
Fetches price data from Oanda forex/CFD broker.
Methods:
fetch(symbol, interval, count, from_time, to_time)- Fetch candle data (uses universal format)
Streamers
BinanceStreamer
Real-time WebSocket streaming from Binance.
Methods:
connect()- Connect to WebSocketdisconnect()- Close connectionsubscribe_kline(symbol, interval, callback)- Subscribe to kline streamsubscribe_trade(symbol, callback)- Subscribe to trade streamsubscribe_ticker(symbol, callback)- Subscribe to 24hr tickersubscribe_depth(symbol, callback)- Subscribe to order book
OandaStreamer
Real-time price streaming from Oanda.
Methods:
connect()- Initialize connectiondisconnect()- Close connectionsubscribe(symbol, callback)- Subscribe to price stream
Cache Handlers
ParquetCache
Saves and loads data in Parquet format (compressed, efficient).
Methods:
save(data, filename)- Save DataFrame to Parquetload(filename)- Load DataFrame from Parquet
CSVCache
Saves and loads data in CSV format (human-readable).
Methods:
save(data, filename)- Save DataFrame to CSVload(filename)- Load DataFrame from CSV
Real-time Streaming
Binance WebSocket Streaming
import asyncio
from kline_package.streamers import BinanceStreamer
async def on_kline(data):
print(f"Price: {data['close']}, Closed: {data['is_closed']}")
async def main():
streamer = BinanceStreamer()
await streamer.connect()
await streamer.subscribe_kline("BTCUSDT", "1m", on_kline)
await asyncio.sleep(60) # Stream for 60 seconds
await streamer.disconnect()
asyncio.run(main())
Oanda Price Streaming
import asyncio
from kline_package.streamers import OandaStreamer
async def on_price(data):
print(f"Bid: {data['bid']}, Ask: {data['ask']}")
async def main():
streamer = OandaStreamer(api_key="your-key", account_id="your-id")
await streamer.connect()
await streamer.subscribe("EURUSD", on_price)
await asyncio.sleep(60)
await streamer.disconnect()
asyncio.run(main())
Installation Options
# Basic installation (fetchers + caching)
pip install kline-package
# With streaming support
pip install kline-package[streaming]
# All features
pip install kline-package[all]
# Development
pip install kline-package[dev]
Examples
See the examples/ directory for more usage examples:
basic_binance_usage.py- Basic Binance data fetchingbasic_oanda_usage.py- Basic Oanda data fetching with authenticationadvanced_caching.py- Advanced caching strategiesstreaming_binance.py- Real-time Binance WebSocket streamingstreaming_oanda.py- Real-time Oanda price streaming
Development
Running Tests
pytest
Code Formatting
black src/
isort src/
Type Checking
mypy src/
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Disclaimer
This package is for educational and informational purposes only. Always ensure you comply with the terms of service of the APIs you're using. Trading and financial data access may be subject to regulations in your jurisdiction.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kline_package-0.1.0.tar.gz.
File metadata
- Download URL: kline_package-0.1.0.tar.gz
- Upload date:
- Size: 26.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05f6f4a3b32b5d668ec932ff5beda7b6bc64b35719772f7f61a0c2f113debe72
|
|
| MD5 |
0a7b048c547daa22141bb45745682bb0
|
|
| BLAKE2b-256 |
71ca18273ae3b0636740c4eeb6cbc09549207d0c229ca7efe8a1de1c39a5a8d5
|
File details
Details for the file kline_package-0.1.0-py3-none-any.whl.
File metadata
- Download URL: kline_package-0.1.0-py3-none-any.whl
- Upload date:
- Size: 26.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
751d94a446115e106feb11e5492230743bf07789a79902fd6229d99984a0b4da
|
|
| MD5 |
a9c9a8999fada5ef475c211d7dc95eba
|
|
| BLAKE2b-256 |
a44180f791924c33985ea907d1c1a9e3b6803bc5a43d1050ef7156618754376b
|