Sirf seekhne ke liye. Yeh investment advice nahi hai aur yahan kuch bhi buy ya sell recommendation nahi hai. Trading mein paisa doob sakta hai, aur purane results future ki guarantee nahi dete. Backtest hypothetical hote hain: practice sirf paper par karo; in lessons se kabhi broker API keys connect mat karo ya live orders mat lagao. Personal advice ke liye SEBI-registered Investment Adviser se baat karo.
Is lesson ke end tak aapke paas pandas mein moving-average rule ka chalta hua backtest hoga, aap woh ek line samjhoge jo look-ahead rokti hai, aur Excel mein result haath se check kar chuke hoge.
Aapko chahiye: lesson 2 wali data/nifty50_daily.csv, aapka coding agent, Excel ya Google Sheets · Time: 10 minute
Lesson 1 wala rule lagbhag shabd-ba-shabd prompt mein jaata hai. Code agent likhta hai; aapka kaam hai rule aur check.
Prompt · ise apne AI agent mein paste karoWrite backtest.py for my rule. Read data/nifty50_daily.csv (columns Date, Close, High, Low, Open, Volume). Rule (learning only, test data): - fast = 20-day simple moving average of Close, slow = 50-day simple moving average of Close. Skip the first days where slow is empty. - signal = 1 on days when fast is above slow at the close, else 0. - position = the previous day's signal (shift by 1). A signal can only affect returns from the next day. - market return = daily % change of Close; strategy return = position x market return. - equity curves for the strategy and for buy and hold, starting at Rs 1. Print the final value of Rs 1 for both, and a check table with 5 rows around the latest day the signal changed from 0 to 1 (Close, fast, slow, signal, position). Save all rows to data/backtest_rows.csv. No costs yet (next lesson). Explain each line in simple English, then run it.
Pythonimport pandas as pd FAST, SLOW = 20, 50 # rule inputs (examples, not "best" values) df = pd.read_csv("data/nifty50_daily.csv", parse_dates=["Date"], index_col="Date") df["fast"] = df["Close"].rolling(FAST).mean() df["slow"] = df["Close"].rolling(SLOW).mean() df = df.dropna(subset=["slow"]).copy() # skip the first days with no slow average # Signal at today's close: 1 = fast above slow, 0 = not df["signal"] = (df["fast"] > df["slow"]).astype(int) # We can only act on it from the NEXT bar: shift(1) removes look-ahead df["position"] = df["signal"].shift(1).fillna(0) df["market_ret"] = df["Close"].pct_change().fillna(0) df["strategy_ret"] = df["position"] * df["market_ret"] df["equity"] = (1 + df["strategy_ret"]).cumprod() df["buy_hold"] = (1 + df["market_ret"]).cumprod() print("Final value of Rs 1, strategy :", round(df["equity"].iloc[-1], 2)) print("Final value of Rs 1, buy & hold:", round(df["buy_hold"].iloc[-1], 2)) # Check table: rows around the latest cross up, to verify in Excel first = df.index[(df["signal"].diff() == 1)][-1] i = df.index.get_loc(first) print(df.iloc[i - 2 : i + 3][["Close", "fast", "slow", "signal", "position"]].round(1)) df.to_csv("data/backtest_rows.csv")pandas ke shabd, ek-ek line mein:
rolling(20).mean(): pichhli 20 rows ka average, har row par (jaise Excel mein=AVERAGE(B2:B21)neeche drag karna).shift(1): column ko ek row neeche khiskao, taaki har din kal ki value dekhe.pct_change(): aaj ka close divided by kal ka close, minus 1.cumprod(): growth ko din-ba-din multiply karna, jisse daily returns Rs 1 ki chalti value ban jaate hain.
Kyun?pandas likhna zaroori nahi, par yeh chaar shabd pehchaanne zaroori hain. Lagbhag har backtest bug inhi mein chhupa hota hai.
Baaki lesson padhne ke liye free login karo
Har lesson free hai. Free account se saare lessons khul jaate hain aur aapki progress save hoti hai.
Is lesson mein aage:
- Signal aaj, position kal se
- Chalao aur haath se check karo
- Look-ahead apni aankhon se dekho
- Recap