Assalam-o-alaikum, traders! Umeed hai pichle lessons clear honge.
Yeh lesson iss module ka sab se important lesson hai. Kyun? Kyun ke aaj hum woh exact data pipeline banayenge jo mera Polymarket Oracle, theta_sniper, live production mein use karta hai. No theory, no BS, seedha code jo aap copy-paste karke apne bot mein daal sakte hain. Yahi woh engine hai jo kachra markets ko filter karke sirf sone ke danay (golden opportunities) aapke AI ke saamne rakhta hai.
Ab tak hum ne 3 alag alag cheezein seekhi hain:
scanner.py): API se saari active markets ka data uthana.db.py): Jin markets ko pehle analyze kar chuke hain, unko baar baar check na karna taake API calls aur AI ka paisa bache.Aaj hum in teeno cheezon ko ek single, powerful function mein jor denge: get_candidates(). Yeh function aapke bot ka dil (heart) hai. Isne aacha kaam kiya, toh bot paisa banayega. Isne ghalat markets utha li, toh AI bhi confuse ho jayega aur aapka loss hoga.
get_candidates() FunctionChalo bhai, code dekhte hain. Yeh function hamare strategies/theta_sniper.py module ka core hissa hai. Iska maqsad hai hazaron markets mein se sirf woh 10-15 "best" candidates nikalna jin par hamara AI apna dimagh lagaye.
Yeh raha woh function jisko hum abhi line-by-line samjhenge.
# Yeh function aapke bot ki "ankhain" aur "dimaagh" ka pehla hissa hai.
# It finds the best markets to analyze.
def get_candidates(max_hours=96, min_price=60, max_price=97, min_vol=3000):
"""Complete pipeline: Fetch → Filter → Cache → Score → Rank"""
# 1. Fetch
markets = fetch_active_markets(200)
print(f'[PIPELINE] Fetched {len(markets)} markets')
# 2. Filter
expiring = filter_expiring_soon(markets, 24, max_hours)
priced = filter_price_range(expiring, min_price, max_price)
liquid = filter_volume(priced, min_vol)
# 3. Cache check
fresh = []
for m in liquid:
mid = m.get('conditionId', m.get('id'))
if should_reanalyze(mid, m['_yes_price']):
fresh.append(m)
update_cache(mid, m['_yes_price'])
# 4. Sort by soonest expiry (fastest capital rotation)
fresh.sort(key=lambda x: x['_hours_to_expiry'])
print(f'[PIPELINE] {len(markets)} → {len(liquid)} liquid → {len(fresh)} fresh candidates')
return fresh[:15] # Top 15 candidates for AI analysis
Dekhne mein simple lag raha hai, lekin iske har step ke peeche ek solid logic hai. Let's break it down.
# 1. Fetch
markets = fetch_active_markets(200)
print(f'[PIPELINE] Fetched {len(markets)} markets')
Yeh line seedha hamare scanner.py mein likhe hue function ko call karti hai. Hum Polymarket API ko kehte hain, "Bhai, jitni bhi active markets hain, un mein se top 200 (by volume) utha ke le aao."
fetch_active_markets(200): Yeh function API call maarta hai. 200 ek aacha number hai. Is se kam loge toh shayad aachi opportunity miss ho jaye. Is se zyada loge toh agle steps mein fuzool data process karne mein time lagega.print(): Apne bot mein logging daalna bohot zaroori hai. Aapko har step pe pata hona chahiye ke kya ho raha hai. Yahan hum print kar rahe hain ke total kitni markets uthayi hain.Ab hamare paas 200 markets ka data hai. In mein se 95% hamare liye bekaar hain. Koi market 6 mahine baad expire hogi, kisi mein 2 dollar ka volume hai, kisi ka price 1 cent hai. Aisi markets pe AI ka time aur paisa zaya karna bewakoofi hai.
Toh hum ek "filter chain" chalate hain. Output of first filter becomes input of the second.
# 2. Filter
expiring = filter_expiring_soon(markets, 24, max_hours)
priced = filter_price_range(expiring, min_price, max_price)
liquid = filter_volume(priced, min_vol)
filter_expiring_soon(markets, 24, max_hours):
filter_price_range(expiring, min_price, max_price):
filter_volume(priced, min_vol):
Is step ke baad, 200 markets shayad 20-30 reh jayengi. Yeh hain hamari "potential" opportunities.
💡 Pro Tip: Dynamic Filtering
Bhai, yeh jo
min_price,max_price,min_volke numbers hain na, yeh pathar pe lakeer nahi hain. Main apne production bot mein inko market conditions ke hisaab se adjust karta hoon. For example, agar market bohot volatile hai (jaise election ke din), toh mainmin_priceko 70c kar deta hoon aurmax_priceko 95c. Is se risk kam hojata hai. Yeh advanced concept hai, lekin abhi se dimaagh mein rakho. Apne bot ko itna smart banao ke woh environment ke hisaab se apne parameters khud change kar sake.
Ab jo 20-30 markets bachi hain, ho sakta hai in mein se 15 ko hamare bot ne 5 minute pehle hi analyze kiya ho. Agar market price mein koi khaas tabdeeli nahi aayi, toh us par dobara AI (Gemini/Haiku) ki expensive API call maarna fazool hai. Yahan hamara cache, yaani db.py ka system, kaam aata hai.
# 3. Cache check
fresh = []
for m in liquid:
mid = m.get('conditionId', m.get('id'))
if should_reanalyze(mid, m['_yes_price']):
fresh.append(m)
update_cache(mid, m['_yes_price'])
should_reanalyze() function call karte hain.should_reanalyze(market_id, current_price): Yeh function database mein check karta hai: