Testing for volatility
This revision is from 2025/01/01 23:05. You can Restore it.
import requests
import pandas as pd
# Function to fetch historical price datadef fetch_historical_prices(crypto_id, vs_currency, days=1, interval='minute'):
url = f'https://api.coingecko.com/api/v3/coins/{crypto_id}/market_chart'
params = {
'vs_currency': vs_currency,
'days': days,
'interval': interval
}
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
return data['prices']
else:
print(f'Error fetching data: {response.status_code}')
return None
# Function to count 2% price changesdef count_2pct_changes(prices):
count = 0
for i in range(1, len(prices)):
previous_price = prices[i-1][1]
current_price = prices[i][1]
pct_change = ((current_price - previous_price) / previous_price) * 100
if abs(pct_change) >= 2:
count += 1
return count
# Main scriptdef main():
crypto_id = 'bitcoin' # Replace with the desired cryptocurrency
vs_currency = 'usd'
interval = 'minute' # Can be 'minute' or 'hour'
# Fetch historical prices for the last 24 hours
prices = fetch_historical_prices(crypto_id, vs_currency, days=1, interval=interval)
if prices:
# Count the number of 2% price changes
count = count_2pct_changes(prices)
print(f'Total 2% price changes in the last 24 hours: {count}')
if __name__ == main:
main()