Bismillah. Chalo bhai, shuru karte hain.
Assalam-o-Alaikum, future bot masters! Last lesson mein humne project structure dekha. Aaj ek aisi cheez pe baat karenge jo ek bachay ke code aur ek professional-grade bot ke beech ka farq hai. Scene yeh hai ke jab tumhara bot live chal raha ho, tum har choti cheez ke liye code band karke, edit karke, phir se deploy nahi karna chahte. That's a recipe for disaster.
Isi liye, hum seekhenge Config-Driven Architecture. Matlab, bot ka dimaagh code mein hardcode nahi hoga, balke ek alag file se control hoga jisko hum config.yaml kehte hain. Let's get into it.
Socho tumne ek bot banaya jo Polymarket pe "Pakistan to win the next T20 match" pe bet karta hai. Tumne code mein likh diya:
# execution_hardcoded.py
def place_bet_on_pakistan_win():
bet_amount_pkr = 5000 # Hardcoded value
market_odds = 1.85 # Hardcoded value
if market_odds > 1.5:
print(f"Placing bet of {bet_amount_pkr} PKR on Pakistan to win.")
# ... actual bet placing logic ...
else:
print("Odds too low, not betting.")
place_bet_on_pakistan_win()
Ab kal ko tumne bet amount 5000 se 7000 PKR karna hai. Ya odds ki condition 1.5 se 1.6 karni hai. Tumhe kya karna paray ga?
execution_hardcoded.py file kholo.Bhai, yeh bohat lamba aur risky kaam hai. Production mein aisi harkatain nahi chalti. What if you make a typo while editing? Poora bot crash ho sakta hai.
The solution? Saari settings ko code se bahar nikaal do.
YAML (YAML Ain't Markup Language) ek simple, insaan ko samajh aanay wali data format hai. Yeh key-value pairs pe chalti hai, bilkul Python dictionaries ki tarah. Hum apne bot ki saari important settings — capital, bet size, risk management rules, strategy parameters — ek config.yaml file mein rakhenge.
Iska fayda? Settings change karni hain? Sirf text file edit karo, bot ko restart karo, and you're done. No code change needed.
Chalo, hamare Polymarket Oracle bot ki config file dekhte hain. Yeh woh file hai jo hamare scanner.py, execution.py, aur saari strategies istemal karengi.
File: config.yaml
# Global settings for the entire bot
total_eur: 30
max_per_trade_eur: 5
take_profit_pct: 94
stop_loss_move_pct: 12
alert_move_pct: 4
theta_stall_hours: 12
max_positions_per_theme: 2
# Strategy-specific settings
strategies:
theta_sniper:
enabled: true
interval_min: 120
max_allocation_pct: 25
bet_size: 5
geo_scout:
enabled: true
interval_min: 480
max_allocation_pct: 20
bet_size: 4
# Alerting and notification settings
alerts:
triggers:
trade_executed: true
position_alert: true
daily_summary: true
heartbeat: true
Chalo isko section by section torte hain:
total_eur: Tumhara total capital kitna hai.max_per_trade_eur: Ek trade mein maximum kitna paisa lagana hai. Risk management ka pehla rule.take_profit_pct: Agar market price 94 cents (ya 94%) tak pohanch jaye, position close kardo aur profit lelo.stop_loss_move_pct: Agar market price tumhare khilaf 12% move kar jaye, foran nikal jao. Loss ko control karna sab se zaroori hai.theta_sniper aur geo_scout do alag strategies hain.
enabled: Kya yeh strategy chalani hai ya nahi? false kar do, bot isko ignore kar dega.interval_min: Yeh strategy kitni der baad market scan karegi? theta_sniper har 120 minutes (2 ghantay) mein.max_allocation_pct: Total capital ka kitna percent is strategy ko assign karna hai? theta_sniper ko 25% milega.bet_size: Jab yeh strategy trade karegi, tou kitne EUR ka bet lagayegi.trade_executed: Jab bhi trade ho, alert bhejo.daily_summary: Roz raat ko summary report bhejo.Dekha? Kitna clean aur organized hai.
Ab is file ko Python mein kaise use karna hai? Iske liye humein ek library chahiye: PyYAML.
Pehle isko install karo:
pip install PyYAML
Ab woh code dekho jo maine tumhein pehle dikhaya tha. Yeh hamare bot ki main.py ya __init__.py mein sab se pehle run hoga.
import yaml
import os
# For demonstration, we'll create the config file if it doesn't exist.
# In a real project, this file will be there already.
CONFIG_TEMPLATE = """
total_eur: 30
max_per_trade_eur: 5
take_profit_pct: 94
stop_loss_move_pct: 12
alert_move_pct: 4
theta_stall_hours: 12
max_positions_per_theme: 2
strategies:
theta_sniper:
enabled: true
interval_min: 120
max_allocation_pct: 25
bet_size: 5
geo_scout:
enabled: true
interval_min: 480
max_allocation_pct: 20
bet_size: 4
alerts:
triggers:
trade_executed: true
position_alert: true
daily_summary: true
heartbeat: true
"""
# Create the config.yaml file
if not os.path.exists('config.yaml'):
with open('config.yaml', 'w') as f:
f.write(CONFIG_TEMPLATE)
# --- The Core Logic Starts Here ---
def load_config(path='config.yaml'):
"""Loads the configuration from a YAML file."""
print(f"Loading configuration from: {path}")
try:
with open(path, 'r') as f:
# Always use safe_load to prevent arbitrary code execution from a malicious YAML file.
config_data = yaml.safe_load(f)
print("Configuration loaded successfully.")
return config_data
except FileNotFoundError:
print(f"ERROR: Configuration file not found at {path}. Exiting.")
exit(1) # Exit the script if config is missing
except yaml