bugfixing

This commit is contained in:
MuslemRahimi 2024-12-27 22:18:50 +01:00
parent 2831b0916c
commit 37dd342e22
10 changed files with 92 additions and 45 deletions

3
.gitignore vendored
View File

@ -16,4 +16,5 @@ fastify/node_modules
pocketbase/*
helper.txt
env/*
app/ml_models/weights
app/ml_models/weights
app/utils/__pycache__

View File

@ -75,7 +75,6 @@ def save_to_daily_file(data, directory):
def get_data():
try:
response = requests.get(url, headers=headers, params=querystring)

View File

@ -53,6 +53,15 @@ def calculate_neutral_premium(data_item):
return safe_round(neutral_premium)
def generate_time_intervals(start_time, end_time):
"""Generate 1-minute intervals from start_time to end_time."""
intervals = []
current_time = start_time
while current_time <= end_time:
intervals.append(current_time.strftime('%Y-%m-%d %H:%M:%S'))
current_time += timedelta(minutes=1)
return intervals
def get_sector_data():
try:
url = "https://api.unusualwhales.com/api/market/sector-etfs"
@ -60,6 +69,7 @@ def get_sector_data():
data = response.json().get('data', [])
res_list = []
processed_data = []
for item in data:
symbol = item['ticker']
@ -98,6 +108,13 @@ def get_sector_data():
new_item['price'] = round(quote_data.get('price', 0), 2)
new_item['changesPercentage'] = round(quote_data.get('changesPercentage', 0), 2)
#get prem tick data:
if symbol != 'SPY':
prem_tick_history = get_net_prem_ticks(symbol)
#if symbol == 'XLB':
# print(prem_tick_history[10])
new_item['premTickHistory'] = prem_tick_history
processed_data.append(new_item)
return processed_data
@ -105,22 +122,12 @@ def get_sector_data():
print(e)
return []
def generate_time_intervals(start_time, end_time):
"""Generate 1-minute intervals from start_time to end_time."""
intervals = []
current_time = start_time
while current_time <= end_time:
intervals.append(current_time.strftime('%Y-%m-%d %H:%M:%S'))
current_time += timedelta(minutes=1)
return intervals
def get_net_prem_ticks(symbol):
# Fetch data from the API
url = f"https://api.unusualwhales.com/api/stock/{symbol}/net-prem-ticks"
response = requests.get(url, headers=headers)
data = response.json().get('data', [])
print(data[0])
# Sort data by date in descending order
data = sorted(data, key=lambda x: datetime.fromisoformat(x['date'].replace('Z', '+00:00')), reverse=True)
@ -144,22 +151,41 @@ def get_net_prem_ticks(symbol):
# Create a dictionary for fast lookups of existing tape_time
data_dict = {entry['tape_time']: entry for entry in data}
# Populate data with 1-minute intervals
populated_data = []
# Initialize aggregated data with cumulative sums
aggregated_data = {time: {
'net_call_premium': 0,
'net_put_premium': 0,
'net_call_volume': 0,
'net_put_volume': 0,
'tape_time': time,
'close': None
} for time in intervals}
# Variable to track cumulative sums
cumulative_net_call_premium = 0
cumulative_net_put_premium = 0
cumulative_net_call_volume = 0
cumulative_net_put_volume = 0
# Aggregate data for each minute, cumulatively adding values
for time in intervals:
if time in data_dict:
populated_data.append(data_dict[time])
else:
populated_data.append({
'date': time.split(' ')[0],
'net_call_premium': None,
'net_call_volume': None,
'net_put_premium': None,
'net_put_volume': None,
'tape_time': time,
'close': None
})
entry = data_dict[time]
# Add current values to cumulative sums
cumulative_net_call_premium += float(entry.get('net_call_premium', 0))
cumulative_net_put_premium += float(entry.get('net_put_premium', 0))
cumulative_net_call_volume += float(entry.get('net_call_volume', 0))
cumulative_net_put_volume += float(entry.get('net_put_volume', 0))
# Set the aggregated values for this minute
aggregated_data[time]['net_call_premium'] = cumulative_net_call_premium
aggregated_data[time]['net_put_premium'] = cumulative_net_put_premium
aggregated_data[time]['net_call_volume'] = cumulative_net_call_volume
aggregated_data[time]['net_put_volume'] = cumulative_net_put_volume
# Populate data with aggregated results
populated_data = list(aggregated_data.values())
# Add 'close' values if matches found in price_list
matched = False
for entry in populated_data:
@ -168,18 +194,17 @@ def get_net_prem_ticks(symbol):
entry['close'] = price['close']
matched = True
break # Exit inner loop once a match is found
# Return the populated data if matches exist; otherwise, return an empty list
print(populated_data)
return populated_data if matched else []
def main():
#sector_data = get_sector_data()
sector_data = []
net_premium_tick_data = get_net_prem_ticks('XLC')
'''
sector_data = get_sector_data()
if len(sector_data) > 0:
save_json(sector_data)
'''
get_net_prem_ticks('XLB')
if __name__ == '__main__':
main()

View File

@ -175,9 +175,11 @@ for item in searchbar_data:
# Look up the symbol in the stock_screener_data_dict
symbol = item['symbol']
item['isin'] = stock_screener_data_dict[symbol]['isin']
except:
except Exception as e:
item['isin'] = None
etf_set, crypto_set = set(etf_symbols), set(crypto_symbols)
@ -1841,7 +1843,7 @@ async def get_stock(
return JSONResponse(content=[])
# Check for exact ISIN match first
exact_match = next((item for item in searchbar_data if item.get("isin") == query), None)
exact_match = next((item for item in searchbar_data if item.get("isin",None) == query), None)
if exact_match:
return JSONResponse(content=[exact_match])

View File

@ -5,16 +5,36 @@ import os
load_dotenv()
api_key = os.getenv('UNUSUAL_WHALES_API_KEY')
querystring = {"limit":"200"}
url = "https://api.unusualwhales.com/api/darkpool/recent"
url = 'https://api.unusualwhales.com/api/stock/XLB/net-prem-ticks'
headers = {
"Accept": "application/json, text/plain",
"Authorization": api_key
'Accept': 'application/json',
'Authorization': api_key
}
response = requests.get(url, headers=headers, params=querystring)
response = requests.get(url, headers=headers)
data = response.json()['data']
print(len(response.json()['data']))
print(response.json()['data'][0])
fields_to_sum = [
"net_call_premium",
"net_call_volume",
"net_put_premium",
"net_put_volume"
]
result = []
for idx, e in enumerate(data):
e['net_call_premium'] = float(e['net_call_premium'])
e['net_put_premium'] = float(e['net_put_premium'])
#e['net_call_volume'] = float(e['net_call_volume'])
#e['net_put_volume'] = float(e['net_put_volume'])
if idx != 0:
for field in fields_to_sum:
e[field] += result[idx-1].get(field, 0)
result.append(e)
#print(result)
print(result[-1]['net_put_volume']*result[-1]['net_put_premium']*10**(-6))