<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aleks Budzanowski on Medium]]></title>
        <description><![CDATA[Stories by Aleks Budzanowski on Medium]]></description>
        <link>https://medium.com/@aleksbudzanowski?source=rss-a5c5d84cbc0d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*D-E-JvZVykYRKJ2MeI8DCQ.jpeg</url>
            <title>Stories by Aleks Budzanowski on Medium</title>
            <link>https://medium.com/@aleksbudzanowski?source=rss-a5c5d84cbc0d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 22:19:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aleksbudzanowski/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building an Adaptive Trading Strategy with HMM & GARCH]]></title>
            <link>https://medium.com/@aleksbudzanowski/building-an-adaptive-trading-strategy-with-hmm-garch-052303710936?source=rss-a5c5d84cbc0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/052303710936</guid>
            <category><![CDATA[hidden-markov-models]]></category>
            <category><![CDATA[quantitative-finance]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[quantitative-trading]]></category>
            <category><![CDATA[algorithmic-trading]]></category>
            <dc:creator><![CDATA[Aleks Budzanowski]]></dc:creator>
            <pubDate>Sun, 03 May 2026 23:03:57 GMT</pubDate>
            <atom:updated>2026-05-03T23:03:57.955Z</atom:updated>
            <content:encoded><![CDATA[<p>Financial markets are notoriously difficult to predict because they are fundamentally non-stationary (they constantly change over time). Traditional models often rely on simple historical standard deviation to measure volatility, assuming an average risk profile over a fixed period.</p><p>However, markets don’t behave like a simple average; they shift between distintc regimes of calm trends and chaotic sell-offs. Because of this, I wanted to build a dynamic model that doesn’t just look at past averages, but actually detects these hidden market regimes and adapts to changing volatility in real time.</p><p>Recently, this project earned me 3rd place at a university scientific conference, and in this article, I want to break down exactly how I built it.</p><h4><strong>Step 1: Detecting Market Regimes with HMM</strong></h4><p>To adapt to the market, we first need to know what “mood” it’s in. This is where Hidden Markov Model (HMM) comes in. Think of it as the strategic radar for our portfolio.<br>Under the hood, this module relies on two heavy-duty algorithms working in tandem:<br>- The Baum-Welch Algorithm: This acts as the training engine. It analyzes historical returns to find the hidden parameters and “rules” for different market states (e.g., Bullish, Bearish, and Sideways).<br>- The Viterbi Algorithm: Once the rules are established, Viterbi is to decode the present. It evaluates the most recent data and assigns the most probable regime for today.<br>Based on Viterbi classification, the HMM generates a binary trading signal. If the market is in Bullish or Sideways regime, the model outputs 1 and it says to be invested. If it detects a Bearish state, it outputs 0 which is to pull out.</p><h4>Step 2: GARCH(1,1) — Volatility Modeling and Position Sizing</h4><p>The previous HMM model claims if we invest or not, but not the size. This is where GARCH comes in. The GARCH model calculates our position size and it can use leverage up to 1.5x. Mathematically, its role is simply: the volatility target divided by the GARCH volatility forecast.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*uUWSUjk_9AaNI5phvts0Cg.png" /><figcaption>Left side is volatility by GARCH and on the right is position size</figcaption></figure><h4>Step 3: Model Fusion</h4><p>As mentioned earlier, the HMM’s role is to give a binary signal (1 or 0), and GARCH calculates the position size. Mathematically, the fusion is simply the signal multiplied by the position size.<br>Therefore, when it’s a bearish regime, the HMM outputs 0. Zero times the GARCH part equals zero — that’s when our model doesn’t invest and keeps everything in cash to protect capital. On the flip side, when the regime is bullish or sideways (signal 1), GARCH fully dictates our market exposure and leverage.</p><h4>The Setup: Methodology &amp; Parameters</h4><p>Before jumping into the results and the reality check of trading costs, let’s establish the ground rules of the backtest.<br>To ensure the strategy is robust and not just overfitted to historical data, I implemented a Walk-Forward validation approach. This means models were periodically retrained on a rolling window of 1000 days and tested on 125 days of unseen future data.<br>For the risk parameters, I set an annualized volatility target of 20% and strictly capped the maximum leverage at 1.5x. This ensures the algorithm doesn’t take extreme risk even when it feels highly confident in a trend.</p><h4>Step 4: The Silent Killer — Transaction Costs &amp; Overtrading</h4><p>On paper, without including transaction costs, our model is significantly more profitable than a simple Buy &amp; Hold strategy on the underlying asset. However, the moment we introduce realistic trading fees, the constant buing and selling eats away the profits, dropping our model’s performance below the But &amp; Hold baseline.</p><h4>The Solution: A Rebalancing Buffer</h4><p>Why were the transaction costs so high initially? Because GARCH predicts a new volatility level every single day. This means the model was making many transactions.<br>As a solution, I introduced a Smart Buffer. The rule is simple: the algorithm ignores minor fluctuations and only executes a trade if the newly calculated position size differs from the current one by a significant threshold. This effectively stopped the overtrading and drastically reduced the number of transactions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OXVGrExvJxzgT-xG0lTSIw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IJklkr7JIh_F5rwHCFSJcg.png" /></figure><h4>Step 5: Testing on Multiple Asset Classes (Gold &amp; Nikkei 225)</h4><p>To ensure model flexibility, I tested the strategy on completely different asset classes: Gold (Commodities) and the Nikkei 225 (Japanese stock market index).<br>For Gold, I kept the exact same parameters as the S&amp;P 500. However, the Japanese stock market is very noisy and prone to false signals. To account for this unique market behavior, I made one targeted adjustment for the Nikkei: I extended the volatility window in the Baum-Welch algorithm to 42 days from the previous 21 to filter out the noise.<br>Here is how the adaptive volatility modeling performed across the board:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vDOlJLmKA8FsozOE3cFnOQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*LvXQv8K_N0k_tT4VTEQhOg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*azsSrY4mo-wHu32lzJDihQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*YN8kz65pq6lZkuNK9cHxiQ.png" /></figure><h4>Conclusion: The Price of Stability &amp; Future Work</h4><p>First, in the equity markets (S&amp;P 500 and Nikkei 225), the core objective of the project was successfully achieved. We drastically reduced the Maximum Drawdown, cutting the worst-case losses almost in half compared to a Buy &amp; Hold strategy. Yes, this stability came at the cost of total return.<br>Second, the results on Gold serve as a great reality check. The Pure HMM model did exactly what it was supposed to do — it lowered volatility while still maintaining solid returns. However, adding GARCH on top of it introduced excessive turnover. In this specific case, the cost of overtrading outweighed the benefits of dynamic position sizing, proving that adding complexity isn’t always the right answer.</p><h4>What’s Next?</h4><p>This project is a solid foundation, but the model still needs optimization. In the coming days, I plan to expand on this architecture — potentially exploring different variants of GARCH, optimizing the rebalancing thresholds.</p><p>If you want to dive into the math/code, the complete Python code is available on my <a href="https://github.com/Aleksander-Budzanowski/HMM-GARCH-Volatility-Strategy">GitHub</a><br>Feel free to connect with me on <a href="https://www.linkedin.com/in/aleksander-budzanowski-b42283398/">LinkedIn</a> if you want to discuss quantitative finance, algorithmic trading or data science.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=052303710936" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>