bugfixing

This commit is contained in:
MuslemRahimi 2024-12-30 16:02:06 +01:00
parent 9db7c5308a
commit 6ac8611e3a
3 changed files with 43 additions and 19 deletions

View File

@ -88,7 +88,7 @@ def main():
historical_directory = 'json/dark-pool/historical-flow'
# Load the latest JSON file from the directory
existing_data = load_latest_json(historical_directory)
existing_data = load_latest_json(historical_directory, find=False)
existing_keys = {item.get('trackingID', None) for item in existing_data}
# Fetch new data from the API

View File

@ -1,17 +1,33 @@
import requests
from dotenv import load_dotenv
import os
import sqlite3
load_dotenv()
api_key = os.getenv('UNUSUAL_WHALES_API_KEY')
con = sqlite3.connect('stocks.db')
etf_con = sqlite3.connect('etf.db')
cursor = con.cursor()
cursor.execute("PRAGMA journal_mode = wal")
cursor.execute("SELECT DISTINCT symbol FROM stocks WHERE symbol NOT LIKE '%.%'")
stocks_symbols = [row[0] for row in cursor.fetchall()]
etf_cursor = etf_con.cursor()
etf_cursor.execute("PRAGMA journal_mode = wal")
etf_cursor.execute("SELECT DISTINCT symbol FROM etfs")
etf_symbols = [row[0] for row in etf_cursor.fetchall()]
con.close()
etf_con.close()
total_symbols = stocks_symbols[:1000] #+ etf_symbols
total_symbols = ",".join(total_symbols)
print(total_symbols)
url = "https://api.unusualwhales.com/api/screener/stocks"
querystring = {
'order': 'net_premium',
'order_direction': 'desc',
'sectors[]': 'Technology'
}
querystring = {"ticker": total_symbols}
headers = {
"Accept": "application/json, text/plain",
@ -23,6 +39,6 @@ response = requests.get(url, headers=headers, params=querystring)
data = response.json()['data']
print(data[0])
print(len(data))
print(data[-1]['ticker'])

View File

@ -32,10 +32,11 @@ def check_market_hours():
return False #"Market is closed."
def load_latest_json(directory: str):
def load_latest_json(directory: str, find=True):
"""
Load the JSON file corresponding to today's date (New York time) or the last Friday if today is a weekend.
If the file is not found, try going back one day up to 10 times.
If `find` is True, try going back one day up to 10 times until a JSON file is found.
If `find` is False, only check the current date (or adjusted Friday for weekends).
"""
try:
# Get today's date in New York timezone
@ -48,9 +49,10 @@ def load_latest_json(directory: str):
elif today_ny.weekday() == 6: # Sunday
today_ny -= timedelta(days=2)
# Attempt to find the file for up to 10 days
attempts = 0
while attempts < 10:
# Loop to find the JSON file
while True:
# Construct the filename based on the adjusted date
target_filename = f"{today_ny}.json"
target_file_path = os.path.join(directory, target_filename)
@ -58,18 +60,24 @@ def load_latest_json(directory: str):
# Check if the file exists and load it
if os.path.exists(target_file_path):
with open(target_file_path, 'rb') as file:
print(today_ny)
print(f"JSON file found for date: {today_ny}")
return orjson.loads(file.read())
# If the file is not found, go one day back
print(f"No JSON file found for date: {today_ny}. Trying the previous day...")
today_ny -= timedelta(days=1)
attempts += 1
# If find is False, only check the current date and exit
if not find:
print(f"No JSON file found for date: {today_ny}. Exiting as `find` is set to False.")
break
# If no file is found after 10 attempts, return an empty list
#print("No JSON file found after 10 attempts.")
# Increment attempts and move to the previous day
attempts += 1
if attempts >= 10:
print("No JSON file found after 10 attempts.")
break
today_ny -= timedelta(days=1)
# Return an empty list if no file is found
return []
except Exception as e:
#print(f"Error loading JSON file: {e}")
print(f"Error loading JSON file: {e}")
return []