Python for Trading
Learn to code from scratch and put Python to work on real market data
A plain-English, India-first course that teaches a complete beginner to program in Python and then use it to fetch, analyse, and chart real market data. Start from installing Python and writing your first line of code, build up through the core language, then learn the data tools every quantitative trader relies on, NumPy, pandas, and plotting, on real Indian market data. Finish by computing returns, moving averages, and volatility, building a simple signal, and backtesting it honestly, with clear eyes about the traps that make a backtest lie. The first rung of the coding ladder, leading into Algorithmic Trading.
Getting started with Python
The goal of Part 1 is to take someone who has never written a line of code and get them comfortably running small programs, with the environment set up and the absolute basics in hand.
- 1What coding lets you doPython lets you do in seconds, and repeat without error, the data work a trader would otherwise do by hand: fetching prices, computing returns, testing ideas. This chapter shows what you will build and sets honest expectations about what coding can and cannot give you. 8 min
- 2Your workbenchBefore writing code you need Python installed and a place to run it, and this chapter walks through setting up a free, beginner-friendly environment and running a first snippet, so the tools never become the obstacle. 7 min
- 3Making the computer speakYour first program prints a message, and in doing so teaches how code runs, how output appears, and how to read the errors that will be your constant companions. Starting tiny makes the whole thing un-scary. 7 min
- 4Storing a valueA variable is a named box that holds a value, and values come in types, numbers, text, and true-or-false, that behave differently. These are the nouns of the language. 7 min
- 5Calculating with codePython is a calculator that never tires, and this chapter uses its arithmetic to compute something a trader cares about, a trade's profit and return, introducing operators and the order they apply in. 8 min
The core of the language
Part 2 covers the essential building blocks every Python program uses, taught with market-flavoured examples so the skills land in context rather than in the abstract.
- 6Working with wordsTickers, dates, and labels are text, and Python has rich tools for building and formatting strings, above all the f-string, which you will use constantly to report results cleanly. 7 min
- 7A sequence of thingsA list holds an ordered sequence, a run of closing prices, a set of tickers, and lets you add to it, read from it, and slice pieces out. It is the first container, and the workhorse of early programs. 7 min
- 8Looking things up by nameA dictionary maps a key to a value, a ticker to its price, a name to a number, so you can look things up by name rather than position. It is how you model labelled data before pandas arrives. 7 min
- 9Making decisions in codeAn if statement lets a program choose what to do based on a condition, the foundation of any rule, such as flagging a day when the price crossed a level. This is where code starts to make decisions. 8 min
- 10Doing something many timesA loop repeats an action over a sequence, letting you process every price in a list or every row of data without writing the same line a thousand times. Repetition without error is where code beats hand-work. 8 min
- 11Packaging logic to reuseA function is a named, reusable piece of logic that takes inputs and returns a result, so you write the calculation once, a return, say, and call it everywhere. Functions are how programs stay organised as they grow. 8 min
- 12When things go wrongReal data is messy and code breaks, so you learn to anticipate failure with try and except, handling a missing value or a bad input gracefully rather than crashing. This is the habit that separates fragile scripts from dependable ones. 7 min
Working with data
Part 3 is the turning point, where the course moves from plain Python to the data tools that make it worth a trader's time: the package ecosystem, NumPy, pandas, and getting real market data in.
- 13Standing on others' workPython's real strength is the vast collection of packages others have written, installed with a package manager and brought in with import, so you rarely build from scratch. This chapter explains the ecosystem you are about to lean on. 7 min
- 14Fast math on arraysNumPy gives Python the fast numerical array, letting you do arithmetic on a whole series of prices at once, vectorised, instead of looping. It is the engine under pandas and the right tool for number-heavy work. 7 min
- 15The trader's spreadsheet in codepandas is the heart of data work in Python, giving you the Series and the DataFrame, a labelled column and a labelled table, which behave like a programmable spreadsheet built for time series like prices. 8 min
- 16Reading a file of pricesMost market data starts life as a CSV file, and pandas reads one into a DataFrame in a single line, giving you a table of dates and prices to work with. This is the most common way data enters an analysis. 7 min
- 17Getting live and historical pricesBeyond files, you can fetch real market data programmatically, historical OHLCV for an Indian stock or index, so your analysis runs on genuine prices. This chapter pulls real data in, through a broker's SDK or an open library. 8 min
- 18Trusting your data firstBefore computing anything, you inspect and clean the data, checking its size, its types, and its missing values, because a wrong result from dirty data is worse than no result. Real prices have gaps, splits, and errors. 7 min
- 19Asking questions of the dataThe power of pandas is asking questions of a table, selecting columns, slicing date ranges, and filtering to rows that meet a condition, such as every day the stock rose more than two percent. This is how you interrogate prices. 7 min
- 20Working with the calendarMarket data is a time series, indexed by date, and pandas has special tools for it, parsing dates, sorting by time, and resampling daily data to weekly or monthly. Handling time correctly is essential and easy to get wrong. 7 min
Analysing prices
With data in hand, Part 4 computes the quantities that matter to a trader, returns, moving averages, volatility, and draws the charts that make them legible.
- 21Turning prices into performanceA price on its own says little; what matters is the return, the percentage change, and the cumulative return that compounds many of them into a total. These are the first real measures of performance, computed in a line of pandas. 7 min
- 22Smoothing the noiseA moving average smooths a noisy price into a trend by averaging a rolling window, and pandas computes it in one call. It is the simplest and most common derived series, and the basis of many signals. 7 min
- 23Measuring how much it movesVolatility, the standard deviation of returns, measures how much a price swings, and it is the quantitative face of risk from the Risk and Psychology course. You compute it from returns and annualise it to compare across instruments. 7 min
- 24Seeing the dataA chart reveals what a table hides, and Python's plotting tools draw a price series, overlay a moving average, or show a distribution of returns in a few lines. Visualising data is part of understanding it. 7 min
- 25Building a measure from scratchTo cement the tools, you build a common technical indicator yourself from the raw prices, seeing exactly how a number on a chart is computed rather than trusting a black box. Understanding the calculation is the point. 8 min
Putting it together
Part 5 assembles the skills into a tiny research workflow, building a simple signal and testing it on history, while teaching, hard, the traps that make a backtest lie and the honest limits of the whole exercise.
- 26Turning a rule into codeA classic beginner strategy is the moving-average crossover, buy when a fast average crosses above a slow one, and you now have every tool to express that rule in code and mark the signals on the data. This is a rule made computable, not yet a recommendation. 8 min
- 27Testing on history, honestlyA backtest runs the signal over past data to see how it would have done, and it is the most seductive and most dangerous tool in trading. You compute the strategy's historical return, then spend the chapter on the traps, lookahead, costs, and overfitting, that make backtests lie. 9 min
- 28Judging it like a professionalA single return figure is not enough to judge a strategy; you compute the measures from the Risk and Psychology course, the total and annualised return, the maximum drawdown, and a risk-adjusted ratio, in code from the backtest. Judge the whole picture, not the headline. 8 min
- 29Writing code you can trustCode that touches money must be trustworthy, so this chapter covers the habits, readable code, reproducibility, checking your data and your assumptions, and the deeper pitfalls, survivorship bias and curve-fitting, that fool even careful people. The goal is honest, reliable analysis. 9 min
- 30Where this leadsThe course closes by recapping what you can now do and pointing to what comes next: turning analysis into automated trading, which the Algorithmic Trading course covers, with its own demands of execution, reliability, and risk. Analysis is not execution, and the gap is large. 8 min