In partnership with

Where Accomplished Peers Share What Really Works

Long Angle connects accomplished entrepreneurs and executives with institutional-grade investment opportunities. No membership fees. Access includes a private forum to exchange insights and ideas.

  • Confidential, peer-only discussions — no solicitation

  • Portfolio insights from active investors ($100M+ invested annually)

  • Proven strategies tested by experienced members

🚀 Your Investing Journey Just Got Better: Premium Subscriptions Are Here! 🚀

It’s been 4 months since we launched our premium subscription plans at GuruFinance Insights, and the results have been phenomenal! Now, we’re making it even better for you to take your investing game to the next level. Whether you’re just starting out or you’re a seasoned trader, our updated plans are designed to give you the tools, insights, and support you need to succeed.

Here’s what you’ll get as a premium member:

  • Exclusive Trading Strategies: Unlock proven methods to maximize your returns.

  • In-Depth Research Analysis: Stay ahead with insights from the latest market trends.

  • Ad-Free Experience: Focus on what matters most—your investments.

  • Monthly AMA Sessions: Get your questions answered by top industry experts.

  • Coding Tutorials: Learn how to automate your trading strategies like a pro.

  • Masterclasses & One-on-One Consultations: Elevate your skills with personalized guidance.

Our three tailored plans—Starter Investor, Pro Trader, and Elite Investor—are designed to fit your unique needs and goals. Whether you’re looking for foundational tools or advanced strategies, we’ve got you covered.

Don’t wait any longer to transform your investment strategy. The last 4 months have shown just how powerful these tools can be—now it’s your turn to experience the difference.

Simulated Annealing of the Simple Moving Average Strategy on Apple Stock Chart

Introduction

In financial markets, success is rarely about perfect prediction. Instead, traders thrive by adapting to changing market conditions and exploiting statistical advantages wherever they appear.

One fundamental approach in trading is to use technical indicators, such as moving averages, to systematically generate buy and sell signals.

However, even a simple strategy like a Simple Moving Average (SMA) crossover relies heavily on the choice of its parameters, particularly the window length used to calculate the average. Selecting the wrong window can significantly reduce profits, while the right window can amplify returns.

Instead of manually testing different parameters or relying on heuristics, we can turn to a mathematical optimization method inspired by physics: Simulated Annealing (SA).

This approach allows us to explore a wide range of possible SMA windows, balancing exploration and exploitation, and ultimately finding a near-optimal configuration that maximizes cumulative returns.

Learn AI in 5 minutes a day

What’s the secret to staying ahead of the curve in the world of AI? Information. Luckily, you can join 1,000,000+ early adopters reading The Rundown AI — the free newsletter that makes you smarter on AI with just a 5-minute read per day.

The Simple Moving Average Strategy

Before diving into simulated annealing, it is essential to understand the strategy we are optimizing.

The Simple Moving Average (SMA) strategy is one of the most widely used technical indicators in trading:

  • The SMA is calculated as the average of the last N closing prices.

  • A buy signal is triggered when the stock price crosses above the SMA.

  • A sell signal is triggered when the stock price crosses below the SMA.

  • The parameter N — the length of the moving average window — is critical: too short, and the strategy becomes noisy; too long, and it lags behind trends.

Mathematically, the SMA at time t with window N is:

The Simple Moving Average Function

where P_{t-i} represents the closing price i days before t.

Once signals are generated, the cumulative return of the strategy can be calculated as:

The Cumulative Returns Function

where:

  • r_t = \frac{P_t — P_{t-1}}{P_{t-1}} is the daily return,

  • s_{t-1} \in {0,1} indicates whether we are in a long position (1 = holding, 0 = out of market) based on the previous day’s signal.

This cumulative return serves as the objective function that we aim to maximize.

Simulated Annealing: A Trading Optimization Tool

Simulated annealing is a probabilistic optimization technique inspired by the physical process of annealing in metallurgy, where materials are heated and then cooled slowly to reduce defects and reach a stable state. The algorithm adapts this idea for optimization problems:

  1. Start with an initial solution — in our case, an SMA window.

  2. Iteratively propose a small change (a neighboring window).

  3. Accept the new solution if it improves the objective function.

  4. Occasionally accept worse solutions based on a probability that decreases over time — this prevents the algorithm from getting stuck in local maxima.

Kickstart your holiday campaigns

CTV should be central to any growth marketer’s Q4 strategy. And with Roku Ads Manager, launching high-performing holiday campaigns is simple and effective.

With our intuitive interface, you can set up A/B tests to dial in the most effective messages and offers, then drive direct on-screen purchases via the remote with shoppable Action Ads that integrate with your Shopify store for a seamless checkout experience.

Don’t wait to get started. Streaming on Roku picks up sharply in early October. By launching your campaign now, you can capture early shopping demand and be top of mind as the seasonal spirit kicks in.

Get a $500 ad credit when you spend your first $500 today with code: ROKUADS500. Terms apply.

The Mathematics Behind Simulated Annealing

The key equations in simulated annealing are:

  • Acceptance probability for worse solutions:

The Acceptance Probability

where:

  • \Delta E = R_{new} — R_{current} is the difference in cumulative return between the proposed and current solution.

  • T is the “temperature,” a parameter that starts high and decreases gradually according to a cooling schedule:

The Cooling Schedule

The cooling schedule controls the balance between exploration (high temperature) and exploitation (low temperature).

Early in the process, larger changes are more likely to be accepted, allowing the algorithm to explore widely. Later, the algorithm converges, refining the best-found solution.

Applying Simulated Annealing to SMA Optimization

AAPL Adjusted Close Price Chart

In our experiment, we applied simulated annealing to optimize the SMA window for a stock over a six-month historical period. Here’s the workflow:

  1. Initialization: Randomly select an initial SMA window within a plausible range (e.g., 2–60 days).

  2. Iteration: For each step:

  • Propose a new window by adding a small random change to the current window.

  • Compute the cumulative return for the new window.

  • Accept the new window if it improves the return or probabilistically if it worsens it.

  • Update the temperature according to the cooling schedule.

3. Convergence: Continue until a predetermined number of iterations or until temperature approaches zero.

4. Multi-start refinement: Run multiple SA processes with different random seeds and select the best overall SMA window.

Insights from the Optimization

Several key observations emerged from this process:

  • The best SMA window is not necessarily intuitive; it often lies in a non-obvious part of the search space.

  • Simulated annealing allows the algorithm to escape local maxima, finding solutions that traditional gradient-like methods may miss.

  • The optimized SMA consistently generated buy/sell signals that aligned well with price trends, resulting in superior cumulative returns compared to arbitrarily chosen windows.

Best SMA overlay with buy/sell signals on stock prices

  • A quick scan of the objective function across all SMA windows before optimization helps to visualize the rugged landscape that SA must navigate.

Objective function landscape before SA

Why Simulated Annealing Works for Trading

Stock price movements are noisy and non-linear, making many traditional optimization methods unreliable. Simulated annealing excels in this environment because:

  • It does not rely on derivatives or smooth objective functions.

  • It balances exploration and exploitation, enabling robust search across complex landscapes.

  • It is simple to implement and highly adaptable to different trading strategies, including SMAs, exponential moving averages, or multi-parameter indicators.

Conclusion: From Physics to Profitable Decisions

Simulated annealing offers a bridge between the stochastic world of markets and structured optimization. By carefully tuning the SMA window through this method, traders can enhance the reliability and profitability of their strategies, even when underlying price movements are unpredictable.

While it does not guarantee success on future data, it provides a systematic, mathematically grounded way to explore trading parameter spaces and uncover hidden opportunities.

Final objective curve with optimized SMA highlighted

Simulated annealing is not just a computational curiosity, it is a practical tool that translates the principles of physics into actionable trading strategies.

Check out this GitHub Repository for the full code implementation of the Simulated Annealing Algorithm.

Keep Reading

No posts found