Testing for volatility
import requests
import time
# Define parameterstimeframe = 5 # in minutes
volatility = 2 # in percent
pairs = ['BTC/USDT', 'ETH/USDT']
# Function to fetch klines data from Binance APIdef fetch_klines(symbol, interval, startTime, endTime):
url = 'https://api.binance.com/api/v3/klines'
params = {
'symbol': symbol,
'interval': interval,
'startTime': startTime,
'endTime': endTime
}
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
closing_prices = [float(candle[4]) for candle in data]
return closing_prices
else:
print(f'Error fetching data for {symbol}: {response.status_code}')
return None
# Main functiondef main():
# Calculate start and end times for 24 hours ago and now
endTime = int(time.time() * 1000)
startTime = endTime - 24 * 60 * 60 * 1000 # 24 hours in milliseconds
# Define Binance intervals based on timeframe
if timeframe < 1:
interval = '1m'
elif 1 <= timeframe < 5:
interval = '1m'
elif 5 <= timeframe < 15:
interval = '5m'
elif 15 <= timeframe < 30:
interval = '15m'
elif 30 <= timeframe < 60:
interval = '30m'
else:
interval = '1h' # Default to 1 hour if timeframe is 60 minutes or more
# Process each pair
for pair in pairs:
# Convert pair to Binance symbol
symbol = pair.replace('/', '')
# Fetch klines data
prices = fetch_klines(symbol, interval, startTime, endTime)
if prices:
count = 0
if len(prices) >= 1:
reference_price = prices[0]
for price in prices[1:]:
pct_change = ((price - reference_price) / reference_price) * 100
if abs(pct_change) >= volatility:
count += 1
reference_price = price
print(f'Total {volatility}% price changes for {pair} in the last 24 hours: {count}')
else:
print(f'No price data available for {pair}')
else:
print(f'Failed to fetch data for {pair}')
if __name__ == main:
main()