What is a trading algorithm? Rules, signals and code explained

A trading algorithm is nothing more mysterious than a fixed set of rules that turns market data into buy, sell or hold decisions — written precisely enough that a computer can follow it without you. It is the difference between “buy when it looks cheap” and “buy when the 14-period RSI closes below 30.” The first is a hunch; the second is an algorithm, because every word maps to a number a machine can test. This guide breaks down exactly what a trading algorithm contains, how a signal is built, and shows you a real, readable Python example.

On this page
  1. The definition
  2. The four parts
  3. Anatomy of a signal
  4. A real example
  5. Algorithm vs bot vs strategy
  6. Where to start
  7. FAQ

A precise definition

A trading algorithm is a deterministic set of rules that maps market data to an action. Given the same inputs, it always produces the same output — that reproducibility is the whole point. It removes emotion, hesitation and inconsistency, and it makes the strategy testable: you can run it over years of history on the backtester and measure exactly how it would have performed. A hunch cannot be backtested; an algorithm can.

The four parts of every algorithm

Strip any trading algorithm down and you find the same four components. An entry rule decides when to open a position. An exit rule decides when to close it — usually a stop-loss and take-profit. A position-sizing rule decides how much to risk, covered in position sizing. And a universe defines which markets it watches. Miss any one and you do not have a complete, runnable algorithm.

market data entry rule size + risk exit rule order
Every trading algorithm is the same pipeline: data → entry → sizing → exit → order.

The anatomy of a signal

A signal is the output of the entry/exit logic: a value that says long, short or flat. It is built from indicators computed on price — a moving average, an RSI, a breakout level. The key discipline is that a signal must use only data available at decision time; peeking at the close of a candle before it forms is the classic look-ahead bias that makes a worthless algorithm look brilliant in a backtest.

A real, readable example

python · sma_algo.pyimport pandas as pd

def signal(df, fast=20, slow=50):
    """Return 1 (long) or 0 (flat) for each bar — a complete entry algorithm."""
    df['fast'] = df['close'].rolling(fast).mean()
    df['slow'] = df['close'].rolling(slow).mean()
    # shift(1): only act on the PREVIOUS closed bar — no look-ahead
    df['pos'] = (df['fast'] > df['slow']).shift(1).fillna(0).astype(int)
    return df

That is a full trading algorithm in six lines: an entry/exit rule expressed as a position of 1 or 0 per bar. Bolt on a sizing rule and a venue and it becomes a runnable bot.

Algorithm vs bot vs strategy

People use these words loosely, so pin them down. A strategy is the idea (“follow trends”). An algorithm is that idea written as exact, testable rules. A bot is the running program that executes the algorithm live against an exchange API. You design a strategy, encode it as an algorithm, and a bot runs it.

Where to start

Begin with one simple, transparent algorithm — an SMA crossover is the canonical first one — and test it honestly on the backtester before adding any complexity. Resist the urge to stack indicators; every extra rule is another knob to overfit. A simple algorithm you understand beats a complex one you do not.

Not financial advice. This content is educational. Automated and algorithmic trading carries a real risk of financial loss. Never trade money you cannot afford to lose. Review the SEC investor.gov and CFTC resources before trading.

Frequently asked questions

What is a trading algorithm in simple terms?

A trading algorithm is a fixed, precise set of rules that turns market data into buy, sell or hold decisions — written so exactly that a computer can follow it without human judgement. The precision is what makes it testable: unlike a hunch, an algorithm can be run over years of history to measure how it would have performed.

What are the parts of a trading algorithm?

Every complete trading algorithm has four parts: an entry rule (when to open a trade), an exit rule (when to close it, usually a stop and target), a position-sizing rule (how much to risk), and a universe (which markets it trades). Missing any one means you have an idea, not a runnable algorithm.

Is a trading algorithm the same as a trading bot?

No. The algorithm is the set of rules; the bot is the running program that executes those rules live against an exchange. You can backtest an algorithm without a bot, but a bot always runs some algorithm. The strategy is the idea, the algorithm is the encoded rules, and the bot is the executor.

Do trading algorithms guarantee profit?

No. An algorithm only enforces a strategy consistently and without emotion; it cannot create an edge that is not there. If the underlying rules have no real predictive edge after fees and slippage, automating them simply produces losses more reliably. The value of an algorithm is discipline and testability, not magic.

MB

Mustafa Bilgic

Algorithmic trading practitioner · Founder, AITradingBot.us

Mustafa builds and backtests automated trading systems and writes about them without the hype. Every tool on this site is free and runs entirely in your browser.