bugfixing
This commit is contained in:
parent
9db7c5308a
commit
6ac8611e3a
@ -88,7 +88,7 @@ def main():
|
|||||||
historical_directory = 'json/dark-pool/historical-flow'
|
historical_directory = 'json/dark-pool/historical-flow'
|
||||||
|
|
||||||
# Load the latest JSON file from the directory
|
# 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}
|
existing_keys = {item.get('trackingID', None) for item in existing_data}
|
||||||
|
|
||||||
# Fetch new data from the API
|
# Fetch new data from the API
|
||||||
|
|||||||
28
app/test.py
28
app/test.py
@ -1,17 +1,33 @@
|
|||||||
import requests
|
import requests
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import os
|
import os
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
api_key = os.getenv('UNUSUAL_WHALES_API_KEY')
|
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"
|
url = "https://api.unusualwhales.com/api/screener/stocks"
|
||||||
|
|
||||||
querystring = {
|
querystring = {"ticker": total_symbols}
|
||||||
'order': 'net_premium',
|
|
||||||
'order_direction': 'desc',
|
|
||||||
'sectors[]': 'Technology'
|
|
||||||
}
|
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"Accept": "application/json, text/plain",
|
"Accept": "application/json, text/plain",
|
||||||
@ -23,6 +39,6 @@ response = requests.get(url, headers=headers, params=querystring)
|
|||||||
data = response.json()['data']
|
data = response.json()['data']
|
||||||
|
|
||||||
|
|
||||||
print(data[0])
|
|
||||||
print(len(data))
|
print(len(data))
|
||||||
|
print(data[-1]['ticker'])
|
||||||
|
|
||||||
|
|||||||
@ -32,10 +32,11 @@ def check_market_hours():
|
|||||||
return False #"Market is closed."
|
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.
|
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:
|
try:
|
||||||
# Get today's date in New York timezone
|
# Get today's date in New York timezone
|
||||||
@ -48,9 +49,10 @@ def load_latest_json(directory: str):
|
|||||||
elif today_ny.weekday() == 6: # Sunday
|
elif today_ny.weekday() == 6: # Sunday
|
||||||
today_ny -= timedelta(days=2)
|
today_ny -= timedelta(days=2)
|
||||||
|
|
||||||
# Attempt to find the file for up to 10 days
|
|
||||||
attempts = 0
|
attempts = 0
|
||||||
while attempts < 10:
|
|
||||||
|
# Loop to find the JSON file
|
||||||
|
while True:
|
||||||
# Construct the filename based on the adjusted date
|
# Construct the filename based on the adjusted date
|
||||||
target_filename = f"{today_ny}.json"
|
target_filename = f"{today_ny}.json"
|
||||||
target_file_path = os.path.join(directory, target_filename)
|
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
|
# Check if the file exists and load it
|
||||||
if os.path.exists(target_file_path):
|
if os.path.exists(target_file_path):
|
||||||
with open(target_file_path, 'rb') as file:
|
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())
|
return orjson.loads(file.read())
|
||||||
|
|
||||||
# If the file is not found, go one day back
|
# If find is False, only check the current date and exit
|
||||||
print(f"No JSON file found for date: {today_ny}. Trying the previous day...")
|
if not find:
|
||||||
today_ny -= timedelta(days=1)
|
print(f"No JSON file found for date: {today_ny}. Exiting as `find` is set to False.")
|
||||||
attempts += 1
|
break
|
||||||
|
|
||||||
# If no file is found after 10 attempts, return an empty list
|
# Increment attempts and move to the previous day
|
||||||
#print("No JSON file found after 10 attempts.")
|
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 []
|
return []
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
#print(f"Error loading JSON file: {e}")
|
print(f"Error loading JSON file: {e}")
|
||||||
return []
|
return []
|
||||||
Loading…
x
Reference in New Issue
Block a user