-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbazaarFetch.py
More file actions
259 lines (213 loc) · 8.56 KB
/
bazaarFetch.py
File metadata and controls
259 lines (213 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import requests
import json
import time
import statistics
import os
from dotenv import load_dotenv
# Load API key from .env file
load_dotenv()
_API_KEY = os.getenv("API_KEY", "")
# --- Cache for API data so we don't spam the endpoint ---
_cache = {
'data': None,
'timestamp': 0,
'ttl': 30 # seconds before cache expires
}
def get_bazaar_data():
"""Fetches full bazaar JSON. Uses an in-memory cache (30s TTL)."""
now = time.time()
if _cache['data'] and (now - _cache['timestamp']) < _cache['ttl']:
return _cache['data']
url = "https://api.hypixel.net/v2/skyblock/bazaar"
# Send API key as header to avoid rate-limit / IP ban
headers = {}
if _API_KEY:
headers['API-Key'] = _API_KEY
# Retry up to 3 times if the API returns empty/invalid JSON
for attempt in range(3):
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status() # Raise on HTTP errors (4xx/5xx)
data = response.json()
# Update cache
_cache['data'] = data
_cache['timestamp'] = now
return data
except (requests.RequestException, json.JSONDecodeError) as e:
if attempt < 2:
time.sleep(1) # Wait 1s before retry
else:
# All retries failed — return error dict
return {'success': False, 'cause': str(e)}
def invalidate_cache():
"""Force next fetch to hit the API."""
_cache['data'] = None
_cache['timestamp'] = 0
def save_bazaar_json():
with open("bazaar.json", "w") as f:
json.dump(get_bazaar_data(), f)
# ---- Single-product helpers (use cached data) ----
def get_product(product_id):
data = get_bazaar_data()
if not data.get('success'):
print(f"Error fetching bazaar data: {data.get('cause', 'Unknown error')}")
return None
return data['products'].get(product_id)
def get_buy_summary(product_id):
product = get_product(product_id)
if product:
return product.get('buy_summary', [])
return []
def get_sell_summary(product_id):
product = get_product(product_id)
if product:
return product.get('sell_summary', [])
return []
def get_quick_status(product_id):
product = get_product(product_id)
if product:
return product.get('quick_status', {})
return {}
def getPrice(product_id, type='buy'):
"""
Returns the best price.
type='buy' returns the lowest price people are selling for (instant buy price).
type='sell' returns the highest price people are buying for (instant sell price).
"""
quick_status = get_quick_status(product_id)
if not quick_status:
return 0
if type == 'buy':
return quick_status.get('buyPrice', 0)
elif type == 'sell':
return quick_status.get('sellPrice', 0)
return 0
# ---- Bulk data extraction ----
def get_all_products():
"""Returns the full products dict from the API (cached)."""
data = get_bazaar_data()
if not data.get('success'):
return {}
return data.get('products', {})
def compute_price_stability(product):
"""
Score 0–100. Measures how tightly clustered the top sell orders are.
100 = all orders at the same price (very stable).
0 = huge variance in the order book (volatile).
Uses coefficient of variation of the top sell order prices.
"""
sell_summary = product.get('sell_summary', [])
if len(sell_summary) < 2:
return 50 # not enough data, neutral score
prices = [order['pricePerUnit'] for order in sell_summary[:10]]
mean = statistics.mean(prices)
if mean == 0:
return 50
stdev = statistics.stdev(prices)
# Coefficient of variation (lower = more stable)
cv = stdev / mean
# Map CV to a 0-100 score: CV of 0 -> 100, CV of 0.5+ -> 0
score = max(0, min(100, int((1 - cv / 0.5) * 100)))
return score
def compute_volume_stability(quick_status):
"""
Score 0–100. Compares current volume to weekly moving average.
100 = volume today matches the weekly average perfectly.
0 = massive deviation from weekly average.
"""
sell_vol = quick_status.get('sellVolume', 0)
buy_vol = quick_status.get('buyVolume', 0)
sell_week = quick_status.get('sellMovingWeek', 0)
buy_week = quick_status.get('buyMovingWeek', 0)
# Weekly average per-day (divide by 7)
sell_daily_avg = sell_week / 7 if sell_week > 0 else 0
buy_daily_avg = buy_week / 7 if buy_week > 0 else 0
if sell_daily_avg == 0 and buy_daily_avg == 0:
return 50 # no data, neutral
# Ratio of current volume to daily average (closer to 1.0 = stable)
ratios = []
if sell_daily_avg > 0:
ratios.append(sell_vol / sell_daily_avg)
if buy_daily_avg > 0:
ratios.append(buy_vol / buy_daily_avg)
avg_ratio = statistics.mean(ratios) if ratios else 1.0
# Deviation from 1.0 (perfect match)
deviation = abs(avg_ratio - 1.0)
# Map deviation to score: 0 deviation -> 100, 2.0+ deviation -> 0
score = max(0, min(100, int((1 - deviation / 2.0) * 100)))
return score
def detect_spike(product):
"""
Detects if the current margin is caused by a price spike.
Returns (is_spike: bool, confidence: float 0-1).
A spike is when few orders at extreme prices are inflating the margin.
"""
buy_summary = product.get('buy_summary', [])
sell_summary = product.get('sell_summary', [])
quick_status = product.get('quick_status', {})
buy_price = quick_status.get('buyPrice', 0)
sell_price = quick_status.get('sellPrice', 0)
if buy_price == 0 or sell_price == 0:
return False, 0.0
margin = buy_price - sell_price
# If margin is tiny or negative, no spike
if margin <= 0 or sell_price == 0:
return False, 0.0
margin_pct = (margin / sell_price) * 100
# Check if the top buy order is an outlier (very few orders, high price)
if len(buy_summary) >= 2:
top_buy = buy_summary[0]
second_buy = buy_summary[1]
# If the top buy order has very few items AND its price is much
# higher than the second order, it's likely a spike
price_jump = (top_buy['pricePerUnit'] - second_buy['pricePerUnit'])
price_jump_pct = (price_jump / second_buy['pricePerUnit'] * 100) if second_buy['pricePerUnit'] > 0 else 0
if top_buy['orders'] <= 2 and price_jump_pct > 20 and top_buy['amount'] < 1000:
confidence = min(1.0, price_jump_pct / 100)
return True, confidence
# Also check sell side for manipulation
if len(sell_summary) >= 2:
top_sell = sell_summary[0]
second_sell = sell_summary[1]
price_drop = (second_sell['pricePerUnit'] - top_sell['pricePerUnit'])
price_drop_pct = (price_drop / top_sell['pricePerUnit'] * 100) if top_sell['pricePerUnit'] > 0 else 0
if top_sell['orders'] <= 2 and price_drop_pct > 20 and top_sell['amount'] < 1000:
confidence = min(1.0, price_drop_pct / 100)
return True, confidence
return False, 0.0
def get_all_items_summary():
"""
Returns a list of dicts with computed data for every product.
Each dict has: product_id, buy_price, sell_price, margin, margin_percent,
buy_volume, sell_volume, buy_orders, sell_orders, buy_moving_week,
sell_moving_week, price_stability, volume_stability, is_spike, spike_confidence.
"""
products = get_all_products()
items = []
for pid, product in products.items():
qs = product.get('quick_status', {})
buy_price = qs.get('buyPrice', 0)
sell_price = qs.get('sellPrice', 0)
margin = buy_price - sell_price
margin_pct = (margin / sell_price * 100) if sell_price > 0 else 0
price_stab = compute_price_stability(product)
vol_stab = compute_volume_stability(qs)
is_spike, spike_conf = detect_spike(product)
items.append({
'product_id': pid,
'buy_price': round(buy_price, 2),
'sell_price': round(sell_price, 2),
'margin': round(margin, 2),
'margin_percent': round(margin_pct, 2),
'buy_volume': qs.get('buyVolume', 0),
'sell_volume': qs.get('sellVolume', 0),
'buy_orders': qs.get('buyOrders', 0),
'sell_orders': qs.get('sellOrders', 0),
'buy_moving_week': qs.get('buyMovingWeek', 0),
'sell_moving_week': qs.get('sellMovingWeek', 0),
'price_stability': price_stab,
'volume_stability': vol_stab,
'is_spike': is_spike,
'spike_confidence': round(spike_conf, 2),
})
return items