Pakistan Ka Pehla Professional Trading Bot CourseModule 3

3.3Smart Cache — API Calls Waste Mat Karo

25 min 3 code blocks Quiz (4Q)

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.

python
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.

python
# 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

Combining caching with filters ensures your bot runs efficiently without wasting API quota or slowing down decision-making.

📺 Recommended Videos & Resources

  • Python Caching Strategies — Using @lru_cache and caching decorators
    • Type: Python Official Docs
    • Link description: Learn functools.lru_cache for efficient memoization
  • Redis for Distributed Caching — Advanced caching for production
    • Type: Official Website
    • Link description: Learn Redis for scaling cache across multiple bots
  • API Rate Limiting & Caching Strategies — Handling API quotas
    • Type: YouTube
    • Link description: Search "API rate limiting Python caching strategies"
  • Time-Based Cache Invalidation — Understanding cache freshness
    • Type: Wikipedia
    • Link description: Learn cache invalidation strategies (TTL, event-based)
  • Memory Management in Python — Efficient memory usage
    • Type: Python Official Docs
    • Link description: Learn about garbage collection and memory efficiency

🎯 Mini-Challenge

5-Minute Practical Task: Create a cached version of a function that fetches market data. Use @lru_cache with maxsize=100 and a 60-second TTL (hint: use a decorator or timestamp check). Verify that repeated calls within 60 seconds return cached data without making new "API calls" (simulate API calls with print statements).

🖼️ Visual Reference

code
📊 Smart Caching vs Naive API Calls
┌──────────────────────────────────┐
│ WITHOUT CACHE (Inefficient)      │
│                                  │
│ Loop iteration 1: Fetch markets  │
│ → API CALL (#1)                  │
│ Loop iteration 2: Fetch markets  │
│ → API CALL (#2) [Duplicate!]     │
│ Loop iteration 3: Fetch markets  │
│ → API CALL (#3) [Duplicate!]     │
│                                  │
│ Total: 3 API calls for same data │
└──────────────────────────────────┘

┌──────────────────────────────────┐
│ WITH CACHE (Efficient)           │
│                                  │
│ Loop iteration 1: Fetch markets  │
│ → API CALL (#1)                  │
│ → Store in cache (TTL: 60s)      │
│ Loop iteration 2: Fetch markets  │
│ → Cache HIT (return cached data) │
│ Loop iteration 3: Fetch markets  │
│ → Cache HIT (return cached data) │
│                                  │
│ Total: 1 API call + 2 cache hits │
│ Savings: 66% fewer API calls     │
└──────────────────────────────────┘

Lesson Summary

3 runnable code examples4-question knowledge check below

Quiz: Smart Cache — API Calls Waste Mat Karo

4 questions to test your understanding. Score 60% or higher to pass.