This script is designed to assist traders and data analysts who need continuous access to fresh market candles in their database for backtesting. It fetches market candles for any given exchange, symbols, and starting date, and does so indefinitely to ensure your database is always up-to-date.
- Multiple Symbols: Users can specify as many trading pairs (symbols) as they want.
- Customizable Starting Date: You can choose any starting date for fetching the candles.
- Automatic Retry: The script automatically retries in case of a network failure.
- Selective Fetch: It only fetches candles that are not already present in the database, skipping the ones that are.
- 24-hour Sleep: Once all the candles for the specified symbols have been fetched, the script sleeps for 24 hours before fetching again.
-
Initialization: Define the
exchange
,symbols
, andstart_date
.exchange = "Binance Perpetual Futures" symbols = ['BTC-USDT', 'ETH-USDT'] start_date = "2018-06-01"
-
Fetching Function: Uses the
fetch_candles
function to fetch candles.def fetch_candles(exchange, symbol, start_date): # ... (fetching logic)
-
Error Handling: If a network issue occurs, the function will print an error message and return
False
.except requests.exceptions.ConnectionError: print("Network is down. Retrying in 5 minutes.") return False
-
Continuous Operation: The script runs indefinitely.
while True: # ... (fetching loop)
-
Retries: If fetching fails, it will wait for 5 seconds before retrying.
-
24-Hour Sleep: Once the candles for all symbols are fetched, the script will sleep for 24 hours before it runs again.
print("Completed fetching candles for all symbols. Sleeping for 24 hours.") time.sleep(86400) # Sleep for 24 hours
Ideal for traders and analysts who:
- Need up-to-date candles for backtesting.
- Do not want to manually import candles using the GUI dashboard.
- Need a resilient solution that can handle network failures.
-
Jesse: This is the only required dependency for running this script. Jesse Documentation
The script uses Jesse's research module. You can find more details here.
- Include in Jesse Project: Make sure to include this file within your existing Jesse project.
- .env File: A valid
.env
file should be present within your project folder, containing database information and other necessary configurations. - Install Jesse if you haven't already.
- Run the script.
python your_script_name.py
Feel free to modify this script as you see fit for your specific use-case.