Javed Iqbal Securities (Pvt.) Limited
Javed Iqbal Securities (Pvt) Ltd.Company & market research / Topic 79

Quantitative Analysis with Python

Build a reproducible PSX research pipeline before searching for a statistical edge.

Advanced5 chaptersCharts & tablesReviewed 22 Oct 2026
Read this topic in Urdu ↗
Topic introduction

The big picture

Python can process thousands of observations quickly, but speed also scales mistakes. A sector ranking built from today’s surviving companies, unadjusted prices and future financial disclosures may look precise while answering the wrong question. This topic connects data lineage, return arithmetic, statistical inference and executable backtests. The goal is a research result another learner can reproduce and challenge. A historical pattern becomes a candidate hypothesis, not an automatic trading instruction or a promise of an investment advantage.

Core concepts

Data availability at decision time determines whether a test is fair.Price, total return and corporate-action adjustment are distinct conventions.A statistical estimate needs assumptions and uncertainty.Out-of-sample results and implementation costs can overturn an attractive backtest.

Learning objectives

  1. Import and audit a dated market dataset with reproducible settings.
  2. Calculate returns, volatility and a correctly specified Sharpe ratio.
  3. Prevent look-ahead, survivorship and repeated-testing bias.
  4. Evaluate a sector hypothesis after costs on untouched data.
Chapter 01

1. Create a reproducible data contract

1.1 Provenance and schema

Begin with a manifest: provider, retrieval date, permission, universe, date range, currency, frequency and price convention. Save a read-only raw file and checksum, then create cleaned data separately. Record Python and library versions and any random seed. A notebook whose final cells depend on forgotten manual edits is not reproducible. Another learner should be able to run the pipeline from raw inputs to the same outputs.

1.2 Validate before plotting

In pandas, parse dates explicitly, sort by security and trading date, and test that the security-date key is unique. Inspect missing values, impossible prices and duplicate observations before joining datasets. A missing trading day may reflect a holiday, suspension or unavailable record; filling every gap with the last price invents zero returns. Preserve the reason for exclusion. PSX portal availability does not mean every desired historical adjusted CSV or point-in-time constituent file is provided.

Step-by-step method

  1. Write a data dictionary and save the original download unchanged.
  2. Validate unique keys, dates, numeric types and missing observations.
  3. Log each cleaning decision and pin the analysis environment.
Practical application / case study

A hypothetical CSV contains two rows for one date. Dropping duplicates blindly changes the return series. The learner checks whether one row is a correction, documents the resolution and retains the rejected record in an audit log.

Chapter 02

2. Construct a return series that matches the question

2.1 Corporate actions and distributions

For a simple price return, r[t]=P[t]/P[t−1]−1. A total-return calculation also includes relevant distributions under a stated reinvestment convention. Bonus issues, splits, rights and dividends require careful treatment; adjusted prices must be understood rather than adjusted a second time. Maintain a corporate-action ledger and check unusual jumps against actual disclosures. A halved unadjusted price after a two-for-one split is not automatically a fifty-percent economic loss.

2.2 Alignment and rolling calculations

On an appropriately prepared price Series, pct_change(fill_method=None) calculates fractional change; multiply by100 only for percentage display. Compute within each security so the last price of one company never becomes another’s denominator. Align dates before combining holdings or comparing benchmarks. A rolling standard deviation measures the selected observation window, not an immutable risk characteristic. State whether sample standard deviation uses ddof=1 and how incomplete windows are handled.

Step-by-step method

  1. Choose price return or total return and document adjustments.
  2. Calculate changes within security after chronological sorting.
  3. Inspect extreme returns and verify corporate actions before removal.
Practical application / case study

A teaching series moves from100 to50 with two shares replacing one. The raw price return is−50%, but a split-adjusted holding has unchanged value before costs. The learner fixes the series convention, not the economic history.

Chapter 03

3. Measure risk and test a limited hypothesis

3.1 Volatility and Sharpe assumptions

For n comparable observations, sample volatility is the standard deviation of returns. Annualising daily volatility as daily standard deviation times √N assumes the selected scaling is appropriate; N must be a disclosed trading-period convention, not a hidden universal constant. Serial dependence or changing volatility can weaken this shortcut. Sharpe measures mean excess return divided by its standard deviation using consistent frequency, currency and risk-free assumptions. It does not describe every tail risk.

3.2 Significance after many searches

Define the hypothesis before ranking results: for example, whether a specified sector signal predicts next-period returns after a fixed lag. Estimate uncertainty and investigate sample size, dependence and extreme observations. Testing hundreds of sectors, windows and thresholds makes a lucky winner likely. Report the search space, retain failed variants and use suitable multiple-testing controls or fresh validation data. A small p-value is not a probability that the strategy will make money.

Step-by-step method

  1. Specify return frequency and matching risk-free inputs.
  2. Publish the hypothesis and number of variants investigated.
  3. Report uncertainty, drawdown and stability alongside average returns.
Practical application / case study

A learner selects the best of200 variants. Its attractive Sharpe is treated as a discovery to investigate, not evidence equivalent to one pre-specified test. The learner reserves later data to test that exact variant.

Chapter 04

4. Simulate decisions available at the time

4.1 Leakage and changing universes

A fair backtest uses information only after it became available. Financial statements belong to their publication dates, not simply the fiscal period end. Signals computed from the closing price generally cannot assume a fill at that same close without a defensible order process. Lag the position and define the next permitted execution opportunity. Historical sector research needs historical constituents, delisted companies and classification changes; today’s membership creates survivorship bias.

4.2 Execution and costs

Subtract realistic commissions, spreads, applicable charges, taxes and liquidity-sensitive slippage under explicit assumptions. Model turnover and whether the intended order size could trade in observed volume. A backtest cannot assume a buyer, a permitted product or an unrestricted broker interface merely because code can express a trade. Distinguish research calculations from live execution permissions. Test pessimistic cost and delayed-fill cases before judging whether the observed effect remains economically meaningful.

Step-by-step method

  1. Join fundamental data by actual availability date.
  2. Lag signals and reconstruct the eligible historical universe.
  3. Stress execution delays, turnover costs and capacity limits.
Practical application / case study

A hypothetical sector signal earns2.0% gross in a period. Modeled implementation costs total0.6percentage points, leaving1.4% before other effects. If doubling assumed cost erases the advantage over the benchmark, the result is fragile.

Chapter 05

5. Validate, report and monitor without rewriting history

5.1 Walk-forward discipline

Separate development, validation and a final untouched test chronologically. Fit transformations and parameters using past training data only. In walk-forward research, roll the training window forward, freeze the rule for the next segment and concatenate genuinely unseen predictions. Repeatedly checking the final test while tuning turns it into training data. Compare the strategy with a simple benchmark using the same return convention and exposure constraints.

5.2 A research report someone can challenge

Deliver the manifest, scripts, environment file, cleaning log, parameter choices, gross and net results and failure cases. Chart cumulative wealth, drawdown and turnover rather than only the best period. State unresolved data limitations, such as incomplete historical sector membership. If the original task asks for the best sector over ten years but the required records are unavailable, narrow the conclusion instead of manufacturing completeness. Paper monitoring can then test whether future behaviour resembles the original assumptions.

Step-by-step method

  1. Freeze the rule before opening the final test segment.
  2. Compare net results with a relevant baseline and stress cases.
  3. Publish limitations and set monitoring triggers for model review.
Practical application / case study

A learner’s sector rule works in development but fails in the untouched period. The final report retains that failure and concludes that the evidence does not support deployment. The code remains valuable because it made the rejection reproducible.

Visual learning

A small gross effect is sensitive to execution assumptions

Gross modeled return2 %
After0.6-point cost1.4 %
After1.2-point cost0.8 %
00.541.081.622.16
Hypothetical one-period2.0% gross return minus0.6 or1.2percentage points of implementation cost yields1.4% or0.8%. No tax or benchmark return assumed. This is arithmetic, not PSX history or a forecast.
Chart data
MeasureValue
Gross modeled return2 %
After0.6-point cost1.4 %
After1.2-point cost0.8 %

Reference table

A reproducible quantitative research audit
StageRequired check
Raw dataProvider, retrieval date, checksum and permissions
ReturnsCorporate actions, distributions and chronological keys
SignalAvailability date and execution lag
BacktestHistorical universe, turnover, costs and capacity
ValidationUntouched dates, baseline and uncertainty
Comprehensive knowledge base

Glossary of key terms

Corporate-action adjustment
Treatment of distributions and capital changes so returns match a stated convention.
Data leakage
Use of information that was unavailable when the modeled decision occurred.
Drawdown
Decline from a previous portfolio value peak.
Out-of-sample
Data not used to choose or fit the tested rule.
Sharpe ratio
Mean excess return divided by its standard deviation on a consistent basis.
Slippage
Difference between a modeled reference price and achieved execution.
Survivorship bias
Distortion caused by excluding companies that later disappeared.
Turnover
Trading activity relative to portfolio size under a stated definition.

Frequently asked questions

Does a high Sharpe guarantee profit?

No. Estimates can be unstable, biased and blind to some tail risks.

Can missing prices be forward-filled automatically?

Only with a justified rule; otherwise invented zero returns can distort risk.

Is today’s sector list enough for a ten-year ranking?

Not for an unbiased historical membership study; point-in-time coverage is needed.

Does Python provide live PSX trading permission?

No. Data tools and code do not establish broker access or regulatory permission.

Common pitfalls

Common pitfalls
Common mistakeA better approach
Using future financial statementsJoin by publication availability.
Adjusting an adjusted series twiceRead the provider’s corporate-action policy.
Selecting only the winning experimentDisclose all variants and validate separately.
Ignoring trading frictionCompare gross, net and stressed implementation results.
Apply your learning

Your learner task

Create a reproducible research folder for a clearly bounded sector question. Include a manifest, validation report, documented return convention, lagged signal, cost model, chronological split and limitations. If historical membership is incomplete, state precisely which conclusion cannot be drawn.

Sources & verification

Use the linked documents to check the factual and regulatory context. Source dates and limitations are stated where relevant. Educational examples and analytical frameworks are not personalised recommendations.

  1. JISPL · Original knowledge course ↗

    Course identity and original subtopics; expanded explanations correct dated or overconfident statements.

  2. pandas · Fractional change ↗

    Official return calculation API; check the installed version.

  3. pandas · Window functions ↗

    Rolling calculations, including standard deviation.

  4. PSX · Data Portal ↗

    Market observations and issuer disclosures; independently confirm export availability and adjustment policy.

Editorial review: 22 October 2026