Bismillah. Chalo bachon, course mein wapis خوش آمدید.
Assalam-o-Alaikum! Umeed hai pichla lesson samajh aa gaya hoga. Abhi tak hum ne sirf data fetch karna seekha hai. Lekin API se jo data aata hai, woh kachra aur kaam ki cheezon ka mixture hota hai. Garbage in, garbage out. Agar apne bot ko kachra khilao ge, toh woh trade bhi kachra hi karega.
Aaj ka lesson is course ki rooh (soul) hai. Hum seekhein ge ke hazaron markets mein se sirf woh "golden" candidates kaise nikalne hain jin par hamara bot actually kaam karega. Yeh bilkul waise hai jaise Karachi mein hiring hoti hai: company ke paas 200 CVs aati hain, HR un mein se 20 shortlist karta hai, manager 5 interview karta hai, aur end mein 1 larka select hota hai. Hum apne bot ke liye wohi HR aur manager banenge.
Socho, har filter ek stage hai. Har stage pe kuch markets fail ho kar bahar nikal jaati hain. Jo akhir tak bachti hain, woh hamari high-potential candidates hain.
Is approach ko "pipeline" kehte hain. Data ek taraf se enter hota hai, filters se guzarta hai, aur doosri taraf se saaf ho kar nikalta hai. Simple, powerful, aur professional tareeka hai.
Pehle, kuch helper functions aur dummy data bana lete hain taake code runnable ho. Asal mein, fetch_active_markets seedha Polymarket API se data laayega, lekin yahan hum simulation kar rahe hain.
import datetime
import time
import random
# --- Helper Functions (Yeh asal mein API calls honge) ---
def get_market_price(market):
"""
Simulates fetching the 'YES' price for a market.
In a real system, this would be part of the market data object.
"""
# Polymarket returns prices as floats between 0.0 and 1.0
return market.get('yes_price', 0.0) * 100 # Convert to percentage
def hours_to_expiry(market):
"""
Calculates hours until a market expires from its timestamp.
Polymarket provides expiry timestamps.
"""
expiry_timestamp = market.get('expiry_ts')
if not expiry_timestamp:
return float('inf')
now_ts = time.time()
seconds_diff = expiry_timestamp - now_ts
return seconds_diff / 3600
def fetch_active_markets(limit=200):
"""
MOCK FUNCTION: Simulates fetching a list of active markets.
In our actual codebase (scanner.py), this would hit the Polymarket API.
"""
print(f"Fetching {limit} mock markets from the 'API'...")
markets = []
now = time.time()
for i in range(limit):
markets.append({
'id': f'market_{i}',
'question': f'Will event {i} happen?',
# Expiry from 2 hours to 200 hours from now
'expiry_ts': now + random.uniform(2 * 3600, 200 * 3600),
# Price from 1% to 99%
'yes_price': random.uniform(0.01, 0.99),
# 24h Volume from $100 to $50,000
'volume24hr': str(random.uniform(100, 50000))
})
print("...Done.")
return markets
# --- Ab hamara lesson shuru hota hai ---
Yeh setup code copy-paste karlo. Iske neeche hum apne filters likhna shuru karenge.
Hamari theta_sniper strategy (jo hum strategies/theta_sniper.py mein banayenge) time decay pe chalti hai. Matlab hum aisi markets dhoond rahe hain jo jald expire hone wali hain.
Chalo iska filter likhte hain.
def filter_expiring_soon(markets, min_hours=24, max_hours=96):
"""Markets jo 24-96 hours mein expire ho rahe hain."""
result = []
print(f"Filtering for expiry between {min_hours}h and {max_hours}h...")
for m in markets:
h = hours_to_expiry(m)
if min_hours <= h <= max_hours:
# IMPORTANT: Hum market object ko 'decorate' kar rahe hain.
# Yeh calculated values aagey pipeline mein kaam aayengi.
m['_hours_to_expiry'] = round(h, 1)
m['_yes_price'] = get_market_price(m) # Calculate once
m['_volume_24h'] = float(m.get('volume24hr', 0)) # Clean up volume data
result.append(m)
# Expiry ke hisaab se sort karna acha practice hai.
# Is se hum sab se qareeb expire hone wali market ko pehle dekh sakte hain.
return sorted(result, key=lambda x: x['_hours_to_expiry'])
Code Breakdown:
filter_expiring_soon: Yeh function markets ki list leta hai, aur min_hours, max_hours ke parameters.for m in markets: Hum har market pe loop chala rahe hain.h = hours_to_expiry(m): Har market ke liye hum calculate kar rahe hain ke kitne ghante baaki hain.if min_hours <= h <= max_hours: Yeh hai hamara filter condition. Agar expiry hamari range mein hai, toh aage barho.m['_...'] = ...: Yeh part bohot important hai. Hum hours_to_expiry, get_market_price jaise functions ko baar baar call nahi karna chahte. Inefficient hai. Isliye, hum ek baar value calculate karke market dictionary mein hi save kar dete hain. Ise "decorating the object" kehte hain. Maine underscore _ se start kiya hai taake pata chale yeh hamare internal, calculated fields hain.return sorted(...): Hum filtered list ko return kar rahe hain, lekin expiry time ke hisaab se sort karke. Is se execution.py ko aasani hogi ke kis market ko pehle target karna hai.Ab hamare paas woh markets hain jinka time frame theek hai. Agla step hai unki probability check karna. Polymarket mein, price hi probability hoti hai. Agar "Will Pakistan win the next T20?" market ka price 70c hai, iska matlab market 70% chance de rahi hai ke Pakistan jeetega.
Hamari theta_sniper strategy high-probability events pe bet karti hai. Hum aisi markets dhoond rahe hain jinke "YES" hone ka chance bohot zyada ho.
> 60%: Is se neeche risk barh jaata hai. 50-50 wali market mein prediction mushkil hai.< 97%: Is se oopar, profit margin bohot kam reh jaata hai. 99c ki cheez khareed ke 1 dollar pe bechne ka faida nahi, fees mein hi nikal jaayega.Yeh filter likhna bohot aasan hai, especially list comprehension ke saath.
def filter_price_range(markets, min_pct=60, max_pct=97):
"""
Filters for markets where the 'YES' price is within a specific