<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Market Regime &#8211; The Financial Hacker</title>
	<atom:link href="https://financial-hacker.com/tag/market-regime/feed/" rel="self" type="application/rss+xml" />
	<link>https://financial-hacker.com</link>
	<description>A new view on algorithmic trading</description>
	<lastBuildDate>Sat, 04 Jul 2026 12:18:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://financial-hacker.com/wp-content/uploads/2017/07/cropped-mask-32x32.jpg</url>
	<title>Market Regime &#8211; The Financial Hacker</title>
	<link>https://financial-hacker.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>The Market Regime Filter</title>
		<link>https://financial-hacker.com/the-market-regime-filter/</link>
					<comments>https://financial-hacker.com/the-market-regime-filter/#comments</comments>
		
		<dc:creator><![CDATA[Petra Volkova]]></dc:creator>
		<pubDate>Sat, 04 Jul 2026 09:02:32 +0000</pubDate>
				<category><![CDATA[Indicators]]></category>
		<category><![CDATA[Petra on Programming]]></category>
		<category><![CDATA[Market Regime]]></category>
		<guid isPermaLink="false">https://financial-hacker.com/?p=5025</guid>

					<description><![CDATA[The market changes all the time. Sometimes it trends, sometimes it oscillates, sometimes it goes sidewards. Trading systems that do not react on market regime change will bring uncomfortable times for their traders (and their wallets). In TASC 9/2026, Gaetano Di Prima and Fabio Baruffa provide a solution. Their market regime filter consists of three &#8230; <a href="https://financial-hacker.com/the-market-regime-filter/" class="more-link">Continue reading<span class="screen-reader-text"> "The Market Regime Filter"</span></a>]]></description>
										<content:encoded><![CDATA[<p><em></p>
<p>The market changes all the time. Sometimes it trends, sometimes it oscillates, sometimes it goes sidewards. Trading systems that do not react on market regime change will bring uncomfortable times for their traders (and their wallets). In TASC 9/2026, <strong>Gaetano Di Prima</strong> and <strong>Fabio Baruffa</strong> provide a solution. Their market regime filter consists of three components for detecting trend, volatility, and credit spread. Does this filter improve trading?</p>
<p></em></p>
<p><span id="more-5025"></span></p>
<p>Although the 3 detectors are not this complex, the authors provided 9 pages Python code for realizing them. Python is an effective language, but not so for financial indicators. Neither I, nor Claude or ChatGPT did really want to convert 9 pages of code, so I programmed the three detectors from scratch. This took five minutes:</p>
<pre class="prettyprint">int marketDir(int TimePeriod)
{
  asset("SPY");
  return priceC() &gt; SMA(seriesC(),TimePeriod);
}

int marketVolatility()
{
  return assetPrice("VIX") &lt; assetPrice("VIX3M");
}

var marketCC(int TimePeriod)
{
  var Ratio = assetPrice("HYG")/fix0(assetPrice("IEF"));
  return zscore(Ratio,TimePeriod);
}</pre>
<p>The <strong>assetPrice</strong> function is a helper function that returns the current price of the given asset and then switches back to the original asset. The <strong>fix0</strong> function prevents division by zero, just in case. The first two functions return <strong>1</strong> when the condition is fulfilled, otherwise <strong>0</strong>.</p>
<p>The authors consider the market in a favorable state when all 3 detectors give green light. If only 2 do, trading should be done with caution, and otherwise entrirely suspended. This is the code to plot a green, orange, or red bar depending on the market state:</p>
<pre class="prettyprint">function run()
{
  BarPeriod = 1440;
  StartDate = 2008;
  EndDate = 2026;
  LookBack = 200;
  setf(PlotMode,PL_ALL);
  assetList("AssetsIB");

  int Score = marketDir(200) 
    + marketVolatility() 
    + (marketCC(100) &gt; -2);

  plot("#0",0,NEW,WHITE); // plot in a new window
  if(Score == 3)
    plot("On",1,BARS,GREEN);
  else if(Score == 2)
    plot("Caution",1,BARS,ORANGE);
  else
    plot("Risk",1,BARS,RED);}
}</pre>
<p>The resulting chart:</p>
<p><img fetchpriority="high" decoding="async" width="882" height="486" class="wp-image-5026" src="https://financial-hacker.com/wp-content/uploads/2026/07/word-image-5025-1.png" srcset="https://financial-hacker.com/wp-content/uploads/2026/07/word-image-5025-1.png 882w, https://financial-hacker.com/wp-content/uploads/2026/07/word-image-5025-1-300x165.png 300w, https://financial-hacker.com/wp-content/uploads/2026/07/word-image-5025-1-768x423.png 768w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px" /></p>
<p>Now we’re going to put the market detector to the test. We invest all capital in SPY when the market is in a green state, half the capital in an orange state, and go out of the market in a red state. For this we add this code to the <strong>run</strong> function:</p>
<pre class="prettyprint">asset("SPY");
Capital = 10000;
Leverage = 1;
int Exposure = (Capital+ProfitTotal)/MarginCost;
if(Score &lt; 3) Exposure /= 2;
if(Score &lt; 2) Exposure = 0;

if(Exposure &gt; LotsPool)
  enterLong(Exposure-LotsPool);
else if(Exposure &lt; LotsPool)
  exitLong(0,0,LotsPool-Exposure);</pre>
<p>The backtest reproduces the results from the TASC article, with 8% CAGR and 18% drawdown:</p>
<p><img decoding="async" width="882" height="486" class="wp-image-5027" src="https://financial-hacker.com/wp-content/uploads/2026/07/word-image-5025-2.png" srcset="https://financial-hacker.com/wp-content/uploads/2026/07/word-image-5025-2.png 882w, https://financial-hacker.com/wp-content/uploads/2026/07/word-image-5025-2-300x165.png 300w, https://financial-hacker.com/wp-content/uploads/2026/07/word-image-5025-2-768x423.png 768w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px" /></p>
<p>8% return is good, but not too exciting. However drawdown and risk are remarkably smaller than with a buy-and-hold strategy.</p>
<p>The code can be downloaded from the 2026 script repository.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://financial-hacker.com/the-market-regime-filter/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
