Two Trading Strategies Using Daily Data in Python

Backtest, interpret, and adapt them to your own workflow

Dhruv Singh

6/21/20253 min read

Quantitative trading can feel intimidating when you're just starting out - but the first step doesn't have to be complicated. In this post, I’ll walk you through two foundational trading strategies you can implement in Python using nothing more than daily market data.

Both strategies come from a course I completed by Alexander Hagmann titled “Algorithmic Trading A-Z with Python, Machine Learning & AWS.” I’ve since adapted the code and structure for teaching and exploratory use. The result is a flexible template that lets you understand core trading logic, test your assumptions, and experiment confidently before risking any real capital.

You’ll walk away with:

  • Two complete strategy templates (SMA crossover and mean reversion)

  • Fully functional Python code

  • A GitHub repo to clone and run

  • Guidance on adapting strategies to your own instrument or timeframe

What You'll Learn

This post is geared toward intermediate learners - those with some Python and statistics experience who want to dive deeper into building, backtesting, and experimenting with trading strategies.

We’ll cover:

  • The logic and mechanics behind two classic strategies

  • How to code them in a clean, modular way

  • How to run simple backtests and visualize performance

  • How to experiment with new instruments or timeframes

  • Where these strategies fall short - and how to extend them

Let’s dive in.

Strategy 1: Simple Moving Average Crossover (Trend-Following)

The first strategy is a trend follower - it assumes that when short-term price momentum overtakes longer-term trends, it's often the beginning of a new directional move.

The Concept

We use two moving averages:

  • A short window (e.g., 50 days)

  • A long window (e.g., 200 days)

When the short MA crosses above the long MA, we go long. When it crosses below, we go short.

This logic gives us a simple system to ride trends.

Sample Code Snippet

Below is a snippet from the SMA strategy backtester module:

data["sma_short"] = data["price"].rolling(self.SMA_S).mean()

data["sma_long"] = data["price"].rolling(self.SMA_L).mean()

data["position"] = np.where(data["sma_short"] > data["sma_long"], 1, -1)

data["strategy"] = data["position"].shift(1) * data["returns"]

See full strategy code on GitHub

We then compute cumulative returns, visualize strategy performance against buy-and-hold, and optimize the SMA window parameters using brute-force grid search.

Try It Yourself

Change the instrument (e.g., from EURUSD to Apple stock), or the time period. Try different moving average windows. What combinations yield the highest returns? What happens when you shorten or lengthen the time horizon?

Start small: test it first on a paper trading account before applying anything live.

Strategy 2: Mean Reversion Using Bollinger Bands

This second strategy assumes that prices tend to revert to a mean over time - particularly when they stray too far in either direction.

The Concept

We use Bollinger Bands, which place two lines above and below a moving average - typically 2 standard deviations away. When price breaks below the lower band, we go long. When it breaks above the upper band, we go short. We exit when price returns to the moving average.

We also introduce transaction costs, and logic to avoid trading noise.

Sample Code Snippet

Here’s a piece of the logic from our mean reversion backtester:

data["SMA"] = data["price"].rolling(self.SMA).mean()

data["Lower"] = data["SMA"] - data["price"].rolling(self.SMA).std() * self.dev

data["Upper"] = data["SMA"] + data["price"].rolling(self.SMA).std() * self.dev

data["position"] = np.where(data["price"] < data["Lower"], 1, np.nan)

data["position"] = np.where(data["price"] > data["Upper"], -1, data["position"])

data["position"] = np.where(data["distance"] * data["distance"].shift(1) < 0, 0, data["position"])

data["position"] = data["position"].ffill().fillna(0)

See full strategy code on GitHub

Try It Yourself

Use a volatile stock - maybe a small-cap, or a company around earnings season. What happens when you increase the standard deviation band? When you shrink the moving average window?

Compare your results with the SMA strategy. When does one outperform the other?

Remember: these are templates, not turnkey systems. Adapt, test, and stress them before using in live trading

Neither strategy includes position sizing, leverage, or live execution logic - and that’s by design. They’re meant to help you focus on signal logic, data handling, and performance testing.

Once you’re comfortable here, you can begin exploring more advanced ideas like:

  • Stop-loss rules

  • Portfolio-level backtesting

  • Intraday data

  • Execution slippage

Have a Question?

If anything was unclear - or you ran into something interesting - drop a comment below. I’ll be happy to respond and iterate with you.

Citations and Use of AI Tools

  • Core strategy logic was originally adapted from Alexander Hagmann’s Udemy course “Algorithmic Trading A-Z with Python, Machine Learning & AWS.”

  • Code was restructured into modular backtesters and tested independently for this post.

  • I used ChatGPT as a collaborative assistant to help refine structure, wording, and pedagogy.