Back to Curriculum

Smart Cache — API Calls Waste Mat Karo

Alright, class. Settle down. Chai waghera pee li? Chalo, let's get serious.


COURSE: Pakistan Ka Pehla Professional Trading Bot Course

MODULE 3: Market Data Pipeline — API Se Real-Time Data Kaise Fetch Karein

LESSON 3.3: Smart Cache — API Calls Waste Mat Karo

Assalam-o-Alaikum, future trading gurus. Aapka senior bhai, حاضر hai. Aaj ka topic aam nahi hai, yeh woh cheez hai jo ek professional bot ko ek student project se alag karti hai. Scene simple hai: har AI API call ke paise lagte hain. Agar aapka bot har 5 second mein Gemini ya Haiku ko call kar raha hai, toh aapka account mahine ke end tak saaf ho jayega, profit ho ya na ho.

Hum seekhenge ke smart kaise banna hai. Code ko aqalmand banana hai taake woh fazool calls na kare, aur aapke 80% tak paise bachaye. Let's dive in.


Problem Kya Hai? The Naive Approach

Pehle yeh samjho ke ghalti ho kahan rahi hai. Ek naya developer kya karta hai? Woh ek simple sa loop banata hai.

import time

def fetch_market_price(market_id):
    # Imagine this function calls an API
    # For now, we'll simulate it
    print(f"API CALL: Fetching price for {market_id}...")
    # ... API call logic here ...
    return 150.50 # Dummy price

def analyze_with_ai(price):
    # Imagine this calls Gemini/Haiku - costs money!
    print(f"AI CALL: Analyzing price {price}...")
    # ... AI analysis logic here ...
    return "HOLD"

# The "Paise Zaya Karo" Loop
while True:
    price = fetch_market_price("PSX-FFC")
    decision = analyze_with_ai(price)
    print(f"Decision for FFC: {decision}")
    time.sleep(60) # Har minute call ho rahi hai!

Upar dekho. Har 60 second mein, chahe Fauji Fertilizer ka price change ho ya na ho, humara bot API call bhi kar raha hai aur AI ko bhi bula raha hai. Agar market 4 ghante tak 150.50 pe hi atki hui hai, toh aapne 4 * 60 = 240 fazool AI calls kar deen. Agar ek AI call ka 1 ropya bhi lagta hai, toh yeh 240 rupay kachre mein gaye.

This is dumb. We need to be smart.

Solution: The Smart Cache

Cache kya hai? Cache aapke code ki short-term memory hai. Isse pehle ke hum API ya AI ko call karein, hum apni local memory (cache) se poochte hain, "Bhai, is market ka scene kya hai? Pehle dekha hai isko?"

Humara cache do cheezein store karega:

  1. Last seen price: Pichli dafa kya price tha?
  2. Timestamp: Woh price kab dekha tha?

In do cheezon se hum decide karenge ke dobara mehngi wali AI call karni hai ya nahi.

Core Logic: should_reanalyze Function

Yeh hai is lesson ka dil. Is function ko ghaur se samjho. Yeh woh gatekeeper hai jo decide karta hai ke AI ko call karna hai ya nahi.

# CODE EXPLANATION STARTS HERE
# ==============================

from datetime import datetime, timezone, timedelta

# Yeh hai humara in-memory cache. Simple Python dictionary.
# Real world projects mein, jaise hamare Polymarket Oracle mein,
# yeh thora complex ho sakta hai, but concept yehi hai.
_cache = {}

def should_reanalyze(market_id, current_price, min_hours=4, min_change=2.0):
    """
    Kya is market ko dobara analyze karna chahiye?
    
    Args:
        market_id (str): Market ka unique identifier, e.g., "KSE-100"
        current_price (float): Abhi ka taza price from data source.
        min_hours (int): Kitne ghante baad force re-analysis karna hai.
        min_change (float): Kitne percent price change pe re-analysis karna hai.
    """
    # Step 1: Check karo ke market cache mein hai bhi ya nahi.
    if market_id not in _cache:
        print(f"DEBUG: '{market_id}' not in cache. First time analysis.")
        return True  # Pehli baar dekh rahe hain, toh zaroor analyze karo.

    # Step 2: Agar cache mein hai, toh purana data nikalo.
    last = _cache[market_id]
    last_price = last['price']
    last_timestamp = last['timestamp']
    
    # Step 3: Time difference nikalo (ghanton mein).
    # Hum timezone.utc istemal kar rahe hain to avoid any timezone confusion. Hamesha UTC use karo.
    hours_since = (datetime.now(timezone.utc) - last_timestamp).total_seconds() / 3600
    
    # Step 4: Price difference nikalo (percentage mein).
    # abs() ensures ke change positive ho, chahe price upar jaye ya neeche.
    if last_price == 0: # Zero se division se bachne ke liye
        price_change_percent = float('inf') if current_price > 0 else 0.0
    else:
        price_change_percent = (abs(current_price - last_price) / last_price) * 100
    
    print(f"DEBUG: '{market_id}' | Hours since last analysis: {hours_since:.2f} | Price change: {price_change_percent:.2f}%")

    # Step 5: The Golden Rule. Asal faisla yahan hota hai.
    # Agar 4 ghante se zyada ho gaye HAIN, YA price 2% se zyada change hua HAI.
    if hours_since >= min_hours or price_change_percent >= min_change:
        print(f"DEBUG: Re-analyzing '{market_id}'. Reason: Time threshold ({hours_since:.2f}h >= {min_hours}h) OR Price threshold ({price_change_percent:.2f}% >= {min_change}%)")
        return True
    
    # Step 6: Agar upar wali condition nahi chali, iska matlab market stagnant hai.
    print(f"DEBUG: Skipping analysis for '{market_id}'. Market is stagnant.")
    return False  # Skip karo, paise bachao.

def update_cache(market_id, price):
    """
    Jab bhi hum successfully analyze karte hain, hum cache ko update karte hain.
    """
    print(f"DEBUG: Updating cache for '{market_id}' with price {price}.")
    _cache[market_id] = {
        'price': price,
        'timestamp': datetime.now(timezone.utc)
    }

# ============================
# CODE EXPLANATION ENDS HERE

Yeh Code Asal Project Mein Kahan Fit Hota Hai?

Aap log Polymarket Oracle ka codebase dekh rahe ho. Yeh logic execution.py file ke andar rehta hai.

  1. scanner.py: Iska kaam hai markets ko dhondna. Yeh Polymarket se active markets ki list uthata hai aur unka current price fetch karta hai.
  2. execution.py: scanner.py se isko market aur price milta hai. Phir yeh should_reanalyze() ko call karta hai.
  3. ai/gemini.py or ai/haiku.py: Agar should_reanalyze() kehta hai True, tab hi execution.py in AI modules ko call karta hai. Warna, skip.
  4. update_cache(): AI se analysis aane ke baad, execution.py is function ko call karke cache update kar deta hai for the next time.

Toh execution.py hamara traffic controller hai, jo is cache ki logic se fazool AI traffic ko rokta hai.

Putting It All Together: The Smart Loop

Ab dekho hamara pehle wala "