Skip to content

Instantly share code, notes, and snippets.

@veridelisi
Created April 4, 2024 14:12
Show Gist options
  • Save veridelisi/6625f92724acc33b4b0121719c579b92 to your computer and use it in GitHub Desktop.
Save veridelisi/6625f92724acc33b4b0121719c579b92 to your computer and use it in GitHub Desktop.
VP.py
import requests
import json
import time
import sqlite3
# Initialize and connect to the SQLite database
conn = sqlite3.connect('btcturk_volume_data.db')
c = conn.cursor()
# Create the table if it doesn't exist
c.execute('''
CREATE TABLE IF NOT EXISTS volume_data (
date TEXT,
pair TEXT,
volume REAL,
last REAL
)
''')
# Dictionaries to store the last volume and last price for each pair
last_volumes = {}
last_prices = {}
def fetch_and_store_data():
base = "https://api.btcturk.com"
method = "/api/v2/ticker/currency?symbol=usdt"
uri = base + method
try:
response = requests.get(url=uri)
if response.status_code == 200:
data = response.json()
if 'data' in data:
for item in data['data']:
pair = item.get('pair', 'N/A')
volume = float(item.get('volume', 0)) # Convert volume to float for calculation
last = float(item.get('last', 0)) # Convert last price to float for calculation
now = time.strftime('%Y-%m-%d %H:%M:%S')
# Check for significant volume change
if pair in last_volumes:
last_volume = last_volumes[pair]
if last_volume > 0: # Prevent division by zero
volume_change_percent = ((volume - last_volume) / last_volume) * 100
if abs(volume_change_percent) > 500: # Alert for significant volume change
print(f"ALERT: {pair} volume change: {volume_change_percent:.2f}% at {now}")
# Check for significant price change
if pair in last_prices:
last_price = last_prices[pair]
if last_price > 0: # Prevent division by zero
price_change_percent = ((last - last_price) / last_price) * 100
if abs(price_change_percent) > 100: # Alert for significant price change
print(f"ALERT: {pair} price change: {price_change_percent:.2f}% at {now}")
# Update the last volume and price for the pair
last_volumes[pair] = volume
last_prices[pair] = last
# Insert the new data into the SQLite database
c.execute("INSERT INTO volume_data (date, pair, volume, last) VALUES (?, ?, ?, ?)",
(now, pair, volume, last))
conn.commit()
else:
print("Expected data not found in the response.")
else:
print(f"Failed to fetch data, status code: {response.status_code}")
except Exception as e:
print(f"An error occurred: {e}")
try:
while True:
fetch_and_store_data()
time.sleep(60) # Wait for one minute before fetching new data
except KeyboardInterrupt:
print("Data fetching stopped by user.")
finally:
conn.close() # Close the database connection before exiting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment