6.2.5 · HinglishBacktesting Frameworks

Understand transaction cost modeling

3,088 words14 min readRead in English

6.2.5 · Stock-Market › Backtesting Frameworks

Transaction Costs Kya Hote Hain?

Transaction costs woh asli paisa hai jo tum khoте ho quoted market price se upar, trade execute karte waqt. Inke teen main components hote hain:

Har Component Kyun Exist Karta Hai

Commissions: Brokers infrastructure provide karne, orders route karne, aur regulatory compliance ke liye charge karte hain. Kyun matter karta hai: "zero-commission" brokers bhi payment-for-order-flow ke zariye paisa kamaate hain, jo aksar worse execution deta hai (hidden slippage).

Slippage: Markets tab move karte hain jab tum trade karne ka decide karte ho aur jab tumhara order fill hota hai. Kyun matter karta hai: volatile markets mein ya slow execution ke saath, slippage 0.5% per trade se bhi zyada ho sakta hai. Low liquidity mein market order midpoint se 2-3 ticks door fill ho sakta hai.

Market Impact: Bade orders prices ko tumhare against move karte hain—buying prices ko upar push karta hai, selling neeche. Kyun matter karta hai: daily volume ke relative bade positions ke liye, tumhara order visible ho jaata hai aur doosre traders adjust karte hain. 1M ki buying measurable impact create karti hai.

Total Cost Formula Derive Karna

Chalte hain transaction cost model ko first principles se build karte hain.

Step 1: Commission Model

Yeh formula kyun? Brokers minimum fee ya percentage mein se jo zyada ho woh charge karte hain. Chhote trades mein minimum hit hota hai. Bade trades mein percentage kick in karti hai.

Example:

  • Trade: 100 shares $50 par buy karo
  • Broker: $1 fixed ya 0.1% of trade value
  • Trade = 5,000
  • Percentage fee = 0.001 × 5
  • Commission = max(5) = $5

Yeh step kyun? Hum maximum lete hain kyunki broker hamesha kam se kam fixed fee toh charge karta hi hai, lekin bade trades ke liye zyada kamaane ke liye percentage par switch kar leta hai.

Step 2: Slippage Model

Yeh formula kyun?

  1. Spread component (): Market orders spread cross karte hain. Buy karte waqt tum ask par pay karte ho (mid ke bajaye), toh baseline par har share par aadha spread lose hota hai.
  2. Volatility component (): Volatile markets mein, tumhara order fill hote hote prices move kar jaati hain. yeh capture karta hai ki bade orders fill hone mein zyada time lagte hain (zyada time = zyada price drift). Yeh order execution data mein empirically observe kiya gaya hai.

Example:

  • Bid: 50.05, Spread: $0.10
  • Volatility σ: $0.20 per 5 minutes
  • Market order se 500 shares buy karo
  • α = 0.5, β = 0.1

Spread cost = 0.5 × 25** Volatility cost = 0.1 × 0.45** Total slippage ≈ $25.45

Yeh step kyun? Spread cost dominate karta hai kyunki hum bid se ask tak cross kar rahe hain. Volatility term yahan chhoti hai lekin fast markets ya bade orders mein badh jaati hai.

Step 3: Market Impact Model

Yeh formula kyun? Yeh Almgren-Chriss market impact model hai, jo empirical observations se derive hua hai:

  1. Square-root of order fraction: Impact linear nahi hota. 1% ka order 0.1% order ka 10× impact nahi karta—liquidity pools chhote orders ko better absorb karte hain. Square root diminishing marginal impact ko model karta hai.
  2. Volatility scaling: Volatile stocks mein price moves already bade hote hain, toh tumhare order ka contribution blend ho jaata hai. Calm stocks mein tumhara order zyada stand out karta hai.
  3. Order value ke proportional: Bada dollar amount = zyada impact.

Derivation intuition: Market depth ko ek aisi resource samjho jo tumhare order se "use up" hoti hai. Pehle 100 shares muskil se price move karte hain (best bid/ask par bahut saare resting orders hote hain). Agle 1,000 shares kai price levels ko kha jaate hain. Price change ki rate per share badhti hai jaise tum deeper jaate ho—lekin sublinearly (square root).

Example:

  • Stock price 100
  • Daily volume shares
  • Tumhara order shares (1% of daily volume)
  • Daily volatility (2%)
  • Impact coefficient

Yeh step kyun? Hum compute kar rahe hain ki execute karte waqt price kitni hamare against move hoti hai. Square root dikhata hai ki 1% order linear scaling se naive expectation ka sirf 10% impact karta hai.

Step 4: Total Transaction Cost

Upar ke example ko combine karte hain:

  • Commission: $5
  • Slippage: $25.45
  • Impact: $1,000
  • Total: **1,000,000 trade par
  • Cost: 0.103%

Common Mistakes Aur Kyun Woh Sahi Lagte Hain

Recall Ek 12-Saal Ke Bacche Ko Samjhaao

Socho tum school mein Pokémon cards trade kar rahe ho. Tum Charizard khareedna chahte ho.

Commission = Woh $2 jo tum card shop owner ko dete ho taaki woh tumhe apni table aur inventory system use karne do.

Slippage = Charizard 52 ka hai. Timing ki wajah se tumne $2 kho diye.

Market Impact = Tum 10 Charizard khareedna chahte ho. Pehla 51, aur uske baad wale ke liye $52 kar deta hai. Tumhare bade order ne price upar push kar diya.

Tum sochte the 10 × 500 kharcha hoga, lekin actually 2 (slippage) + 519**. Yahi extra $19 transaction costs ka matlab hai—trade actually karne ki hidden costs.

Backtesting Mein Implementation

def calculate_transaction_cost(
    side: str,  # 'buy' or 'sell'
    quantity: float,
    price: float,
    bid: float,
    ask: float,
    daily_volume: float,
    volatility: float,  # daily
    commission_fixed: float = 1.0,
    commission_rate: float = 0.0005,
    impact_coeff: float = 0.5,
    spread_fraction: float = 0.5,
    vol_sensitivity: float = 0.1
) -> float:
    """
    Calculate total transaction cost for a trade.
    Returns: Total cost in dollars (positive = cost to trader)
    """
    trade_value = price * quantity
    
    # 1. Commission
    commission = max(commission_fixed, commission_rate * trade_value)
    
    # 2. Slippage
    spread = ask - bid
    spread_cost = spread_fraction * spread * quantity
    vol_cost = vol_sensitivity * volatility * (quantity ** 0.5)
    slippage = spread_cost + vol_cost
    
    # 3. Market Impact
    order_fraction = quantity / daily_volume
    impact = impact_coeff * volatility * (order_fraction ** 0.5) * trade_value
    total_cost = commission + slippage + impact
    return total_cost

Yeh implementation kyun?

  • Teeno components transparency ke liye alag kiye gaye hain
  • Parameters calibration ke liye exposed hain (hard-code mat karo!)
  • Dollar cost return karta hai (percentage se audit karna aasaan)
  • Backtest mein har simulated trade ke liye call kiya ja sakta hai

Calibration Aur Parameter Selection

Calibrate kaise karein:

  1. Real execution data collect karo paper trading ya chhote live trades se
  2. Actual vs. expected costs compare karo apne model ka use karke
  3. Parameters adjust karo error minimize karne ke liye
  4. Conservative estimates use karo (costs round up karo) backtest overfitting se bachne ke liye

Connections

  • 6.2.01-Choose-backtesting-platform - Tumhare platform ko custom cost models support karne chahiye
  • 6.2.03-Implement-realistic-order-fills - Transaction costs fill assumptions ke saath couple hote hain
  • 6.2.04-Handle-corporate-actions-splits-dividends - Corporate actions ke aas-paas costs change hoti hain (wider spreads)
  • 6.3.02-CalculateSharpe-Sortino-ratios - Returns transaction costs ke net hone chahiye
  • 6.4.01-Paper-trade-validate-backtest - Paper trading reveal karta hai ki tumhara cost model accurate tha ya nahi
  • 5.1.03-Set-position-size-limits - Position sizing market impact costs ko affect karta hai

#flashcards/stock-market

Transaction costs ke teen components kya hain? :: Commission (broker fees), Slippage (execution ke dauran price movement), Market Impact (tumhare apne order ki wajah se price movement)

Market impact formula linear scaling ki jagah square root kyun use karta hai?
Kyunki liquidity chhote orders ko aasaani se absorb karti hai lekin bade orders ke liye deplete hoti hai. 1% ka order 0.1% order ka 10x impact nahi karta—empirically yeh follow karta hai
Agar ek backtest 2% monthly return dikhata hai lekin strategy 100 baar/month trade karti hai 0.1% cost per trade ke saath, toh real return kya hai?
Net return = 2% - (100 trades × 0.1%) = 2% - 10% = -8% per month. Strategy costs ke baad actually paisa kho rahi hai.
Backtest execution ke liye midpoint prices use karna kyun galat hai?
Kyunki market orders midpoint par execute nahi hote—tum ask par buy karte ho aur bid par sell karte ho. Midpoint use karna spread cost ignore karta hai, jo har trade par guaranteed loss hai.
Ek stock ka daily volume 500k worth khareedna chahte ho. Kya market impact significant hai?
Haan. 10M = daily volume ka 5%. Is size par, square-root law measurable impact predict karta hai: ek 1% baseline ke relative. Tum price noticeably move karoge.
High-frequency strategies successful backtests ke baad live trading mein sabse zyada kyun fail hoti hain?
Kyunki woh bahut saari trades generate karti hain jisme per trade edge chhoti hoti hai. Agar tumhara edge 8/trade hain, toh tum apna 80% profit kho dete ho. Costs ignore karne wale backtests profitability ko dramatically overestimate karte hain.

Concept Map

component 1

component 2

component 3

max of fixed or rate

spread and volatility

large orders move price

sum into

sum into

sum into

reduces

if ignored

Transaction Costs

Commission Fees

Slippage

Market Impact

C comm = max Cfixed, Crate x P x Q

C slip formula

Order Size vs Daily Volume

Total Cost per Trade

Real Profit vs Paper Profit

Backtest Disappointment