<?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:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Trading the Breaking: Research]]></title><description><![CDATA[Welcome to my lab! A testing ground for algorithms, models, and ideas built at the edge of what’s possible. Here, we dissect the machinery behind finance, technology, and intelligent systems—where theory is challenged, experiments are run, and innovation is forced to prove itself in the market.]]></description><link>https://www.quantbeckman.com/s/research</link><image><url>https://substackcdn.com/image/fetch/$s_!AJt2!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png</url><title>Trading the Breaking: Research</title><link>https://www.quantbeckman.com/s/research</link></image><generator>Substack</generator><lastBuildDate>Sun, 08 Nov 2026 11:41:50 GMT</lastBuildDate><atom:link href="https://www.quantbeckman.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Quant Beckman]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[quantbeckman@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[quantbeckman@substack.com]]></itunes:email><itunes:name><![CDATA[Quant Beckman]]></itunes:name></itunes:owner><itunes:author><![CDATA[Quant Beckman]]></itunes:author><googleplay:owner><![CDATA[quantbeckman@substack.com]]></googleplay:owner><googleplay:email><![CDATA[quantbeckman@substack.com]]></googleplay:email><googleplay:author><![CDATA[Quant Beckman]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[[WITH CODE] Macroeconomic news]]></title><description><![CDATA[What to trade and how to trade events and news]]></description><link>https://www.quantbeckman.com/p/with-code-macroeconomic-news</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-macroeconomic-news</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Tue, 20 Oct 2026 12:44:09 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uWS0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" width="1456" height="911" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:911,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bFmQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bFmQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png 424w, https://substackcdn.com/image/fetch/$s_!bFmQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png 848w, https://substackcdn.com/image/fetch/$s_!bFmQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png 1272w, https://substackcdn.com/image/fetch/$s_!bFmQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bFmQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png" width="753" height="822" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:822,&quot;width&quot;:753,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:118695,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/221308135?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bFmQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png 424w, https://substackcdn.com/image/fetch/$s_!bFmQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png 848w, https://substackcdn.com/image/fetch/$s_!bFmQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png 1272w, https://substackcdn.com/image/fetch/$s_!bFmQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbda23a7c-9bb4-461f-a565-6fc987a24219_753x822.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p style="text-align: center;">&#8230;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!UuYq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UuYq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png 424w, https://substackcdn.com/image/fetch/$s_!UuYq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png 848w, https://substackcdn.com/image/fetch/$s_!UuYq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png 1272w, https://substackcdn.com/image/fetch/$s_!UuYq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!UuYq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png" width="753" height="292" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:292,&quot;width&quot;:753,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:40126,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/221308135?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!UuYq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png 424w, https://substackcdn.com/image/fetch/$s_!UuYq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png 848w, https://substackcdn.com/image/fetch/$s_!UuYq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png 1272w, https://substackcdn.com/image/fetch/$s_!UuYq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13355c31-c611-483f-bdbd-38139d64e6a4_753x292.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized</strong> by clicking below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CZIx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1151002,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;text&quot;:&quot;INDEX&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.quantbeckman.com/p/welcome"><span>INDEX</span></a></p><div><hr></div><h2>From macroeconomic releases to executable trading signals</h2><p>Macroeconomic event trading depends on the information available when a decision is made and the price movement still accessible after execution. Using economic calendars, forecast surprises, macroeconomic state factors, and central bank communications, this framework connects incoming news to conditional return forecasts and feasible positions.</p><p><strong>What&#8217;s inside:</strong></p><ol><li><p><strong>Risks and limitations:</strong> Examines sparse release histories, changing policy regimes, unreliable liquidity, and text-processing errors. Explains why statistical regularization cannot replace independent evidence, and how feed interruptions, imperfect timestamps, and optimistic execution assumptions can undermine an otherwise coherent research framework.</p></li><li><p><strong>The information clock:</strong> Separates economic reference periods, publication, data receipt, feature completion, decisions, and fills. Covers calendar snapshots, forecasts frozen before publication, and field-level availability to reconstruct what a strategy could know at each decision point.</p></li><li><p><strong>Economic levels versus forecast surprises:</strong> Distinguishes the economic state, deviations from consensus, internal-model innovations, revisions, and forecaster disagreement. Shows why an indicator can improve while disappointing expectations, and why substituting a previous observation for a missing forecast changes the signal being tested.</p></li><li><p><strong>Daily macroeconomic states:</strong> Combines asynchronous activity, employment, and inflation releases into continuously available factors using recursively estimated principal components. Addresses historical scaling, changing indicator coverage, and factor-update attribution, while explaining why repeated daily values do not create additional independent observations.</p></li><li><p><strong>Information age and uncertainty:</strong> Models how uncertainty grows between releases and contracts when new observations arrive. Separates publication age from the age of the measured economic period, compares state-space updates with age-weighted composites, and distinguishes uncertainty about economic conditions from asset-return volatility.</p></li><li><p><strong>Joint releases and incremental news:</strong> Treats payrolls, unemployment, wages, and revisions as related components of a release package. Uses covariance shrinkage, multivariate surprise measures, and conditional residuals to assess unusual combinations and incremental information, while preserving the timing and historical versions of each component.</p></li><li><p><strong>Policy narratives and text versions:</strong> Incorporates central bank statements and news through separate growth, employment, inflation, and policy-stance scores. Tracks document revisions, duplicate passages, model versions, and processing times so repeated reporting does not inflate the evidence and later interpretations do not enter earlier decisions.</p></li><li><p><strong>Conditional forecasts and position sizing:</strong> Models how the same surprise can produce different price responses across economic environments and holding horizons. Combines surprises, the pre-release state, and selected regularized interactions, then sizes exposure using expected costs, return risk, model uncertainty, and market impact.</p></li><li><p><strong>Execution and achievable returns:</strong> Measures performance from attainable fills and calculates cash results using executable bid and ask prices. Covers latency, spread widening, slippage, financing, partial fills, and broker constraints, including situations where minimum contract sizes make zero exposure the appropriate allocation.</p></li><li><p><strong>Chronological validation:</strong> Builds training samples from fully observed outcomes, removes overlapping labels, and keeps related release components together. Uses chronological model selection, simple benchmarks, and portfolio accounting to assess whether additional complexity improves unseen forecasts and net returns within available capital.</p></li></ol><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!YlXN!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F038ad937-bc08-411c-8cff-07d583ffc443_1544x1032.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Sample</div><div class="file-embed-details-h2">2.08MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/3d6d3489-f4f6-441c-9ecd-83beb3304c8f.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/3d6d3489-f4f6-441c-9ecd-83beb3304c8f.pdf"><span class="file-embed-button-text">Download</span></a></div></div>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-macroeconomic-news">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Event-driven alpha]]></title><description><![CDATA[Part I]]></description><link>https://www.quantbeckman.com/p/with-code-event-driven-alpha</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-event-driven-alpha</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Tue, 15 Sep 2026 12:42:48 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uWS0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" width="1456" height="911" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:911,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FzNy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FzNy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png 424w, https://substackcdn.com/image/fetch/$s_!FzNy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png 848w, https://substackcdn.com/image/fetch/$s_!FzNy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png 1272w, https://substackcdn.com/image/fetch/$s_!FzNy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FzNy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png" width="620" height="833" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:833,&quot;width&quot;:620,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:142615,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/215813794?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FzNy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png 424w, https://substackcdn.com/image/fetch/$s_!FzNy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png 848w, https://substackcdn.com/image/fetch/$s_!FzNy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png 1272w, https://substackcdn.com/image/fetch/$s_!FzNy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18ec3333-e6eb-44f3-9122-0a669087ea2a_620x833.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized</strong> by clicking below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CZIx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1151002,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;text&quot;:&quot;INDEX&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.quantbeckman.com/p/welcome"><span>INDEX</span></a></p><div><hr></div><h3>From corporate catalysts to tradable event-driven alpha</h3><p>Event-driven alpha depends on the return still available after a signal becomes observable and a trade executes. Using earnings surprises, pre-event momentum, and contrarian retail order flow, this framework separates historical event patterns from forward-looking forecasts and executable portfolios. The emphasis is on information timing, and incremental predictive value.</p><p><strong>What&#8217;s inside:</strong></p><ol><li><p><strong>What event-driven alpha means:</strong> Separates gross event returns from alpha by accounting for systematic exposures and execution costs. Explains why post-earnings drift, deal-completion risk, and mandatory index flows represent different economic opportunities rather than variations of one universal trading rule.</p></li><li><p><strong>Risks and limitations:</strong> Examines the trade-off between waiting for clearer signals and losing the remaining opportunity. Covers noisy retail classification, clustered announcements, and specification search, alongside borrow fees, illiquidity, and short squeezes that can undermine attractive historical patterns.</p></li><li><p><strong>Methods for extracting event-driven alpha:</strong> Reviews earnings drift, mergers, buybacks and issuance, spin-offs, restructurings, index changes, and macroeconomic releases. Connects each catalyst family to its own economic mechanism, modeling approach, and execution constraints, from continuous price adjustment to binary transaction outcomes and scheduled liquidity demand.</p></li><li><p><strong>From hypothesis to trading rules:</strong> Turns a catalyst into an auditable signal by fixing the observation cutoff and attainable entry before testing thresholds. Requires retail flow to improve forecasts beyond earnings surprise, momentum, and the initial price reaction under comparable universe rules, trading costs, and execution assumptions.</p></li><li><p><strong>Earnings news and conditional opportunities:</strong> Combines earnings surprise, prior momentum, and retail response to isolate specific post-announcement states. Historical patterns among prior losers with bad earnings and heavy retail buying motivate a short-focused hypothesis, while the weaker long-side evidence requires independent validation rather than assumed symmetry.</p></li><li><p><strong>The information clock:</strong> Aligns observation, execution, and return measurement to prevent crediting the strategy with unavailable gains. The daily example observes flow through day +1, enters at the day +2 close, and exits on day +22, producing twenty daily return observations&#8212;not a fresh twenty-two-session holding period.</p></li><li><p><strong>Early signals and predictive models:</strong> Demonstrates how completed-window retail flow can absorb information from the outcome being predicted. One synthetic example illustrates this leakage, while fixed quintile rules and compact interaction models provide alternative ways to express the early-flow hypothesis without treating retrospective classifications as executable signals.</p></li></ol><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!bRIq!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2631459-ef2e-4488-92c7-15efce7c1096_985x796.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Sample</div><div class="file-embed-details-h2">3.61MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/43ed1456-023d-47ff-b4e3-0e5710ec9f34.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/43ed1456-023d-47ff-b4e3-0e5710ec9f34.pdf"><span class="file-embed-button-text">Download</span></a></div></div>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-event-driven-alpha">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Feature selection: Embedded methods]]></title><description><![CDATA[Model-integrated approaches]]></description><link>https://www.quantbeckman.com/p/with-code-feature-selection-embedded</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-feature-selection-embedded</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Sun, 16 Aug 2026 15:04:14 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uWS0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" width="1456" height="911" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:911,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EIxo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EIxo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png 424w, https://substackcdn.com/image/fetch/$s_!EIxo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png 848w, https://substackcdn.com/image/fetch/$s_!EIxo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png 1272w, https://substackcdn.com/image/fetch/$s_!EIxo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EIxo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png" width="874" height="1073" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1073,&quot;width&quot;:874,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:107644,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/211405518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EIxo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png 424w, https://substackcdn.com/image/fetch/$s_!EIxo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png 848w, https://substackcdn.com/image/fetch/$s_!EIxo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png 1272w, https://substackcdn.com/image/fetch/$s_!EIxo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F381519ad-7110-4007-a2a5-a9673d5b48b9_874x1073.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p style="text-align: center;"><span>Before you begin, remember that you have an index with the newsletter content </span><strong>organized</strong><span> by clicking below.</span></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CZIx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1151002,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;text&quot;:&quot;INDEX&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.quantbeckman.com/p/welcome"><span>INDEX</span></a></p><div><hr></div><h3>From model estimation to production-ready feature sets</h3><p>Embedded feature selection integrates variable selection into model training. Rather than ranking inputs beforehand or testing separate subsets, embedded methods decide which features survive while estimating coefficients, decision boundaries, tree structures, or stochastic gates. This connects feature selection to the model&#8217;s actual objective while controlling redundancy, instability, and implementation frictions.</p><p><strong>What&#8217;s inside:</strong></p><ol><li><p><strong>Lasso regression:</strong> Produces compact linear models by shrinking weak coefficients to zero. Its simplicity makes the selected support easy to interpret, although strongly correlated predictors can lead to unstable winner-takes-all choices.</p></li><li><p><strong>Elastic Net:</strong> Combines sparse selection with ridge-style stabilization, allowing groups of correlated predictors to remain in the model. This makes it better suited to feature libraries containing similar signals across multiple horizons or transformations.</p></li><li><p><strong>Group and sparse-group Lasso:</strong> Selects economically related feature families together while preserving sparsity within each family. The approach aligns model structure with predefined groups such as momentum, volatility, or event-driven variables.</p></li><li><p><strong>Penalized Logistic regression:</strong> Selects inputs according to their contribution to a regularized probabilistic decision boundary. Its usefulness depends on aligning probability calibration with the confidence thresholds and asymmetric costs used during execution.</p></li><li><p><strong>Sparse Support Vector Machines:</strong> Combines margin-based classification with coefficient sparsity, retaining variables that help separate classes. Feature survival depends on observations near the decision boundary rather than on overall fit alone.</p></li><li><p><strong>Tree-induced feature selection:</strong> Builds nonlinear and conditional support through split selection, depth restrictions, minimum leaf sizes, and pruning. Out-of-sample permutation tests are needed because impurity-based importance can favor noisy variables with many possible split points.</p></li><li><p><strong>SCAD and MCP:</strong> Apply nonconvex penalties that preserve sparsity while reducing the excessive shrinkage imposed on large coefficients by Lasso. They can recover stronger signals with less bias, but introduce initialization sensitivity and more difficult optimization.</p></li><li><p><strong>Stochastic feature gates:</strong> Learn probabilistic switches that approximate an explicit feature budget inside differentiable models. They support nonlinear interactions and operational constraints, but require stable gate selection and deterministic deployment rules.</p></li></ol><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!lZNI!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ee4741-9a4e-4d73-8f91-66f3996f204f_985x796.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Sample</div><div class="file-embed-details-h2">675KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/fbc55a5c-6d11-40c6-bc80-db533b9049b3.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/fbc55a5c-6d11-40c6-bc80-db533b9049b3.pdf"><span class="file-embed-button-text">Download</span></a></div></div><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!aZQL!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d343eab-71c7-461b-b904-25da907101d5_664x812.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Feature Selection Embedded Methods</div><div class="file-embed-details-h2">1.14MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/0b5260b5-0791-44a9-bd6d-fe2e055504ad.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/0b5260b5-0791-44a9-bd6d-fe2e055504ad.pdf"><span class="file-embed-button-text">Download</span></a></div></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://colab.research.google.com/drive/1QhMER5oYAzsOj6_wJg2Afucc21a5GVPE?usp=sharing" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5xfi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 424w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 848w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1272w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png" width="300" height="57.074175824175825" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:277,&quot;width&quot;:1456,&quot;resizeWidth&quot;:300,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:&quot;https://colab.research.google.com/drive/1QhMER5oYAzsOj6_wJg2Afucc21a5GVPE?usp=sharing&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5xfi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 424w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 848w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1272w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><p> </p><p></p>]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Feature selection: Wrapper-based feature selection methods ]]></title><description><![CDATA[Model-dependent approaches]]></description><link>https://www.quantbeckman.com/p/with-code-feature-selection-wrapper</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-feature-selection-wrapper</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Sun, 12 Jul 2026 13:11:32 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uWS0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" width="1456" height="911" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:911,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1039286,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zI5r!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zI5r!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png 424w, https://substackcdn.com/image/fetch/$s_!zI5r!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png 848w, https://substackcdn.com/image/fetch/$s_!zI5r!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png 1272w, https://substackcdn.com/image/fetch/$s_!zI5r!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zI5r!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png" width="1068" height="922" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:922,&quot;width&quot;:1068,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:85050,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zI5r!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png 424w, https://substackcdn.com/image/fetch/$s_!zI5r!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png 848w, https://substackcdn.com/image/fetch/$s_!zI5r!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png 1272w, https://substackcdn.com/image/fetch/$s_!zI5r!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F413a87ea-12e9-4c05-983d-80523e9bcb05_1068x922.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p style="text-align: center;"><span>Before you begin, remember that you have an index with the newsletter content </span><strong>organized</strong><span> by clicking below.</span></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CZIx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1151002,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;text&quot;:&quot;INDEX&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/p/welcome"><span>INDEX</span></a></p><div><hr></div><h3><strong>From candidate features to validated trading models</strong></h3><p><span>Wrapper-based feature selection evaluates predictive inputs through the exact model and validation framework intended for live deployment. Unlike statistical filters that judge variables in isolation, wrappers score candidate subsets by repeatedly training the target algorithm under purged cross-validation. This structure ties feature value directly to net economic performance, accounting for turnover, execution fees, and collinearity risks.</span></p><p><strong>What&#8217;s inside:</strong></p><ol><li><p><strong><span>Defining the wrapper paradigm:</span></strong><span> The framework wraps a search algorithm around a target model, scoring subsets on out-of-sample performance to capture joint feature interactions that univariate statistical filters miss.</span></p></li><li><p><strong><span>Accounting for market frictions:</span></strong><span> Objective functions must penalize execution friction, turnover, and subset dimension, ensuring statistical predictive power translates into net strategy returns.</span></p></li><li><p><strong><span>Sequential Forward Selection:</span></strong><span> Building compact feature sets from zero limits latency and operational pipeline fragility, though greedy forward steps risk missing variables that only add value in specific pairs.</span></p></li><li><p><strong><span>Sequential Backward Selection:</span></strong><span> Starting with the complete feature matrix preserves joint interaction structures, but early pruning steps require regularization to manage matrix instability from collinear market inputs.</span></p></li><li><p><strong><span>Exhaustive Feature Selection:</span></strong><span> Evaluating all possible subsets guarantees locating the global in-sample optimum, but exponential search spaces create massive computational costs and high selection bias under noise.</span></p></li><li><p><strong><span>Sequential Floating Selection:</span></strong><span> Integrating conditional backward passes into forward search paths dynamically purges early variable additions that become redundant as complementary features enter the model.</span></p></li><li><p><strong><span>Recursive Feature Elimination:</span></strong><span> Pruning variables based on standardized coefficients or block-permutation scores directly leverages internal model weights, requiring grouped evaluations when features share underlying market drivers.</span></p></li></ol><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!feYe!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ca8216-b4cd-4c8a-9903-824b5a0b6b03_1789x1280.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Sample</div><div class="file-embed-details-h2">575KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/552c9623-2034-4742-8d88-eaae2f8b0f3a.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/552c9623-2034-4742-8d88-eaae2f8b0f3a.pdf"><span class="file-embed-button-text">Download</span></a></div></div>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-feature-selection-wrapper">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Feature selection: Filter-based methods ]]></title><description><![CDATA[Supervised approches]]></description><link>https://www.quantbeckman.com/p/with-code-feature-selection-filter</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-feature-selection-filter</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Tue, 16 Jun 2026 12:53:04 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!c5Nu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!c5Nu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!c5Nu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!c5Nu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!c5Nu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!c5Nu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png" width="1456" height="911" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:911,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1039286,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/201584274?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!c5Nu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!c5Nu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!c5Nu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!c5Nu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2decaa86-b219-4d27-b13f-2583ed9dd00e_1586x992.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GuOQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GuOQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png 424w, https://substackcdn.com/image/fetch/$s_!GuOQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png 848w, https://substackcdn.com/image/fetch/$s_!GuOQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png 1272w, https://substackcdn.com/image/fetch/$s_!GuOQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GuOQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png" width="1032" height="1080" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1080,&quot;width&quot;:1032,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:97084,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/201584274?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GuOQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png 424w, https://substackcdn.com/image/fetch/$s_!GuOQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png 848w, https://substackcdn.com/image/fetch/$s_!GuOQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png 1272w, https://substackcdn.com/image/fetch/$s_!GuOQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F257d7f4d-fb4f-4ce6-96b0-59ed30aaef5f_1032x1080.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized</strong> by clicking below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1vhK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1vhK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!1vhK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!1vhK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!1vhK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1vhK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1151002,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/201584274?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1vhK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!1vhK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!1vhK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!1vhK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffce7fdf2-b963-47d8-b6c6-d61b2b3537c8_1535x1024.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;text&quot;:&quot;INDEX&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/p/welcome"><span>INDEX</span></a></p><div><hr></div><h3><strong>From raw market data to scalable statistical filters</strong></h3><p><span>Filter-based feature selection evaluates the intrinsic statistical properties of candidate variables independent of downstream model training. By screening out noisy inputs before fitting complex algorithms, statistical filters reduce dimensional overreach, limit execution friction, and protect systematic strategies from temporal leakage.</span></p><p><strong>What&#8217;s inside:</strong></p><ol><li><p><strong><span>Defining the filter paradigm:</span></strong><span> Filters evaluate features through direct statistical tests against target labels outside the model training loop, providing a fast screening layer against high-dimensional noise.</span></p></li><li><p><strong><span>Controlling selection risks:</span></strong><span> Ranking variables on statistical scores can mislead researchers if a feature increases turnover, inflates execution costs, or relies on unavailable forward bar data.</span></p></li><li><p><strong><span>Measuring Information Gain:</span></strong><span> Discretizing continuous metrics into quantile bins isolates entropy reduction, revealing non-linear regime thresholds that standard linear filters overlook.</span></p></li><li><p><strong><span>Class separation with Fisher Score and ANOVA:</span></strong><span> Comparing between-class variance against within-class dispersion highlights features with distinct conditional distributions, while Welch&#8217;s ANOVA corrects for fat tails and non-constant variance.</span></p></li><li><p><strong><span>Capturing non-linear signals through Mutual Information:</span></strong><span> Applying continuous nearest-neighbor estimators detects non-linear dependencies, preserving valuable predictive signals such as U-shaped volume reversals.</span></p></li><li><p><strong><span>Uncovering joint dependencies with ReliefF:</span></strong><span> Evaluating instances against their nearest hits and misses in multi-dimensional space isolates localized feature interactions without training a full model.</span></p></li><li><p><strong><span>Preventing collinearity with mRMR:</span></strong><span> Optimizing target relevance against pairwise mutual information keeps feature sets diverse and stops models from clustering around identical risk drivers.</span></p></li></ol><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!mDzE!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2eb7261-b3ad-4995-a80d-e8f3c007a598_985x796.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Sample</div><div class="file-embed-details-h2">386KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/31ec27a5-66a7-45ed-aacd-2b6527dcf479.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/31ec27a5-66a7-45ed-aacd-2b6527dcf479.pdf"><span class="file-embed-button-text">Download</span></a></div></div><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!bNFX!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab4955e0-5d98-4083-9eb5-cd405be3c7fe_753x649.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Feature Selection Filter Methods</div><div class="file-embed-details-h2">789KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/205e1148-f60c-4750-a820-4f793b7c9730.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/205e1148-f60c-4750-a820-4f793b7c9730.pdf"><span class="file-embed-button-text">Download</span></a></div></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://colab.research.google.com/drive/1saQs8iQcbPwHSqb0wHM8JhGXWrjFYa6H?usp=sharing" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5xfi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 424w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 848w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1272w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png" width="300" height="57.074175824175825" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:277,&quot;width&quot;:1456,&quot;resizeWidth&quot;:300,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:&quot;https://colab.research.google.com/drive/1saQs8iQcbPwHSqb0wHM8JhGXWrjFYa6H?usp=sharing&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5xfi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 424w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 848w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1272w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Infra: Free data from Tradingview ]]></title><description><![CDATA[Download all the data from TradingView&#8217;s servers for $0.]]></description><link>https://www.quantbeckman.com/p/with-code-infra-free-data-from-tradingview</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-infra-free-data-from-tradingview</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Mon, 25 May 2026 12:44:06 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uWS0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" width="1456" height="911" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:911,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1039286,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rLmG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rLmG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png 424w, https://substackcdn.com/image/fetch/$s_!rLmG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png 848w, https://substackcdn.com/image/fetch/$s_!rLmG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png 1272w, https://substackcdn.com/image/fetch/$s_!rLmG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rLmG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png" width="1031" height="1135" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1135,&quot;width&quot;:1031,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:97228,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rLmG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png 424w, https://substackcdn.com/image/fetch/$s_!rLmG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png 848w, https://substackcdn.com/image/fetch/$s_!rLmG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png 1272w, https://substackcdn.com/image/fetch/$s_!rLmG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd324619-49e3-42e1-9a52-6686553e8c02_1031x1135.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized</strong> by clicking below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CZIx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1151002,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;text&quot;:&quot;INDEX&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.quantbeckman.com/p/welcome"><span>INDEX</span></a></p><div><hr></div><h3><strong>From market endpoints to research-ready data</strong></h3><p><span>Quantitative trading strategies depend on the historical records that feed them. Acquiring free market data through custom web scrapers lowers upfront capital costs and expands research flexibility, but it introduces operational risks like protocol shifts, rate limits, and missing fields. Building a transparent pipeline around raw WebSocket connections ensures data transformations remain visible and verifiable before strategy testing begins.</span></p><p><strong>What&#8217;s inside:</strong></p><ol><li><p><strong><span>Data trade-offs:</span></strong><span> Free feeds provide rapid experimentation and broad market access, but lack the service-level guarantees, corporate-action histories, and delisting data typical of institutional vendors.</span></p></li><li><p><strong><span>Managing protocol fragility:</span></strong><span> Relying on unofficial endpoints exposes pipelines to breakage when vendors adjust message headers, change packet envelopes, or enforce stricter rate limits.</span></p></li><li><p><strong><span>Maintaining a tight dependency envelope:</span></strong><span> Constructing sockets and HTTP requests using standard library modules keeps data transformations visible and prevents third-party wrappers from hiding parsing errors.</span></p></li><li><p><strong><span>Handling raw WebSocket transport:</span></strong><span> Direct management of TCP connections, TLS handshakes, frame masking, and ping-pong liveness maintains link stability across large multi-symbol requests.</span></p></li><li><p><strong><span>Parsing TradingView message envelopes:</span></strong><span> Reading length-prefixed packet markers prevents buffer corruption, while explicit session and adjustment flags document whether prices reflect raw or split-adjusted trades.</span></p></li><li><p><strong><span>Enforcing structured schema validation:</span></strong><span> Formatting raw bars into typed NumPy arrays preserves missing volume as unallocated values rather than zero, protecting liquidity filters from distortion.</span></p></li><li><p><strong><span>Harvesting and de-duplicating universes:</span></strong><span> Paginating scanner endpoints allows researchers to build multi-asset ticker lists, though static current-day universes carry survivorship bias that requires explicit research controls.</span></p></li></ol><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!A0I7!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F777ba3be-e05f-437a-bbcd-d6be502b9aa2_985x796.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Sample</div><div class="file-embed-details-h2">429KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/0004a6e1-5c1f-44d5-aae0-8cc80a555103.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/0004a6e1-5c1f-44d5-aae0-8cc80a555103.pdf"><span class="file-embed-button-text">Download</span></a></div></div>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-infra-free-data-from-tradingview">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Data transformations: Text preprocessing ]]></title><description><![CDATA[From market language to model input]]></description><link>https://www.quantbeckman.com/p/with-code-data-transformations-text</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-data-transformations-text</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Mon, 04 May 2026 11:45:13 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uWS0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" width="1456" height="911" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:911,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1039286,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!s1lx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!s1lx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png 424w, https://substackcdn.com/image/fetch/$s_!s1lx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png 848w, https://substackcdn.com/image/fetch/$s_!s1lx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png 1272w, https://substackcdn.com/image/fetch/$s_!s1lx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!s1lx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png" width="1029" height="1102" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1102,&quot;width&quot;:1029,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:95127,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/195628963?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!s1lx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png 424w, https://substackcdn.com/image/fetch/$s_!s1lx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png 848w, https://substackcdn.com/image/fetch/$s_!s1lx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png 1272w, https://substackcdn.com/image/fetch/$s_!s1lx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88d4d2ba-ac98-4923-8b80-a526b70254f3_1029x1102.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized</strong> by clicking below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CZIx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1151002,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;text&quot;:&quot;INDEX&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.quantbeckman.com/p/welcome"><span>INDEX</span></a></p><div><hr></div><p><strong>From raw text streams to market trading signals</strong></p><p><span>Text preprocessing in quantitative trading acts as an active signal-shaping operator rather than a passive data-cleaning routine. Splitting tokens, stripping punctuation, or deleting words directly alters the geometry of the feature space, changing the conditional expectation of forward returns and impacting strategy execution.</span></p><p><strong>What&#8217;s inside:</strong></p><ol><li><p><strong><span>Evaluating preprocessing as a signal operator:</span></strong><span> Preprocessing logic directly modifies input vectors, meaning every string transformation parameterizes the feature space and dictates what the classifier can observe.</span></p></li><li><p><strong><span>Preventing semantic and structural failure modes:</span></strong><span> Unaligned text rules introduce structural risks, including semantic inversion from deleted negations, unstable feature representations across vendor feeds, and collapsed entity definitions.</span></p></li><li><p><strong><span>Preserving entity meaning through canonicalization:</span></strong><span> Universal lowercasing acts as a destructive projection operator, merging distinct ticker symbols, corporate entities, and ordinary nouns into identical feature coordinates.</span></p></li><li><p><strong><span>Defining typed token boundaries:</span></strong><span> Protected regular expressions preserve specialized financial spans, keeping currency pairs, percentages, and execution log values intact before standard tokenizers shatter them.</span></p></li><li><p><strong><span>Protecting logical sign with targeted stopword filters:</span></strong><span> Retaining direction-governing terms like &#8220;not,&#8221; &#8220;under,&#8221; and &#8220;without&#8221; prevents semantic inversion in sentiment classification and stops false trade signals.</span></p></li><li><p><strong><span>Validating morphological reduction under economic loss:</span></strong><span> Naive stemming merges distinct temporal and probabilistic risk states, making it necessary to distill contextual language model insights into deterministic lookup tables.</span></p></li><li><p><strong><span>Combining multi-channel sparse and dense representations:</span></strong><span> Ensembling deterministic sparse vectors with dense contextual embeddings balances explicit, auditable feature attribution against abstract semantic interpretation.</span></p></li></ol><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!9lNC!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cda77a-97df-430a-bff0-fe62c67ca120_985x796.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Sample</div><div class="file-embed-details-h2">759KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/9b8d0926-7e56-4aa9-950b-bdf1be5b9995.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/9b8d0926-7e56-4aa9-950b-bdf1be5b9995.pdf"><span class="file-embed-button-text">Download</span></a></div></div><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!p-kf!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F641069a0-4f10-4a29-98b0-e4ace3cc2a9a_480x540.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Text Preprocessing</div><div class="file-embed-details-h2">1.29MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/23d6f716-629f-492d-9ad7-d4f26841f961.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/23d6f716-629f-492d-9ad7-d4f26841f961.pdf"><span class="file-embed-button-text">Download</span></a></div></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://colab.research.google.com/drive/1EbwgiScDep5nfgdya-iv9suWPhqNKMqX?usp=sharing" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5xfi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 424w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 848w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1272w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png" width="302" height="57.45467032967033" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:277,&quot;width&quot;:1456,&quot;resizeWidth&quot;:302,&quot;bytes&quot;:40536,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://colab.research.google.com/drive/1EbwgiScDep5nfgdya-iv9suWPhqNKMqX?usp=sharing&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/205475473?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!5xfi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 424w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 848w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1272w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><a href="https://www.quantbeckman.com/api/v1/file/fd6ef173-2114-425a-bb8f-8561e53e5ed4.pdf">Download</a></p>]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Data transformations: Data shape and predictive features ]]></title><description><![CDATA[Table of contents:]]></description><link>https://www.quantbeckman.com/p/with-code-data-transformations-data</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-data-transformations-data</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Mon, 13 Apr 2026 11:45:13 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uWS0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" width="1456" height="911" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:911,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fAPp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fAPp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png 424w, https://substackcdn.com/image/fetch/$s_!fAPp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png 848w, https://substackcdn.com/image/fetch/$s_!fAPp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png 1272w, https://substackcdn.com/image/fetch/$s_!fAPp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fAPp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png" width="1374" height="1209" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1209,&quot;width&quot;:1374,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:108205,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/193786830?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fAPp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png 424w, https://substackcdn.com/image/fetch/$s_!fAPp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png 848w, https://substackcdn.com/image/fetch/$s_!fAPp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png 1272w, https://substackcdn.com/image/fetch/$s_!fAPp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42232cfc-86e8-4448-82c4-2559e1eaea6f_1374x1209.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read the newsletter index</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CZIx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;text&quot;:&quot;INDEX&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/p/welcome"><span>INDEX</span></a></p><div><hr></div><h4><strong>From raw price paths to structurally aligned features</strong></h4><p><span>Data-shape transformation represents the first structural choice in model construction rather than a routine cleanup step. Every transformation operator selects specific variations in a market path while suppressing others, determining whether a model observes genuine economic signals or sample-specific noise. Aligning these operators with the strategy&#8217;s target holding period prevents false statistical stability and preserves the underlying alpha.</span></p><p><strong>What&#8217;s inside:</strong></p><ol><li><p><strong><span>Reframing data transformations:</span></strong><span> Treating transformations as active structural choices ensures the mathematical shape of the input feature matches the economic mechanism behind the trade.</span></p></li><li><p><strong><span>Recognizing transformation risks:</span></strong><span> Indiscriminate differencing or scaling can amputate trend signals, introduce causality leakage through full-sample parameters, or distort high-dimensional distance geometry.</span></p></li><li><p><strong><span>Extracting state-space surprises:</span></strong><span> Applying local linear trend models yields one-step-ahead forecast errors, isolating orthogonal price surprises from predictable local drift.</span></p></li><li><p><strong><span>Neutralizing dynamic benchmarks:</span></strong><span> Rolling regression residuals project asset returns into the null space of a benchmark, stripping out market beta to expose pure relative-value dislocations.</span></p></li><li><p><strong><span>Eliminating stochastic drift via cointegration:</span></strong><span> Engle-Granger equilibrium spreads remove shared unit-root trends between paired assets, producing stationary inputs designed for mean-reversion models.</span></p></li><li><p><strong><span>Isolating multiscale wavelet details:</span></strong><span> Discrete Haar wavelet filter banks decouple overlapping frequency bands, targeting scale-specific shocks while filtering out macro drift and microstructural noise.</span></p></li><li><p><strong><span>Encoding bounded state ranges:</span></strong><span> Rolling min-max positions compress nominal prices into a [0, 1] interval, removing cross-sectional scale disparities and allowing tree algorithms to process boundary rejections.</span></p></li></ol><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!AVQr!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa451fd6e-a840-4db0-81b8-726ba5764aa8_985x796.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Sample</div><div class="file-embed-details-h2">450KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/8dff7726-a7a3-42b4-9949-8689d82e46d4.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/8dff7726-a7a3-42b4-9949-8689d82e46d4.pdf"><span class="file-embed-button-text">Download</span></a></div></div>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-data-transformations-data">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Data transformations: Time series preparation ]]></title><description><![CDATA[The hidden statistical problems inside untransformed market series]]></description><link>https://www.quantbeckman.com/p/with-code-data-transformations-preprocessing</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-data-transformations-preprocessing</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Fri, 27 Mar 2026 15:55:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uWS0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png" width="1456" height="911" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:911,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uWS0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 424w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 848w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1272w, https://substackcdn.com/image/fetch/$s_!uWS0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e362632-ec02-4161-b0cc-44aa6d20ad66_1586x992.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Kzml!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Kzml!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png 424w, https://substackcdn.com/image/fetch/$s_!Kzml!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png 848w, https://substackcdn.com/image/fetch/$s_!Kzml!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png 1272w, https://substackcdn.com/image/fetch/$s_!Kzml!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Kzml!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png" width="940" height="1133" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1133,&quot;width&quot;:940,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:107188,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/191955670?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Kzml!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png 424w, https://substackcdn.com/image/fetch/$s_!Kzml!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png 848w, https://substackcdn.com/image/fetch/$s_!Kzml!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png 1272w, https://substackcdn.com/image/fetch/$s_!Kzml!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dac974-82f0-47ff-9ad8-566b9c59d0f9_940x1133.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read the newsletter index</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CZIx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CZIx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 424w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 848w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!CZIx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F574b714e-6f5e-4ee8-b8a6-6a3b64ec3da2_1535x1024.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;text&quot;:&quot;INDEX&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/p/welcome"><span>INDEX</span></a></p><div><hr></div><p><strong>From raw market series to stationary model inputs</strong></p><p><span>Raw price series carry unbounded drift, non-constant variance, and heavy tails that break numerical optimizers through singular matrices and vanishing gradients. Effective time series preparation replaces naive preprocessing with targeted mathematical operators that make market structure legible to learning algorithms without destroying predictive memory or leaking future data.</span></p><p><strong>What&#8217;s inside:</strong></p><ol><li><p><strong><span>Controlling transformation risks:</span></strong><span> Standard Z-scores distort skewed downside risks, hard Winsorization erases capitulation signals, and global scaling leaks future variance into historical training sets.</span></p></li><li><p><strong><span>Preserving long-range memory:</span></strong><span> Integer differencing (d=1) discards over 60% of predictive memory, whereas fractional differencing uses exponentially decaying weights to achieve stationarity while retaining historical path memory.</span></p></li><li><p><strong><span>Mapping fat-tailed distributions:</span></strong><span> Converting skewed returns through Cornish-Fisher expansions or rank-to-Gaussian transforms prevents optimizers from treating real market tail shocks as mathematically impossible anomalies.</span></p></li><li><p><strong><span>Sampling on information time:</span></strong><span> Chronological time bars generate zero-inflated distributions and intraday volatility spikes, while Dollar Bars sample data on dynamic fiat thresholds to produce more stable return properties.</span></p></li><li><p><strong><span>Eliminating volatility ghost effects:</span></strong><span> Standardizing returns with GARCH(1,1) conditional volatility adjusts for autoregressive variance clustering, avoiding the abrupt artificial drops caused by flat rolling lookback windows.</span></p></li><li><p><strong><span>Mitigating noise and multicollinearity:</span></strong><span> Combining Shannon entropy soft-clipping with Marchenko-Pastur PCA thresholding compresses extreme shocks without losing rank order and strips out correlated noise vectors.</span></p></li><li><p><strong><span>Reconstructing phase-space geometry:</span></strong><span> Applying Takens&#8217; Delay Embedding Theorem lifts 1D price series into multi-dimensional phase space, exposing hidden market regime structures to spatial classifiers.</span></p></li></ol><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!JmYs!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F233f304d-45cc-4f6a-8bd4-db87b3a196ea_985x796.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Sample</div><div class="file-embed-details-h2">650KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/7fb18cda-de77-4c84-ae81-e1cbed8e9ab6.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/7fb18cda-de77-4c84-ae81-e1cbed8e9ab6.pdf"><span class="file-embed-button-text">Download</span></a></div></div><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!clG5!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcd612009-7344-4d92-96ba-04d6414331c3_480x540.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Time Series Preparation</div><div class="file-embed-details-h2">1.25MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/58dfaad9-58aa-404e-b8a0-26d8f1b94398.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/58dfaad9-58aa-404e-b8a0-26d8f1b94398.pdf"><span class="file-embed-button-text">Download</span></a></div></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://colab.research.google.com/drive/1K8CrwSpW3dF9ElKvLEW3waoI4c7o2nQr?usp=sharing" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5xfi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 424w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 848w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1272w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png" width="299" height="56.88392857142857" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:277,&quot;width&quot;:1456,&quot;resizeWidth&quot;:299,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:&quot;https://colab.research.google.com/drive/1K8CrwSpW3dF9ElKvLEW3waoI4c7o2nQr?usp=sharing&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5xfi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 424w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 848w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1272w, https://substackcdn.com/image/fetch/$s_!5xfi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faaeea670-8cac-489b-ae9a-c4b9b7a6dc06_1539x293.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><h4></h4><p> </p>]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Infra: Financial APIs ]]></title><description><![CDATA[How to build a trading API]]></description><link>https://www.quantbeckman.com/p/with-code-infra-financial-apis</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-infra-financial-apis</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Fri, 13 Mar 2026 13:52:22 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>The API layer.</p></li><li><p>Protocol semantics, transport choice, and system state.</p></li><li><p>Risk and financial APIs limitations.</p></li><li><p>Historical-live non-equivalence and data reconstruction error.</p></li><li><p>Execution state machines and order routing.</p></li><li><p>Simulation and live trading.</p></li><li><p>Creating the architecture for a trading API.</p></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized</strong> by clicking on &#8220;<em>Read the newsletter index</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Introduction</strong></h4><p>Before moving on to the next series, there&#8217;s an important point that any algorithmic trader needs to know: the point about APIs. </p><p>A quant can state the issue in simple terms. Let <em>s<sub>t</sub></em>&#8203; denote latent market state and let <em>x<sub>t</sub></em>&#8203; denote observed state at decision time. The strategy trades on <em>x<sub>t</sub></em>&#8203; rather than <em>s<sub>t</sub></em>&#8203;. The transformation from <em>s<sub>t</sub></em>&#8203; to <em>x<sub>t</sub></em>&#8203; includes vendor capture, transport, buffering, schema validation, clock alignment, and local state reconstruction. When that transformation changes, the strategy changes with it, even if the signal formula stays fixed. </p><p>The API layer belongs to the hypothesis itself because it determines the admissible information set on which the strategy acts.</p><p>This point matters because the gap between research and deployment tends to emerge at the boundary where information becomes software state. Bars in a notebook arrive sorted and complete. Live messages arrive through sessions, quotes, reconnect paths, and transport rules. Orders in a backtest pass from signal to fill in one step. Orders in production pass through acceptance, routing, queue interaction, etc. In a systematic process, those choices carry mathematical weight because they alter the state space on which the strategy operates.</p><p>The live strategy therefore admits a fuller description than the research signal alone. A useful expression is</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\text{Live strategy} = \\text{Signal model} + \\text{Information contract} + \\text{Execution contract} + \\text{Reconciliation logic}.&quot;,&quot;id&quot;:&quot;SMVQEKKDTA&quot;}" data-component-name="LatexBlockToDOM"></div><p>Many teams place their energy on features, models, and optimization while the interface layer receives less formal treatment. That habit carries a cost. A signal derived from one information regime and deployed through another regime becomes a different object. A simulator that treats execution as an atomic event studies a market process distinct from the one live code encounters.</p><p>The API layer is where these fractures surface. It is also where discipline can restore coherence. If you want a stack to start with, here it is:</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!9ehK!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d3c6d3f-f0ab-4f46-a8f6-a88b28e6094c_542x763.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Algorithmic Trading in the Global FX Market</div><div class="file-embed-details-h2">176KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/faf01f8c-e6c6-481d-8aaf-46526281bdb0.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/faf01f8c-e6c6-481d-8aaf-46526281bdb0.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>An API is an information contract, a timing contract, and an execution contract. That&#8217;s to say, data fields need causal meaning, timestamps need clear authority, order states need legal transitions, internal services need shared schemas across research, simulation, and live trading. Do you get the ida right? Under those conditions, performance claims acquire stronger footing because the path from observation to action remains explicit.</p><p>The subject here is the form through which market enters the strategy and the form through which strategy intent reaches the market. Code style, framework choice, and endpoint count matter through their effect on that form. A live trading stack earns coherence when the objects that move across it stay explicit, typed, and stable across the full path from research to execution. That coherence is a design choice and it begins at the API layer.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!r9dP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!r9dP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!r9dP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!r9dP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!r9dP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!r9dP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:768483,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!r9dP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!r9dP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!r9dP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!r9dP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1f6989e-1012-46f5-bc8e-bd1771886d19_1536x1024.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>The API layer</h4><p>In a trading system, the central question asks what is known, when it becomes known, and which representation carries it. The API layer lives at that boundary. It defines the admissible information set available to the strategy at decision time. That framing captures the practical source of failure with precision.</p><p>Let the latent market state at event index <em>t</em> be <em>s<sub>t</sub></em>. The strategy never accesses that state in raw form. It receives an encoded view <em>x<sub>t</sub></em>, produced through venue generation, vendor capture, normalization, transport, buffering, decoding, and local deserialization. If those operations are compressed into a single operator <em>H</em>, then the strategy acts on</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;x_t = H(s_t)&quot;,&quot;id&quot;:&quot;JXTUHTSDVI&quot;}" data-component-name="LatexBlockToDOM"></div><p>In live systems, <em>H</em> belongs to a family of transformations shaped by endpoint choice, session state, codec, throttling behavior, aggregation policy, and recovery logic. The research question therefore asks two things at once: whether a mapping  <em>f</em>(<em>x<sub>t</sub></em>) carries predictive value, and whether the <em>x<sub>t</sub></em> used in research belongs to the same family as the <em>x<sub>t </sub></em>available in deployment.</p><p>A useful decomposition is</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;x_t^{obs} = H_t(s_{0:t}) + \\varepsilon_t&quot;,&quot;id&quot;:&quot;PAVKYAWEWM&quot;}" data-component-name="LatexBlockToDOM"></div><p>The term <em>H<sub>t</sub></em>(<em>s<sub>0:t</sub></em>) shows that observed state may depend on a path rather than a single contemporaneous latent state. Corrections, delayed packets, local aggregation, and dropped messages create that path dependence. The residual term &#949;<em><sub>t</sub></em> collects the user-facing uncertainty generated by packet loss, schema surprises, decoder failure, clock mismatch, and hidden vendor behavior. In a calm hourly-bar strategy, &#949;<em><sub>t</sub></em> may occupy a small role. In a short-horizon or execution-sensitive strategy, it often defines the core challenge.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gm9c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gm9c!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png 424w, https://substackcdn.com/image/fetch/$s_!gm9c!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png 848w, https://substackcdn.com/image/fetch/$s_!gm9c!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png 1272w, https://substackcdn.com/image/fetch/$s_!gm9c!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!gm9c!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png" width="1456" height="899" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:899,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;A Crash Course on REST APIs - ByteByteGo Newsletter&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="A Crash Course on REST APIs - ByteByteGo Newsletter" title="A Crash Course on REST APIs - ByteByteGo Newsletter" srcset="https://substackcdn.com/image/fetch/$s_!gm9c!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png 424w, https://substackcdn.com/image/fetch/$s_!gm9c!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png 848w, https://substackcdn.com/image/fetch/$s_!gm9c!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png 1272w, https://substackcdn.com/image/fetch/$s_!gm9c!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3699a497-03e9-47b7-b294-b41491e3b5ab_1600x988.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This perspective dissolves a common misconception. Many quants treat the API as a latency channel and connect latency risk with high-frequency trading. A richer view sees the API as a state-visibility mechanism. Consider a breakout rule triggered when price crosses a level and volume in the same interval exceeds a threshold. One endpoint may publish aggregated bars with finalized volume, while another may publish incremental trades that require local accumulation. The rule then sees different objects. </p><blockquote><p>Speed alone does not resolve that difference. One representation is final. The other is provisional. One is bounded by bar closure. The other evolves through the interval. If research evaluates the final object while deployment trades the provisional one, then backtest and live trading inhabit different information worlds.</p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iINK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iINK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png 424w, https://substackcdn.com/image/fetch/$s_!iINK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png 848w, https://substackcdn.com/image/fetch/$s_!iINK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png 1272w, https://substackcdn.com/image/fetch/$s_!iINK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!iINK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png" width="1189" height="590" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:590,&quot;width&quot;:1189,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iINK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png 424w, https://substackcdn.com/image/fetch/$s_!iINK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png 848w, https://substackcdn.com/image/fetch/$s_!iINK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png 1272w, https://substackcdn.com/image/fetch/$s_!iINK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c9fb5ed-8c93-46af-8e5a-9e13968167c1_1189x590.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The official protocol and framework documentation supports this view from the software side. HTTP semantics are defined as stateless at the application-protocol level. That property makes HTTP well suited for resource retrieval, explicit commands, and idempotent inspection, while state continuity moves into higher layers. The OpenAPI specification makes HTTP service capabilities machine-readable through paths, parameters, schemas, and responses. FastAPI builds on that structure by generating OpenAPI descriptions and interactive documentation from Python declarations. Pydantic treats typed data models as executable contracts and emits JSON Schema from those models. Finance gives special weight to this alignment because state integrity sits at the center of the problem.</p><p>For a systematic trading system, the internal data model becomes a compact statement of admissible knowledge. A well-defined <code>Bar</code> object can enforce monotonic timestamps, nonnegative volume, high-low consistency, a declared timezone, and provenance metadata. A <code>Signal</code> object can enforce a causal timestamp, a horizon, a generation identifier, and a confidence field interpreted consistently by the risk layer. An <code>OrderIntent</code> object can separate intent from broker acknowledgment, so the system preserves a clean distinction between desired action, accepted action, and executed action. These structures carry operational value because they keep portfolio state, execution state, and research state aligned across time.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><h4>Protocol semantics, transport choice, and system state</h4><p>A familiar hierarchy pairs REST with slower interactions, WebSocket with streaming interactions, and FIX with institutional workflows. A stronger formulation sees each protocol as an organizer of interaction and, through that role, as a designer of local architecture.</p><p>Start with HTTP. Under RFC 9110, HTTP defines resource-oriented request-response semantics, methods, status codes, headers, and representations. It serves clear operations with strong structure: fetching historical bars, querying account state, placing an order, canceling an order, requesting configuration, and obtaining a health report. It also supports documentation with high clarity. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yUPz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yUPz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png 424w, https://substackcdn.com/image/fetch/$s_!yUPz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png 848w, https://substackcdn.com/image/fetch/$s_!yUPz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png 1272w, https://substackcdn.com/image/fetch/$s_!yUPz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!yUPz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png" width="859" height="277" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/75af3677-fc18-45b2-b377-38b30148585f_859x277.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:277,&quot;width&quot;:859,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:58617,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yUPz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png 424w, https://substackcdn.com/image/fetch/$s_!yUPz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png 848w, https://substackcdn.com/image/fetch/$s_!yUPz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png 1272w, https://substackcdn.com/image/fetch/$s_!yUPz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75af3677-fc18-45b2-b377-38b30148585f_859x277.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Request methods</figcaption></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LpqW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LpqW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png 424w, https://substackcdn.com/image/fetch/$s_!LpqW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png 848w, https://substackcdn.com/image/fetch/$s_!LpqW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png 1272w, https://substackcdn.com/image/fetch/$s_!LpqW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!LpqW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png" width="859" height="430" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:430,&quot;width&quot;:859,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:76737,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!LpqW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png 424w, https://substackcdn.com/image/fetch/$s_!LpqW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png 848w, https://substackcdn.com/image/fetch/$s_!LpqW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png 1272w, https://substackcdn.com/image/fetch/$s_!LpqW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F358ddfb6-0a76-490f-b256-3e916bc54ef7_859x430.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Status codes</figcaption></figure></div><p>For internal trading services, that structure has high value. The stack gains clear contracts, reproducible input schemas, and rapid client generation.</p><p>HTTP also channels interaction through discrete exchanges. Applications that consume a continuous stream of trade events, order updates, heartbeats, or PnL deltas benefit from WebSocket. RFC 6455 defines a bidirectional protocol over TCP with an opening handshake and framed messages, built for two-way communication.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IKp0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IKp0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png 424w, https://substackcdn.com/image/fetch/$s_!IKp0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png 848w, https://substackcdn.com/image/fetch/$s_!IKp0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png 1272w, https://substackcdn.com/image/fetch/$s_!IKp0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!IKp0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png" width="859" height="289" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d752399c-a161-4996-aacd-40a728e3feec_859x289.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:289,&quot;width&quot;:859,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:40881,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IKp0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png 424w, https://substackcdn.com/image/fetch/$s_!IKp0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png 848w, https://substackcdn.com/image/fetch/$s_!IKp0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png 1272w, https://substackcdn.com/image/fetch/$s_!IKp0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd752399c-a161-4996-aacd-40a728e3feec_859x289.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Comparison</figcaption></figure></div><p>For example, Alpaca&#8217;s streaming documentation&#8212;I mean, the broker&#8212;presents trade, account, and order updates over WebSocket and highlights frame types across endpoints, plus authorization and stream subscription after connection. For a quant system, the implication is clear: <em>WebSocket provides continuity, and continuity assigns responsibility.</em> A persistent connection requires reconnect handling, duplicate control, interval recovery, subscription replay, sequence validation, and local buffering.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eiUI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eiUI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png 424w, https://substackcdn.com/image/fetch/$s_!eiUI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png 848w, https://substackcdn.com/image/fetch/$s_!eiUI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png 1272w, https://substackcdn.com/image/fetch/$s_!eiUI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!eiUI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png" width="1047" height="597" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:597,&quot;width&quot;:1047,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:119806,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eiUI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png 424w, https://substackcdn.com/image/fetch/$s_!eiUI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png 848w, https://substackcdn.com/image/fetch/$s_!eiUI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png 1272w, https://substackcdn.com/image/fetch/$s_!eiUI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F21bd769e-cd32-49e5-9131-727dd6371a19_1047x597.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;plaintext&quot;,&quot;nodeId&quot;:&quot;fec69625-79aa-45ea-b712-1a1dbb8974f7&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-plaintext">{
  "action": "auth",
  "key": "{YOUR_API_KEY_ID}",
  "secret": "{YOUR_API_SECRET_KEY}"
}</code></pre></div><p><em>FIX</em> addresses a different problem. The FIX Trading Community describes FIX as: </p><blockquote><p>A standardized transaction language across the securities trade cycle, with message types aligned to steps such as quote request and new order, and with standardized fields as the building blocks. </p></blockquote><p>That matters because institutional trading depends on precise business semantics across a workflow that spans indication, order, execution report, allocation, and post-trade reporting. FIX draws strength from a protocol that already speaks the language of the transaction. A modern retail or semi-professional quant stack may begin on other connectivity paths, yet it can still absorb the lesson: trading systems gain stability when message meanings stay explicit, durable, and tied to the lifecycle they govern.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6P6Y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6P6Y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png 424w, https://substackcdn.com/image/fetch/$s_!6P6Y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png 848w, https://substackcdn.com/image/fetch/$s_!6P6Y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png 1272w, https://substackcdn.com/image/fetch/$s_!6P6Y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!6P6Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png" width="1171" height="727" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:727,&quot;width&quot;:1171,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:295288,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6P6Y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png 424w, https://substackcdn.com/image/fetch/$s_!6P6Y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png 848w, https://substackcdn.com/image/fetch/$s_!6P6Y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png 1272w, https://substackcdn.com/image/fetch/$s_!6P6Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d7ebda4-eefa-441c-bcff-e95bd231abcc_1171x727.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">FIX Protocol and technical standards</figcaption></figure></div><p>The mathematics of transport stays simple. Let <em>T<sub>total</sub></em> denote end-to-end delay from the moment a signal becomes ready to the moment a fill event reaches local state. Then</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;T_{\\text{total}} = T_{\\text{signal}} + T_{\\text{serialize}} + T_{\\text{uplink}} + T_{\\text{broker}} + T_{\\text{venue}} + T_{\\text{downlink}} + T_{\\text{reconcile}}&quot;,&quot;id&quot;:&quot;WLAXFIHBQX&quot;}" data-component-name="LatexBlockToDOM"></div><p>A robust analysis evaluates the whole sum. In many implementations, <em>T<sub>broker</sub></em> and <em>T<sub>reconcile</sub></em> generate more variation than raw network travel time. Serialization, authentication, request signing, broker throttling, internal queuing, and local reconciliation can dominate the tails. Tail behavior matters more than averages because losses tend to concentrate there. A mean delay of 20 ms with a 95th percentile of 60 ms may fit one strategy and damage another whose edge concentrates in short-lived bursts.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iJka!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iJka!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png 424w, https://substackcdn.com/image/fetch/$s_!iJka!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png 848w, https://substackcdn.com/image/fetch/$s_!iJka!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png 1272w, https://substackcdn.com/image/fetch/$s_!iJka!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!iJka!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png" width="765" height="449" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:449,&quot;width&quot;:765,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:33948,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iJka!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png 424w, https://substackcdn.com/image/fetch/$s_!iJka!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png 848w, https://substackcdn.com/image/fetch/$s_!iJka!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png 1272w, https://substackcdn.com/image/fetch/$s_!iJka!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4ab6b56-6d43-48d5-9ac6-ece5e203f7e8_765x449.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The histogram decomposes end-to-end order latency into plausible components and shows the aggregate distribution. The plot reveals that total latency is a sum of heterogeneous terms and that the tail can widen even when most components remain well behaved.</p><h4>Risk and financial APIs limitation</h4><p>Once the API is treated as a formal component, the next task is classification. Risk in financial APIs represents a family of failure modes that interact. A useful taxonomy separates at least seven categories: <em>data risk, temporal risk, schema risk, session risk, throughput risk, execution risk, and governance risk. </em></p><ol><li><p><strong>Data risk</strong><em> </em>concerns what is transmitted. This includes missing fields, inconsistent bar construction, delayed corrections, undocumented transformations, and ambiguity about what a timestamp refers to. Is the bar timestamp the interval open, the interval close, or publication time? Are trades condition-filtered? Are odd lots included? Is volume consolidated at the same cadence in historical and live endpoints? Vendor documentation sometimes answers these questions, sometimes not. The point is that the risk exists even when the feed looks clean. A clean feed can still describe the wrong object for the strategy.</p></li><li><p><strong>Temporal risk </strong>concerns when the data is available and in which order. Clock drift between client, broker, and data vendor is the obvious part. More subtle is the distinction between event time and arrival time. A message may encode one time but arrive at another. Streaming systems can also deliver temporary disorder. If the strategy assumes a total order while the transport offers only a best-effort order plus reconnect recovery, then the local state machine must restore order or at least detect that order was broken.</p></li><li><p><strong>Schema risk</strong> concerns how the data is structured. An endpoint that changes a field name, adds a nullable attribute, changes a numeric type from integer to string, or alters nesting may be perfectly legal at the API level and still break live trading. This is why type validation libraries matter. </p></li><li><p><strong>Session risk </strong>is operational but critical. Authentication expiry, refresh tokens, signed requests, environment separation, and broker session exclusivity determine whether the trading path remains live. Some APIs are quite clear that trading-enabled sessions have different restrictions than generic access, including brokerage-level single-session limits.</p></li><li><p><strong>Throughput risk </strong>includes rate limits, quotas, pagination, backpressure, and resource exhaustion. This category is often underestimated by quants who work first in notebooks. Yet the feasible research surface is shaped by what the API allows. If a cross-sectional study requires more requests per minute than the provider permits, then the signal either needs a different collection design, a local cache, a licensed feed, or a narrower universe. Throughput is a constraint on the set of admissible strategies.</p></li><li><p><strong>Execution risk</strong> concerns the gap between request acceptance and actual market interaction. Here a good example of why this matters: &#8220;the order object has identifiers, statuses, and a lifecycle that can be queried after placement. That is useful, but it also confirms the obvious point that the initial placement call is only one transition in a larger process. SEC routing and execution disclosures exist because the path from broker to venue materially affects outcomes&#8221;. In trading, the request path and the execution path should never be conflated.</p></li><li><p><strong>Governance risk</strong> covers versioning, deprecation, environment drift, and organizational ambiguity. An internal API that is undocumented, lacks version tags, or mixes research-only fields with live-only fields can corrupt an otherwise sound strategy. Governance is what keeps the contract stable long enough for experiments to be comparable.</p></li></ol><p>A convenient formalization is to model observed system risk as the union of these categories:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;R_{\\mathrm{API}}\n=\nR_{\\mathrm{data}}\n\\cup\nR_{\\mathrm{time}}\n\\cup\nR_{\\mathrm{schema}}\n\\cup\nR_{\\mathrm{session}}\n\\cup\nR_{\\mathrm{throughput}}\n\\cup\nR_{\\mathrm{exec}}\n\\cup\nR_{\\mathrm{governance}}&quot;,&quot;id&quot;:&quot;VEJBSYOXAB&quot;}" data-component-name="LatexBlockToDOM"></div><p>Each component can be associated with observables: gap counts, duplicate counts, validation failures, authentication renewals, request rejections, order rejects, and contract-version mismatches. The important point is that API risk becomes measurable only after categories are explicit.</p><p>The table below compresses this pitfalls:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YT9s!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YT9s!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png 424w, https://substackcdn.com/image/fetch/$s_!YT9s!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png 848w, https://substackcdn.com/image/fetch/$s_!YT9s!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png 1272w, https://substackcdn.com/image/fetch/$s_!YT9s!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!YT9s!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png" width="1021" height="645" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:645,&quot;width&quot;:1021,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:114949,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!YT9s!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png 424w, https://substackcdn.com/image/fetch/$s_!YT9s!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png 848w, https://substackcdn.com/image/fetch/$s_!YT9s!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png 1272w, https://substackcdn.com/image/fetch/$s_!YT9s!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07bdaea1-ad3d-4b3c-883b-92bfbc33a22c_1021x645.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Throughput deserves one extra comment because it changes research conclusions. Assume that trade opportunities survive for a characteristic time constant &#964; and that the system polls every &#916; units of time rather than consuming a push stream. A rough first-order miss model is:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(\\mathrm{miss}) \\approx \\exp\\!\\left(-\\frac{\\tau}{\\Delta}\\right)&quot;,&quot;id&quot;:&quot;WJHNJQHEUH&quot;}" data-component-name="LatexBlockToDOM"></div><p>This formula is for illustrative purposes. It says that when opportunity duration is short relative to the observation interval, miss probability rises quickly. Coarser polling  changes the distribution of observed opportunities. The picture below visualizes that relationship:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BhWp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BhWp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png 424w, https://substackcdn.com/image/fetch/$s_!BhWp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png 848w, https://substackcdn.com/image/fetch/$s_!BhWp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png 1272w, https://substackcdn.com/image/fetch/$s_!BhWp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!BhWp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png" width="610" height="361" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:361,&quot;width&quot;:610,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:29377,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BhWp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png 424w, https://substackcdn.com/image/fetch/$s_!BhWp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png 848w, https://substackcdn.com/image/fetch/$s_!BhWp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png 1272w, https://substackcdn.com/image/fetch/$s_!BhWp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64ca9516-cd62-40d4-b205-1154a48296a7_610x361.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Quants often frame API problems as reliability problems. They are also selection problems. The feed, the protocol, and the quota choose which events you ever get the chance to model. That means a strategy discovered under one data-access regime may not even be defined under another. Once this is understood, the response becomes sharper. </p><h4>Historical-live non-equivalence and data reconstruction error</h4><p>Historical endpoints and live endpoints are seldom equivalent in the strict sense required by causal research. They may deliver related objects, but related is not enough. Historical data is often cleaned, deduplicated, repaired, aligned to bar boundaries, and packaged into stable records. Live data is incremental, provisional, and susceptible to temporary disorder. A strategy that learns on one and trades on the other inherits a<em> reconstruction problem.</em></p><p>There are at least four sources of non-equivalence:</p><ol><li><p>The first is <strong>finalization</strong>: Historical bars tend to be final objects. Live bars, unless the provider specifically publishes only completed intervals, may be in progress. </p></li><li><p>The second is <strong>correction policy</strong><em>:</em> Historical vendors can revise prior records after exchange corrections or internal data-quality routines. Live systems receive the pre-correction path and may or may not later receive explicit correction messages. </p></li><li><p>The third is <strong>aggregation method</strong>: A historical endpoint may aggregate on server-side rules that differ from a client&#8217;s local reconstruction from trade ticks. </p></li><li><p>The last one is <strong>delivery semantics</strong>: Historical data is fetched as a completed array. Live data is delivered as a stream in which messages can be delayed, duplicated, or arrive after reconnect.</p></li></ol><p>Any research process should ask a simple but uncomfortable question: i<em>s the historical object used in research reproducible from the live stream using only information available at the time</em>? If the answer is no, then the strategy is at risk of hidden lookahead or deployment drift. Sometimes the mismatch is harmless. Often it is not. Opening-range logic, event-triggered strategies, and execution-sensitive systems are especially vulnerable because they depend on exact temporal boundaries.</p><p>One clean way to formalize the problem is to define a reconstruction operator G that maps a live stream path into a bar or state record:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;b_t^{live-recon} = G(m_{0:t})&quot;,&quot;id&quot;:&quot;NOEXHANYKF&quot;}" data-component-name="LatexBlockToDOM"></div><p>The historical dataset supplies b<sub>t</sub><sup>hist</sup>. The research assumption that often goes unstated is that <em>b<sub>t</sub><sup>hist</sup> = b<sub>t</sub><sup>live_recon</sup></em>. This is rare. The true object of concern is the reconstruction error:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;&#948;_t = b_t^{hist} - b_t^{live-recon}&quot;,&quot;id&quot;:&quot;KPMCWDJJSE&quot;}" data-component-name="LatexBlockToDOM"></div><p>A strategy that is stable under small &#948;<em><sub>t</sub></em> may survive deployment. A strategy that changes sign, entry timing, or position size under small &#948;<em><sub>t</sub></em> may lose money even if the backtest looks good. This is one reason why mature groups insist on <em>replay tests.</em> They record live messages, reconstruct the internal state offline, and compare it with both the production decision log and the historical vendor view.</p><p>Out-of-order delivery is a particularly sharp example. Suppose messages carry event timestamps <em>e<sub>i</sub></em> but arrive at times <em>a<sub>i</sub></em>. A client that sorts by arrival time instead of event time assumes that sign(<em>a<sub>i</sub></em> - <em>a<sub>j</sub></em>) equals sign(<em>e<sub>i</sub></em> - <em>e<sub>j</sub></em>) for all pairs. That assumption is false in many real networks. Once it breaks, local bars, rolling indicators, or event-triggered conditions may shift. The correct response is to build reordering logic with bounded buffers and explicit policy for what to do when order cannot be restored with confidence.</p><p>Here is a minimal sketch of such a guard:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;09716d76-be62-4dcd-9251-47c208b32b22&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">from dataclasses import dataclass
from heapq import heappush, heappop


@dataclass(order=True)
class MarketEvent:
    seq: int
    ts_event_ns: int
    payload: dict


class ReorderBuffer:
    def __init__(self, max_gap: int = 32):
        self.expected = 0
        self.max_gap = max_gap
        self.heap = []

    def push(self, event: MarketEvent):
        if event.seq &lt; self.expected:
            return []  # duplicate or stale replay

        heappush(self.heap, event)
        out = []

        while self.heap and self.heap[0].seq == self.expected:
            out.append(heappop(self.heap))
            self.expected += 1

        if self.heap and self.heap[0].seq - self.expected &gt; self.max_gap:
            raise RuntimeError("sequence gap too large")

        return out</code></pre></div><p>The point of the snippet is to show the shape of the invariant. Local state should advance only when sequence integrity is acceptable. If the gap exceeds a declared tolerance, the client should raise an incident, resubscribe, or rebuild state from a safer source.</p><p>The next plot shows the operational consequence of delayed observations on a simple synthetic crossover-style entry. The delayed observation sees the same price path later and enters later. The lesson is that even modest observation delay can transform entry timing in a way that a clean historical backtest never exposed.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!k1Hg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!k1Hg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png 424w, https://substackcdn.com/image/fetch/$s_!k1Hg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png 848w, https://substackcdn.com/image/fetch/$s_!k1Hg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png 1272w, https://substackcdn.com/image/fetch/$s_!k1Hg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!k1Hg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png" width="1456" height="589" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:589,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:127426,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!k1Hg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png 424w, https://substackcdn.com/image/fetch/$s_!k1Hg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png 848w, https://substackcdn.com/image/fetch/$s_!k1Hg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png 1272w, https://substackcdn.com/image/fetch/$s_!k1Hg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d2f7726-4c67-4fe1-88d6-2d70fae394b6_1719x695.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Delayed entre vs True entry</figcaption></figure></div><p>A common response is to smooth the signal until these issues disappear. That can help, but it does not solve the identification problem. If the strategy only works when final corrected bars are used, then the strategy depends on a state that may not exist in live trading. </p><p>First, define decision-time objects. Second, test historical-live parity by replaying captured live streams into the same internal schemas used in production. Third, store provenance metadata with every derived bar or indicator: source, reconstruction mode, timezone, sequence completeness, and whether the object is provisional or finalized. Fourth, make non-equivalence visible in research by introducing uncertainty envelopes. If a signal flips under plausible reconstruction perturbations, that signal should not be promoted.</p><h4>Execution state machines and order routing</h4><p>The industry still tolerates expressions like: &#8220;send the order&#8221;, &#8220;the order is in&#8221;, &#8220;the order got filled&#8221;. For software, these phrases hide critical distinctions. For PnL, they hide money. However, execution should be modeled as a state machine.</p><p>A request to place an order is only a statement of intent until the broker acknowledges it. The acknowledgment is not yet a fill or the fill may be partial. The remaining quantity may rest, cancel, expire, or be modified by subsequent logic. Some APIs reflect these transitions in a REST resource that can be queried after placement; others also emit asynchronous trade or order updates over a stream; sometimes users place, monitor, and cancel orders through the Trading API, and that order identifiers and status objects support later inspection. The best approach is to maintain two separate layers: <em>account and order routing</em>. That separation is healthy because it prevents one channel from pretending to be the whole lifecycle.</p><p>Okay, let&#8217;s talk about the second one: routing. SEC rules on order execution and order routing disclosure exist because the path an order takes after leaving the customer-facing system affects economic results. A retail API client sees only part of the venue-level journey, and the system designer should treat that journey as relevant. Even if the internal API abstracts away venue detail, it should preserve enough state to distinguish user intent, broker acceptance, downstream routing outcome, and final position reconciliation.</p><p>A minimal lifecycle can be written as:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\text{Intent} \\rightarrow \\text{Accepted} \\rightarrow \\text{Working} \\rightarrow \\text{Partially filled} \\rightarrow \\text{F\nilled}\n&quot;,&quot;id&quot;:&quot;WRORVBUDAM&quot;}" data-component-name="LatexBlockToDOM"></div><p>with additional branches for rejected, canceled, expired, and replaced. Not every broker surfaces each branch in the same way. The internal system should anyway. Doing so has three advantages. First, it prevents false certainty. Second, it enables reconciliation. Third, it supports simulation. A sound backtest represents the full state machine between signal and filled position, because those intermediate transitions reveal failure modes that materially shape PnL.</p><p>Consider expected execution price. A crude but useful decomposition is:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P_{\\mathrm{exec}} = P_{\\mathrm{ref}} + S_{\\mathrm{queue}} + S_{\\mathrm{latency}} + S_{\\mathrm{impact}} + S_{\\mathrm{fees}}\n&quot;,&quot;id&quot;:&quot;RMPXOCPUNY&quot;}" data-component-name="LatexBlockToDOM"></div><p>Here <em>P<sub>ref</sub></em> is a chosen reference price, <em>S<sub>queue</sub></em> captures queue-position effects and spread interaction, <em>S<sub>latency</sub></em> captures adverse movement while the order is in flight, <em>S<sub>impact</sub></em> captures self-induced price movement for non-negligible size, and <em>S<sub>fees</sub></em> captures commissions, exchange fees, rebates, and financing where relevant. Many prototype systems focus on <em>S<sub>latency</sub></em> and ignore the rest. That is tolerable for a toy simulator, but not for a research workflow that wants to claim tradability. Even a simple simulator should at least model partial fills, expiry, and side-dependent slippage under volatility.</p><p>An internal order model should therefore separate three objects:</p><ol><li><p><strong>OrderIntent</strong>: what the strategy wants. </p></li><li><p><strong>BrokerOrderState</strong>: what the broker has acknowledged and how it has evolved. </p></li><li><p><strong>PositionLedgerEntry</strong>: what the portfolio believes happened after reconciliation. </p></li></ol><p>Conflating them creates the classic bug where the strategy believes it is flat because its own intent was to exit, while the broker still shows a partially open quantity.</p><p>A minimal type sketch might look like this:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;0383c249-993c-4a4a-8808-40be0a81c760&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">from enum import Enum
from pydantic import BaseModel, Field, field_validator
from typing import Optional


class OrderSide(str, Enum):
    BUY = "BUY"
    SELL = "SELL"


class OrderState(str, Enum):
    INTENT = "INTENT"
    ACCEPTED = "ACCEPTED"
    WORKING = "WORKING"
    PARTIALLY_FILLED = "PARTIALLY_FILLED"
    FILLED = "FILLED"
    REJECTED = "REJECTED"
    CANCELED = "CANCELED"
    EXPIRED = "EXPIRED"


class OrderIntent(BaseModel):
    client_order_id: str
    symbol: str
    side: OrderSide
    qty: float = Field(gt=0)
    limit_price: Optional[float] = Field(default=None, gt=0)


class BrokerOrderState(BaseModel):
    client_order_id: str
    broker_order_id: Optional[str] = None
    state: OrderState
    filled_qty: float = Field(ge=0)
    avg_fill_price: Optional[float] = None

    @field_validator("avg_fill_price")
    @classmethod
    def positive_price_if_present(cls, v):
        if v is not None and v &lt;= 0:
            raise ValueError("avg_fill_price must be positive")
        return v</code></pre></div><p>The exact fields can be adjusted. Once these objects are defined, live logic becomes cleaner. Reconciliation compares the latest broker state with the internal ledger. Simulation advances through legal states instead of jumping from signal to fill. Risk logic defines kill-switch conditions in terms of state divergence, not only PnL.</p><p>Quants often focus on price improvement as the hard part of execution. In many systems, the hard part is state certainty. A robust system determines with high confidence whether an order is live, whether a cancel request succeeded, and whether a partial fill changed net exposure. Alpha only matters when the process itself is reliable. The real objective is to build an execution engine whose states are clear enough that later improvements rest on solid assumptions.</p><p>This leads to prototype API design. Even at the prototyping stage, order endpoints benefit from idempotency, accept client-generated identifiers, distinguish acceptance from fill, and expose a machine-readable state model. Internal consumers rely on structured state as the source of truth. Human-readable logs still help, but they support the contract rather than define it.</p><p>The external side of the problem therefore becomes clear: financial APIs present data and orders as evolving states carried through channels with meaningful semantics. The constructive response is to assign those states formal names and transitions. Once that structure is in place, the internal API becomes the stable surface across which research, simulation, and live code remain aligned.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lF1e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" width="1456" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1158490,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3&quot;,&quot;text&quot;:&quot;Send me a copy&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3"><span>Send me a copy</span></a></p><h4>Simulation and live trading</h4><p>One script uses the same field names as the next, timezones remain consistent across services, the execution engine works with normalized order states, the backtester uses the same fill object as live trading, and the monitoring service reads position exposure from a shared contract rather than reconstructing it from logs. That  is manageable at small scale and essential at medium scale. The solution is to design an internal API.</p><p>The main purpose of an internal API is contract preservation. Research, paper trading, backtesting, and live services should consume and emit the same conceptual objects even when their data sources differ. If one system produces <code>Signal(direction, generated_at, horizon, confidence)</code> and another expects <code>alpha_score</code>, <code>entry_ts</code>, and <code>weight_hint</code>, the boundary needs stronger alignment. The internal API creates that alignment by enforcing shared names, units, valid ranges, and temporal interpretation.</p><p>A sensible contract design starts with five invariants:</p><ol><li><p><strong>Causality</strong>: Every object that can influence a decision carries a timestamp or event key that identifies when it became admissible. This includes bars, features, signals, risk checks, and order updates. With that structure in place, replay testing becomes meaningful.</p></li><li><p><strong>Provenance</strong>: A bar is more than OHLCV. It carries information about whether it came from vendor history, live reconstruction, a synthetic resampler, or a replay archive. That small field turns future debugging into straightforward engineering.</p></li><li><p><strong>State separability</strong>: Data objects, signal objects, order-intent objects, broker-state objects, and portfolio-ledger objects each remain distinct. This separation keeps assumptions from leaking across modules and preserves clarity at the interfaces.</p></li><li><p><strong>Versioning</strong>: Schemas evolve, and they should evolve explicitly. A <code>schema_version</code> field together with artifacted OpenAPI snapshots provides inexpensive and reliable protection.</p></li><li><p><strong>Idempotency</strong>: Commands that change state, especially order-related commands, should support replay without accidental duplication. Client order IDs provide the standard mechanism.</p></li></ol><p>These invariants can be supported with concise Pydantic models. Here is a small example:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;92e3c7d5-e9f9-4b3b-b16b-aba26bafb311&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">from datetime import datetime, timezone
from pydantic import BaseModel, Field, field_validator
from typing import Literal


class Bar(BaseModel):
    schema_version: str = "1.0"
    source: Literal["vendor_history", "live_recon", "replay"]
    symbol: str
    ts_open: datetime
    ts_close: datetime
    open: float = Field(gt=0)
    high: float = Field(gt=0)
    low: float = Field(gt=0)
    close: float = Field(gt=0)
    volume: float = Field(ge=0)

    @field_validator("ts_open", "ts_close")
    @classmethod
    def enforce_timezone(cls, v: datetime):
        if v.tzinfo is None:
            raise ValueError("timestamps must be timezone-aware")
        return v.astimezone(timezone.utc)


class Signal(BaseModel):
    schema_version: str = "1.0"
    generated_at: datetime
    symbol: str
    direction: int
    horizon_s: int = Field(gt=0)
    confidence: float = Field(ge=0.0, le=1.0)
    model_tag: str</code></pre></div><p>The snippet already delivers three concrete benefits. Invalid states fail early, schemas become inspectable by both humans and machines and, client libraries can be generated or written against a stable description rather than inherited through oral tradition.</p><p>The internal API also makes research and live mode comparable by design. A strong pattern places all market-data acquisition behind adapter interfaces and exposes only canonical internal models to the rest of the stack. The rest of the code then works with the same objects whether bars came from Alpaca, IBKR, a CSV archive, a replay file, or MetaTrader. Sources are not interchangeable, but their specific oddities belong at the boundary, where they can be normalized, instead of spreading through the strategy codebase.</p><p>Do you know what is premature? To build a large platform before the contracts are understood. A lean internal API can live inside a single FastAPI application with a few strict models and a small set of endpoints. What matters is that the contracts are defined before the complexity arrives.</p><p>A practical minimal endpoint set for a quant prototyping API includes <code>/health</code>, <code>/bars</code>, <code>/signal/run</code>, <code>/orders/simulate</code>, <code>/orders/submit</code>, <code>/positions</code>, <code>/portfolio/state</code>, and <code>/backtest/run</code>. Each one benefits from typed inputs and outputs, deterministic validation, and a precise statement of time semantics.</p><p>The internal API also supports replay, allowing a team to verify that the object evaluated in research matches the object the live system would have seen. An endpoint that consumes archived messages and emits canonical internal states often adds more value than a second optimization endpoint. Little by little we will be building things like this: </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kG1j!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kG1j!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png 424w, https://substackcdn.com/image/fetch/$s_!kG1j!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png 848w, https://substackcdn.com/image/fetch/$s_!kG1j!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png 1272w, https://substackcdn.com/image/fetch/$s_!kG1j!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!kG1j!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png" width="1456" height="376" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:376,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:129673,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/190500174?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kG1j!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png 424w, https://substackcdn.com/image/fetch/$s_!kG1j!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png 848w, https://substackcdn.com/image/fetch/$s_!kG1j!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png 1272w, https://substackcdn.com/image/fetch/$s_!kG1j!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F900c7fd1-9507-4857-8597-be81a3bd3157_1506x389.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Creating the architecture for a trading API</h4>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-infra-financial-apis">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Infra: Scraping financial data ]]></title><description><![CDATA[Systematic fund scraping]]></description><link>https://www.quantbeckman.com/p/with-code-infra-scraping-financial</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-infra-scraping-financial</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Mon, 23 Feb 2026 13:36:36 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>What is web scraping in a quantitative context?</p></li><li><p>Where does it come from?</p></li><li><p>How web scraping actually works.</p></li><li><p>Risks and limitations of the protocol.</p></li><li><p>Screener network topography.</p></li><li><p>Scraping fund positions.</p></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read the newsletter index</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Introduction</h4><p>In fund research, the input that matters most is simple: <em>what stocks are inside the funds right now, and in what weight. </em>Without that look-through layer, fund momentum, category rotation, or risk exposure becomes a label-driven proxy. </p><p>We are going to construct and execute a systematic fund scraping operation. The explicit objective of this architecture is to expose what underlying stocks these otherwise opaque mutual funds and ETFs are holding. </p><p>If you intend to calculate true portfolio variance, isolate specific factor exposures, or detect hidden beta overlap across multiple assets, you must possess the exact equity constituents and their precise percentage weightings for every fund in your cross-section. Without ticker-level resolution, you might allocate capital to two distinct Diversified Value funds that both hold massive, highly correlated positions in the exact same mega-cap technology stock. </p><p>So, we will engineer a deterministic data pipelineto get a clean, structured JSON. The architecture we are deploying will execute in distinct, logical phases:</p><ol><li><p>We will intercept the provider&#8217;s APIs to extract the core fund metadata and historical return vectors.</p></li><li><p>We will utilize concurrent, threaded I/O operations to query the global holdings API.</p></li><li><p>We will map this extracted data into memory-contiguous C-arrays. </p></li><li><p>We will apply algorithmic deduplication using tokenized lexical analysis and tuple signatures to eliminate share class multicollinearity. </p></li></ol><p>The final output of this operation will be a sanitized, lightweight matrix of unique portfolios, ready for immediate ingestion into our engine.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!r4e6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!r4e6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png 424w, https://substackcdn.com/image/fetch/$s_!r4e6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png 848w, https://substackcdn.com/image/fetch/$s_!r4e6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png 1272w, https://substackcdn.com/image/fetch/$s_!r4e6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!r4e6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png" width="1046" height="620" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:620,&quot;width&quot;:1046,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:871421,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/188876954?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!r4e6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png 424w, https://substackcdn.com/image/fetch/$s_!r4e6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png 848w, https://substackcdn.com/image/fetch/$s_!r4e6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png 1272w, https://substackcdn.com/image/fetch/$s_!r4e6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b798247-5c17-4000-a4b2-6fdaad08c2ab_1046x620.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>What is web scraping in a quantitative context?</h4><p>To understand the architecture we build for trading, we must first define what web scraping is at a fundamental, computational level. In the broader software engineering industry, web scraping is often defined as the automated gathering of information from the internet. In quantitative finance, we require a technical definition.</p><blockquote><p>Web scraping is the automated, programmatic execution of HTTP network protocols to request, receive, and extract specific data structures from external servers, translating unstructured or semi-structured web payloads into strict, localized, memory-contiguous matrices for immediate algorithmic ingestion.</p></blockquote><p>To know more about scraping check that pdf:</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!n9VH!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d47193c-446f-44b1-9ef1-41cc02cb6375_547x737.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Web Scrapping: The basics explained</div><div class="file-embed-details-h2">236KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/6efdb7f0-5724-495a-b99b-58f116652a48.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/6efdb7f0-5724-495a-b99b-58f116652a48.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>When a quantitative researcher builds a cross-sectional momentum strategy or a fundamental mean-reversion model, the model requires historical price vectors, dividend yields, expense ratios, constituent weights, etc. Historically, this data was locked behind expensive, proprietary data terminals operated by a few institutional vendors.</p><p>Web scraping bypasses this vendor lock-in. It allows a trading desk to aggregate fragmented public data from asset managers, regulatory filings, and financial portals directly into a local array or a structured SQL database. It is the direct acquisition of the raw material required for alpha generation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rT0U!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rT0U!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png 424w, https://substackcdn.com/image/fetch/$s_!rT0U!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png 848w, https://substackcdn.com/image/fetch/$s_!rT0U!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png 1272w, https://substackcdn.com/image/fetch/$s_!rT0U!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!rT0U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png" width="1456" height="780" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:780,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:298301,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/188876954?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rT0U!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png 424w, https://substackcdn.com/image/fetch/$s_!rT0U!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png 848w, https://substackcdn.com/image/fetch/$s_!rT0U!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png 1272w, https://substackcdn.com/image/fetch/$s_!rT0U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6703f57f-a627-4f79-a0c3-6bfd52e6a26a_1562x837.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Furthermore, standard vendor data is often commoditized. If every quant receives the exact same fundamental dataset at the exact same millisecond via an identical API feed, the alpha decays. Constructing custom extraction architecture allows a quantitative team to ingest alternative data&#8212;such as retail sentiment from localized forums, unstructured XBRL data from SEC 10-Q filings, or dynamically updating ETF composition lists directly from the issuer&#8217;s backend. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><h4>Where does it come from?</h4><p>The methodology of data extraction has evolved in parallel with the evolution of web architecture itself. We can trace this lineage through distinct technological eras, driven by the need to parse complex data delivery mechanisms. Understanding this history is critical to understanding why modern quantitative infrastructure rejects certain parsing libraries.</p><p><strong>Era 1: Static transfer and regular expressions</strong></p><p>In the early days of the internet, web pages were static HTML documents stored directly on server hard drives. When a client sent an HTTP GET request, the server responded with the exact text file. Scraping in this era involved basic command-line tools executing standard network fetches.</p><p>The data was extracted using Regular Expressions (Regex). Regex operates as a finite state machine that matches specific string patterns. If a researcher wanted a stock price, they wrote a strictly defined string-matching algorithm to locate a sequence of numbers immediately following a specific HTML text block.</p><p>This was efficient computationally. A regex search over a 10-megabyte text file resolves in milliseconds. However, it was incredibly brittle from an operational standpoint. Consider the following HTML string: <code>&lt;td class="price"&gt;150.25&lt;/td&gt;</code>. A regex designed to capture the float inside the tags (<code>r'class="price"&gt;(\d+\.\d+)&lt;'</code>) works fine. But if the webmaster updates the website and injects a bold tag for visual styling&#8212;<code>&lt;td class="price"&gt;&lt;b&gt;150.25&lt;/b&gt;&lt;/td&gt;</code>&#8212;the regex logic fails. It returns a null value. In a trading pipeline, a null value propagates through the covariance matrix, zeroes out the optimization weights, and halts the entire trading execution.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zHzs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zHzs!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png 424w, https://substackcdn.com/image/fetch/$s_!zHzs!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png 848w, https://substackcdn.com/image/fetch/$s_!zHzs!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png 1272w, https://substackcdn.com/image/fetch/$s_!zHzs!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!zHzs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png" width="1906" height="899" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:899,&quot;width&quot;:1906,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:401133,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/188876954?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76d57406-d8bc-47ca-ba77-80b77a42ab10_1906x899.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zHzs!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png 424w, https://substackcdn.com/image/fetch/$s_!zHzs!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png 848w, https://substackcdn.com/image/fetch/$s_!zHzs!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png 1272w, https://substackcdn.com/image/fetch/$s_!zHzs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c02dbd4-6d7c-45cb-bb4a-7674de80d4cf_1906x899.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Pattern matching</figcaption></figure></div><p><strong>Era 2: The Document Object Model and structural parsing</strong></p><p>As web pages evolved into generated templates mapping to backend databases, relying on raw string matching became impossible. The industry shifted to structural parsing. Browsers render HTML into a hierarchical tree structure known as the Document Object Model (DOM).</p><p>Scrapers evolved to replicate this exact rendering process in memory. Python libraries, such as <code>BeautifulSoup</code> utilizing <code>lxml</code> parsers, were developed to download the HTML string, parse it into an internal tree data structure, and allow engineers to search the tree using XPath or CSS selectors. We stopped searching for text strings and started searching for specific structural nodes, such as the third cell of the second row within a specific table ID.</p><p>While more robust than Regex, structural parsing introduces severe computational overhead. Converting a 5-megabyte HTML string into a fully traversable DOM tree requires significant CPU cycles and RAM allocation. The search algorithm must execute <em>O</em>(<em>N</em>) traversal operations across thousands of nested nodes. When running high-frequency data ingestion across thousands of financial instruments, the CPU time spent building these DOM trees severely limits the concurrency capacity of the extraction server.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CNxt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff943d96-ae62-4612-aa8b-233a35b19373_778x908.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CNxt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff943d96-ae62-4612-aa8b-233a35b19373_778x908.png 424w, https://substackcdn.com/image/fetch/$s_!CNxt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff943d96-ae62-4612-aa8b-233a35b19373_778x908.png 848w, https://substackcdn.com/image/fetch/$s_!CNxt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff943d96-ae62-4612-aa8b-233a35b19373_778x908.png 1272w, https://substackcdn.com/image/fetch/$s_!CNxt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff943d96-ae62-4612-aa8b-233a35b19373_778x908.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!CNxt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff943d96-ae62-4612-aa8b-233a35b19373_778x908.png" width="778" height="908" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ff943d96-ae62-4612-aa8b-233a35b19373_778x908.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:908,&quot;width&quot;:778,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:307989,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/188876954?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b149c15-f6c0-40f9-bbbe-333030c2d015_778x908.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CNxt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff943d96-ae62-4612-aa8b-233a35b19373_778x908.png 424w, https://substackcdn.com/image/fetch/$s_!CNxt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff943d96-ae62-4612-aa8b-233a35b19373_778x908.png 848w, https://substackcdn.com/image/fetch/$s_!CNxt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff943d96-ae62-4612-aa8b-233a35b19373_778x908.png 1272w, https://substackcdn.com/image/fetch/$s_!CNxt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff943d96-ae62-4612-aa8b-233a35b19373_778x908.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">DOM</figcaption></figure></div><p><strong>Era 3: Single Page Applications and JavaScript rendering</strong></p><p>The critical inflection point occurred when web development shifted to reactive JavaScript frameworks (React, Angular, Vue). The server no longer returned HTML containing the financial data. Instead, the server returned an empty HTML shell and a massive JavaScript file. The client&#8217;s browser was expected to execute the JavaScript, which would then dynamically fetch the data and build the DOM locally on the user&#8217;s machine.</p><p>Standard HTML parsers broke; they would download the empty shell, parse it, and find zero data. To combat this, scrapers evolved into headless browsers. Infrastructure had to spin up entirely automated, invisible instances of Chromium in the background, load the page, wait for the V8 JavaScript engine to execute the rendering logic, wait for the network requests to resolve, and then extract the data from the rendered DOM.</p><p>This was disastrous. Running a single headless browser instance requires hundreds of megabytes of RAM. If a quantitative pipeline requires 100 concurrent workers to update the daily pricing matrix before the market opens, the server must allocate over 50 gigabytes of RAM strictly for browser overhead. This is inefficient and unscalable for certain types of trading systems.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pcHE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pcHE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp 424w, https://substackcdn.com/image/fetch/$s_!pcHE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp 848w, https://substackcdn.com/image/fetch/$s_!pcHE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp 1272w, https://substackcdn.com/image/fetch/$s_!pcHE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!pcHE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp" width="1200" height="630" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:630,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!pcHE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp 424w, https://substackcdn.com/image/fetch/$s_!pcHE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp 848w, https://substackcdn.com/image/fetch/$s_!pcHE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp 1272w, https://substackcdn.com/image/fetch/$s_!pcHE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4492ba9-b648-43cf-8ea2-51cdd7fcb5d5_1200x630.webp 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">TPL vs SPA</figcaption></figure></div><p><strong>Era 4: API interception</strong></p><p>This brings us to the modern quantitative standard. Because Single Page Applications must retrieve their dynamic data from somewhere, they utilize Application Programming Interfaces (APIs)&#8212;typically REST or GraphQL architectures.</p><p>Instead of rendering a heavy headless browser, we utilize network monitoring tools to observe the traffic generated by the web application. We identify the exact API endpoint the JavaScript is querying, and we replicate that precise HTTP request from our Python architecture. We bypass the HTML entirely. We bypass the JavaScript rendering engine. We request the raw, serialized JSON data directly from the provider&#8217;s backend database. This is the optimal state of data extraction: maximum fidelity, minimum computational overhead.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_ktc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_ktc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png 424w, https://substackcdn.com/image/fetch/$s_!_ktc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png 848w, https://substackcdn.com/image/fetch/$s_!_ktc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png 1272w, https://substackcdn.com/image/fetch/$s_!_ktc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!_ktc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png" width="1344" height="569" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:569,&quot;width&quot;:1344,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:388749,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/188876954?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_ktc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png 424w, https://substackcdn.com/image/fetch/$s_!_ktc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png 848w, https://substackcdn.com/image/fetch/$s_!_ktc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png 1272w, https://substackcdn.com/image/fetch/$s_!_ktc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e20dee7-93cb-4300-ad71-d8941deae556_1344x569.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Scraping vs API</figcaption></figure></div><h4>How web scraping actually works</h4><p>To extract data, you must understand the exact sequence of network events that occur during a programmatic request. Web scraping is the programmatic automation of the standard client-server architecture, executing at speeds unachievable by human interaction.</p><p><strong>Step 1: The network handshake and request formulation</strong></p><p>The process begins when our Python architecture instantiates a TCP connection. We formulate a strict HTTP request, this request consists of a specific method (<code>GET</code> for retrieving data, <code>POST</code> for submitting query parameters), the target URL, and a dictionary of Headers.</p><p>Headers are critical, they dictate the exact rules of engagement with the target server. We set the <code>User-Agent</code> header to declare what type of client we are. By default, Python networking libraries broadcast their standard identity (e.g., <code>python-requests/2.31.0</code>). Modern Web Application Firewalls flag this string and drop the TCP connection. We must inject standard browser headers to bypass basic heuristic checks.</p><p>Furthermore, we must configure advanced headers such as <code>Accept-Encoding: gzip, deflate, br</code> to inform the server that we can handle compressed byte streams, reducing network transit time.</p><p>Modern security also involves TLS (Transport Layer Security) fingerprinting. Servers analyze the specific cryptographic cipher suites our client proposes during the initial connection setup (the Client Hello packet). Standard Python networking libraries propose a different sequence of ciphers than a standard Google Chrome browser. If the server detects a mismatch between the <code>User-Agent</code> claiming to be Chrome and a TLS fingerprint matching Python, it blocks the request. Advanced quantitative architecture utilizes specialized network adapters to mimic exact browser TLS signatures, guaranteeing connection acceptance.</p><p>Before we even introduce anything, let&#8217;s start here with our practical example.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;b78902e9-28fe-4c9e-a614-0631f0d8c37e&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">import requests

url = "https://lt.morningstar.com/api/rest.svc/klr5zyak8x/security/screener?page=1&amp;pageSize=50&amp;sortOrder=ReturnM120%20desc&amp;outputType=json&amp;version=1&amp;languageId=es-ES&amp;currencyId=EUR&amp;universeIds=FOESP%24%24ALL&amp;securityDataPoints=SecId%7CName%7CPriceCurrency%7CTenforeId%7CLegalName%7CClosePrice%7CYield_M12%7CCategoryName%7CAnalystRatingScale%7CStarRatingM255%7CQuantitativeRating%7CSustainabilityRank%7CReturnD1%7CReturnW1%7CReturnM1%7CReturnM3%7CReturnM6%7CReturnM0%7CReturnM12%7CReturnM36%7CReturnM60%7CReturnM120%7CFeeLevel%7CManagerTenure%7CMaxDeferredLoad%7CInitialPurchase%7CFundTNAV%7CEquityStyleBox%7CBondStyleBox%7CAverageMarketCapital%7CAverageCreditQualityCode%7CEffectiveDuration%7CMorningstarRiskM255%7CAlphaM36%7CBetaM36%7CR2M36%7CStandardDeviationM36%7CSharpeM36%7CTrackRecordExtension&amp;filters=&amp;term=&amp;subUniverseId="

r = requests.get(url, timeout=20)
print("status:", r.status_code)
print("content-type:", r.headers.get("content-type"))
print("content-encoding:", r.headers.get("content-encoding"))
print("head:", r.text[:200])</code></pre></div><p><strong>Step 2: Server-side resolution and response</strong></p><p>Upon receiving the formulated request, the target server evaluates it against its internal security protocols. It checks for IP reputation, rate limit thresholds (Token Bucket algorithms), and geographic blocking rules. If the request passes these validation checks, the server queries its internal SQL or NoSQL database, formats the result into the requested format, and transmits the payload back to our server over the open connection.</p><p>The server prefaces the payload with an HTTP Status Code. We evaluate this code before reading the data buffer. A <code>200 OK</code> indicates absolute success. A <code>403 Forbidden</code> indicates our IP or TLS signature was blocked by the WAF. A <code>429 Too Many Requests</code> indicates we have exceeded the permissible concurrency limit. When a 429 is encountered, our infrastructure immediately halts execution on that specific thread and initiates a deterministic exponential backoff algorithm, calculating a precise wait time before re-attempting the connection.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;plaintext&quot;,&quot;nodeId&quot;:&quot;a406989d-b7ce-4f46-a6a6-eba17f3e1e09&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-plaintext">status: 200
content-type: application/json
content-encoding: gzip
head: {"total": 49441,"page": 1,"pageSize": 50,"rows": [{"SecId": "F00000S8RT","Name": "Multipartner Konwave Gold Equity C USD","PriceCurrency": "USD","TenforeId": "52.8.LU1001014080","LegalName": "Multipar</code></pre></div><p><strong>Step 3: Payload deserialization and matrix mapping</strong></p><p>Once the payload bytes are received locally via the network interface card, they must be transformed into operational data. In modern scraping (API interception), the response header <code>Content-Type</code> is typically <code>application/json</code>. The data arrives as a serialized, UTF-8 encoded byte string.</p><p>We execute a high-speed deserialization function. In environments prioritizing microseconds, standard libraries are replaced by C-compiled alternatives like <code>orjson</code> or <code>ujson</code> that map JSON strings to memory structures faster. The deserializer parses the string and maps the key-value pairs into native Python dictionaries and lists.</p><p>From this state, we execute our extraction logic. We iterate through the arrays, filtering out the specific financial metrics we require, and allocating them into contiguous memory blocks. We actively cast data types at this stage&#8212;ensuring strings representing numbers are cast directly to <code>float64</code> objects in a structured array,  preparing the data for immediate ingestion into linear algebra operations.</p><p>To demonstrate the evolutionary mechanics, observe the explicit difference in code execution between legacy DOM parsing and modern high-speed JSON deserialization:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;91dc2caf-14ab-4550-b673-e6bf3cc14d5e&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">import json
from bs4 import BeautifulSoup
import orjson
import numpy as np

# Legacy Mechanics: Structural DOM Parsing (Era 2) 
# The server returns a heavy string of markup logic mixed with data.
html_payload = """
&lt;div class="financial-table"&gt;
    &lt;div class="row"&gt;
        &lt;span class="ticker"&gt;AAPL&lt;/span&gt;
        &lt;span class="price"&gt;150.25&lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;
"""
# We must allocate significant memory to build the entire tree structure just to find one float.
# This requires CPU cycles to parse the entire string, handle malformed tags, and build node relationships.
tree = BeautifulSoup(html_payload, "html.parser")
legacy_price = float(tree.find("span", class_="price").text)

# Modern Mechanics: API Deserialization (Era 4) 
# The server returns strictly serialized data with zero HTML rendering logic.
json_payload_bytes = b'{"data": [{"ticker": "AAPL", "price": 150.25}]}'

# We execute a direct memory mapping using a C-optimized parser (orjson).
# This bypasses standard Python object creation overhead where possible.
parsed_data = orjson.loads(json_payload_bytes)

# We extract the specific float and immediately allocate it into a NumPy array type.
modern_price = np.float64(parsed_data["data"][0]["price"])</code></pre></div><p>The difference is structural and computational. Legacy scraping reads documents; modern scraping queries databases. By understanding the lineage and the strict network mechanics, we engineer scraping pipelines that don&#8217;t simulate human browsing. Instead, they operate as direct, high-speed, database-to-database communication protocols executing over the public internet.</p><h4>Risks and limitations of the protocol</h4><p>The execution of thousands of sequential HTTP requests introduces specific, quantifiable, and systemic risks to the trading infrastructure. These risks are guaranteed events in any large-scale network operation. They manifest as missing data rows, unhandled exceptions, TCP timeouts, and truncated JSON payloads, which directly corrupt the backtesting matrix.</p><p>The primary operational risks are categorized by standard HTTP response codes. We anticipate HTTP 429 (Too Many Requests) errors when our extraction loops exceed the provider&#8217;s token bucket algorithm limits. We expect TCP connection timeouts when network routing degrades. We will encounter 502 Bad Gateway responses during dynamic load balancer reallocations, and 503 Service Unavailable responses when the backend database cluster is executing routine locking operations or snapshots.</p><p>To mitigate these risks, we construct a session adapter using the <code>urllib3</code> library. We implement a deterministic exponential backoff algorithm. When a 429 or 5xx status code is returned, the system pauses before executing a retry, reducing the concurrency pressure on the provider&#8217;s server. We target the 429, 500, 502, 503, and 504 codes. We exclude 400 (Bad Request) and 404 (Not Found) codes, as those represent structural errors in our query parameters that a retry will never resolve.</p><p>Here is the implementation of the retry adapter used in our pipeline:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;60dfef9e-2d18-42a2-ba64-f47b2f5fd043&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">import time
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

def _make_retry_adapter() -&gt; HTTPAdapter:
    retry = Retry(
        total=6,
        backoff_factor=0.5,
        status_forcelist=(429, 500, 502, 503, 504),
        allowed_methods=("GET",),
        raise_on_status=False,
    )
    return HTTPAdapter(max_retries=retry, pool_connections=50, pool_maxsize=50)

def _lt_session() -&gt; requests.Session:
    # We precisely replicate standard browser headers to bypass basic WAF heuristic checks.
    LT_HEADERS = {
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122 Safari/537.36",
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": "es-ES,es;q=0.9,en;q=0.8",
        "Referer": "[https://lt.morningstar.com/](https://lt.morningstar.com/)",
    }
    s = requests.Session()
    s.headers.update(LT_HEADERS)
    adapter = _make_retry_adapter()
    s.mount("https://", adapter)
    s.mount("http://", adapter)
    return s
</code></pre></div><p>Notice the specific performance parameters initialized in the adapter: <code>pool_connections=50</code> and <code>pool_maxsize=50</code>. This keeps 50 distinct TCP connections open in the background, bound to the <code>Session</code> object. By utilizing the HTTP Keep-Alive header within the <code>requests.Session</code> architecture, we completely bypass the severe latency introduced by repeated Transport Layer Security handshakes.</p><p>Every new HTTPS request requires DNS resolution, a 3-way TCP handshake (SYN, SYN-ACK, ACK), and an expensive TLS cryptographic key exchange. This sequence  requires 150 to 300 milliseconds of network overhead before a single byte of actual data is transmitted. This is the <em>protocol tax</em>. If we initiate a new session for 5,000 distinct requests, we incur 25 minutes of pure dead time negotiating connections. By pooling connections and persisting them, we reduce network overhead to the strict physical transfer time of the payload itself, dropping the marginal connection time to zero.</p><p>To visualize the progression of the exponential backoff risk mitigation, consider the following plot. It models the exact delay duration sequence based on the specified <code>backoff_factor</code>, demonstrating how the system slows down to prevent triggering a permanent IP ban.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wq8E!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wq8E!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png 424w, https://substackcdn.com/image/fetch/$s_!wq8E!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png 848w, https://substackcdn.com/image/fetch/$s_!wq8E!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png 1272w, https://substackcdn.com/image/fetch/$s_!wq8E!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!wq8E!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png" width="846" height="475" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:475,&quot;width&quot;:846,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wq8E!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png 424w, https://substackcdn.com/image/fetch/$s_!wq8E!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png 848w, https://substackcdn.com/image/fetch/$s_!wq8E!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png 1272w, https://substackcdn.com/image/fetch/$s_!wq8E!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28da627c-f305-4938-84cc-130ea10a1e3c_846x475.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is the precise moment the entire architecture upgrades. We stop looking at the rendered web page, and we start interrogating the network layer.</p><p>By opening the developer tools within a controlled Chromium environment and monitoring the <code>Fetch/XHR</code> network traffic generated by the web application during standard user interaction, we discover and intercept the underlying API. We isolate the exact network request executing the database search. We identify the explicit endpoint serving the raw data to the frontend UI: </p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;plaintext&quot;,&quot;nodeId&quot;:&quot;5418d0fd-9e99-4adb-9106-9b007600413c&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-plaintext">https://lt.morningstar.com/api/rest.svc/klr5zyak8x/security/screener.</code></pre></div><p>This endpoint doesn&#8217;t return complex, nested HTML documents. It accepts defined query parameters that dictate the SQL-equivalent operations on the backend: pagination offsets, sorting directives, universe limiters, and projection columns (<code>securityDataPoints</code>). In return, it provides a raw JSON payload.</p><p>By interfacing with this REST API, the extraction process transitions from a heuristic, error-prone text-parsing operation into a deterministic database query. We send a formalized HTTP GET request, and we receive a serialized data structure that maps into our operational memory. A float value is transmitted as a float. A string is transmitted as a string. There is no string-manipulation required to strip percentage signs or currency symbols, which eliminates an entire class of data engineering bugs.</p><p>This single event renders all traditional web scraping techniques&#8212;BeautifulSoup HTML parsing, XPath selection, and resource-heavy headless browsers like Selenium or Playwright&#8212; obsolete within our infrastructure.</p><h4>Screener network topography</h4><p>Having established the foundational infrastructure and intercepted the correct REST API endpoint, we now face the challenge of extracting the financial data. The provider&#8217;s backend database contains tens of thousands of mutual funds, exchange-traded funds, and institutional share classes. This dataset is vastly larger than the maximum permitted payload size for a single HTTP response.</p><p>The endpoint enforces a strict <code>pageSize</code> limit. This is a direct mechanism to prevent memory exhaustion on their internal database cluster. When a client requests data, the provider&#8217;s server must execute a SQL or NoSQL query, allocate a continuous block of RAM to hold the result set, serialize that data into a JSON string, and transmit it over the network interface. Requesting 10,000 rows creates a massive serialization overhead. To bypass these hard limits, we define our extraction parameter as <code>pageSize=50</code>.</p><p>Extracting the complete cross-sectional universe requires mapping the network topography and iterating over a paginated endpoint recursively. We must calculate the required number of requests based on the total record count returned in the initial response. We can&#8217;t hardcode the total page count, as the universe size fluctuates daily due to fund liquidations, mergers, and new product launches.</p><p>The algorithmic execution proceeds sequentially for the initialization phase. We fetch page 1. We parse the initial JSON payload to read the <code>total</code> integer field, which dictates the absolute size of the dataset for that specific query. We calculate the ceiling of the division of <code>total</code> by <code>pageSize</code> (<code>math.ceil(total / page_size)</code>). This yields the exact integer value of total pages required to complete the extraction. If the total is 0, we default to 1 to avoid ZeroDivisionError exceptions in downstream processing logic.</p><p>Besides, we pass specific query parameters in the HTTP request: <code>universeIds=FOESP$$ALL</code> specifies the precise fund universe routing key on the provider&#8217;s backend. We inject the parameter <code>securityDataPoints=SecId|LegalName|CategoryName|ReturnM120</code> to  isolate only the necessary relational columns.</p><p>We request the <code>SecId</code>, the <code>LegalName</code>, the <code>CategoryName</code>, and the <code>ReturnM120</code> (the 120-month historical return, which is the foundational variable for long-term quantitative momentum analysis).</p><p>If we omit the <code>securityDataPoints</code> parameter, the API defaults to returning every available column&#8212;over 100 data points including tracking error, manager tenure, and daily standard deviation. This inflates the JSON payload from 50 kilobytes to over 5 megabytes per page. Requesting unnecessary data points saturates network bandwidth, increases TLS decryption time, and degrades the JSON parsing speed. We limit the payload to our exact algorithmic requirements.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;b0082820-febf-4aa4-8a91-6bba71039409&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">import math
import numpy as np
import requests

LT_BASE = "[https://lt.morningstar.com/api/rest.svc/klr5zyak8x/security/screener](https://lt.morningstar.com/api/rest.svc/klr5zyak8x/security/screener)"
SECURITY_POINTS = "SecId|LegalName|CategoryName|ReturnM120"

def fetch_lt_page(session: requests.Session, page: int, page_size: int) -&gt; dict:
    params = {
        "page": int(page),
        "pageSize": int(page_size),
        "sortOrder": "ReturnM120 desc",
        "outputType": "json",
        "version": 1,
        "languageId": "es-ES",
        "currencyId": "EUR",
        "universeIds": "FOESP$$ALL",
        "securityDataPoints": SECURITY_POINTS,
        "filters": "",
        "term": "",
        "subUniverseId": "",
    }
    # Notice the exact tuple for the timeout: (4, 20)
    # 4 seconds allocated for the TCP SYN/ACK connection handshake.
    # 20 seconds allocated for the server to process the query and return the first byte.
    r = session.get(LT_BASE, params=params, timeout=(4, 20))
    ct = (r.headers.get("content-type") or "").lower()
    if r.status_code != 200 or "json" not in ct or not r.content:
        raise RuntimeError(f"LT bad response: {r.status_code} ct={ct} head={r.text[:200]!r}")
    return r.json()

def _normalize_pages(pages, total_pages: int):
    # This logic dictates the strict operational scope of the extraction matrix.
    if pages is None:
        # None implies a full universe extraction. WARNING: Huge data pull spanning gigabytes of memory.
        return range(1, total_pages + 1)
    if isinstance(pages, (int, np.integer)):
        # Integer implies pulling the top N pages strictly based on the sortOrder parameter.
        n = int(pages)
        return range(1, min(max(n, 0), total_pages) + 1)
    # List implies pulling specific, explicit page indices, useful for error-handling retry loops.
    # We apply a set comprehension to mathematically eliminate duplicate page requests.
    lst = sorted({int(p) for p in pages if 1 &lt;= int(p) &lt;= total_pages})
    return lst</code></pre></div><p>The pagination handler <code>_normalize_pages</code> is engineered to accept three distinct input states, providing the system with deterministic flexibility. If a researcher is running a lightweight daily update to assess the top decile of momentum performers, they pass an integer <code>pages=10</code> to scan only the top 500 funds. If the infrastructure team is bootstrapping the initial quantitative database from a blank state, they pass <code>None</code> to fetch the absolute entirety of the universe. If a previous extraction loop dropped specific network packets, the error handler passes a list <code>pages=[14, 22, 105]</code> to specifically target the missing vectors. The use of a set comprehension <code>{int(p) ...}</code> guarantees that duplicate page indices are stripped before execution, preventing redundant network I/O.</p><p>We append these scalar values into localized lists. Python lists operate with <em>O</em>(1) amortized append time complexity because they over-allocate memory buffers under the hood. We can&#8217;t initialize the final, rigid structured array at this specific stage because we don&#8217;t yet know the exact valid row count; we filter out invalid JSON objects&#8212;such as funds missing a critical <code>SecId</code>&#8212;during the loop.</p><p>The primary obstacle at the screener stage is managing the sequential nature of the pagination discovery. Because we don&#8217;t possess the <code>total</code> integer until the very first request resolves and the JSON is parsed, the initial discovery phase is linear. However, once the <code>total_pages</code> parameter is acquired, the subsequent page requests become entirely stateless and independent. This sets the theoretical stage for future parallelization. Yet, for this specific screener layer, executing sequential HTTP requests over an established, persistent TLS connection pool is efficient,  completing the initial metadata extraction in a matter of a few seconds, preparing the matrix for the much heavier, threaded Global API processing phase.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lF1e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" width="1456" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1158490,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3&quot;,&quot;text&quot;:&quot;Send me a copy&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3"><span>Send me a copy</span></a></p><h4>Scraping fund positions</h4><p>The screener endpoint provides the fund-level metadata, consisting of basic identifying parameters and historical return vectors. However, a fund&#8217;s surface-level metadata is insufficient for risk management and alpha generation. To construct a  look-through portfolio, assess true factor exposure, or run Barra risk decomposition models, we require the exact equity holdings inside each fund. We need the underlying constituents, and we need them weighted.</p><p>This data requirement introduces a separate, more complex endpoint into our architecture: the Global API (<code>api-global.morningstar.com</code>). Unlike the public screener interface, this endpoint demands an explicit authentication mechanism. We must inject an <code>apikey</code> (it&#8217;s <em>free</em>) header into every request. </p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;b333192c-0263-4705-be1d-e6a043247ebc&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python"># 2) Direct holdings 
APIKEY = "HERE YOUR API KEY"</code></pre></div><p>Furthermore, the endpoint requires the specific <code>SecId</code> extracted from the first phase to be formatted directly into the URL path.</p><p>The primary architectural obstacle is the sheer volume of network requests. The screener API supports bulk pagination; the Global API doesn&#8217;t. If the screener returns an array of 5,000 distinct funds, we must execute 5,000 separate, individual HTTP GET requests to the Global API to retrieve the constituent holdings for each individual fund.</p><p>A standard, sequential execution loop operates at approximately 0.5 seconds per request. This delay is a function of the physical network latency&#8212;the round-trip time between our trading servers and the provider&#8217;s data center, plus the provider&#8217;s internal database query execution time. Executing 5,000 requests sequentially requires 2,500 seconds, equating to over 40 minutes of pure execution time. This is unacceptable for a daily production pipeline that must process data, run optimizations, and generate execution orders before the market open.</p><p>We resolve this processing bottleneck through concurrent execution. We implement a <code>ThreadPoolExecutor</code> from the <code>concurrent.futures</code> module. Because HTTP network requests are I/O-bound tasks&#8212;meaning the CPU sits idle while waiting for the network interface card to receive the inbound packet from the remote server&#8212;the GIL doesn&#8217;t inhibit parallel execution in this specific scenario. The interpreter releases the GIL during standard I/O blocking operations. Therefore, we can launch 20 concurrent threads to process the requests without migrating to a complex multiprocessing architecture.</p><p>We chose thread pooling over an asynchronous event loop (like <code>asyncio</code>) because our existing network infrastructure relies on the <code>requests</code> library and custom <code>urllib3</code> retry adapters. Threaded execution provides the exact same I/O concurrency benefits without requiring a complete rewrite of the underlying synchronous network stack.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;plaintext&quot;,&quot;nodeId&quot;:&quot;79b6c62e-ef59-4e48-a9e6-117021d75db6&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-plaintext">import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
import requests
import numpy as np

APIKEY = "HERE YOUR API KEY"
MS_BASE = "[https://api-global.morningstar.com/sal-service/v1/fund/portfolio/holding/v2](https://api-global.morningstar.com/sal-service/v1/fund/portfolio/holding/v2)"
MS_HEADERS = {
    "apikey": APIKEY,
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36",
    "Accept": "application/json, text/plain, */*",
}

_thread_local = threading.local()

def _ms_session() -&gt; requests.Session:
    # We must ensure strict thread safety across concurrent network operations.
    s = getattr(_thread_local, "session", None)
    if s is None:
        s = requests.Session()
        s.headers.update(MS_HEADERS)
        # Assuming _make_retry_adapter is defined as in Part 1 for exponential backoff
        # s.mount("https://", _make_retry_adapter())
        _thread_local.session = s
    return s

def fetch_holdings_us_topN(secid: str, country: str = "United States", top: int = 10):
    s = _ms_session()
    url = f"{MS_BASE}/{secid}/data"
    params = {
        "clientId": "MDC",
        "version": "4.71.0",
        "premiumNum": 10000,
        "freeNum": 10000,
    }
    r = s.get(url, params=params, timeout=(4, 25))
    if r.status_code != 200 or not r.content:
        return []
    
    data = r.json()
    page = (data.get("equityHoldingPage") or {})
    hlist = (page.get("holdingList") or [])
    
    out = []
    for h in hlist:
        c = h.get("country") or h.get("countryName")
        if c == country:
            t = h.get("ticker") or h.get("tradingSymbol")
            if t:
                out.append(t)
                if len(out) &gt;= top:
                    break
    return out

def attach_holdings_all(secids, max_workers: int = 20, country: str = "United States", top: int = 10):
    holdings = [None] * len(secids)
    with ThreadPoolExecutor(max_workers=max_workers) as ex:
        futs = {ex.submit(fetch_holdings_us_topN, sid, country, top): i for i, sid in enumerate(secids)}
        for fut in as_completed(futs):
            i = futs[fut]
            try:
                holdings[i] = fut.result()
            except Exception:
                # We enforce strict failure handling to maintain array dimensions.
                holdings[i] = []
    return np.asarray(holdings, dtype=object)</code></pre></div><p>Observe the use of <code>threading.local()</code>. This is an essential architectural detail that prevents systemic failure. We instantiate a unique <code>requests.Session()</code> object for every single active thread. When multiple concurrent connections interact with a single, shared <code>urllib3</code> connection pool, race conditions occur at the socket level, leading to corrupted TCP streams and dropped connections. By isolating the session object to local thread memory storage, we guarantee that each thread maintains its own independent pool of Keep-Alive HTTP sockets. This ensures deterministic network execution.</p><p>We limit <code>max_workers=20</code> as calculated threshold based on the provider&#8217;s implicit rate limits. Exceeding this thread count doesn&#8217;t yield linear performance gains; instead, it triggers the provider&#8217;s Web Application Firewall, which responds with HTTP 429 status codes, forcing our exponential backoff adapter into extended sleep cycles. Twenty workers provide the optimal equilibrium between maximum extraction speed and minimal server-side rejection.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NEvV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NEvV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png 424w, https://substackcdn.com/image/fetch/$s_!NEvV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png 848w, https://substackcdn.com/image/fetch/$s_!NEvV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png 1272w, https://substackcdn.com/image/fetch/$s_!NEvV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!NEvV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png" width="863" height="565" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:565,&quot;width&quot;:863,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NEvV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png 424w, https://substackcdn.com/image/fetch/$s_!NEvV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png 848w, https://substackcdn.com/image/fetch/$s_!NEvV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png 1272w, https://substackcdn.com/image/fetch/$s_!NEvV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F03b1e553-4ad7-4237-8d97-521736e8bd81_863x565.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The next part is to map the extracted data into a NumPy structured array. A structured array allocates a single, contiguous block of memory in C. We define exact byte boundaries for each field in the matrix. When the CPU executes operations on this array, it loads sequential memory blocks directly into the L1/L2 cache, executing instructions with zero pointer-chasing overhead.</p><p>We allocate a 16-character unicode string (<code>U16</code>) for the <code>SecId</code>, a 256-character unicode string (<code>U256</code>) for the <code>Fund</code> name, an 8-byte float (<code>f8</code>) for the return metric, and an object pointer (<code>O</code>) for the variable-length list of constituent stock holdings.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;e107fb74-cdd3-4b92-a987-823131f42cbe&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python"># Execution assembly demonstrating the structured array initialization
import numpy as np

# Assuming secids, names, rets, and stocks are already populated lists/arrays from the extraction phase
# We allocate exactly the required memory footprint before population.
arr = np.empty(len(secids), dtype=[("Id", "U16"), ("Fund", "U256"), ("ReturnM120", "f8"), ("Stocks", "O")])
arr["Id"] = np.asarray(secids, dtype="U16")
arr["Fund"] = np.asarray(names, dtype="U256")
arr["ReturnM120"] = rets
arr["Stocks"] = stocks</code></pre></div><p>We acknowledge the use of the object pointer (<code>"O"</code>) for the stocks column breaks strict C-contiguity for the holding string arrays themselves. However, this is computationally acceptable because our heavy numerical operations&#8212;the sorting and ranking logic&#8212;operate on the <code>f8</code> float array, which remains contiguous and hardware-optimized.</p><p>Furthermore, we must handle missing data points at the numerical level. Web scraped data contains holes; some funds lack a 10-year track record. We temporarily replace <code>NaN</code> values with negative infinity (<code>-np.inf</code>) prior to sorting. Standard sorting functions in C don&#8217;t possess financial logic. Mathematical comparisons against <code>NaN</code> fail silently (<code>NaN &gt; 5</code> evaluates to False, <code>NaN &lt; 5</code> evaluates to False). If we don&#8217;t execute this specific substitution, <code>NaN</code> values sort unpredictably, injecting null pointers into the highest momentum deciles and breaking the downstream portfolio construction models. This substitution ensures that funds lacking complete return histories are forced to the absolute bottom of the generated matrix.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;f3889e17-a97f-4b2c-8eae-f0b4e7af0167&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">def sort_by_return_desc(arr: np.ndarray) -&gt; np.ndarray:
    if arr.size == 0:
        return arr
    r = arr["ReturnM120"]
    
    # Isolate NaN values and push them to the lowest possible finite float boundary (-inf).
    # This prevents incomplete track records from ranking highly in momentum strategies.
    r2 = np.where(np.isfinite(r), r, -np.inf)
    
    # Execute the C-level sort on the substituted float array, and reverse it ([::-1]) for descending order.
    idx = np.argsort(r2)[::-1]
    
    # Return the newly aligned, completely contiguous memory view.
    return arr[idx]
</code></pre></div><p>The final, most complex important barrier in systematic fund scraping is the share class multiplicity problem. Asset managers construct a single underlying portfolio of equities, but they distribute that identical portfolio across dozens of distinct administrative wrappers. They do this to target different regulatory jurisdictions, implement varying management fee structures, and separate retail investors from institutional capital. Consequently, you will see Institutional classes, Retail classes, Accumulating (Acc) classes, Distributing (Inc) classes, GBP hedged variants, and EUR unhedged variants&#8212;all holding the exact same underlying assets in the exact same proportions.</p><p>A standard database unique constraint operating on the <code>SecId</code> is insufficient here because each distinct share class possesses a unique Morningstar <code>SecId</code>. If we include all share classes in a model without aggressive deduplication, we introduce  multicollinearity into the output.</p><p>We must reduce the dataset to a single representative vector per underlying portfolio. We achieve this by cross-referencing two distinct topographical vectors: the exact list signature of the underlying top holdings, and a strict lexical analysis of the fund&#8217;s string name.</p><p>First, we define a tokenization function to strip punctuation and structural noise from the fund name strings. We use regular expressions to explicitly remove parentheses and brackets, as share classes are frequently denoted by suffixes like &#8220;(USD)&#8221; or &#8220;[Acc]&#8221;. Stripping these allows us to isolate the core lexical identity of the fund. We then define an algorithmic function to calculate the strict length of the common prefix between two tokenized arrays.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;240a3ffe-f2e2-45af-8db8-1e0b99289047&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">def _name_tokens(name: str) -&gt; list[str]:
    s = (name or "").strip().lower()
    # Normalize common typographic variations in fund prospectuses
    s = s.replace("&#8211;", "-").replace("&#8212;", "-")
    # Strip isolating punctuation typically used for share class designations
    s = re.sub(r"[()\[\],]", " ", s)
    return [t for t in s.split() if t]

def _common_prefix_len(a: list[str], b: list[str]) -&gt; int:
    n = min(len(a), len(b))
    k = 0
    # Calculate exact sequential token matches from index 0
    while k &lt; n and a[k] == b[k]:
        k += 1
    return k
</code></pre></div><p>The clustering logic dictates a two-factor rule: two rows are identified as overlapping if and only if their top 10 holding tickers match exactly (creating a deterministic tuple signature) AND their fund names share a strong lexical prefix.</p><p>Why both? Relying solely on the holdings signature is dangerous. Two different passive index funds managed by different firms might hold the exact same top 10 mega-cap technology stocks. If we only checked holdings, we would collapse two distinct operational funds into one.</p><p>By enforcing the lexical prefix check, we ensure accuracy. If &#8216;Global Equity Fund Class A USD&#8217; and &#8216;Global Equity Fund Class B EUR&#8217; possess the exact same stocks, their holding tuple signature matches. Their tokenized prefix <code>['global', 'equity', 'fund']</code> also matches based on our defined threshold (e.g., 60% of the token length). Therefore, they are safely clustered together as a single entity.</p><p>Once clustered, we invoke a selection algorithm. We iterate over the array indices within the cluster, evaluate the <code>ReturnM120</code> float vector, and retain the exact index containing the maximum finite return value.</p><p>We select for the maximum 120-month return because older, institutional share classes exhibit longer, uninterrupted historical track records and lower fee drags than newer retail classes. Retaining the asset with the longest valid track record is optimal for backtesting; it maximizes our sample size and statistical significance across different historical market regimes.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;d18486a9-861d-4998-b925-04636ce829fe&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">def _pick_best_index(arr: np.ndarray, idxs: list[int], keep: str = "max_return") -&gt; int:
    if keep == "first":
        return idxs[0]
    
    best = idxs[0]
    best_r = arr["ReturnM120"][best]
    
    for i in idxs[1:]:
        r = arr["ReturnM120"][i]
        r_ok = np.isfinite(r)
        b_ok = np.isfinite(best_r)
        
        # We strictly prioritize finite, measurable returns over NaN or infinite values.
        # If the current best is NaN, any finite return replaces it immediately.
        if (r_ok and not b_ok) or (r_ok and b_ok and r &gt; best_r):
            best = i
            best_r = r
            
    return best

def dedupe_unnecessary_share_classes(arr: np.ndarray, min_prefix_tokens=8, prefix_ratio=0.60, keep="max_return") -&gt; np.ndarray:
    if arr.size == 0:
        return arr
        
    groups: dict[tuple, list[int]] = {}
    unique_empty_keys: list[int] = []
    tokens_cache = [None] * arr.size
    
    for i in range(arr.size):
        tokens_cache[i] = _name_tokens(str(arr["Fund"][i]))
        stocks = arr["Stocks"][i]
        try:
            # Convert the list of tickers into an immutable, hashable tuple for O(1) dictionary lookups
            sig = tuple(stocks) if stocks else tuple()
        except TypeError:
            sig = tuple()
            
        if len(sig) == 0:
            unique_empty_keys.append(i)
        else:
            groups.setdefault(sig, []).append(i)
            
    kept = set(unique_empty_keys)
    
    for sig, idxs in groups.items():
        # Sort by name to guarantee deterministic execution across multiple pipeline runs
        idxs_sorted = sorted(idxs, key=lambda i: arr["Fund"][i])
        clusters: list[list[int]] = []
        reps: list[int] = []
        
        for i in idxs_sorted:
            ti = tokens_cache[i]
            placed = False
            for c_idx, rep_i in enumerate(reps):
                tr = tokens_cache[rep_i]
                cpl = _common_prefix_len(ti, tr)
                
                # Dynamic threshold calculation ensures short names and long names are evaluated fairly
                threshold = max(min_prefix_tokens, int(math.ceil(prefix_ratio * min(len(ti), len(tr)))))
                if cpl &gt;= threshold:
                    clusters[c_idx].append(i)
                    placed = True
                    break
            if not placed:
                reps.append(i)
                clusters.append([i])
                
        for cl in clusters:
            kept.add(_pick_best_index(arr, cl, keep=keep))
            
    kept_idx = np.array(sorted(kept), dtype=int)
    return arr[kept_idx]</code></pre></div><p>So let&#8217;s see what we got from this. Time to test!</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!d5Q1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!d5Q1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png 424w, https://substackcdn.com/image/fetch/$s_!d5Q1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png 848w, https://substackcdn.com/image/fetch/$s_!d5Q1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png 1272w, https://substackcdn.com/image/fetch/$s_!d5Q1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!d5Q1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png" width="989" height="590" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:590,&quot;width&quot;:989,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!d5Q1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png 424w, https://substackcdn.com/image/fetch/$s_!d5Q1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png 848w, https://substackcdn.com/image/fetch/$s_!d5Q1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png 1272w, https://substackcdn.com/image/fetch/$s_!d5Q1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df8fccb-6566-4d18-ae5f-e07f1bdaa177_989x590.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Oh! This looks good to me!</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;plaintext&quot;,&quot;nodeId&quot;:&quot;cf64e4cd-9820-40e9-b668-ae62bced7618&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-plaintext">Screener Extract: 498 funds in 11.567s | Holdings Intercept: 7.711s | Total Architecture Execution: 19.278s | Matrix Dedupe Reduction: 498 -&gt; 346 strict portfolios
[('F00000S8RT', 'Multipartner SICAV - Konwave Gold Equity Fund C USD', 25.92, list(['AU']))
 ('0P0000OMTB', 'Polar Capital Funds PLC - Polar Capital Global Technology Fund I Income EUR', 25.56, list(['NVDA', 'AVGO', 'AMD', 'META', 'LRCX', 'KLAC', 'LITE', 'COHR', 'SNDK', 'STX']))
 ('F0GBR04H0X', 'Multipartner SICAV - Konwave Gold Equity Fund B USD', 25.38, list(['AU']))
 ('F00000VI1X', 'Bakersteel Global Funds SICAV - Precious Metals Fund D EUR', 24.62, list(['NEM', 'AU', 'CDE']))
 ('F00001S8MC', 'CT (Lux) - Global Technology Class ZE (EUR Accumulation Shares)', 23.5 , list(['BE', 'LRCX', 'NVDA', 'AVGO', 'WDC', 'GOOGL', 'AMAT', 'MSFT', 'MRVL', 'AAPL']))]

Ticker    Count   PctFunds
--------------------------
NVDA        146      59.11
MSFT        132      53.44
AAPL        119      48.18
AVGO        113      45.75
GOOGL       110      44.53
AMZN        110      44.53
META        104      42.11
GOOG         68      27.53
NEM          44      17.81
LLY          40      16.19
MA           36      14.57
LRCX         33      13.36
MU           33      13.36
CRM          29      11.74
TSLA         28      11.34
AMAT         23       9.31
V            22       8.91
CDE          20       8.10
AMD          18       7.29
MRVL         18       7.29
FSLR         18       7.29
DC           16       6.48
PWR          16       6.48
RGLD         15       6.07
NFLX         15       6.07</code></pre></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KKRX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 424w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 848w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1272w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png" width="1456" height="271" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:271,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:381253,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!KKRX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 424w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 848w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1272w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Okay! No surprises here! This output matrix is formatted, computationally lightweight, and ready to be serialized to a Parquet file or whatelse. The architecture is resolved and ready for daily production scheduling.</p><p>Alright crew&#8212;good session today. Until the next article! And remember, may liquidity is with you! &#128181;&#8203;</p><div><hr></div><p>This is an<strong> invitation-only access</strong> to our <strong>QUANT COMMUNITY</strong>, so we verify numbers to avoid spammers and scammers. Feel free to join or decline at any time. <em>Tap the WhatsApp icon below to join</em></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://chat.whatsapp.com/ElDI4mIZJXuCOhLFxRi3RC" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nbxE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 424w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 848w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1272w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png" width="331" height="91.025" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:352,&quot;width&quot;:1280,&quot;resizeWidth&quot;:331,&quot;bytes&quot;:87001,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://chat.whatsapp.com/ElDI4mIZJXuCOhLFxRi3RC&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/181419543?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!nbxE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 424w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 848w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1272w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><div><hr></div><p><strong>Appendix</strong></p><p>Full script:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;cc440356-9526-44af-93a8-6c4ec89498a8&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">import math
import time
import re
import numpy as np
import requests
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

# 1) LT Screener
LT_BASE = "https://lt.morningstar.com/api/rest.svc/klr5zyak8x/security/screener"

LT_HEADERS = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122 Safari/537.36",
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "es-ES,es;q=0.9,en;q=0.8",
    "Referer": "https://lt.morningstar.com/",
}

SECURITY_POINTS = "SecId|LegalName|CategoryName|ReturnM120"


def _make_retry_adapter() -&gt; HTTPAdapter:
    retry = Retry(
        total=6,
        backoff_factor=0.5,
        status_forcelist=(429, 500, 502, 503, 504),
        allowed_methods=("GET",),
        raise_on_status=False,
    )
    return HTTPAdapter(max_retries=retry, pool_connections=50, pool_maxsize=50)


def _lt_session() -&gt; requests.Session:
    s = requests.Session()
    s.headers.update(LT_HEADERS)
    adapter = _make_retry_adapter()
    s.mount("https://", adapter)
    s.mount("http://", adapter)
    return s


def fetch_lt_page(session: requests.Session, page: int, page_size: int) -&gt; dict:
    params = {
        "page": int(page),
        "pageSize": int(page_size),
        "sortOrder": "ReturnM120 desc",
        "outputType": "json",
        "version": 1,
        "languageId": "es-ES",
        "currencyId": "EUR",
        "universeIds": "FOESP$$ALL",
        "securityDataPoints": SECURITY_POINTS,
        "filters": "",
        "term": "",
        "subUniverseId": "",
    }
    r = session.get(LT_BASE, params=params, timeout=(4, 20))
    ct = (r.headers.get("content-type") or "").lower()
    if r.status_code != 200 or "json" not in ct or not r.content:
        raise RuntimeError(f"LT bad response: {r.status_code} ct={ct} head={r.text[:200]!r}")
    return r.json()


def _normalize_pages(pages, total_pages: int):
    if pages is None:
        return range(1, total_pages + 1)
    if isinstance(pages, (int, np.integer)):
        n = int(pages)
        return range(1, min(max(n, 0), total_pages) + 1)
    lst = sorted({int(p) for p in pages if 1 &lt;= int(p) &lt;= total_pages})
    return lst


def scrape_lt_screener(pages=None, page_size: int = 50, category_contains: str = "RV", max_funds: int | None = None):
    secids, names, rets = [], [], []

    with _lt_session() as s:
        first = fetch_lt_page(s, 1, page_size)
        total = int(first.get("total", 0))
        total_pages = int(math.ceil(total / page_size)) if total &gt; 0 else 1

        count = 0
        for pg in _normalize_pages(pages, total_pages):
            data = first if pg == 1 else fetch_lt_page(s, pg, page_size)
            for row in (data.get("rows") or []):
                cat = str(row.get("CategoryName") or "")
                if category_contains in cat:
                    sid = row.get("SecId")
                    if not sid:
                        continue
                    secids.append(sid)
                    names.append(row.get("LegalName") or "")
                    v = row.get("ReturnM120")
                    try:
                        rets.append(float(v) if v is not None else np.nan)
                    except Exception:
                        rets.append(np.nan)

                    count += 1
                    if max_funds is not None and count &gt;= int(max_funds):
                        break
            if max_funds is not None and count &gt;= int(max_funds):
                break

    return secids, names, np.asarray(rets, dtype="f8")

# 2) Direct holdings 
APIKEY = "HERE YOUR API KEY"

MS_BASE = "https://api-global.morningstar.com/sal-service/v1/fund/portfolio/holding/v2"
MS_HEADERS = {
    "apikey": APIKEY,
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122 Safari/537.36",
    "Accept": "application/json, text/plain, */*",
}

_thread_local = threading.local()


def _ms_session() -&gt; requests.Session:
    s = getattr(_thread_local, "session", None)
    if s is None:
        s = requests.Session()
        s.headers.update(MS_HEADERS)
        adapter = _make_retry_adapter()
        s.mount("https://", adapter)
        s.mount("http://", adapter)
        _thread_local.session = s
    return s


def fetch_holdings_us_topN(secid: str, country: str = "United States", top: int = 10):
    s = _ms_session()
    url = f"{MS_BASE}/{secid}/data"
    params = {
        "clientId": "MDC",
        "version": "4.71.0",
        "premiumNum": 10000,
        "freeNum": 10000,
    }
    r = s.get(url, params=params, timeout=(4, 25))
    if r.status_code != 200 or not r.content:
        return []

    data = r.json()
    page = (data.get("equityHoldingPage") or {})
    hlist = (page.get("holdingList") or [])

    out = []
    for h in hlist:
        c = h.get("country") or h.get("countryName")
        if c == country:
            t = h.get("ticker") or h.get("tradingSymbol")
            if t:
                out.append(t)
                if len(out) &gt;= top:
                    break
    return out


def attach_holdings_all(secids, max_workers: int = 20, country: str = "United States", top: int = 10):
    holdings = [None] * len(secids)
    with ThreadPoolExecutor(max_workers=max_workers) as ex:
        futs = {ex.submit(fetch_holdings_us_topN, sid, country, top): i for i, sid in enumerate(secids)}
        for fut in as_completed(futs):
            i = futs[fut]
            try:
                holdings[i] = fut.result()
            except Exception:
                holdings[i] = []
    return np.asarray(holdings, dtype=object)

# 3) De-dup share classes (fixes your repeated funds)
#    Rule: same holdings signature AND strong common name prefix.
def _name_tokens(name: str) -&gt; list[str]:
    s = (name or "").strip().lower()
    s = s.replace("&#8211;", "-").replace("&#8212;", "-")
    s = re.sub(r"[()\[\],]", " ", s)
    return [t for t in s.split() if t]


def _common_prefix_len(a: list[str], b: list[str]) -&gt; int:
    n = min(len(a), len(b))
    k = 0
    while k &lt; n and a[k] == b[k]:
        k += 1
    return k


def _pick_best_index(arr: np.ndarray, idxs: list[int], keep: str = "max_return") -&gt; int:
    if keep == "first":
        return idxs[0]
    # keep == "max_return" (nan-safe)
    best = idxs[0]
    best_r = arr["ReturnM120"][best]
    for i in idxs[1:]:
        r = arr["ReturnM120"][i]
        r_ok = np.isfinite(r)
        b_ok = np.isfinite(best_r)
        if (r_ok and not b_ok) or (r_ok and b_ok and r &gt; best_r):
            best = i
            best_r = r
    return best


def dedupe_unnecessary_share_classes(
    arr: np.ndarray,
    min_prefix_tokens: int = 8,
    prefix_ratio: float = 0.60,
    keep: str = "max_return",
) -&gt; np.ndarray:
    """
    Dedup only when:
      - Stocks tuple matches exactly (same signature)
      - AND fund names share a long common prefix (token-based)

    This fixes cases like:
      '... Fund I Income GBP' vs '... Fund R Income'
      '... Fund B USD' vs '... Fund B EUR'
    """
    if arr.size == 0:
        return arr

    # Group by holdings signature; empty holdings =&gt; never dedup (keep all)
    groups: dict[tuple, list[int]] = {}
    unique_empty_keys: list[int] = []

    tokens_cache = [None] * arr.size

    for i in range(arr.size):
        toks = _name_tokens(str(arr["Fund"][i]))
        tokens_cache[i] = toks

        stocks = arr["Stocks"][i]
        try:
            sig = tuple(stocks) if stocks else tuple()
        except TypeError:
            sig = tuple()

        if len(sig) == 0:
            unique_empty_keys.append(i)
        else:
            groups.setdefault(sig, []).append(i)

    kept = set(unique_empty_keys)

    # For each signature group, cluster by name-prefix similarity
    for sig, idxs in groups.items():
        # sort by name (stable) to make clustering deterministic
        idxs_sorted = sorted(idxs, key=lambda i: arr["Fund"][i])

        clusters: list[list[int]] = []
        reps: list[int] = []  # representative index per cluster

        for i in idxs_sorted:
            ti = tokens_cache[i]
            placed = False

            for c_idx, rep_i in enumerate(reps):
                tr = tokens_cache[rep_i]
                cpl = _common_prefix_len(ti, tr)

                threshold = max(min_prefix_tokens, int(math.ceil(prefix_ratio * min(len(ti), len(tr)))))
                if cpl &gt;= threshold:
                    clusters[c_idx].append(i)
                    placed = True
                    break

            if not placed:
                reps.append(i)
                clusters.append([i])

        # keep one per cluster
        for cl in clusters:
            best = _pick_best_index(arr, cl, keep=keep)
            kept.add(best)

    kept_idx = np.array(sorted(kept), dtype=int)
    return arr[kept_idx]


def sort_by_return_desc(arr: np.ndarray) -&gt; np.ndarray:
    if arr.size == 0:
        return arr
    r = arr["ReturnM120"]
    r2 = np.where(np.isfinite(r), r, -np.inf)
    idx = np.argsort(r2)[::-1]
    return arr[idx]

# 4) Runner
def run(
    pages=10,
    page_size: int = 50,
    max_workers: int = 20,
    category_contains: str = "RV",
    holdings_country: str = "United States",
    holdings_top: int = 10,
    max_funds: int | None = None,  # recommended if pages=None
    dedupe: bool = True,
):
    t0 = time.perf_counter()

    secids, names, rets = scrape_lt_screener(
        pages=pages,
        page_size=page_size,
        category_contains=category_contains,
        max_funds=max_funds,
    )
    t1 = time.perf_counter()

    stocks = attach_holdings_all(secids, max_workers=max_workers, country=holdings_country, top=holdings_top)
    t2 = time.perf_counter()

    arr = np.empty(len(secids), dtype=[("Id", "U16"), ("Fund", "U256"), ("ReturnM120", "f8"), ("Stocks", "O")])
    arr["Id"] = np.asarray(secids, dtype="U16")
    arr["Fund"] = np.asarray(names, dtype="U256")
    arr["ReturnM120"] = rets
    arr["Stocks"] = stocks

    arr = sort_by_return_desc(arr)

    before = arr.size
    if dedupe:
        arr = dedupe_unnecessary_share_classes(
            arr,
            min_prefix_tokens=8,   # if you still see duplicates, increase to 10&#8211;12
            prefix_ratio=0.60,
            keep="max_return",
        )
        arr = sort_by_return_desc(arr)
    after = arr.size

    print(
        f"Screener: {len(secids)} funds in {t1 - t0:.3f}s | "
        f"Holdings in {t2 - t1:.3f}s | Total {t2 - t0:.3f}s | "
        f"Dedupe: {before}-&gt;{after}"
    )
    return arr


import numpy as np
import matplotlib.pyplot as plt
from collections import Counter

def topk_stock_table_and_plot(arr: np.ndarray, topk: int = 20, *, title: str = "Top holdings frequency"):
    """
    Counts how often each ticker appears in arr["Stocks"] (per-fund top holdings list),
    returns a NumPy table with (Ticker, Count, PctFunds) and shows a barplot in BLACK + CYAN.

    Percentage = Count / (# funds with non-empty Stocks) * 100
    """
    if arr.size == 0:
        out = np.empty(0, dtype=[("Ticker", "U16"), ("Count", "i4"), ("PctFunds", "f8")])
        return out

    counter = Counter()
    funds_with_holdings = 0

    for stocks in arr["Stocks"]:
        if not stocks:
            continue
        funds_with_holdings += 1
        # count presence within a fund once (avoid double-counting duplicates inside the list)
        seen = set()
        for t in stocks:
            if t is None:
                continue
            t = str(t).strip().upper()
            if not t:
                continue
            seen.add(t)
        counter.update(seen)

    if funds_with_holdings == 0 or not counter:
        out = np.empty(0, dtype=[("Ticker", "U16"), ("Count", "i4"), ("PctFunds", "f8")])
        return out

    items = counter.most_common(topk)
    tickers = [k for k, _ in items]
    counts = np.array([v for _, v in items], dtype=np.int32)
    pct = counts / float(funds_with_holdings) * 100.0

    table = np.empty(len(tickers), dtype=[("Ticker", "U16"), ("Count", "i4"), ("PctFunds", "f8")])
    table["Ticker"] = np.array(tickers, dtype="U16")
    table["Count"] = counts
    table["PctFunds"] = pct

    fig, ax = plt.subplots(figsize=(10, 6))
    fig.patch.set_facecolor("black")
    ax.set_facecolor("black")

    y = np.arange(len(tickers))
    ax.barh(y, pct, color="cyan", edgecolor="cyan")

    ax.set_yticks(y)
    ax.set_yticklabels(tickers)
    ax.invert_yaxis()

    ax.set_xlabel(f"% of funds (N={funds_with_holdings})")
    ax.set_title(title)

    ax.tick_params(colors="cyan")
    ax.xaxis.label.set_color("cyan")
    ax.yaxis.label.set_color("cyan")
    ax.title.set_color("cyan")
    for spine in ax.spines.values():
        spine.set_color("cyan")
    ax.grid(True, axis="x", linestyle="--", linewidth=0.6, color="cyan", alpha=0.35)

    plt.tight_layout()
    plt.show()

    return table


def print_topk_table(table: np.ndarray, decimals: int = 2):
    """Pretty-prints the output table (Ticker, Count, PctFunds)."""
    if table.size == 0:
        print("Empty table.")
        return
    w_t = max(6, int(max(len(t) for t in table["Ticker"])))
    header = f"{'Ticker':&lt;{w_t}}  {'Count':&gt;7}  {'PctFunds':&gt;9}"
    print(header)
    print("-" * len(header))
    for row in table:
        print(f"{row['Ticker']:&lt;{w_t}}  {int(row['Count']):&gt;7}  {row['PctFunds']:&gt;9.{decimals}f}")

if __name__ == "__main__":
    matrix = run(pages=10, max_workers=20)  # pages=None --&gt; all pages (very large)
    print(matrix[:5])

    table = topk_stock_table_and_plot(matrix, topk=25, title="Most-held stocks (top holdings lists)")
    print_topk_table(table)</code></pre></div>]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Infra: ETL process ]]></title><description><![CDATA[Extract, Transform, Load]]></description><link>https://www.quantbeckman.com/p/with-code-infra-etl-process</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-infra-etl-process</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Mon, 09 Feb 2026 12:46:21 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>The role of ETL in the design of trading systems.</p></li><li><p>ETL failure modes and risks.</p></li><li><p>Technical preliminaries.</p></li><li><p>Designing an ETL.</p></li><li><p>Layers, contracts and canonical schema.</p></li><li><p>ETL vs ELT for trading.</p></li><li><p>What breaks ETL in trading systems.</p></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read the newsletter index</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Introduction</h4><p>We enter the part of the quant stack that drives everything: <em>data</em>. We treat data as the machinery that tells a trading system what the market <em>is</em>&#8212;minute by minute, venue by venue, revision by revision. </p><p>This new serie introduces a sequence of articles on infrastructure&#8212;ETLs, scraping, APIs&#8212;written from the quant-dev perspective: trading systems need <em>repeatable market states</em>. Research needs to recreate yesterday. Live trading needs to ingest today. Both need to refer to the same definitions.</p><p>A familiar pattern shows up in almost every desk, prop shop, or solo setup. Data starts as a handful of scripts, then grows into a maze of<em> just this once</em> fixes: a quick endpoint here, a patched column there, a notebook cell that becomes a dependency because it worked during a good backtest. The system still runs. Results still appear. But the pipeline becomes a moving object&#8212;shifting definitions, shifting assumptions, shifting outputs&#8212;until the only thing that remains stable is confusion.</p><p>That is why ETL deserves a dedicated article. The reason? A pipeline accumulates decisions: <em>what gets stored, what gets normalized, what gets ignored, what gets re-requested, what gets overwritten, what gets trusted&#8230;</em> Those decisions compound, and at some point they dominate the behavior of the entire stack. The strategy stops being the bottleneck. The data becomes the bottleneck.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MKgo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MKgo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png 424w, https://substackcdn.com/image/fetch/$s_!MKgo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png 848w, https://substackcdn.com/image/fetch/$s_!MKgo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png 1272w, https://substackcdn.com/image/fetch/$s_!MKgo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!MKgo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png" width="1456" height="832" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:832,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Diagram showing key parts of Extract, Transform, Load (ETL)&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Diagram showing key parts of Extract, Transform, Load (ETL)" title="Diagram showing key parts of Extract, Transform, Load (ETL)" srcset="https://substackcdn.com/image/fetch/$s_!MKgo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png 424w, https://substackcdn.com/image/fetch/$s_!MKgo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png 848w, https://substackcdn.com/image/fetch/$s_!MKgo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png 1272w, https://substackcdn.com/image/fetch/$s_!MKgo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcbae6a5a-6f47-45a6-8bd6-ad61df06a9f1_1600x914.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Scraping and APIs deserve equal attention because they are how most modern trading pipelines actually exist in the wild. They impose constraints, quirks, and failure patterns: response truncation, pagination inconsistencies, silent field changes, intermittent outages, caching surprises, and vendor helpfulness.</p><p>The goal across this series is to make data infrastructure feel less like maintenance and more like a controlled instrument. A good pipeline behaves like a well-specified system: deterministic when it should be deterministic, conservative when it must be conservative, and transparent when something deviates. </p><p>Check this paper to know more about ETLs:</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!ufHw!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcf9ff437-d834-4834-9618-0c668aad5e7d_447x549.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Data extraction, Transformation, and Loading process automation </div><div class="file-embed-details-h2">299KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/f9f68374-185f-4f2e-810e-7d65e5bef13a.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/f9f68374-185f-4f2e-810e-7d65e5bef13a.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>So the promise of this article is concrete. By the end, it becomes natural to answer questions that otherwise stay vague: <em>Where did this feature come from? Which definition did it use? When did it change? Can the dataset be recreated byte-for-byte? What happens when the vendor revises history? How does the system fail when the feed degrades?</em> Those are operational questions, but they decide whether research can be trusted and whether live behavior can be diagnosed.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ruL8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ruL8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!ruL8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!ruL8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!ruL8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!ruL8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e63263f2-879a-4c14-9023-28891d320bef_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1597099,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/187194814?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ruL8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!ruL8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!ruL8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!ruL8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe63263f2-879a-4c14-9023-28891d320bef_1536x1024.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>The role of ETL in the design of trading systems</h4><p>Three components are typically treated as <em>core</em> in any trading system: <em>strategy logic, execution, and risk</em>. The ETL process&#8212;Extract, Transform, Load&#8212;is often categorized as a secondary support function. However, this categorization understates the structural role ETL plays in defining the parameters of the trading environment. It is the ETL process that establishes timestamp semantics and interval conventions, just as it defines the mechanics of volume aggregation across venues, determines the handling of missing prints or stale quotes, and establishes the boundaries of the trading day, including the definition of market close during partial sessions or under vendor-specific conventions.</p><p>A weak data pipeline permits a model to stay numerically consistent even when it lacks economic coherence. This failure manifests as an epistemic mismatch. The model trains on information unavailable at the time of execution or on data representations that drift between the training environment and the live feed. Backtests appear profitable but lack causal validity. Live trading degradation follows as a predictable consequence of this architecture.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kvJi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kvJi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png 424w, https://substackcdn.com/image/fetch/$s_!kvJi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png 848w, https://substackcdn.com/image/fetch/$s_!kvJi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png 1272w, https://substackcdn.com/image/fetch/$s_!kvJi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!kvJi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png" width="1456" height="832" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:832,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Extract and Load in the data pipeline&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Extract and Load in the data pipeline" title="Extract and Load in the data pipeline" srcset="https://substackcdn.com/image/fetch/$s_!kvJi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png 424w, https://substackcdn.com/image/fetch/$s_!kvJi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png 848w, https://substackcdn.com/image/fetch/$s_!kvJi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png 1272w, https://substackcdn.com/image/fetch/$s_!kvJi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fd8b909-b503-4e46-82a8-2fa06b9d6a84_1600x914.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Scale makes ETL automation a key pillar. But it is a challenge that requires automating the whole process. That means, new failures. From a quant dev perspective, this presents a tension between three competing criteria: <em>velocity, correctness, and observability</em>. Velocity is necessary because research iteration and feature publication are time-sensitive. Correctness is critical because a single semantic mismatch can introduce systematic lookahead bias without any explicit error in the code. Observability is essential because, without reproducibility and data lineage, it is impossible to distinguish between a failure of strategy logic and a failure of data construction. Once models are trained on warehouse-ready features, the ETL pipeline becomes an implicit component of the model itself. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><h4>ETL failure modes and risks</h4><p>ETL failure modes exist across domains, but in trading they are amplified because signals are marginal and backtests are sensitive to subtle causal violations. It is useful to name the principal failure modes,  as a map from data defects to PnL leakage.</p><ol><li><p><strong>Temporal integrity risk</strong> is the most frequent source of silent contamination. Timezone normalization errors&#8212;exchange time, broker server time, and UTC being conflated&#8212;create misalignment that can masquerade as alpha. Bar timestamp conventions are often ambiguous: some providers label bars by start time, others by end time, and others by a vendor-defined label that is neither. DST discontinuities can duplicate or remove hourly timestamps. APIs frequently return date fields with implicit timezones. Resampling without explicit session calendars can manufacture bars during market closures. None of these defects need an explicit future reference to introduce lookahead, misalignment is sufficient.</p></li><li><p><strong>Market microstructure risk</strong> emerges when sampling and aggregation rules are treated as interchangeable. For example, volume is venue-specific; cross-venue volume is not additive unless definitions match. VWAP computed from incomplete trade prints is not VWAP. OHLC bars from heterogeneous vendors can be numerically similar while being non-equivalent, which means return distributions can look stable while microstructure features drift. Mixing bid/ask and last-trade series destroys interpretability of returns, spread-sensitive features, and execution assumptions. Even quote filtering&#8212;removing stale quotes or enforcing spread constraints&#8212;changes the effective sampling distribution and can alter feature&#8211;target dependence.</p></li><li><p>Firm actions and instrument definition risk is where many useless backtests are born. Splits, dividends, symbol changes, roll schedules for futures, and specification changes in tick size or lot size all require explicit treatment. A toxic pattern is using adjusted close alongside raw open/high/low, which creates a <strong>synthetic object with inconsistent scale.</strong></p></li><li><p><strong>Data leakage risk </strong>is the highest-impact failure mode because it converts structural error into apparent predictive power. Vendor revisions&#8212;late prints, corrected OHLC&#8212;can enter historical features without an <em>as-of </em>policy. Using the close price for signals intended to be decided at open changes the filtration. Filling missing values using future data is leakage by construction. End-of-day fundamentals used for intraday signals without controlling for publication time inject future information. Even innocuous preprocessing, such as z-scoring using full-sample statistics, can leak information when deployed online.</p></li><li><p><strong>Heterogeneity risk </strong>is both schema and semantics. NaN encodings differ across sources (null, zero, empty string). Identical field names can have different meaning, as when volume is shares, contracts, or notional. Data types differ and can change silently (float versus decimal, integer timestamps versus ISO strings). Precision and rounding differences can alter microstructure-sensitive computations and create spurious stability.</p></li><li><p><strong>Operational risk</strong> appears when automation is treated as job finished equals success. Cron jobs can succeed while returning partial data. API rate limits can truncate responses. Network errors can create gaps, retries can create duplicates. Incremental loads can fail to be idempotent. Failures often cluster on holidays, early closes, vendor maintenance windows, and schedule drift that causes overlapping extraction windows.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!nSdA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nSdA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png 424w, https://substackcdn.com/image/fetch/$s_!nSdA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png 848w, https://substackcdn.com/image/fetch/$s_!nSdA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png 1272w, https://substackcdn.com/image/fetch/$s_!nSdA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!nSdA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png" width="1300" height="336" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fb58362a-4215-4be5-bf03-771412566ef2_1300x336.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:336,&quot;width&quot;:1300,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:277215,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/187194814?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!nSdA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png 424w, https://substackcdn.com/image/fetch/$s_!nSdA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png 848w, https://substackcdn.com/image/fetch/$s_!nSdA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png 1272w, https://substackcdn.com/image/fetch/$s_!nSdA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb58362a-4215-4be5-bf03-771412566ef2_1300x336.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Heavy transformations performed repeatedly inside modelling loops change the feasible hypothesis class. Recomputing features that should be materialized wastes iteration budget and induces inconsistent definitions when code evolves. Unbounded joins across large fact tables can make the research loop unstable under scaling.</p><h4>Technical preliminaries</h4><p>Trading doesn&#8217;t tolerate imprecise definitions because PnL is sensitive to small causal misalignments. Let <em>P<sub>t</sub></em> denote a price process under an explicit convention (bid, ask, mid, last). Let <em>V<sub>t</sub></em> denote an associated volume process with explicit units (shares, contracts, notional). Let <em>I<sub>t</sub></em> denote the sigma-algebra of information available up to time <em>t</em>. A trading signal <em>S<sub>t</sub></em> must be <em>I<sub>t</sub></em>-measurable.</p><p>An ETL pipeline can be modeled as a composition of maps,</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\mathrm{ETL} = \\mathcal{L} \\circ \\mathcal{T} \\circ \\mathcal{E},&quot;,&quot;id&quot;:&quot;SVAJPUEQTS&quot;}" data-component-name="LatexBlockToDOM"></div><p>where &#949; extracts from raw sources into an event stream, <em>T</em> transforms into conformed representations, and <em>L</em> loads into storage layers (OLTP and DW).</p><blockquote><ul><li><p><strong>Online transaction processing </strong>(OLTP) is a type of database system used in transaction-oriented applications, such as many operational systems. Online refers to the fact that such systems are expected to respond to user requests and process them in real-time (process transactions). The term is contrasted with online analytical processing (OLAP) which instead focuses on data analysis (for example planning and management systems).</p></li><li><p><strong>Data warehouse</strong> (DW), is a system used for reporting and data analysis and is a core component of goberning. Data warehouses are central repositories of data integrated from disparate sources.</p></li></ul></blockquote><p>The critical property is causal admissibility. For any feature <em>F<sub>t</sub></em> used at decision time <em>t</em>, <em>F<sub>t</sub></em>  must be measurable with respect to <em>I<sub>t</sub></em>. ETL violates this property through mechanisms that look operationally reasonable: aggregations that use end-of-window values while decisions occur at window start; imputation via centered filters; backfills that overwrite previously published features without version control; and &#8220;helpful&#8221; resampling rules that use future ticks to fill an OHLC bar.</p><p>This is why causal admissibility must be enforced by automated tests. If the system can&#8217;t prove the property, the property doesn&#8217;t hold operationally.</p><p>To remove ambiguity, define three timelines. Observation time <em>t</em><sup>(</sup><em><sup>e</sup></em><sup>)</sup> (event-time) is when the market produced the observable. Processing time <em>t</em><sup>(</sup><em><sup>p</sup></em><sup>)</sup> is when the system ingested it. Decision time <em>t</em><sup>(</sup><em><sup>d</sup></em><sup>)</sup> is when the strategy commits to an action.</p><p>A bar at index <em>k</em> has an interval [<em>t<sub>k,start </sub>, t<sub>k,end</sub></em>). If a strategy acts at bar open, then <em>t<sub>k</sub></em><sup>(</sup><em><sup>d</sup></em><sup>)</sup> . Features must not depend on events with <em>t</em><sup>(</sup><em><sup>e</sup></em><sup>)</sup>&#8805;<em>t<sub>k,start</sub></em>. </p><p>Before diving into more complex staff, understanding where ETL pipelines typically struggle helps prioritize improvement efforts. Here&#8217;s a breakdown of common bottleneck areas:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!g84_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!g84_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png 424w, https://substackcdn.com/image/fetch/$s_!g84_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png 848w, https://substackcdn.com/image/fetch/$s_!g84_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png 1272w, https://substackcdn.com/image/fetch/$s_!g84_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!g84_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png" width="904" height="422" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:422,&quot;width&quot;:904,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:61759,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/187194814?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!g84_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png 424w, https://substackcdn.com/image/fetch/$s_!g84_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png 848w, https://substackcdn.com/image/fetch/$s_!g84_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png 1272w, https://substackcdn.com/image/fetch/$s_!g84_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86bb1e5f-53af-4e16-a56e-44e1763ac3c8_904x422.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Designing an ETL</h4><p>Every strategy carries implicit assumptions about what constitutes a day, how the open is defined under multi-session trading, which clock governs decisions, what constitutes a trade, for example for a VWAP aggregation, and when information enters <em>I<sub>t</sub></em> (publication timestamps, vendor revisions, and the latency of ingestion itself). ETL either formalizes these assumptions or smuggles them.</p><p>The baseline automation flow is simple and sensible: collect data on a schedule, store it in OLTP, transform and load it into a DW, then use the DW for trading workflows. That pattern improves throughput. The difference from a traditional ETL lies in representing the pipeline as a reproducible computational object with explicit contracts so that performance does not amplify error.</p><p>Bounded complexity matters because a system that can&#8217;t be described will not remain stable. Verifiable correctness matters because a property that can&#8217;t be checked automatically is not a property of the system.</p><p>ETL design can be posed as constrained optimization. Let &#952; parametrize pipeline configuration (batch size, partitioning, retry policy, scheduling, feature versions). Let  <em>D</em>(&#952;) denote the dataset emitted for modelling. We care about generalization performance <em>G</em>(<em>D</em>(&#952;)), latency <em>L</em>(&#952;), cost <em>C</em>(&#952;) (compute, storage, API calls), and data quality risk <em>R</em>(&#952;) (probability-weighted impact of faults). A compact objective can be written as</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\min_{\\theta}\\; \\mathbb{E}\\!\\left[\\operatorname{Loss}\\!\\big(\\operatorname{model}(D(\\theta))\\big)\\right]\n\\;+\\; \\lambda_{1}\\mathcal{L}(\\theta)\\;+\\;\\lambda_{2}\\mathcal{C}(\\theta)\\;+\\;\\lambda_{3}\\mathcal{R}(\\theta),&quot;,&quot;id&quot;:&quot;WZYQAHIUQD&quot;}" data-component-name="LatexBlockToDOM"></div><p>subject to causal admissibility, idempotence, and lineage. The key conceptual shift is that ETL is an optimization problem.</p><p>A useful operational shift is to treat the pipeline as a parameterized program with a semantic version <em>v</em>. The output dataset is then <em>D<sub>v</sub></em>(&#952;). If either <em>v</em> or &#952; changes, you must assume the output is a different dataset, even if schemas appear identical. This rule prevents a large fraction of unreproducible model results.</p><p>Scheduling ETL jobs reduces contention with operational systems. However, smart scheduling goes beyond simple timing:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4qxr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4qxr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png 424w, https://substackcdn.com/image/fetch/$s_!4qxr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png 848w, https://substackcdn.com/image/fetch/$s_!4qxr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png 1272w, https://substackcdn.com/image/fetch/$s_!4qxr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!4qxr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png" width="901" height="261" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:261,&quot;width&quot;:901,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:33526,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/187194814?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4qxr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png 424w, https://substackcdn.com/image/fetch/$s_!4qxr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png 848w, https://substackcdn.com/image/fetch/$s_!4qxr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png 1272w, https://substackcdn.com/image/fetch/$s_!4qxr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdad62d1a-8300-44fc-a72b-1087ff14f0a9_901x261.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4><strong>Layers, contracts and canonical schema</strong></h4><p>A robust architecture is best described as layers governed by explicit contracts. At the perimeter, source connectors interact with APIs, brokers, file drops, and vendor feeds. Their contract is to emit raw events with source metadata, explicit time conventions, and enough information to reproduce the request.</p><p>Raw ingestion into OLTP should be append-heavy and minimally transformative. Its contract is to preserve raw information, enforce uniqueness, and preserve provenance. Canonicalization then converts heterogeneous source representations into a canonical schema by enforcing time normalization, type normalization, and symbol mapping. The DW layer provides partitioned fact tables and dimensions (assets, calendars, sources) with stable semantics optimized for analytical access.</p><p>Above the DW, a feature store publishes versioned feature vectors and tensors consumed by research and live engines. The feature store is the interface that enforces immutability by version and train/live parity. Finally, model inputs and backtest feeds are derived from the feature store as arrays, parquet datasets, or tight queries.</p><p>A canonical schema must minimize semantic ambiguity. At minimum, an asset dimension fixes identifiers, venues, tick sizes, and calendars. A calendar dimension fixes session definitions, holidays, and early closes. A source dimension fixes provider identity, endpoint sampling rules, time conventions, and revision policy. Facts separate raw events from derived bars and derived bars from derived features. This separation prevents circularity and leakage.</p><p>Establishing baseline metrics is essential before implementing optimization strategies. Track these KPIs to measure improvement:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rVJ5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rVJ5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png 424w, https://substackcdn.com/image/fetch/$s_!rVJ5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png 848w, https://substackcdn.com/image/fetch/$s_!rVJ5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png 1272w, https://substackcdn.com/image/fetch/$s_!rVJ5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!rVJ5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png" width="896" height="353" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:353,&quot;width&quot;:896,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:43328,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/187194814?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rVJ5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png 424w, https://substackcdn.com/image/fetch/$s_!rVJ5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png 848w, https://substackcdn.com/image/fetch/$s_!rVJ5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png 1272w, https://substackcdn.com/image/fetch/$s_!rVJ5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9eaddcbd-12d4-47cb-9124-069d42520620_896x353.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>A critical design choice is to represent bars with explicit start/end timestamps. Ambiguous labels are a recurring source of leakage.</p><pre><code>CREATE TABLE IF NOT EXISTS fact_bar (
  asset_id           BIGINT NOT NULL,
  ts_start_utc       TIMESTAMPTZ NOT NULL,
  ts_end_utc         TIMESTAMPTZ NOT NULL,
  open               DOUBLE PRECISION,
  high               DOUBLE PRECISION,
  low                DOUBLE PRECISION,
  close              DOUBLE PRECISION,
  volume             DOUBLE PRECISION,
  source_id          TEXT NOT NULL,
  ingest_ts_utc      TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  quality_flags      INTEGER NOT NULL DEFAULT 0,
  PRIMARY KEY (asset_id, ts_start_utc, source_id)
);

CREATE INDEX IF NOT EXISTS idx_fact_bar_end
  ON fact_bar(asset_id, ts_end_utc);</code></pre><p>This schema forces a commitment to interval semantics and prevents a broad class of timestamp-induced lookahead errors.</p><pre><code>CREATE TABLE IF NOT EXISTS feature_vector (
  asset_id            BIGINT NOT NULL,
  ts_utc              TIMESTAMPTZ NOT NULL,
  feature_set_version TEXT NOT NULL,
  f1                  DOUBLE PRECISION,
  f2                  DOUBLE PRECISION,
  f3                  DOUBLE PRECISION,
  quality_flags       INTEGER NOT NULL DEFAULT 0,
  PRIMARY KEY(asset_id, ts_utc, feature_set_version)
);</code></pre><h4>ETL vs ELT for trading</h4>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-infra-etl-process">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Data: Data structures as lifecycle engineering ]]></title><description><![CDATA[Table of contents:]]></description><link>https://www.quantbeckman.com/p/with-code-data-data-structures-as</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-data-data-structures-as</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Tue, 27 Jan 2026 13:16:28 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>When fast becomes slow.</p></li><li><p>Risks and framework limitations.</p></li><li><p>Lifecycle engineering for low latency data structures.</p><ol><li><p>Point 1 &#8212; Capacity-constrained processing.</p></li><li><p>Point 2 &#8212; Tail telemetry.</p></li><li><p>Point 3 &#8212; Consistency domains without global locks.</p></li><li><p>Point 4 &#8212; Schema and layout hashing.</p></li><li><p>Point 5 &#8212; Deterministic replay.</p></li><li><p>Point 6 &#8212; Fault containment, controlled degradation, and restart integrity.</p></li><li><p>Point 7 &#8212; Latency distribution.</p></li></ol></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read the newsletter index</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Introduction</strong></h4><p>Most performance failures in trading systems don&#8217;t look like failures. You know, impressive microbenchmarks, clean profiles, and average latency that inspire confidence. Then the market compresses time. A volatility microburst lands, the arrival process stops behaving like expected and the engine doesn&#8217;t slow down uniformly. One thread stays current, another drifts into backlog, and a third starts making decisions with a reality that no longer exists. This sounds familiar, doesn&#8217;t it?</p><p>The strategy still runs. Orders still go out. PnL still prints. But the system has quietly crossed a boundary. That boundary is the actual subject of low-latency engineering. Not <em>how fast can you execute a loop</em>, but <strong>under what load does your system lose  coherence</strong>&#8212;and what happens to decision quality when it does. </p><p>Whether the state you <em>believed</em> you had matches the book you <em>actually</em> traded against. A system that is quick most of the day yet slow precisely during clustered order-flow is not a fast system. The result? The trading system becomes hardware-dependent, shaped by queue depth, cache residency, coherence traffic, parser variability, and scheduling noise. At that point, <em>edge</em> <em>becomes a random variable</em>.</p><p>To know more about that, check this:</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!33rx!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbfaedc4-b3e7-49b8-8896-1299840838c9_1304x692.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Insights into trading system dynamics</div><div class="file-embed-details-h2">2.36MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/7eb72003-56b5-4037-802a-5ee93baed3ed.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/7eb72003-56b5-4037-802a-5ee93baed3ed.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>Today we review more than simple micro-optimizations. Once allocator jitter, false sharing, and accidental copies are tamed, the remaining problem is <em>lifecycle</em>. How is state created, mutated, published, observed, persisted, replayed, and recovered&#8212;<em>without</em> letting burst dynamics rewrite the signal? </p><p>If your data structures only describe access patterns, they are incomplete. In a low-latency trading engine, the data structure is a<strong> </strong><em>protocol</em>: it defines visibility, atomicity, backpressure, and the admissible ways in which time is allowed to slip between components. When those rules are implicit, the market discovers them for you, usually at the worst possible moment.</p><p>We therefore shift the unit of design from fast-optimized code to <em>bounded behavior. </em>Bounded queues that force overload policies instead of hiding them. Telemetry that measures tails without creating tails. Consistency domains that deliver coherent snapshots without global locks. Schemas treated as binary contracts, so layout drift can&#8217;t silently tax the cache. Replay that reconstructs state transitions bit-for-bit, turning production into a debuggable artifact. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!aPtx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!aPtx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!aPtx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!aPtx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!aPtx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!aPtx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1353366,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/185731674?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!aPtx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!aPtx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!aPtx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!aPtx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc0b5897-be35-49ff-866c-4eb579efb49d_1536x1024.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>When fast becomes slow</h4><p>I would say that by the time you reach this stage of architectural maturity, you have likely solved the easy problems. You have eliminated allocator-driven jitter by pooling objects, you have enforced cache-line alignment to prevent false sharing between cores, and you have stopped letting hidden copies in your language runtime rewrite your latency distribution. You have moved beyond the naive optimization of average-case behavior and have started treating the CPU&#8217;s branch predictor, the Translation Lookaside Buffer, and the L3 cache as constraints.</p><p>However, a new problem appears once the micro-optimizations are complete. You can build a pipeline that is optimized for single-threaded throughput&#8212;clocking nano or microsecond-scale benchmarks in isolation&#8212;and yet remains strategically slow in production.</p><p>This paradox exists because the market doesn&#8217;t compensate participants based on mean latency or high-throughput benchmarks under ideal conditions. The market compensates based on the distributional arrival of information. Specifically, the market pays for decision accuracy conditioned on the state of the order book at the moment of execution. A system that is fast 99% of the time but slow during the critical 1% of volatility is not a fast system in the eyes of the PnL. </p><p>The dilemma is that <em>speed</em> (wire-to-wire latency) and <em>truth</em> (state consistency) are often orthogonal goals in naive architectures. To go faster, engineers often relax consistency guarantees, introduce unbounded buffers to absorb bursts, or rely on optimistic concurrency models that assume low contention. In a clustered arrival regime&#8212;which describes 100% of relevant market microstructure events due to the self-similar nature of order flow&#8212;these optimizations create a divergence between the internal state of the trading engine and the external reality of the exchange.</p><p>The <em>data structure problem</em> therefore shifts from a question of memory layout (Part I and II of the implied series) to a <em>lifecycle problem</em>: How is state created, mutated, observed, snapshotted, and recovered under bursty load without violating the semantic integrity of the strategy?</p><p>You can check them here:</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;d90af429-ab4d-4b57-b703-99c814f3d055&quot;,&quot;caption&quot;:&quot;&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;[WITH CODE] Data: Low-latency data structures&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:311582722,&quot;name&quot;:&quot;Quant Beckman&quot;,&quot;bio&quot;:&quot;A quant research lab in newsletter form. Turning mathematics into automated trading systems and systematic portfolios. &quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/29f90c76-3fd2-4648-8b68-02caa8de3e77_800x800.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:100}],&quot;post_date&quot;:&quot;2025-12-30T13:41:16.510Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.quantbeckman.com/p/with-code-data-low-latency-data-structures&quot;,&quot;section_name&quot;:&quot;Alpha Lab&quot;,&quot;video_upload_id&quot;:null,&quot;id&quot;:182763608,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:10,&quot;comment_count&quot;:0,&quot;publication_id&quot;:3833331,&quot;publication_name&quot;:&quot;Trading the Breaking&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;c856b7fb-9a32-4e4c-a141-14ee86e08cb1&quot;,&quot;caption&quot;:&quot;&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;[WITH CODE] Data: Building micro-trading machines&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:311582722,&quot;name&quot;:&quot;Quant Beckman&quot;,&quot;bio&quot;:&quot;A quant research lab in newsletter form. Turning mathematics into automated trading systems and systematic portfolios. &quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/29f90c76-3fd2-4648-8b68-02caa8de3e77_800x800.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:100}],&quot;post_date&quot;:&quot;2026-01-13T13:00:21.305Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.quantbeckman.com/p/with-code-data-building-micro-trading&quot;,&quot;section_name&quot;:&quot;Alpha Lab&quot;,&quot;video_upload_id&quot;:null,&quot;id&quot;:184204878,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:7,&quot;comment_count&quot;:0,&quot;publication_id&quot;:3833331,&quot;publication_name&quot;:&quot;Trading the Breaking&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><h4>Risks and framework limitations</h4><p>When a trading engine moves from a controlled research environment to live production, the mathematical definition of its operation changes fundamentally.</p><p>In research, the strategy is modeled as a deterministic function <em>F</em>:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;A_t = F(S_t, \\theta)&quot;,&quot;id&quot;:&quot;LFWOHADLCI&quot;}" data-component-name="LatexBlockToDOM"></div><p>Where <em>A<sub>t</sub></em> is the action at time <em>t</em>, <em>S<sub>t</sub></em> is the market state, and &#952; are parameters. In this environment, <em>S<sub>t</sub></em> is perfect, and <em>A<sub>t</sub></em> is instantaneous.</p><p>In production, the strategy degrades into a stochastic operator <em>F&#770;</em>:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;A_{t+\\delta} = \\hat{F}(S_{t-\\epsilon}, \\theta, \\xi_t)&quot;,&quot;id&quot;:&quot;AJZROMGBWO&quot;}" data-component-name="LatexBlockToDOM"></div><p>Here, &#120585;<em><sub>t</sub></em> represents hidden system variables that act as noise terms:</p><ul><li><p><strong>Queue depth (</strong><em>Q<sub>t</sub></em><strong>):</strong> The backlog of unprocessed messages, which acts as a proxy for the information gap between the exchange and the CPU.</p></li><li><p><strong>Cache residency:</strong> The probability that the relevant state <em>S<sub>t</sub></em> is in L1/L2 cache versus main memory (RAM), introducing a variance of ~200 cycles vs ~300 nanoseconds per access.</p></li><li><p><strong>Coherence traffic:</strong> The latency penalty induced by other cores invalidating shared cache lines, often caused by the OS scheduler or noisy neighbor threads.</p></li><li><p><strong>Parsing jitter:</strong> The variance in time required to decode the wire format, often depending on the specific template ID or message schema variability.</p></li></ul><p>The dominant risk is not that the system is slow in a linear sense. The risk is that &#949; (staleness) and &#120575; (execution delay) are <em>correlated</em> with market volatility.</p><p>If Cov(&#949;, | &#916;<em>P<sub>t</sub></em>} &gt; 0, your system is statistically most likely to be acting on stale data exactly when the price is moving the most. For example, during a sudden liquidity collapse, the incoming message rate spikes, increasing &#949;. Simultaneously, |&#916;<em>P<sub>t</sub></em>| is large. Your strategy might try to hit a bid that vanished 50 microseconds ago because it is still processing the queue from 100 microseconds ago. This converts what looks like technical edge into noise, effectively randomizing the PnL of the strategy. The decision function is no longer deterministic, it is a random variable dependent on the micro-state of the hardware.</p><p>The event that exposes this fragility is the <strong>microburst</strong>.</p><blockquote><p>Standard Poisson arrival models fail to capture the reality of market data, which is characterized by self-similarity, heavy tails, and clustering. A microburst is is a <em>phase transition</em> in the queuing system (a <em>very short, intense spike</em> in activity).</p></blockquote><p>As visualized in our queue topology simulation (plot 1 below), when arrival intensity &#955;(t) momentarily exceeds service rate &#956;(t), the queue length <em>Q<sub>t</sub></em> does not just increase linearly; it undergoes a regime shift. The system transitions from a real-time state (<em>Q</em>&#8776;0), where the CPU is waiting for network packets, to a batch state (Q&#8811;0), where the network card is buffering packets faster than the PCI bus or the CPU can ingest them.</p><p>Crucially, in this batch state, the system is <em>partially ordered</em>. Because different components of a trading system (feed handlers, strategy logic, execution gateways, risk checks) often run on decoupled threads or separate cores, a burst desynchronizes their views of the world:</p><ol><li><p>The feed handler receives update <em>U</em><sub>1</sub>, <em>U</em><sub>2</sub>, <em>U</em><sub>3</sub>.</p></li><li><p>The strategy calculates a signal based on <em>U</em><sub>1</sub>.</p></li><li><p>The risk/order manager checks limits based on <em>U</em><sub>3</sub> (because it processes a different stream or shares a flag updated asynchronously).</p></li></ol><p>At this moment, the system has no single time. The strategy logic and the execution logic occupy different temporal frames. This is the <em>main problem</em>: The burst breaks the semantic integrity of the engine. The system is a collection of race conditions executing in parallel, where the winner is determined by cache arbitration rather than financial logic.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><p>We must shift our perspective on what a <em>low-latency data structure</em> actually is.</p><p>In standard quant dev, a data structure is defined by its asymptotic complexity (<em>O</em>(1) lookup, <em>O</em>(log <em>N</em>) insertion). In low latency architectures, this view is insufficient because constants matter, and the cost of the worst-case operation dominates the PnL profile.</p><p>A data structure must be seen as a set of rules. It is a boundary definition that governs:</p><ul><li><p><strong>Visibility:</strong> When exactly does a write by the feed handler become visible to the strategy thread? Is it after a memory fence? Is it relaxed?</p></li><li><p><strong>Atomicity:</strong> Can the strategy see a torn price (e.g., a bid from time <em>t</em> and an ask from time <em>t</em>+1)? If so, the strategy might compute a phantom arbitrage opportunity that exists only in the corrupted memory state.</p></li><li><p><strong>Backpressure:</strong> How does the structure communicate to the upstream socket that it is full? Does it block? Does it drop? Does it signal the sender to slow down (which is impossible with UDP multicast)?</p></li><li><p><strong>Lifecycle:</strong> How is the data persisted for replay without blocking the read/write path? Can we reconstruct the exact sequence of memory states from the log files?</p></li></ul><p>This is different from optimizing code for instruction count. In this point we are optimizing the <em>conditional law of decision latency</em>. We are engineering the system such that the latency distribution <em>P</em>(<em>L<sub>t</sub> &gt; x</em>) decays super-exponentially, ensuring that even in the 99.99th percentile of events (the burst), the system remains within the coherence window of the market.</p><p>The implementation of this approach faces severe structural obstacles that cannot be solved by simply writing faster code:</p><ol><li><p><strong>Boundedness vs. blindness:</strong></p><ul><li><p>Infinite or dynamically resizing buffers (like <code>std::vector</code> or Python lists) hide overload. They allow the system to ingest a burst it cannot process, creating a standing wave of latency that persists long after the burst ends. The system continues to process old data while the market has moved on.</p></li></ul><ul><li><p>We need buffers that are strictly bounded. However, bounding a buffer requires a policy for rejection (drop vs. overwrite). This forces the strategy to handle gaps in data. A gap in a price series creates a discontinuity in derivative calculations, complicating the quant logic and requiring robust exception handling in the math itself.</p></li></ul></li><li><p><strong>Observation vs. perturbation:</strong></p><ul><li><p>Measuring latency typically involves getting timestamps (<code>rdtsc</code> or <code>clock_gettime</code>) and logging them. Writing to a log file involves blocking I/O; writing to memory involves cache pollution. The act of measuring the tail often <em>causes</em> the tail by evicting critical strategy data from the L1 cache to make room for telemetry structs.</p></li><li><p>We need telemetry that is zero-cost in the cache-hot path, storing only summary statistics (quantiles) rather than raw logs, without allocating memory or contending for lock resources.</p></li></ul></li><li><p><strong>Consistency vs. contention:</strong></p><ul><li><p>To ensure the strategy sees a consistent snapshot (e.g., best bid and best ask from the same packet), the naive solution is a <code>mutex</code> or <code>rwlock</code>. Locks induce context switches, force the OS to schedule threads, and stall the CPU pipeline. In a microsecond environment, a context switch is a massive penalty.</p></li><li><p>We need a wait-free snapshot mechanism that guarantees consistency without blocking the writer, even if it requires the reader to retry. We trade CPU cycles (retries) for latency stability (no blocking).</p></li></ul></li><li><p><strong>Replay fidelity:</strong></p><ul><li><p>Production systems often differ from backtests because the production system has state (sequence numbers, partial fills, TCP retransmits) that the backtest ignores. A strategy that worked in the simulator fails in production because the simulator did not model the sequence of partial updates caused by network fragmentation.</p></li><li><p>We need a data structure layout that supports <em>deterministic replay</em>. We must be able to feed the raw wire capture into the engine and produce the <em>exact</em> same state transitions, bit-for-bit, as occurred in live trading. This requires treating the entire engine as a deterministic finite state machine derived solely from the input log.</p></li></ul></li></ol><p>This sets the stage for building a deterministic state machine capable of surviving the hostile environment of live market microstructure.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Bi5e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Bi5e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Bi5e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Bi5e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Bi5e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Bi5e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1306150,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/185731674?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Bi5e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Bi5e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Bi5e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Bi5e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6a99ef2-142a-427e-b1ff-f3e46172d68d_1536x1024.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Lifecycle engineering for low latency data structures</h4><p>This type of engine rests on seven pillars. These pillars are are the axioms that prevent a system from becoming non-deterministic. To continue with the previous articles, we will proceed with the examples implemented in Python.</p><p><strong>Point 1 &#8212; Capacity-constrained processing</strong></p><p>The first misconception to dismantle is the idea that <em>larger buffers equal greater safety</em>. In latency-sensitive systems, <em>buffers are debt</em>. They convert instantaneous overload (a bandwidth problem) into delayed decision error (a latency problem).</p><p>Let arrivals be a clustered process with intensity &#955;(<em>t</em>) and service rate &#956;(<em>t</em>). If we use a dynamically resizing queue (like a Python <code>list</code> or C++ <code>std::vector</code>), we implicitly allow queue length <em>Q<sub>t</sub></em> &#8614; &#8734;.</p><p>During a burst, <em>Q<sub>t</sub></em> grows. If the burst lasts 100ms and the system lags by 50ms, the strategy calculates signals based on prices that are 50ms old. This is an eternity, the alpha has decayed to zero or reversed. Furthermore, the very act of resizing a dynamic container during a burst exacerbates the problem. When a <code>std::vector</code> or dynamic list hits capacity, the runtime must allocate a larger block of memory, copy all existing elements to the new block, and free the old one. This operation causes a massive latency spike exactly when the system can least afford it, effectively pausing processing while the queue is flooding.</p><p>The solution involves bounded and pre-assigned queues with explicit overflow. So we must use fixed-capacity, circular buffers. The overflow behavior must be defined. This forces the engineering team to confront the reality of finite resources before the market does it for them.</p><ul><li><p><strong>Drop newest:</strong> Ignore the new packet (good for keeping the strategy processing contiguous history, bad for recency). This effectively sheds load at the gateway, ensuring the internal engine never processes data faster than it can consume, but it risks <em>blindness</em> to the current market state.</p></li><li><p><strong>Drop oldest:</strong> Overwrite the tail (maximizes recency, but introduces data gaps). This is equivalent to saying, &#8220;If I can&#8217;t process everything, I will at least process the most current thing.&#8221; It breaks the assumption of a continuous time series, forcing the strategy to be robust against missing ticks.</p></li></ul><p>For algorithmic trading, <strong>drop oldest</strong> (circular overwrite) is often preferred for market data (we care about the <em>now</em> price), while <strong>drop newest</strong> might be preferred for <em>order entry</em> (we don&#8217;t want to skip a sequence number).</p><p>We use a <code>numpy</code> based Structure of Arrays. This ensures that reading a specific field (e.g., all timestamps) utilizes full cache lines, unlike an Array of Structures or a list of objects. By keeping data contiguous in memory, we minimize the CPU&#8217;s need to fetch data from main RAM, keeping the hot path executing almost entirely within L1 or L2 cache.</p><pre><code>import numpy as np

class BoundedEventQueue:
    """
    Preallocated circular queue for fixed-schema events.
    Overflow is explicit. No GC. No resizing.
    Uses Structure of Arrays (SoA) to maximize cache locality for column-based analysis.
    """
    __slots__ = ("cap", "mask", "write", "read",
                 "ts", "typ", "sym", "px", "qty",
                 "overflow_count")

    DROP_NEWEST = 0
    DROP_OLDEST = 1

    def __init__(self, cap_pow2: int):
        # Enforcing power of 2 allows bitwise AND masking (x &amp; mask) 
        # which is significantly faster than modulo (x % cap) and avoids division instructions.
        if cap_pow2 &amp; (cap_pow2 - 1) != 0:
            raise ValueError("Capacity must be a power of 2.")
        self.cap = int(cap_pow2)
        self.mask = self.cap - 1
        self.write = 0
        self.read = 0

        # Fixed schema (SoA) - Preallocation prevents runtime allocation
        # We assume 64-bit timestamps and 32-bit prices/quantities.
        self.ts  = np.empty(self.cap, dtype=np.uint64)
        self.typ = np.empty(self.cap, dtype=np.uint8)
        self.sym = np.empty(self.cap, dtype=np.uint16)
        self.px  = np.empty(self.cap, dtype=np.int32)
        self.qty = np.empty(self.cap, dtype=np.int32)

        self.overflow_count = 0

    def push(self, ts, typ, sym, px, qty, overflow_policy=DROP_OLDEST) -&gt; bool:
        """
        Push operation that is strictly O(1) and allocation-free.
        """
        # Check occupancy
        size = self.write - self.read
        if size &gt;= self.cap:
            self.overflow_count += 1
            if overflow_policy == self.DROP_NEWEST:
                return False
            # DROP_OLDEST: We forcibly advance the read pointer, effectively
            # "forgetting" the oldest unread message to make space for the new one.
            # This maintains the "freshness" of the buffer at the cost of history.
            self.read += 1

        # Bitwise mask for circular wrapping
        i = self.write &amp; self.mask
        
        # Direct memory access without object overhead
        self.ts[i]  = ts
        self.typ[i] = typ
        self.sym[i] = sym
        self.px[i]  = px
        self.qty[i] = qty
        
        self.write += 1
        return True</code></pre><p><em>Note: Full low latency data structures is in the provided code of the <strong>appendix</strong>.</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iSsD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iSsD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png 424w, https://substackcdn.com/image/fetch/$s_!iSsD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png 848w, https://substackcdn.com/image/fetch/$s_!iSsD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png 1272w, https://substackcdn.com/image/fetch/$s_!iSsD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!iSsD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png" width="854" height="463" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:463,&quot;width&quot;:854,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iSsD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png 424w, https://substackcdn.com/image/fetch/$s_!iSsD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png 848w, https://substackcdn.com/image/fetch/$s_!iSsD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png 1272w, https://substackcdn.com/image/fetch/$s_!iSsD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6deb5aa-4098-4dd0-b1de-52d321579cc9_854x463.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This structure forces the architect to answer the question: <em>What happens when </em><code>overflow_count &gt; 0</code>? The answer usually involves entering a risk reduction mode. If the queue overflows, the strategy is legally blind; the only safe action is to widen quotes or pull orders until the <code>overflow_count</code> stabilizes and the system catches up.</p><p><strong>Point 2 &#8212; Tail telemetry</strong></p><p>The second pillar is observability. You can&#8217;t manage what you can&#8217;t measure, but measuring the 99.9th percentile (<em>Q<sub>0.999</sub></em>) of latency is statistically difficult.</p><p>A naive average (mean) is useless in HFT because latency distributions are heavy-tailed (Power Law or Pareto-like). A slow event might be 1000x slower than the median due to an OS context switch, a minor page fault, or a Garbage Collection pause. In these distributions, the mean is a misleading artifact of the tail.</p><p>The standard approach of storing all latency samples to compute an exact quantile is memory-prohibitive and causes cache pollution. Writing every single latency sample to a log file acts as an <em>observer effect</em>&#8212;the I/O overhead of logging the latency creates the latency you are trying to measure. We need a <em>streaming quantile estimator</em>&#8212;an algorithm that updates the estimate of the tail using constant memory.</p><p>The <em>P</em><sup>2</sup> (piecewise-parabolic) algorithm is ideal here. It maintains 5 marker points that track the shape of the CDF around the target quantile. As data streams in, these markers adjust their height and position to approximate the curve, allowing for accurate tail estimation without ever storing the raw data points.</p><p>This matters because if your telemetry system allocates memory (e.g., creating a string for a log message <code>logger.info(f"Latency: {lat}")</code>), that allocation itself can trigger a Garbage Collection pause or a <code>malloc</code> lock, causing the very latency spike you are trying to record. Zero-allocation telemetry is a requirement, not an optimization.</p><pre><code>import numpy as np

class P2Quantile:
    """
    Streaming quantile estimator using P^2 algorithm.
    Memory usage: Fixed (small arrays).
    Allocations per update: Zero.
    """
    def __init__(self, p: float):
        self.p = p
        # Markers for the P^2 algorithm
        # We only need 5 doubles to track the curve shape.
        self.q = np.zeros(5, dtype=np.float64) 
        # ... (initialization logic omitted for brevity) ...

    def update(self, x: float):
        # Complex logic to adjust markers based on parabolic interpolation.
        # This allows us to estimate the 99.9th percentile with high accuracy
        # after observing N samples, without storing N samples.
        # This function must be allocation-free to run in the hot path.
        pass</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PL0l!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdd8a20b-9e9f-44e2-973b-336801926968_846x463.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PL0l!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdd8a20b-9e9f-44e2-973b-336801926968_846x463.png 424w, https://substackcdn.com/image/fetch/$s_!PL0l!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdd8a20b-9e9f-44e2-973b-336801926968_846x463.png 848w, https://substackcdn.com/image/fetch/$s_!PL0l!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdd8a20b-9e9f-44e2-973b-336801926968_846x463.png 1272w, https://substackcdn.com/image/fetch/$s_!PL0l!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdd8a20b-9e9f-44e2-973b-336801926968_846x463.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!PL0l!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdd8a20b-9e9f-44e2-973b-336801926968_846x463.png" width="846" height="463" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bdd8a20b-9e9f-44e2-973b-336801926968_846x463.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:463,&quot;width&quot;:846,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PL0l!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdd8a20b-9e9f-44e2-973b-336801926968_846x463.png 424w, https://substackcdn.com/image/fetch/$s_!PL0l!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdd8a20b-9e9f-44e2-973b-336801926968_846x463.png 848w, https://substackcdn.com/image/fetch/$s_!PL0l!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdd8a20b-9e9f-44e2-973b-336801926968_846x463.png 1272w, https://substackcdn.com/image/fetch/$s_!PL0l!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdd8a20b-9e9f-44e2-973b-336801926968_846x463.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The output of this estimator should be fed directly into the autotrader&#8217;s logic. If <em>Q<sub>0.999 </sub></em>&gt; Threshold, the strategy should automatically widen spreads or reduce size, acknowledging that its reaction time has degraded. This turns telemetry from a passive debugging tool into an active risk control.</p><p><strong>Point 3 &#8212; Consistency domains without global locks</strong></p><p>The third pillar addresses the torn read problem.</p><p>Imagine a book update arrives:</p><ol><li><p>Bid Price: 100.05</p></li><li><p>Ask Price: 100.07</p></li></ol><p>The strategy thread reads the Bid (100.05). Before it reads the Ask, a new update arrives:</p><ol><li><p>Bid Price: 100.06</p></li><li><p>Ask Price: 100.08</p></li></ol><p>If the strategy reads the <em>new</em> Ask (100.08) combined with the <em>old</em> Bid (100.05), it sees a spread of 0.03 (100.08 - 100.05). But the real spread was always 0.02. If the strategy&#8217;s logic is &#8220;Buy if spread &gt; 0.025&#8221;, this phantom spread triggers a trade that immediately loses money. This is a <em>Heisenbug</em> that disappears when you add logging (because logging slows the writer down enough to avoid the race).</p><p>To prevent this without locking the writer (which stalls the feed handler), we use a <em>Sequence Lock</em> (Seqlock). A standard Mutex would force the writer to wait for readers, which is unacceptable for a feed handler that must process millions of messages per second.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WPYi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WPYi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png 424w, https://substackcdn.com/image/fetch/$s_!WPYi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png 848w, https://substackcdn.com/image/fetch/$s_!WPYi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png 1272w, https://substackcdn.com/image/fetch/$s_!WPYi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!WPYi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png" width="874" height="354" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:354,&quot;width&quot;:874,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:43995,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/185731674?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WPYi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png 424w, https://substackcdn.com/image/fetch/$s_!WPYi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png 848w, https://substackcdn.com/image/fetch/$s_!WPYi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png 1272w, https://substackcdn.com/image/fetch/$s_!WPYi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844c5a4e-c89e-4b2c-aa69-b45c755f1a24_874x354.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><pre><code>from multiprocessing import Value
import ctypes

class SharedSnapshot:
    def write(self, values):
        s = self.seq.value + 1
        self.seq.value = s       # Odd = Locked / Dirty
        # Implicit memory barrier needed here in C++ (std::memory_order_release)
        self.arr[:] = values     # Bulk copy
        # Implicit memory barrier needed here in C++ (std::memory_order_release)
        self.seq.value = s + 1   # Even = Unlocked / Clean

    def read(self, out):
        s1 = self.seq.value
        if s1 &amp; 1: return False  # Collision: Writer is currently writing
        
        # Optimistic read: assume no one is writing
        out[:] = self.arr        
        
        s2 = self.seq.value
        # If s1 != s2, it means the writer started writing while we were reading.
        # The snapshot in 'out' is corrupted.
        return s1 == s2          # Verification</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BXYz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BXYz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png 424w, https://substackcdn.com/image/fetch/$s_!BXYz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png 848w, https://substackcdn.com/image/fetch/$s_!BXYz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png 1272w, https://substackcdn.com/image/fetch/$s_!BXYz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!BXYz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png" width="841" height="463" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:463,&quot;width&quot;:841,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BXYz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png 424w, https://substackcdn.com/image/fetch/$s_!BXYz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png 848w, https://substackcdn.com/image/fetch/$s_!BXYz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png 1272w, https://substackcdn.com/image/fetch/$s_!BXYz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62a46489-1c34-4d91-b35e-9da1bc6bbeaf_841x463.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This defines a <em>consistency domain</em>. All fields inside <code>arr</code> are guaranteed to be from the same logical timestamp if <code>read()</code> returns True. This effectively eliminates a class of synchronization errors where the strategy acts on state combinations that never existed in reality. It shifts the cost of concurrency from the writer (who must never block) to the reader (who must occasionally retry), which aligns perfectly with the priorities of a trading system.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lF1e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" width="1456" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1158490,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3&quot;,&quot;text&quot;:&quot;Send me a copy&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3"><span>Send me a copy</span></a></p><p><strong>Point 4 &#8212; Schema and layout hashing</strong></p><p>In a dynamic language like Python (often used for the strategy layer wrapping C++ cores), or even in C++ with extensive templating, the physical layout of data can shift silently between code versions. This <em>schema drift</em> is often invisible in source code but fatal in the CPU cache.</p><p>Consider a developer who adds a single <code>bool</code> flag to an order struct to track a new risk check.</p><ul><li><p><strong>Before:</strong> The struct was 16 bytes (aligned). It fits perfectly 4 times into a standard 64-byte CPU cache line.</p></li><li><p><strong>After:</strong> The struct is nominally 17 bytes.</p></li></ul><p>However, the compiler or interpreter will not allocate exactly 17 bytes. To maintain memory alignment requirements for the CPU, it will likely add padding bytes, swelling the size to 24 or 32 bytes. This seemingly trivial change has hardware implications. It reduces the number of items that fit in a CPU cache line, increasing the cache miss rate during iteration. Furthermore, a 65-byte object (just 1 byte over the line) requires <em>two</em> cache line fetches to read a single object, effectively doubling the memory bandwidth cost for that access pattern.</p><p>Additionally, if the strategy logic relies on structured arrays or memory mapping, a mismatch in <code>dtype</code> definitions between the C++ writer and the Python reader can cause the runtime to perform an implicit <em>copy-cast</em> during data access. Instead of a zero-copy view, the system silently mallocs new memory and copies the data to fit the expected layout, introducing microsecond-level stalls on every access.</p><p>The solution is known as schema hashing. We treat the memory layout as a strict API. At startup, the system computes a hash of the data type&#8217;s internal memory layout (offsets, item sizes, field types). This hash acts as a cryptographic signature of the physical memory contract.</p><pre><code>import numpy as np
import hashlib

def dtype_layout_hash(dt: np.dtype) -&gt; str:
    """
    Computes a hash of the physical layout of a numpy dtype.
    Includes byte offsets, field order, and alignment details.
    """
    dt = np.dtype(dt)
    # Capture critical layout details that affect binary compatibility
    parts = [f"itemsize={dt.itemsize}", f"aligned={dt.isalignedstruct}"]
    for name in dt.names or ():
        f, off = dt.fields[name][0], dt.fields[name][1]
        parts.append(f"{name}:{f.str}@{off}")
    
    # Create a unique signature for this layout
    payload = "|".join(parts).encode("utf-8")
    return hashlib.sha256(payload).hexdigest()[:16]

# If this hash changes between the Writer (Feed Handler) and Reader (Strategy),
# the system refuses to start, preventing silent data corruption or performance degradation.</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sx6i!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sx6i!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png 424w, https://substackcdn.com/image/fetch/$s_!sx6i!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png 848w, https://substackcdn.com/image/fetch/$s_!sx6i!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png 1272w, https://substackcdn.com/image/fetch/$s_!sx6i!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!sx6i!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png" width="948" height="386" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:386,&quot;width&quot;:948,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sx6i!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png 424w, https://substackcdn.com/image/fetch/$s_!sx6i!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png 848w, https://substackcdn.com/image/fetch/$s_!sx6i!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png 1272w, https://substackcdn.com/image/fetch/$s_!sx6i!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8bcd0a2-c90c-4d9c-ade5-5a6968d09768_948x386.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This enforcement prevents release drift, where performance degrades slowly over time due to minor schema bloating, ensuring that the cache locality you engineered in Version 1.0 remains intact in Version 10.0.</p><p><strong>Point 5 &#8212; Deterministic replay</strong></p><p>A trading system that can&#8217;t be replayed deterministically is a black box. If a production loss occurs, you must be able to recreate the exact internal state of the strategy step-by-step to distinguish between bad luck (market moved) and bad logic (code failure).</p><p>This requires <em>event sourcing</em> logic. The <em>state</em> is a derivative of the <em>Log</em>.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;S_t = \\text{Apply}(S_0, \\text{Log}_{0 \\to t}) &quot;,&quot;id&quot;:&quot;XBOIDIUOUR&quot;}" data-component-name="LatexBlockToDOM"></div><p>To achieve this without IO blocking, we use<em> Memory Mapped Append-Only Logs.</em> Using standard file I/O (<code>write()</code>) involves a system call overhead and context switch for every packet, which is unacceptable.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HSys!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HSys!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png 424w, https://substackcdn.com/image/fetch/$s_!HSys!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png 848w, https://substackcdn.com/image/fetch/$s_!HSys!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png 1272w, https://substackcdn.com/image/fetch/$s_!HSys!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!HSys!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png" width="874" height="354" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:354,&quot;width&quot;:874,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:69983,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/185731674?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!HSys!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png 424w, https://substackcdn.com/image/fetch/$s_!HSys!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png 848w, https://substackcdn.com/image/fetch/$s_!HSys!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png 1272w, https://substackcdn.com/image/fetch/$s_!HSys!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe08ffee0-1b36-46b7-847e-7df7587eebfe_874x354.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This removes the <code>write()</code> system call from the hot path entirely. The Replay mechanism then simply maps the same file in read-only mode and feeds it to the parser.</p><pre><code>import mmap
import struct

class BinaryEventLog:
    def append(self, *fields):
        # Pack data directly into the memory-mapped buffer
        # Zero system calls. Zero Python allocations.
        # This is as fast as a memcpy.
        self._pack(self.mm, self.pos, *fields)
        self.pos += self.rec_size</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!demJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93601505-8e18-4797-a301-e51af89df061_852x443.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!demJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93601505-8e18-4797-a301-e51af89df061_852x443.png 424w, https://substackcdn.com/image/fetch/$s_!demJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93601505-8e18-4797-a301-e51af89df061_852x443.png 848w, https://substackcdn.com/image/fetch/$s_!demJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93601505-8e18-4797-a301-e51af89df061_852x443.png 1272w, https://substackcdn.com/image/fetch/$s_!demJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93601505-8e18-4797-a301-e51af89df061_852x443.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!demJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93601505-8e18-4797-a301-e51af89df061_852x443.png" width="852" height="443" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/93601505-8e18-4797-a301-e51af89df061_852x443.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:443,&quot;width&quot;:852,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!demJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93601505-8e18-4797-a301-e51af89df061_852x443.png 424w, https://substackcdn.com/image/fetch/$s_!demJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93601505-8e18-4797-a301-e51af89df061_852x443.png 848w, https://substackcdn.com/image/fetch/$s_!demJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93601505-8e18-4797-a301-e51af89df061_852x443.png 1272w, https://substackcdn.com/image/fetch/$s_!demJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93601505-8e18-4797-a301-e51af89df061_852x443.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This guarantees that <em>what you see in test</em>s is <em>what happened in production</em>, provided the parser is deterministic. </p><p><strong>Point 6 &#8212; Fault containment, controlled degradation, and restart integrity</strong></p><p>Downtime is a <em>short volatility position</em>. When the system is down, you can&#8217;t hedge, you can&#8217;t cancel, and you can&#8217;t close positions. You are exposed to the full variance of the market without any control mechanism.</p><p>Therefore, the system must support <em>controlled degradation</em>. We can&#8217;t assume binary operation (working vs. broken). We need intermediate states to handle load without failure:</p><ul><li><p><strong>State A (normal):</strong> Full universe, full feature calculation, all order types active.</p></li><li><p><strong>State B (high load):</strong> Drop Level-2/3 updates (depth), trade only on top-of-book updates. This sheds computational load while maintaining the ability to exit positions.</p></li><li><p><strong>State C (critical/cancel-only):</strong> The strategy stops emitting new risk, but the gateway remains active to process fill confirmations and send cancel requests.</p></li></ul><p>Furthermore, restarts must be safe. A restart clears the volatile memory. If the system restarts and assumes a flat position when it is actually long 500 contracts, it will over-hedge (selling 500 more) or double-down (buying 500 more).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!laHh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!laHh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png 424w, https://substackcdn.com/image/fetch/$s_!laHh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png 848w, https://substackcdn.com/image/fetch/$s_!laHh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png 1272w, https://substackcdn.com/image/fetch/$s_!laHh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!laHh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png" width="858" height="463" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:463,&quot;width&quot;:858,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!laHh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png 424w, https://substackcdn.com/image/fetch/$s_!laHh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png 848w, https://substackcdn.com/image/fetch/$s_!laHh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png 1272w, https://substackcdn.com/image/fetch/$s_!laHh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F905dc40e-a52a-4a38-ac2c-e25703ca4003_858x463.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In the solution the memory-mapped state file (from Point 3) acts as the persistence layer. On restart, the system:</p><ol><li><p>Maps the snapshot file.</p></li><li><p>Validates the Seqlock and Checksum to ensure the snapshot wasn&#8217;t written mid-crash.</p></li><li><p><em>Hot-loads</em> the position and risk limits <em>before</em> opening the network socket.</p></li></ol><p>This ensures <em>restart integrity</em>: The machine wakes up with the same brain it went to sleep with. It knows its inventory, its open orders, and its PnL state immediately, preventing the dangerous &#8220;amnesia&#8221; that leads to duplicate orders and risk breaches.</p><p><strong>Point 7 &#8212; Latency distribution</strong></p><p>Finally, we close the loop by linking engineering to finance.</p><p>Engineers often treat latency as a minimize <em>X</em> problem. Quants treat PnL as a maximize <em>Y</em> problem.</p><p>In reality, realized PnL (&#928;) is a function of latency (<em>L</em>).</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\Pi_{\\text{realized}} = \\Pi_{\\text{theoretical}} - \\text{Cost}(L)&quot;,&quot;id&quot;:&quot;CQFFZQGFDI&quot;}" data-component-name="LatexBlockToDOM"></div><p>The cost function Cost(<em>L</em>) is <em>convex</em>.</p><ul><li><p><strong>1ms latency</strong> might cost 0.1 bps (slippage). The market hasn&#8217;t moved much.</p></li><li><p><strong>10ms latency</strong> might cost 5.0 bps (missed move + adverse selection). By the time you arrive, the liquidity has vanished, or worse, you execute against a price that is already stale, immediately showing a mark-to-market loss.</p></li></ul><p>Because of this convexity (Jensen&#8217;s Inequality), the <em>tail</em> of the latency distribution dominates the expected cost.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;E[\\text{Cost}(L)] \\ge \\text{Cost}(E[L]) &quot;,&quot;id&quot;:&quot;MJERDYTLUQ&quot;}" data-component-name="LatexBlockToDOM"></div><p>When latency pushes you into the tail, you are not competing with other honest market participants. You are trading against the toxic flow&#8212;arbitrageurs who have already picked off the stale quotes. You are eating the waste of the liquidity pool.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!OoVH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OoVH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png 424w, https://substackcdn.com/image/fetch/$s_!OoVH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png 848w, https://substackcdn.com/image/fetch/$s_!OoVH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png 1272w, https://substackcdn.com/image/fetch/$s_!OoVH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!OoVH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png" width="829" height="467" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:467,&quot;width&quot;:829,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!OoVH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png 424w, https://substackcdn.com/image/fetch/$s_!OoVH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png 848w, https://substackcdn.com/image/fetch/$s_!OoVH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png 1272w, https://substackcdn.com/image/fetch/$s_!OoVH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe21f9ab7-d484-4aa0-a71f-e52263c0e5c9_829x467.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Visualizing this reveals that a system with a low median latency but a fat tail will be significantly less profitable than a slower but more deterministic system.</p><p>The <em>path</em> through queue theory, lock-free synchronization, and memory layout leads us to a single, unifying resolution: <em><strong>The system is the strategy.</strong></em></p><p>You design the trading strategy and its implementation as a single artifact from the start, because the system&#8217;s latency profile defines the strategy that actually runs in the market. A mean-reversion strategy with a 100ms tail is, in practice, a momentum strategy entered too late.</p><p>This brings us to the conclusion: you can&#8217;t design a trading strategy in Python and then hand it over to developers to make it fast. So we close the loop with this shift in mindset. That said, there are data structures that don&#8217;t require ultra-low latency and are still effective. We&#8217;ll revisit this in more detail in the future to understand when to use them.</p><p>For now, we&#8217;re going to test the ones we built in Python and see how they perform.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!O1Q7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!O1Q7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png 424w, https://substackcdn.com/image/fetch/$s_!O1Q7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png 848w, https://substackcdn.com/image/fetch/$s_!O1Q7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png 1272w, https://substackcdn.com/image/fetch/$s_!O1Q7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!O1Q7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png" width="915" height="479" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:479,&quot;width&quot;:915,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:97347,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/185731674?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!O1Q7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png 424w, https://substackcdn.com/image/fetch/$s_!O1Q7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png 848w, https://substackcdn.com/image/fetch/$s_!O1Q7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png 1272w, https://substackcdn.com/image/fetch/$s_!O1Q7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0acbef14-f6af-469b-a3d6-1df12f250bb2_915x479.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KKRX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 424w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 848w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1272w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png" width="1456" height="271" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:271,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:381253,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!KKRX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 424w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 848w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1272w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Okay! Not bad! Alright crew&#8212;clean work today. Until the next bell: keep your entries disciplined, your risk capped, and your routine louder than your feelings. May spreads stay tight, drawdowns stay shallow, and your best trades compound while nobody&#8217;s watching. &#9881;&#65039;</p><p><em>PS:<strong> What do you consider more important, a quantitative research process or having a builder that generates thousand of strategies?</strong></em></p><div class="poll-embed" data-attrs="{&quot;id&quot;:439969}" data-component-name="PollToDOM"></div><div><hr></div><p>This is an<strong> invitation-only access</strong> to our <strong>QUANT COMMUNITY</strong>, so we verify numbers to avoid spammers and scammers. Feel free to join or decline at any time. <em>Tap the WhatsApp icon below to join</em></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://chat.whatsapp.com/LsXSHJxfp9l7tOd7OO6PQL" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nbxE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 424w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 848w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1272w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png" width="349" height="95.975" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:352,&quot;width&quot;:1280,&quot;resizeWidth&quot;:349,&quot;bytes&quot;:87001,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://chat.whatsapp.com/LsXSHJxfp9l7tOd7OO6PQL&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/181419543?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!nbxE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 424w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 848w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1272w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><div><hr></div><p><strong>Appendix</strong></p><p>Full code:</p><pre><code>from __future__ import annotations
import os
import time
import hashlib
import numpy as np

# Point 1 &#8212; Capacity-constrained processing 

class BoundedEventQueue:
    """
    Preallocated circular queue for fixed-schema events.
    Overflow is explicit. No GC. No resizing.
    Uses Structure of Arrays (SoA) to maximize cache locality.
      - attributes: cap, mask, write, read, ts, typ, sym, px, qty, overflow_count
      - constants: DROP_NEWEST, DROP_OLDEST
      - method: push(...) exists (but batch-first is push_batch)
    """
    __slots__ = ("cap", "mask", "write", "read",
                 "ts", "typ", "sym", "px", "qty",
                 "overflow_count")

    DROP_NEWEST = 0
    DROP_OLDEST = 1

    def __init__(self, cap_pow2: int):
        if cap_pow2 &amp; (cap_pow2 - 1) != 0:
            raise ValueError("Capacity must be a power of 2.")
        self.cap = int(cap_pow2)
        self.mask = self.cap - 1

        # Use uint64 counters to avoid wrap in long runs.
        self.write = np.uint64(0)
        self.read = np.uint64(0)

        # Fixed schema (SoA)
        self.ts  = np.empty(self.cap, dtype=np.uint64)
        self.typ = np.empty(self.cap, dtype=np.uint8)
        self.sym = np.empty(self.cap, dtype=np.uint16)
        self.px  = np.empty(self.cap, dtype=np.int32)
        self.qty = np.empty(self.cap, dtype=np.int32)

        self.overflow_count = np.uint64(0)

    def size(self) -&gt; np.uint64:
        return self.write - self.read

    @staticmethod
    def _ring_write(dst: np.ndarray, start_write: np.uint64, mask: int, src: np.ndarray) -&gt; None:
        """
        Vectorized ring write: at most 2 slice assignments (wrap handling).
        """
        n = src.shape[0]
        if n == 0:
            return
        cap = dst.shape[0]
        i0 = int(start_write &amp; np.uint64(mask))
        first = min(n, cap - i0)
        dst[i0:i0+first] = src[:first]
        second = n - first
        if second:
            dst[:second] = src[first:first+second]

    def push_batch(
        self,
        ts: np.ndarray,
        typ: np.ndarray,
        sym: np.ndarray,
        px: np.ndarray,
        qty: np.ndarray,
        overflow_policy: int = DROP_OLDEST) -&gt; np.uint64:
        """
        Batch push

        Returns:
            accepted (uint64)
        """
        ts = np.asarray(ts, dtype=np.uint64)
        typ = np.asarray(typ, dtype=np.uint8)
        sym = np.asarray(sym, dtype=np.uint16)
        px = np.asarray(px, dtype=np.int32)
        qty = np.asarray(qty, dtype=np.int32)

        n = int(ts.shape[0])
        if not (typ.shape[0] == n and sym.shape[0] == n and px.shape[0] == n and qty.shape[0] == n):
            raise ValueError("All fields must have same length.")
        if n == 0:
            return np.uint64(0)

        cap = self.cap
        r0 = self.read
        w0 = self.write
        size0 = w0 - r0

        if overflow_policy == self.DROP_NEWEST:
            free0 = np.uint64(cap) - size0 if size0 &lt; cap else np.uint64(0)
            accept = int(min(np.uint64(n), free0))
            dropped = n - accept
            if dropped:
                self.overflow_count += np.uint64(dropped)
            if accept == 0:
                return np.uint64(0)

            start = w0
            self._ring_write(self.ts,  start, self.mask, ts[:accept])
            self._ring_write(self.typ, start, self.mask, typ[:accept])
            self._ring_write(self.sym, start, self.mask, sym[:accept])
            self._ring_write(self.px,  start, self.mask, px[:accept])
            self._ring_write(self.qty, start, self.mask, qty[:accept])

            self.write = w0 + np.uint64(accept)
            return np.uint64(accept)

        # DROP_OLDEST: accept all, but advance read if overflow
        overflow = int(max(0, int(size0) + n - cap))
        if overflow:
            self.overflow_count += np.uint64(overflow)
            self.read = r0 + np.uint64(overflow)

        # If batch &gt;= cap, only last cap events matter (freshness)
        if n &gt;= cap:
            tail = cap
            start = w0 + np.uint64(n - cap)
            self._ring_write(self.ts,  start, self.mask, ts[-tail:])
            self._ring_write(self.typ, start, self.mask, typ[-tail:])
            self._ring_write(self.sym, start, self.mask, sym[-tail:])
            self._ring_write(self.px,  start, self.mask, px[-tail:])
            self._ring_write(self.qty, start, self.mask, qty[-tail:])
        else:
            start = w0
            self._ring_write(self.ts,  start, self.mask, ts)
            self._ring_write(self.typ, start, self.mask, typ)
            self._ring_write(self.sym, start, self.mask, sym)
            self._ring_write(self.px,  start, self.mask, px)
            self._ring_write(self.qty, start, self.mask, qty)

        self.write = w0 + np.uint64(n)
        return np.uint64(n)

    def push(self, ts, typ, sym, px, qty, overflow_policy=DROP_OLDEST) -&gt; bool:
        """
        Wraps scalars into length-1 arrays.
        """
        accepted = self.push_batch(
            np.asarray([ts], dtype=np.uint64),
            np.asarray([typ], dtype=np.uint8),
            np.asarray([sym], dtype=np.uint16),
            np.asarray([px], dtype=np.int32),
            np.asarray([qty], dtype=np.int32),
            overflow_policy=overflow_policy
        )
        return bool(int(accepted) == 1)

    def peek_segments(self, max_n: int):
        """
        Zero-copy read: returns up to 2 contiguous segments as views.
        Returns: n_total, seg1, seg2
        where seg1/seg2 are dicts of views (ts/typ/sym/px/qty).
        """
        max_n = int(max_n)
        if max_n &lt;= 0:
            return 0, {}, {}
        size = int(self.size())
        n = min(max_n, size)
        if n == 0:
            return 0, {}, {}

        cap = self.cap
        i0 = int(self.read &amp; np.uint64(self.mask))
        first = min(n, cap - i0)
        second = n - first

        seg1 = {
            "ts":  self.ts[i0:i0+first],
            "typ": self.typ[i0:i0+first],
            "sym": self.sym[i0:i0+first],
            "px":  self.px[i0:i0+first],
            "qty": self.qty[i0:i0+first],
        }
        seg2 = {}
        if second:
            seg2 = {
                "ts":  self.ts[:second],
                "typ": self.typ[:second],
                "sym": self.sym[:second],
                "px":  self.px[:second],
                "qty": self.qty[:second],
            }
        return n, seg1, seg2

    def commit_read(self, n: int) -&gt; None:
        n = int(n)
        if n &gt; 0:
            self.read = self.read + np.uint64(n)

# Point 2 &#8212; Tail telemetry 

class P2Quantile:
    """
    P2Quantile
    - The classic P&#178; marker update is sequential per-sample.
    - For nano-per-element measurement, we use a *fixed histogram* tail estimator,
      updated in batch with np.searchsorted + np.bincount (C-level loops).

    Interface:
      - update(x): accepts scalar OR 1D array (batch preferred)
      - value(): returns approx quantile (left-edge of bin crossing p)
    """
    __slots__ = ("p", "edges", "counts", "total")

    def __init__(self, p: float, edges: np.ndarray | None = None):
        if not (0.0 &lt; p &lt; 1.0):
            raise ValueError("p must be in (0,1)")
        self.p = float(p)

        if edges is None:
            edges = np.unique(np.concatenate([
                np.array([0.0, 0.02, 0.05, 0.1, 0.2, 0.5, 1.0], dtype=np.float64),
                np.geomspace(1.0, 10_000.0, 220).astype(np.float64),  # up to 10ms in us
            ]))
        edges = np.asarray(edges, dtype=np.float64)
        if edges.ndim != 1 or edges.size &lt; 3:
            raise ValueError("edges must be 1D with size &gt;= 3")
        if not np.all(edges[1:] &gt; edges[:-1]):
            raise ValueError("edges must be strictly increasing")

        self.edges = edges
        self.counts = np.zeros(edges.size - 1, dtype=np.uint64)
        self.total = np.uint64(0)

    def update(self, x):
        """
        Vectorized update. If x is scalar -&gt; wrapped; if array -&gt; batch update.
        """
        x = np.asarray(x, dtype=np.float64)
        if x.size == 0:
            return

        idx = np.searchsorted(self.edges, x, side="right") - 1
        idx = np.clip(idx, 0, self.counts.size - 1).astype(np.int64, copy=False)
        bc = np.bincount(idx, minlength=self.counts.size).astype(np.uint64, copy=False)
        self.counts += bc
        self.total += np.uint64(x.size)

    def value(self) -&gt; float:
        """
        Approx quantile using histogram CDF. Returns left edge of bin crossing p.
        """
        if self.total == 0:
            return float("nan")
        target = int(np.ceil(self.p * int(self.total)))
        cdf = np.cumsum(self.counts, dtype=np.uint64)
        j = int(np.searchsorted(cdf, target, side="left"))
        return float(self.edges[min(j, self.edges.size - 2)])


# Point 3 &#8212; Consistency domains without global locks

class SharedSnapshot:
    """
    SharedSnapshot.

    Seqlock-like semantics, but implemented with *double buffering* for NumPy-only:
      - Writer writes to inactive buffer, then flips seq to publish.
      - Reader reads stable seq, copies from active buffer, verifies seq unchanged.

    This eliminates torn reads in practice for coarse/batched writes.

    Methods:
      - write(values)
      - read(out) -&gt; bool
    """
    __slots__ = ("seq", "arr")

    def __init__(self, N: int, dtype=np.float64):
        N = int(N)
        # Two buffers: arr[0] and arr[1]
        self.arr = np.empty((2, N), dtype=dtype)
        self.seq = np.uint64(0)  # even = stable

    def write(self, values: np.ndarray) -&gt; None:
        v = self.seq
        self.seq = v + np.uint64(1)  # odd = writing

        # inactive buffer selection (based on previous publish epoch)
        # epoch = v//2; active = epoch%2; inactive = 1-active
        epoch = v &gt;&gt; np.uint64(1)
        active = int(epoch &amp; np.uint64(1))
        inactive = 1 - active

        np.copyto(self.arr[inactive], values)     # vectorized memcpy
        self.seq = v + np.uint64(2)               # even = published

    def read(self, out: np.ndarray) -&gt; bool:
        s1 = self.seq
        if (s1 &amp; np.uint64(1)) != 0:
            return False

        epoch = (s1 &gt;&gt; np.uint64(1))
        active = int(epoch &amp; np.uint64(1))
        np.copyto(out, self.arr[active])          # vectorized memcpy

        s2 = self.seq
        return bool(s1 == s2)

# Point 4 &#8212; Schema and layout hashing

def dtype_layout_hash(dt: np.dtype) -&gt; str:
    """
    Computes a hash of the physical layout of a numpy dtype.
    Includes byte offsets, field order, and alignment details.
    """
    dt = np.dtype(dt)
    parts = [f"itemsize={dt.itemsize}", f"aligned={dt.isalignedstruct}"]
    for name in dt.names or ():
        f, off = dt.fields[name][0], dt.fields[name][1]
        parts.append(f"{name}:{f.str}@{off}")
    payload = "|".join(parts).encode("utf-8")
    return hashlib.sha256(payload).hexdigest()[:16]


def _event_dtype(align: bool = True) -&gt; np.dtype:
    return np.dtype(
        [("ts", np.uint64), ("typ", np.uint8), ("sym", np.uint16), ("px", np.int32), ("qty", np.int32)],
        align=align)

# Point 5 &#8212; Deterministic replay 

class BinaryEventLog:
    """
    BinaryEventLog.
      - uses np.memmap with fixed dtype (no per-record syscalls)
      - append batches via slice assignment
      - replay is a zero-copy view over [0:pos]
    """
    __slots__ = ("path", "dt", "mm", "pos")

    def __init__(self, path: str, max_records: int, dt: np.dtype):
        self.path = path
        self.dt = np.dtype(dt)
        # preallocate
        self.mm = np.memmap(self.path, mode="w+", dtype=self.dt, shape=(int(max_records),))
        self.pos = np.uint64(0)

    def append(self, ts, typ, sym, px, qty) -&gt; bool:
        """
        Still loop-free in the kernel path (writes a 1-length slice).
        """
        p = int(self.pos)
        if p + 1 &gt; self.mm.shape[0]:
            return False
        self.mm[p]["ts"] = np.uint64(ts)
        self.mm[p]["typ"] = np.uint8(typ)
        self.mm[p]["sym"] = np.uint16(sym)
        self.mm[p]["px"] = np.int32(px)
        self.mm[p]["qty"] = np.int32(qty)
        self.pos = np.uint64(p + 1)
        return True

    def append_batch(self, events: np.ndarray) -&gt; np.uint64:
        events = np.asarray(events, dtype=self.dt)
        n = int(events.shape[0])
        if n == 0:
            return np.uint64(0)
        p = int(self.pos)
        end = p + n
        if end &gt; self.mm.shape[0]:
            return np.uint64(0)
        self.mm[p:end] = events
        self.pos = np.uint64(end)
        return np.uint64(n)

    def replay_view(self) -&gt; np.ndarray:
        return self.mm[:int(self.pos)]

    def flush(self) -&gt; None:
        self.mm.flush()


# Metrics (batched ns/item)

def _ns_per_item(elapsed_ns: int, items: int) -&gt; float:
    return float(elapsed_ns) / float(items) if items &gt; 0 else float("nan")


def _fmt_ns_item(x: float) -&gt; str:
    return "nan" if not np.isfinite(x) else f"{x:,.1f} ns/item"


def _fmt_mps(elapsed_ns: int, items: int) -&gt; str:
    if items &lt;= 0 or elapsed_ns &lt;= 0:
        return "nan"
    per_s = items / (elapsed_ns * 1e-9)
    return f"{per_s/1e6:,.2f} M items/s"


def benchmark_metrics(
    cap_pow2: int = 1 &lt;&lt; 14,
    batch_n: int = 1 &lt;&lt; 16,
    runs: int = 50,
    warmup: int = 10,
    log_max_records: int = 2_000_000,
    snapshot_n: int = 4096,
    seed: int = 7,
):
    """
    Measures ns/item for:
      - BoundedEventQueue.push_batch (batch_n events)
      - BoundedEventQueue.peek_segments (views up to cap_pow2 events)
      - P2Quantile.update (histogram update on up to cap_pow2 latencies)
      - SharedSnapshot.write (copy snapshot_n float64)
      - BinaryEventLog.append_batch (pack+append up to cap_pow2 events)

    Note:
    - The benchmark loop itself is Python (unavoidable to get distributions).
    - The measured kernels are vectorized NumPy ops.
    """
    rng = np.random.default_rng(seed)
    dt = _event_dtype(align=True)

    q = BoundedEventQueue(cap_pow2)
    telemetry = P2Quantile(0.999)
    snap = SharedSnapshot(snapshot_n, dtype=np.float64)

    log_path = "binary_event_log_bench.mmp"
    try:
        os.remove(log_path)
    except FileNotFoundError:
        pass
    blog = BinaryEventLog(log_path, log_max_records, dt)

    # Pre-generate batch inputs (exclude RNG/allocation from measured time)
    ts0 = np.uint64(1_000_000_000_000_000_000)
    ts = ts0 + np.arange(batch_n, dtype=np.uint64)
    typ = np.full(batch_n, 1, dtype=np.uint8)
    sym = np.full(batch_n, 42, dtype=np.uint16)
    px = (100_000 + np.cumsum(rng.integers(-1, 2, size=batch_n, dtype=np.int32))).astype(np.int32, copy=False)
    qty = np.full(batch_n, 1, dtype=np.int32)

    # Prealloc for logging up to cap_pow2 events per iteration
    ev_buf = np.empty(cap_pow2, dtype=dt)

    # Snapshot payload
    snap_vals = np.tanh(np.linspace(-3, 3, snapshot_n, dtype=np.float64))

    t_push = np.empty(runs, dtype=np.int64)
    t_peek = np.empty(runs, dtype=np.int64)
    t_tel  = np.empty(runs, dtype=np.int64)
    t_snap = np.empty(runs, dtype=np.int64)
    t_log  = np.empty(runs, dtype=np.int64)

    # Warmup (not recorded)
    for _ in range(warmup):
        q.push_batch(ts, typ, sym, px, qty, overflow_policy=BoundedEventQueue.DROP_OLDEST)
        m, seg1, seg2 = q.peek_segments(cap_pow2)
        if m:
            now = ts0 + np.uint64(batch_n + 10_000)
            lat1_us = (now - seg1["ts"]).astype(np.float64, copy=False) * 1e-3
            telemetry.update(lat1_us)
            if seg2:
                lat2_us = (now - seg2["ts"]).astype(np.float64, copy=False) * 1e-3
                telemetry.update(lat2_us)

            q.commit_read(m)

        snap.write(snap_vals)

    # Measured runs
    for i in range(runs):
        # 1) push_batch
        t0 = time.perf_counter_ns()
        q.push_batch(ts, typ, sym, px, qty, overflow_policy=BoundedEventQueue.DROP_OLDEST)
        t1 = time.perf_counter_ns()
        t_push[i] = t1 - t0

        # 2) peek_segments
        t0 = time.perf_counter_ns()
        m, seg1, seg2 = q.peek_segments(cap_pow2)
        t1 = time.perf_counter_ns()
        t_peek[i] = t1 - t0

        if m:
            now = ts0 + np.uint64(batch_n + 10_000)

            # 3) telemetry update (hist quantile)
            t0 = time.perf_counter_ns()
            lat1_us = (now - seg1["ts"]).astype(np.float64, copy=False) * 1e-3
            telemetry.update(lat1_us)
            if seg2:
                lat2_us = (now - seg2["ts"]).astype(np.float64, copy=False) * 1e-3
                telemetry.update(lat2_us)
            t1 = time.perf_counter_ns()
            t_tel[i] = t1 - t0

            # 4) BinaryEventLog.append_batch (pack+append)
            k1 = seg1["px"].shape[0]
            ev = ev_buf[:m]
            t0 = time.perf_counter_ns()
            ev["ts"][:k1]  = seg1["ts"]
            ev["typ"][:k1] = seg1["typ"]
            ev["sym"][:k1] = seg1["sym"]
            ev["px"][:k1]  = seg1["px"]
            ev["qty"][:k1] = seg1["qty"]
            if seg2:
                k2 = seg2["px"].shape[0]
                ev["ts"][k1:k1+k2]  = seg2["ts"]
                ev["typ"][k1:k1+k2] = seg2["typ"]
                ev["sym"][k1:k1+k2] = seg2["sym"]
                ev["px"][k1:k1+k2]  = seg2["px"]
                ev["qty"][k1:k1+k2] = seg2["qty"]
            blog.append_batch(ev)
            t1 = time.perf_counter_ns()
            t_log[i] = t1 - t0

            q.commit_read(m)
        else:
            t_tel[i] = 0
            t_log[i] = 0

        # 5) SharedSnapshot.write
        t0 = time.perf_counter_ns()
        snap.write(snap_vals)
        t1 = time.perf_counter_ns()
        t_snap[i] = t1 - t0

    def summarize(name: str, times: np.ndarray, items: int):
        qx = np.quantile(times.astype(np.float64), [0.0, 0.5, 0.99])
        mn, md, p99 = (int(qx[0]), int(qx[1]), int(qx[2]))
        print(f"\n{name}")
        print(f"  min: {mn:,} ns | {_fmt_ns_item(_ns_per_item(mn, items))} | {_fmt_mps(mn, items)}")
        print(f"  med: {md:,} ns | {_fmt_ns_item(_ns_per_item(md, items))} | {_fmt_mps(md, items)}")
        print(f"  p99: {p99:,} ns | {_fmt_ns_item(_ns_per_item(p99, items))} | {_fmt_mps(p99, items)}")

    print("LIFECYCLE STRUCTURES (ARTICLE NAMES) &#8212; METRICS (BATCHED")
    print(f"dtype hash: {dtype_layout_hash(dt)}")
    print(f"cap_pow2={cap_pow2:,} | batch_n={batch_n:,} | runs={runs} (warmup={warmup})")
    print("Interpretation: ns/item is meaningful because work is vectorized over big contiguous batches.")

    summarize("Point 1 &#8212; BoundedEventQueue.push_batch (writes batch_n events)", t_push, batch_n)
    summarize("Point 1 &#8212; BoundedEventQueue.peek_segments (views up to cap_pow2)", t_peek, cap_pow2)
    summarize("Point 2 &#8212; P2Quantile.update (hist tail update up to cap_pow2)", t_tel, cap_pow2)
    summarize("Point 3 &#8212; SharedSnapshot.write (copy snapshot_n float64)", t_snap, snapshot_n)
    summarize("Point 5 &#8212; BinaryEventLog.append_batch (pack+append up to cap_pow2)", t_log, cap_pow2)

    print(f"Point 1 &#8212; overflow_count: {int(q.overflow_count):,}")
    print(f"\nPoint 2 &#8212; Current Q0.999 (approx, histogram left-edge): {telemetry.value():.6f} (units = input units)")
    print(f"Point 5 &#8212; logged records: {int(blog.pos):,}")

    # Not part of hot path; flush once at the end
    blog.flush()

    return {
        "dtype_hash": dtype_layout_hash(dt),
        "Q0_999": float(telemetry.value()),
        "overflow_count": int(q.overflow_count),
        "logged_records": int(blog.pos),
    }

if __name__ == "__main__":
    benchmark_metrics()</code></pre>]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Data: Building micro-trading machines  ]]></title><description><![CDATA[Microarchitectural design]]></description><link>https://www.quantbeckman.com/p/with-code-data-building-micro-trading</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-data-building-micro-trading</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Tue, 13 Jan 2026 13:00:21 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>The conflict between research and production.</p></li><li><p>Micro-machines architecture.</p><ol><li><p>Design point 1: Cache-line.</p></li><li><p>Design point 2: Coherence engineering.</p></li><li><p>Design point 3: Topology-aware state.</p></li><li><p>Design point 4: Page-level engineering.</p></li><li><p>Design point 5: Control-flow predictability.</p></li><li><p>Design point 6: Wire-format and parsing structures.</p></li><li><p>Design point 7: Time and numeric representation.</p></li></ol></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read the newsletter index</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Introduction</h4><p>Most performance debates in trading start at the wrong layer. Quants argue about languages, compilers, and fast code, as if latency were a property of instructions. In production, latency is a property of structure: <em>how state is laid out, how it is shared, how often the machine is forced to translate addresses, and how predictable the control flow remains</em>. The micro-pauses that ruin fills are concrete events.</p><p>The real conflict is that research optimizes for average throughput, while production lives under the question: <em>did the system took the decision before the market boundary moved?</em> </p><p>That boundary compresses exactly when volatility rises&#8212;precisely when your edge is largest and the penalty for being late is most asymmetric. This is why mean latency is a comforting metric and a dangerous one: the tail determines whether cancels land, hedges actually hedge, and risk gates engage on time.</p><p>Most systems fail here because their data structures are designed for convenience, not for invariants. Pointer-heavy graphs, heterogeneous containers, dynamic dispatch, and ad-hoc parsing become regime-dependent under load. The result is a bimodal latency distribution: stable in calm sessions, unstable when message rates spike.</p><p>The solution is to design micro-machines architectures: treat the CPU as a collection of finite budgets rather than a generic processor. There is a cache-line budget (useful bytes per fetch), a coherence budget (how often lines can bounce between cores), a translation budget (distinct pages touched per unit time), and a prediction budget (branch stability). When you exceed these budgets, the penalties arrive as discontinuities that dominate p99 behavior, not as smooth slowdowns that averages can summarize.</p><p>If you want to go deeper in this topic, check this PDF:</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!n9ZU!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf75d01c-d9df-41b8-8dd3-8cd1d9462693_538x722.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">A Gittins Policy For Optimizing Tail Latency</div><div class="file-embed-details-h2">1.19MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/021979cd-58bb-4440-8343-2d074e30c26a.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/021979cd-58bb-4440-8343-2d074e30c26a.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>The design points that follow are for stability constraints: engineer cache-line layouts to keep hot fields hot, engineer coherence to prevent false sharing and line bouncing, make state topology-aware to avoid NUMA randomness, engineer at the page level to keep translation predictable, stabilize control flow with table-driven state transitions, parse with rigid wire formats that don&#8217;t allocate or branch unpredictably, and use deterministic time and numeric representations to eliminate threshold drift. The target is a tighter distribution so the engine you deploy is the engine you validated.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wCrS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wCrS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png 424w, https://substackcdn.com/image/fetch/$s_!wCrS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png 848w, https://substackcdn.com/image/fetch/$s_!wCrS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png 1272w, https://substackcdn.com/image/fetch/$s_!wCrS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!wCrS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png" width="1012" height="508" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:508,&quot;width&quot;:1012,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:533681,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/184204878?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wCrS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png 424w, https://substackcdn.com/image/fetch/$s_!wCrS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png 848w, https://substackcdn.com/image/fetch/$s_!wCrS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png 1272w, https://substackcdn.com/image/fetch/$s_!wCrS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91529162-f0f3-4699-84d9-0220e3bfe834_1012x508.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>The conflict between research and production</h4><p>In the research phase, a pipeline is evaluated primarily by mathematical correctness and average throughput. We treat the compute substrate as deterministic. But in live production, the objective function collapses to a binary state: did you express the decision before the market boundary shifted?</p><p>This boundary is not static. It is a dynamic variable that compresses aggressively during volatility bursts&#8212;precisely the regime where your edge is highest. The dilemma is effectively a race against a shrinking liquidity window.</p><p>Most traders accept this premise theoretically, yet define their systems with <em>convenient </em>structures&#8212;pointer-heavy graphs, dynamic dispatch, and heterogeneous containers&#8212;that practically guarantee failure. The result is a system with a bimodal latency distribution. </p><p>In the Asian session (quiet regimes), the workflow fits within the window. However, during the London session or when this session overlaps with the American session, state consistency degrades, branch predictors go astray, the CPU gets stuck on page traversals, and cache misses occur. Sometimes, you even experience micro-pauses that disappear in the average statistics but manifest as failed data fills.</p><p>This is not due to a problem with the code or the interpreter, but rather a structural mismatch between the data design and the hardware you are using.</p><p>As you already know, a live trading model is a composition of a<em> signal and a timing apparatus</em>. If the timing apparatus behaves stochastically under load, the realized model differs from the validated one. This divergence manifests in four specific failure modes:</p><ol><li><p><strong>State incoherence under concurrency</strong> means risk flags or inventory states may be consistent within one thread but stale in another at the moment of decision. </p></li><li><p><strong>Service time variance</strong> explodes as the compute work per message becomes regime-dependent due to cache misses or coherence traffic. </p></li><li><p><strong>Latency amplification</strong> occurs where small increases in mean service time produce non-linear increases in p99 latency as utilization approaches saturation. </p></li><li><p><strong>Numerical representation drift</strong> in float-heavy state machines creates rounding artifacts that lead to inconsistent thresholds around cancel/replace boundaries. </p></li></ol><p>These issues are not resolved by faster code but by selecting data layouts that behave predictably on specific hardware.</p><p>The failure that necessitates this architectural shift is rarely a crazy outage but a repeating small incident. For example, a hedge engine receives a burst and computes a cancel/replace based on a risk condition that was true when reading one field but false when reading another. The system sends the wrong intent, and the market punishes the strategy via adverse selection. While post-mortems often debate signals or slippage models, the conflict is that the internal data structure did not provide atomicity at the semantic level required by the strategy. The alpha survived, the code executed, but the structure of state rendered the decision non-deterministic.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><h4>Micro-machines architecture</h4><p>The next step in latency engineering is accepting that the microarchitecture often behaves adversarially because data layouts provide it no alternative. A senior quant must view the CPU as a collection of finite budgets rather than a generic processor. These include the <em>cache line budget</em> (managed at 64-byte granularity), the <em>coherence budget </em>(the frequency of forced invalidations), the<em> translation budget</em> (distinct pages touched per unit time), and the <em>prediction budget</em> (stability of branches) . These budgets are concrete; when exceeded, they manifest as measurable stalls.</p><p>Addressing these budgets involves overcoming practical obstacles: </p><ol><li><p><strong>Averages deceive</strong>: a 10% improvement in mean compute time is irrelevant if the tail is driven by coherence or translation events. </p></li><li><p><strong>Concurrency changes everything</strong>: a single-core benchmark that performs well can become unstable when the pipeline is split across cores. </p></li><li><p><strong>State is not scalar</strong>: it is a set of fields that must be read and written with a semantic consistency boundary. </p></li></ol><p>Finally, the dynamic nature of Python, while convenient, poses a significant danger in the hot path. The challenge is to build structures that enforce predictability even when the flow rate changes abruptly.</p><p><strong>Design point 1: Cache-line</strong></p><p>A common misconception is that contiguous memory guarantees speed. While contiguity is necessary, it is insufficient. If fields straddle cache lines or structures force split loads, the system can remain contiguous yet suffer unpredictable behavior. In trading, this risk is acute because order state is accessed under stress. If a <em>hot</em> field, such as <code>best_bid_px</code>, shares a cache line with a <em>cold</em> field, like <code>client_tag</code>, accessing the cold data drags the line into the cache, evicting necessary data and creating churn that manifests as jitter.</p><p>Cache lines are fetched as a unit, typically 64 bytes. Two mechanisms degrade performance: <em>split accesses</em>, where a load crosses a line boundary requiring two fills, and <em>line pollution</em>, where cold fields waste cache capacity. We model the cost of reading a field <em>x</em> as:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;C(x) = C_{0} + I(x)C_{miss} + J(x)C_{split}&quot;,&quot;id&quot;:&quot;PPUIFPJZCS&quot;}" data-component-name="LatexBlockToDOM"></div><p>Here, <em>I</em>(<em>x</em>) indicates a cache miss and <em>J</em>(<em>x</em>) indicates a split access. The variance of <em>C</em>(<em>x</em>) is driven by the Bernoulli events <em>I</em>(<em>x</em>) and <em>J</em>(<em>x</em>) rather than the baseline cost <em>C</em><sub>0</sub>. The engineering objective is to drive <em>P</em>(<em>I</em>=1) and <em>P</em>(<em>J</em>=1) toward zero in the hot path.</p><p>One cannot force L1 residency from Python, but one can enforce alignment and separation. The first step is an alignment audit to ensure arrays begin at stable offsets relative to cache lines.</p><pre><code>import numpy as np
CACHELINE = 64

def addr_mod_cacheline(arr: np.ndarray) -&gt; int:
    return arr.ctypes.data % CACHELINE

# Packed vs Aligned Dtypes
packed = np.dtype([('px', 'f8'), ('qty', 'f4'), ('ts', 'u8'), ('tag', 'u8')], align=False)
aligned = np.dtype([('px', 'f8'), ('qty', 'f4'), ('ts', 'u8'), ('tag', 'u8')], align=True)

A = np.zeros(1024, dtype=packed)
B = np.zeros(1024, dtype=aligned)
print('packed addr%64:', addr_mod_cacheline(A))
print('aligned addr%64:', addr_mod_cacheline(B))</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!a-u5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!a-u5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png 424w, https://substackcdn.com/image/fetch/$s_!a-u5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png 848w, https://substackcdn.com/image/fetch/$s_!a-u5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png 1272w, https://substackcdn.com/image/fetch/$s_!a-u5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!a-u5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png" width="830" height="539" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7f88ed74-f806-4356-8af3-aa732898971d_830x539.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:539,&quot;width&quot;:830,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:30015,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/184204878?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!a-u5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png 424w, https://substackcdn.com/image/fetch/$s_!a-u5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png 848w, https://substackcdn.com/image/fetch/$s_!a-u5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png 1272w, https://substackcdn.com/image/fetch/$s_!a-u5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f88ed74-f806-4356-8af3-aa732898971d_830x539.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The structural solution involves separating hot and cold fields into distinct arrays keyed by the same index, ensuring that access patterns remain predictable and cache lines are populated only with relevant data.</p><pre><code>N = 1024 # random chosen for this example

# HOT: fields used on every message
hot_px = np.empty(N, dtype=np.float64)
hot_qty = np.empty(N, dtype=np.float32)
hot_ts = np.empty(N, dtype=np.uint64)

# COLD: metadata rarely used in hot path
cold_tag = np.empty(N, dtype=np.uint64)
cold_flags = np.empty(N, dtype=np.uint32)</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!R1zb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!R1zb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png 424w, https://substackcdn.com/image/fetch/$s_!R1zb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png 848w, https://substackcdn.com/image/fetch/$s_!R1zb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png 1272w, https://substackcdn.com/image/fetch/$s_!R1zb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!R1zb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png" width="1456" height="465" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e986acf6-1196-49b2-b037-ea401250a73b_2856x913.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:465,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:93141,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/184204878?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!R1zb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png 424w, https://substackcdn.com/image/fetch/$s_!R1zb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png 848w, https://substackcdn.com/image/fetch/$s_!R1zb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png 1272w, https://substackcdn.com/image/fetch/$s_!R1zb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe986acf6-1196-49b2-b037-ea401250a73b_2856x913.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>By enforcing alignment and segregating hot/cold fields, we minimize local stalls and maximize the density of useful data per fetch .</p><p><strong>Design point 2: Coherence engineering</strong></p><p>The belief that splitting work across cores inevitably lowers latency is dangerous. Multi-core performance is not additive when state forces coherence traffic. Worst-case coherence patterns appear in critical paths: per-symbol risk flags, shared order state, and global counters. If two cores write to different variables that share a cache line, <em>false sharing</em> occurs, causing the line to bounce between cores despite the data being logically independent.</p><p>Best protocols maintain a single writer for each line. If Core A writes to a line and Core B writes to a different word on the same line, the line invalidates repeatedly. This produces a latency term <em>T</em><sub>coh</sub>&#8776;<em>n</em><sub>bounces</sub> &#215; <em>t</em><sub>invalidate</sub>, where <em>n</em><sub>bounces</sub> scales with the event rate.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!cnEl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cnEl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png 424w, https://substackcdn.com/image/fetch/$s_!cnEl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png 848w, https://substackcdn.com/image/fetch/$s_!cnEl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png 1272w, https://substackcdn.com/image/fetch/$s_!cnEl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cnEl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png" width="950" height="343" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:343,&quot;width&quot;:950,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cnEl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png 424w, https://substackcdn.com/image/fetch/$s_!cnEl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png 848w, https://substackcdn.com/image/fetch/$s_!cnEl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png 1272w, https://substackcdn.com/image/fetch/$s_!cnEl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984498be-dc3b-4cc5-8c13-6beb71abe801_950x343.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We treat service time per message as <em>S</em>=<em>S</em><sub>0</sub>+<em>B</em>, where <em>B</em> is the coherence penalty. If bounces occur as a Poisson process with rate <em>&#957;</em> and cost <em>&#964;</em>, then:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\mathbb{E}[S] = S_{0} + \\tau\\nu&quot;,&quot;id&quot;:&quot;CIAFEBOXCC&quot;}" data-component-name="LatexBlockToDOM"></div><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;Var(S) = \\tau^{2}\\nu&quot;,&quot;id&quot;:&quot;PNTDZTKIWH&quot;}" data-component-name="LatexBlockToDOM"></div><p>This variance is critical because queueing tail behavior is highly sensitive to service time variance. As utilization &#961;&#8594;1, waiting time quantiles explode non-linearly.</p><p>To avoid false sharing without complex lock-free structures, we use padding. Each worker&#8217;s counter must occupy its own cache line.</p><pre><code>import numpy as np
CACHELINE = 64
PAD_WORDS = CACHELINE // 8
n_workers = 8

# One uint64 counter per worker, padded to one cache line each
counters = np.zeros(n_workers * PAD_WORDS, dtype=np.uint64)

def counter_view(i: int) -&gt; np.ndarray:
    return counters[i * PAD_WORDS : (i+1) * PAD_WORDS]

# Worker i increments without touching Worker j's cache line
counter_view(3)[0] += 1</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!b6ax!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!b6ax!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png 424w, https://substackcdn.com/image/fetch/$s_!b6ax!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png 848w, https://substackcdn.com/image/fetch/$s_!b6ax!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png 1272w, https://substackcdn.com/image/fetch/$s_!b6ax!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!b6ax!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png" width="830" height="557" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:557,&quot;width&quot;:830,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:61312,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/184204878?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!b6ax!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png 424w, https://substackcdn.com/image/fetch/$s_!b6ax!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png 848w, https://substackcdn.com/image/fetch/$s_!b6ax!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png 1272w, https://substackcdn.com/image/fetch/$s_!b6ax!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f77aea3-1950-4378-a8cb-c8a34c13c583_830x557.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>For global metrics like inventory, we avoid updating a global scalar from multiple workers. Instead, we maintain per-shard state and reduce it only on demand or on a schedule, decoupling the write frequency from the read frequency .</p><p><strong>Design point 3: Topology-aware state</strong></p><p>It is a misconception that memory is uniform; on multi-socket machines, memory access possesses locality. Allocating state on one node and reading it from another incurs a penalty that is regime-dependent. This risk appears in large symbol universes or multi-process architectures where processes are pinned to different sockets . In calm markets, cross-node penalties are masked, but in bursts, they become part of the tail.</p><p>A NUMA miss changes effective bandwidth and increases variance. We model access time as:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;T_{mem} = \\begin{cases} T_{local} &amp; \\text{if data local} \\\\ T_{remote} &amp; \\text{if data remote} \\end{cases}&quot;,&quot;id&quot;:&quot;QOQUHKCLJD&quot;}" data-component-name="LatexBlockToDOM"></div><p>If the probability of remote access <em>p</em> changes with thread scheduling, <em>T</em><sub>mem</sub> becomes stochastic. The variance is given by:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;Var(T_{mem}) = p(1-p)(T_{remote} - T_{local})^{2}&quot;,&quot;id&quot;:&quot;LAWDYRXDAV&quot;}" data-component-name="LatexBlockToDOM"></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bPJN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4e1e475-a268-4209-a411-965817369af5_950x374.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bPJN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4e1e475-a268-4209-a411-965817369af5_950x374.png 424w, https://substackcdn.com/image/fetch/$s_!bPJN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4e1e475-a268-4209-a411-965817369af5_950x374.png 848w, https://substackcdn.com/image/fetch/$s_!bPJN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4e1e475-a268-4209-a411-965817369af5_950x374.png 1272w, https://substackcdn.com/image/fetch/$s_!bPJN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4e1e475-a268-4209-a411-965817369af5_950x374.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!bPJN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4e1e475-a268-4209-a411-965817369af5_950x374.png" width="950" height="374" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c4e1e475-a268-4209-a411-965817369af5_950x374.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:374,&quot;width&quot;:950,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bPJN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4e1e475-a268-4209-a411-965817369af5_950x374.png 424w, https://substackcdn.com/image/fetch/$s_!bPJN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4e1e475-a268-4209-a411-965817369af5_950x374.png 848w, https://substackcdn.com/image/fetch/$s_!bPJN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4e1e475-a268-4209-a411-965817369af5_950x374.png 1272w, https://substackcdn.com/image/fetch/$s_!bPJN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4e1e475-a268-4209-a411-965817369af5_950x374.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This variance again feeds into queueing tails.</p><p>Since the OS kernel typically uses a <em>first touch</em> policy&#8212;where the node that first writes to a page owns it&#8212;we must ensure the consumer process initializes the state.</p><pre><code>import numpy as np
import os

def allocate_and_touch(shape, dtype, touch_value=0):
    x = np.empty(shape, dtype=dtype)
    x[...] = touch_value  # Touch all pages by writing
    return x

def pin_to_cores(cores):
    os.sched_setaffinity(0, set(cores))

# Pin to cores [0,1,2,3] and then allocate state locally
pin_to_cores([0,1,2,3])
state = allocate_and_touch((10000,), np.float64)</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oM8c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oM8c!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png 424w, https://substackcdn.com/image/fetch/$s_!oM8c!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png 848w, https://substackcdn.com/image/fetch/$s_!oM8c!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png 1272w, https://substackcdn.com/image/fetch/$s_!oM8c!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!oM8c!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png" width="821" height="557" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:557,&quot;width&quot;:821,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:51434,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/184204878?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!oM8c!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png 424w, https://substackcdn.com/image/fetch/$s_!oM8c!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png 848w, https://substackcdn.com/image/fetch/$s_!oM8c!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png 1272w, https://substackcdn.com/image/fetch/$s_!oM8c!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43f2b22-d57f-4f9a-a1f3-d480cf051653_821x557.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>As you see, pinning is much more than performance optimization; indeed, it is a stability mechanism that reduces the randomness in execution location, eliminating a hidden mixture component in the latency distribution.</p><p><strong>Design point 4: Page-level engineering</strong></p><p>Another pervasive misconception in low-latency systems is that cache misses represent the primary memory bottleneck. In reality, caches are only half the story; before the CPU can fetch a cache line, it must translate the virtual address to a physical address via the Translation Lookaside Buffer (TLB) . If a strategy touches too many pages too quickly, the TLB misses, and the hardware page walker intervenes, stalling the pipeline. </p><p>This risk is acute when scanning large state arrays with poor stride patterns or maintaining sparse per-symbol objects spread across the heap . A classic pathology involves a hot loop that reads <em>just a few bytes</em> from a vast number of pages; while the data cache sees a low volume of bytes, the TLB sees an overwhelming number of distinct pages.</p><p>We model the translation cost as a variance injection term:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;T_{translate} = T_{0} + \\delta \\cdot \\text{Bernoulli}(p(K))&quot;,&quot;id&quot;:&quot;RZQXLNDICJ&quot;}" data-component-name="LatexBlockToDOM"></div><p>where &#948; is the penalty of a page walk and <em>p</em>(<em>K</em>) is the probability of a miss given <em>K</em> pages touched per message. As <em>K</em> exceeds the TLB&#8217;s coverage capacity <em>M</em>, the miss rate increases sharply, causing latency to become spiky.</p><p>The impact of access patterns is quantified by approximating the number of distinct pages touched when scanning <em>N</em> elements of size <em>b</em> with stride <em>s</em>:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\#pages \\approx \\frac{Nbs}{P}&quot;,&quot;id&quot;:&quot;VVBDWYRWKC&quot;}" data-component-name="LatexBlockToDOM"></div><p>where <em>P</em> is the page size (typically 4KB). This relationship reveals that stride is a multiplier for page footprint. A stride that forces jumps across page boundaries can turn a computationally trivial loop into a translation nightmare.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wc1X!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2067829d-4e96-43c5-8741-7817c004713d_950x343.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wc1X!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2067829d-4e96-43c5-8741-7817c004713d_950x343.png 424w, https://substackcdn.com/image/fetch/$s_!wc1X!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2067829d-4e96-43c5-8741-7817c004713d_950x343.png 848w, https://substackcdn.com/image/fetch/$s_!wc1X!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2067829d-4e96-43c5-8741-7817c004713d_950x343.png 1272w, https://substackcdn.com/image/fetch/$s_!wc1X!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2067829d-4e96-43c5-8741-7817c004713d_950x343.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!wc1X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2067829d-4e96-43c5-8741-7817c004713d_950x343.png" width="950" height="343" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2067829d-4e96-43c5-8741-7817c004713d_950x343.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:343,&quot;width&quot;:950,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wc1X!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2067829d-4e96-43c5-8741-7817c004713d_950x343.png 424w, https://substackcdn.com/image/fetch/$s_!wc1X!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2067829d-4e96-43c5-8741-7817c004713d_950x343.png 848w, https://substackcdn.com/image/fetch/$s_!wc1X!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2067829d-4e96-43c5-8741-7817c004713d_950x343.png 1272w, https://substackcdn.com/image/fetch/$s_!wc1X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2067829d-4e96-43c5-8741-7817c004713d_950x343.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We can diagnose this regime change qualitatively by measuring scan latency as a function of stride. The following Python snippet shows the non-linear latency jump that occurs when the stride causes page thrashing:</p><pre><code>import numpy as np
import time

def scan_sum(arr: np.ndarray, step: int) -&gt; float:
    t0 = time.perf_counter_ns()
    s = float(arr[::step].sum())
    t1 = time.perf_counter_ns()
    return (t1 - t0) / 1e6  # ms

N = 50000000
arr = np.ones(N, dtype=np.float64)
# Observe regime change as step size increases
for step in [1, 2, 4, 8, 64, 512, 4096]:
    print(step, scan_sum(arr, step))</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zeV4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zeV4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png 424w, https://substackcdn.com/image/fetch/$s_!zeV4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png 848w, https://substackcdn.com/image/fetch/$s_!zeV4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png 1272w, https://substackcdn.com/image/fetch/$s_!zeV4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!zeV4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png" width="828" height="558" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:558,&quot;width&quot;:828,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:62458,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/184204878?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zeV4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png 424w, https://substackcdn.com/image/fetch/$s_!zeV4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png 848w, https://substackcdn.com/image/fetch/$s_!zeV4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png 1272w, https://substackcdn.com/image/fetch/$s_!zeV4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29d75612-9c46-4944-b3f8-d009b2d53e25_828x558.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The engineering response is to keep hot loops page-local by operating on contiguous blocks and avoiding sparse pointer chasing. Where appropriate, utilizing huge pages can increase effective coverage, though this requires OS support and careful operational discipline.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lF1e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" width="1456" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1158490,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3&quot;,&quot;text&quot;:&quot;Send me a copy&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3"><span>Send me a copy</span></a></p><p><strong>Design point 5: Control-flow predictability</strong></p>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-data-building-micro-trading">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Data: Low-latency data structures ]]></title><description><![CDATA[Reduce timing error]]></description><link>https://www.quantbeckman.com/p/with-code-data-low-latency-data-structures</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-data-low-latency-data-structures</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Tue, 30 Dec 2025 13:41:16 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>Do we really need low-Latency data structures?</p></li><li><p>Risk and limitations of the data structures.</p></li><li><p>Low latency, reframed: <em>Risk distribution</em>.</p></li><li><p>Memory and allocation.</p></li><li><p>Core optimization.</p><ol><li><p>Tail-latency budgeting as a first-class risk metric (quantile engineering).</p></li><li><p>State and flag encoding under memory-bandwidth constraints.</p></li><li><p>SoA vs AoS for feature pipelines.</p></li><li><p>Preallocation and out=.</p></li><li><p>Queueing primitives.</p></li><li><p>Zero-copy paths and multiprocess topologies.</p></li></ol></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read the newsletter index</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Introduction</h4><p>You can have the right signal, the right risk limits, and the right order type, and still lose money because your system expresses that decision at the wrong moment. That failure mode is especially cruel because it doesn&#8217;t look like <em>bad alpha</em> in a backtest; it looks like slippage, adverse selection, and fills that degrade exactly when you most need precision. The strategy survives on paper while the execution layer taxes it in production.</p><p>Most teams treat that tax as an engineering inconvenience: optimize later, refactor later, rewrite later. But latency is not a single number you <em>improve</em>. The market doesn&#8217;t care about your median. It cares about the rare stalls that occur during stress, when queues build, spreads widen, and your signal&#8217;s half-life collapses. Those stalls are where <em>good research</em> becomes a losing business.</p><p>Today we take a different stance: <em>the execution pipeline is a risk book, and data structures are positions inside it</em>. Each allocation, copy, branch, cache miss, and serialization step is exposure&#8212;not in dollars, but in <em>timing error</em>. If your system occasionally becomes unavailable for milliseconds, you are effectively holding stale orders with unpriced optionality against faster participants. </p><p>If you want to go deeper, don&#8217;t forget to check that:</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!DYTO!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fffe51acd-3315-4351-bb6a-5cb093ec6d51_451x627.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">C++ Design Patterns For Low Latency Applications Including High Frequency Trading</div><div class="file-embed-details-h2">1.67MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/5b773872-02aa-4cd7-95ee-8832948fff9c.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/5b773872-02aa-4cd7-95ee-8832948fff9c.pdf"><span class="file-embed-button-text">Download</span></a></div></div><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!Yryf!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9bc9037-aaaf-4e8c-b738-27b43be281d0_327x314.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Performance, Latency And Scaling</div><div class="file-embed-details-h2">6.09MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/2403277c-9691-463f-9822-50c10f181a06.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/2403277c-9691-463f-9822-50c10f181a06.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>So the goal here is not to make Python <em>fast</em> in the abstract. The goal is to make the latency profile <em>boring</em>: predictable under load, stable under bursts, and engineered around hard budgets. Think of it as deadline-driven execution: </p><div class="callout-block" data-callout="true"><p>A trade has value only while it arrives before a moving boundary. Past that boundary, correctness decays into noise.</p></div><p>What follows is a practical framework for designing that boringness: treating tail latency as a first-class risk metric, treating memory bandwidth as a scarce resource, and treating the allocator as a hazard to be removed from the hot path. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!liGO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!liGO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!liGO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!liGO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!liGO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!liGO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1164658,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!liGO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!liGO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!liGO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!liGO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66a3713e-7b8e-407a-8dca-b82cf9e8187a_1536x1024.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Do we really need low-Latency data structures?</h4><p>In algorithmic trading, <em>low latenc</em>y is frequently fetishized as a vanity metric&#8212;a badge of honor for engineers who enjoy optimizing C++ templates, shaving nanoseconds off serialization protocols, or debating the merits of kernel bypass networking. This is an error in categorization that pervades the industry. Latency is not a stylistic preference, a marketing bullet point, or a nice-to-have feature for the roadmap; it is a measurable, deterministic property of your execution pipeline: </p><p><em>Market event &#8594; State update &#8594; Signal generation &#8594; Risk check &#8594; Order transmission.</em></p><p>Every discrete step in this chain introduces delay. More critically, every step introduces <em>variance</em>.</p><p>The uncomfortable dilemma facing quantitative development teams&#8212;particularly those utilizing Python for live execution&#8212;is that low latency is often treated as an optional optimization phase, something to be <em>bolted on</em> once the strategy demonstrates alpha. </p><p>I know what you&#8217;re going to say: <em>Python is for prototyping and C++ is for running code</em>. But many retailers, and even small businesses, can&#8217;t do that, due to time and cost constraints. Yes, I know, maybe they&#8217;ve done it wrong from the start, maybe they should invest more in migrating their entire infrastructure. But for whatever reason, they can&#8217;t.</p><p>So this creates a dangerous, often fatal, path dependency. Research code, optimized for vectorized throughput and batch processing (e.g., pandas DataFrames loaded from Parquet files), becomes production code. This architecture functions adequately in quiet markets where arrival rates are Poisson-distributed and manageable. But when the regime changes&#8212;a volatility spike, a venue microburst, or a specific symbol becoming the locus of global attention&#8212;the infrastructure reveals its true nature.</p><p>The senior quant&#8217;s version of this dilemma is not the binary, simplistic question &#8220;<em>Are we fast</em>?&#8221; It is the statistical question regarding the distribution of outcomes:</p><blockquote><p>Are we optimizing Expected Latency <em>E</em>[<em>L</em>] or Tail Latency <em>Q</em><sub>99.9</sub>(<em>L</em>)?</p></blockquote><p>If you optimize for the average case (<em>E</em>[<em>L</em>]), you are implicitly accepting that in the moments of highest market stress&#8212;when the opportunity cost is highest, liquidity is most fleeting, and spreads are widening&#8212;your system will perform at its worst. This is the definition of negative convexity. If you operate strategies where the marginal expected return is a function of queue position, adverse selection avoidance, or reactive hedging, latency is not an engineering detail. It is a core parameter of the execution model. It directly impacts fill probabilities (P<sub>fill</sub>), slippage (<em>S</em>), and effective spread capture.</p><p>Consider the implications of <em>average</em> latency in a winner-takes-all auction mechanism like a limit order book. If your system is faster than 90% of the market 90% of the time, but slower than the market during the 10% of intervals where 80% of the volume trades, your effective capture rate is near zero. You are winning races that have no prize money. Therefore, the necessity of low-latency data structures is not about <em>speed</em> in the abstract. It is about reducing the probability of wrong timing outcomes that invalidate the alpha model. We do not need to be fast to feel good; we need to be fast to reduce the <em>timing error</em> term in our PnL equation. A strategy that generates 2 bps of edge but surrenders 3 bps due to tail-latency-induced slippage during volatility is not a <em>good strategy with bad tech</em>; it is a losing strategy.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><h4>Risk and limitations of the data structures</h4><p>The primary risk in Python-based trading systems is not the interpreter overhead itself (though that exists and sets a theoretical floor of perhaps 10-20 microseconds), but the memory access patterns dictated by standard data structures. <em>Just use NumPy</em> is an insufficient heuristic; in fact, naive NumPy usage can be detrimental in event-driven loops. A data structure implies a rigid contract with the hardware. It dictates:</p><ol><li><p><strong>Memory access patterns:</strong> Modern CPUs depend heavily on hardware prefetchers to predict which data to load into the L1 cache before the instruction requests it. Prefetchers thrive on linearity. Are we reading contiguous blocks, or are we chasing pointers across the heap? Random access into a fragmented heap results in Cache Misses (L1/L2/L3). A single L3 cache miss can stall the CPU pipeline for 300+ cycles. If your data structure requires three pointer dereferences to read a price (e.g., <code>OrderBook </code><em>&#8594;</em><code> Level </code><em>&#8594;</em><code> Price</code>), you are guaranteeing execution stalls.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zxk-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zxk-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png 424w, https://substackcdn.com/image/fetch/$s_!Zxk-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png 848w, https://substackcdn.com/image/fetch/$s_!Zxk-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png 1272w, https://substackcdn.com/image/fetch/$s_!Zxk-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zxk-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png" width="1456" height="760" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:760,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:68566,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zxk-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png 424w, https://substackcdn.com/image/fetch/$s_!Zxk-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png 848w, https://substackcdn.com/image/fetch/$s_!Zxk-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png 1272w, https://substackcdn.com/image/fetch/$s_!Zxk-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f9a204e-5f42-4b9d-991e-686a2d279b11_1779x929.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li><li><p><strong>Copy semantics:</strong> Where is data duplicated? Is the copy operation visible to the developer, or hidden behind a convenient API like <code>fancy_indexing</code> or slicing that triggers a deep copy? In Python, <em>ease of use</em> often correlates inversely with <em>memory efficiency</em>. For example, calling a C-compiled function that expects a C-contiguous array with a non-contiguous slice forces NumPy to silently allocate a new buffer, copy all data, execute the function, and then potentially copy results back. This is invisible in the code but devastating to the memory bus.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tl6G!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tl6G!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png 424w, https://substackcdn.com/image/fetch/$s_!tl6G!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png 848w, https://substackcdn.com/image/fetch/$s_!tl6G!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png 1272w, https://substackcdn.com/image/fetch/$s_!tl6G!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!tl6G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png" width="1456" height="681" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:681,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:187727,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tl6G!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png 424w, https://substackcdn.com/image/fetch/$s_!tl6G!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png 848w, https://substackcdn.com/image/fetch/$s_!tl6G!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png 1272w, https://substackcdn.com/image/fetch/$s_!tl6G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa55a6ebb-1bf0-4137-b785-fd516d72e5ad_2179x1019.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li><li><p><strong>Allocation behavior:</strong> Does this operation require finding new memory pages? Does it trigger the OS allocator (<code>malloc</code>/<code>free</code>)? Does it invoke the Python Garbage Collector? Allocation is non-deterministic; it depends on the fragmentation state of the heap at that exact moment. Requesting 64 bytes when the heap is clean might take 50ns. Requesting 64 bytes when the heap is Swiss-cheesed by fragmentation might take 5&#181;s as the allocator searches for a contiguous block or requests new pages from the OS kernel.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!17ht!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!17ht!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png 424w, https://substackcdn.com/image/fetch/$s_!17ht!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png 848w, https://substackcdn.com/image/fetch/$s_!17ht!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png 1272w, https://substackcdn.com/image/fetch/$s_!17ht!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!17ht!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png" width="1456" height="628" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:628,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:109318,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!17ht!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png 424w, https://substackcdn.com/image/fetch/$s_!17ht!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png 848w, https://substackcdn.com/image/fetch/$s_!17ht!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png 1272w, https://substackcdn.com/image/fetch/$s_!17ht!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38c48117-d7fd-4dc0-bdb7-cd87a448cd30_2179x940.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li><li><p><strong>Latency distribution:</strong> Does the system exhibit consistent service times, or is it <em>spiky</em>? In trading, <em>spiky latency is structurally worse than slower-but-smooth latency</em>. A system with a median tick-to-trade time of 50&#956;s and a p99 of 5ms is significantly more dangerous than a system with a median of 100&#956;s and a p99 of 120&#956;s. The former implies that during a burst, the system effectively collapses. This collapse creates <em>stale orders</em> resting in the book, waiting to be picked off by faster competitors who have already updated their valuation models using the new market data you haven&#8217;t processed yet.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!E72R!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!E72R!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png 424w, https://substackcdn.com/image/fetch/$s_!E72R!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png 848w, https://substackcdn.com/image/fetch/$s_!E72R!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png 1272w, https://substackcdn.com/image/fetch/$s_!E72R!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!E72R!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png" width="1456" height="827" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:827,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:132417,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!E72R!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png 424w, https://substackcdn.com/image/fetch/$s_!E72R!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png 848w, https://substackcdn.com/image/fetch/$s_!E72R!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png 1272w, https://substackcdn.com/image/fetch/$s_!E72R!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f7e7937-46c9-4898-81be-7993cac8491f_1779x1010.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li></ol><p>The specific risks introduced by naive structure choices include:</p><ul><li><p><strong>Hidden copying:</strong> Implicit casting (e.g., performing arithmetic between <code>float32</code> and <code>float64</code> resulting in an upcasted temporary array) or <code>np.ascontiguousarray</code> calls in hot paths. A copy operation is <em>O</em>(<em>N</em>) bandwidth consumption; doing this on every tick saturates the memory bus (DRAM bandwidth), which is often the tightest bottleneck in modern multi-core systems.</p></li><li><p><strong>Allocator jitter:</strong> Repeated temporary array creation in chained expressions (e.g., <code>A = B * C + D</code>) forces the allocator to thrash. This involves not just the time to allocate, but the time to zero-out memory for security, and the degradation of cache locality. It fragments the heap, increasing the time complexity of finding a free memory block for subsequent allocations.</p></li><li><p><strong>Bandwidth saturation:</strong> Using <code>np.bool_</code> (1 byte) for binary flags wastes 8x memory bandwidth compared to bit-packed representations. In a bandwidth-constrained environment&#8212;which describes all modern HFT servers sharing Last Level Cache (LLC) across cores&#8212;wasting bandwidth is equivalent to throttling your CPU. Reading 8 bytes to get 1 bit of information is a 6300% efficiency loss.</p></li><li><p><strong>Cache thrashing:</strong> Array-of-Structures layouts (e.g., a list of <code>Trade</code> objects) force the CPU to load cache lines containing data (e.g., <code>exchange_order_id</code>, <code>client_tag</code>, <code>flags</code>) that are irrelevant to the current calculation (e.g., <code>mid_price</code>). This reduces the <em>effective cache size</em> available for the relevant data, causing higher eviction rates. If your working set for a calculation exceeds L2 cache size because of <em>structure bloat</em>, your performance falls off a cliff.</p></li></ul><p>The core problem here manifests during a pivotal event. Consider a mean-reversion strategy that appears stable during backtesting and low-volatility paper trading. The strategy relies on comparing the order book imbalance of a correlated ETF against the futures contract.</p><p>A macro news release (e.g., non-farm payrolls) triggers an auction imbalance burst. The feed handler rate jumps from a manageable 10,000 messages/sec to a torrential 500,000 messages/sec. This is not a gradual ramp; it is a step function change in load.</p><p>In this scenario, the alpha model remains valid. The signal is clear: the futures have moved, the ETF is lagging, and there is a theoretical arbitrage. But the realized fills degrade sharply. You are getting hit on your passive quotes <em>after</em> you intended to cancel them, and your aggressive orders are landing in the queue <em>after</em> the liquidity has been taken. A postmortem analysis typically reveals:</p><ul><li><p><strong>Median tick-to-trade time</strong> remained within tolerance (e.g., 50&#181;s).</p></li><li><p><strong>The p99.9 latency</strong> blew out by orders of magnitude (e.g., from 80&#181;s to 15ms).</p></li><li><p><strong>Correlation:</strong> This blowout correlates perfectly with bursts of memory allocation and Garbage Collection (GC) pauses.</p></li></ul><p>Why does the GC wake up exactly then? Because the high message rate generated a high rate of temporary object creations (allocations)&#8212;parsing new price levels, creating new order objects, instantiating log messages&#8212;filling the <em>generation </em>0 heap and triggering a collection cycle. The GC is a <em>stop-the-world</em> event. While the GC is walking the object graph to find unreachable objects, your network card is buffering packets. When the GC finishes, you process those packets, but the market has moved.</p><p>This is the event that forces the issue. We do not lose because we are slow on average. <em>We lose because we are effectively offline for milliseconds at a time</em> due to structural inefficiencies, exactly when the market is moving the fastest. The conflict is no longer theoretical; it is PnL-negative. The infrastructure has become the primary source of risk, eclipsing market risk.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!U_Oz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!U_Oz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png 424w, https://substackcdn.com/image/fetch/$s_!U_Oz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png 848w, https://substackcdn.com/image/fetch/$s_!U_Oz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png 1272w, https://substackcdn.com/image/fetch/$s_!U_Oz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!U_Oz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png" width="1456" height="723" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:723,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:518709,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!U_Oz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png 424w, https://substackcdn.com/image/fetch/$s_!U_Oz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png 848w, https://substackcdn.com/image/fetch/$s_!U_Oz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png 1272w, https://substackcdn.com/image/fetch/$s_!U_Oz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7faa0ca-92f7-4085-9b72-397589806dd1_1843x915.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Low latency, reframed: <em>Risk distribution</em></h4><p>To solve this, we must reframe our definition of success. A misconception that persists in the industry&#8212;often imported from web development or big data&#8212;is that low latency is equivalent to <em>low mean latency</em>. This is a rookie mistake in the context of stochastic control and competitive games.</p><p>We must treat latency <em>L</em> as a random variable with a probability density function <em>f</em>(<em>L</em>). We are not minimizing the expectation <em>E</em>[<em>L</em>]. We are solving a constrained optimization problem where the tail is the constraint:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\text{minimize } E[L] \\quad \\text{subject to } \\quad P(L > \\ell^*) \\le \\varepsilon&quot;,&quot;id&quot;:&quot;GPNHGAEMQT&quot;}" data-component-name="LatexBlockToDOM"></div><p>Where <em>l</em><sup>*</sup> is the maximum tolerable tick-to-trade latency (the <em>cliff</em> where the alpha decays to zero) and &#1013; is a strictly bounded probability (e.g., 10<sup>-3</sup> or 10<sup>-4</sup>). In high-frequency strategies, <em>l</em><sup>*</sup> might be the time it takes for a signal to traverse the colocation center cross-connect.</p><p>This aligns with standard risk management frameworks. Just as we calculate Value at Risk to understand tail financial loss, we must calculate <em>Latency at Risk</em> to understand tail execution failure. If the cost of latency <em>c</em>(<em>L</em>) is convex&#8212;meaning a 10ms delay costs more than 10x a 1ms delay (common in momentum bursts where the price moves away exponentially)&#8212;then reducing the variance of <em>L</em> yields a significantly higher improvement in Expected PnL (<em>E</em>[&#960;]) than reducing the mean.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;E[\\Pi] \\approx E[\\alpha] - E[c(L)]&quot;,&quot;id&quot;:&quot;JFRNJROELW&quot;}" data-component-name="LatexBlockToDOM"></div><p>Furthermore, we must consider the conditional probability of latency given volatility: <em>P</em>(<em>L</em>&gt;<em>x</em> | &#963;&gt;<em>high</em>). If this probability is high, our system is anti-correlated with opportunity. We are slow exactly when we need to be fast. If <em>c</em>(<em>L</em>) spikes during market bursts (when <em>L</em> also naturally rises due to load), the correlation implies that tail latency reduction is the single highest-leverage engineering activity available. A robust system must have a flat latency profile regardless of input throughput, up to the saturation point of the physical hardware.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IQFg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IQFg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png 424w, https://substackcdn.com/image/fetch/$s_!IQFg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png 848w, https://substackcdn.com/image/fetch/$s_!IQFg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png 1272w, https://substackcdn.com/image/fetch/$s_!IQFg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!IQFg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png" width="1456" height="941" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:941,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IQFg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png 424w, https://substackcdn.com/image/fetch/$s_!IQFg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png 848w, https://substackcdn.com/image/fetch/$s_!IQFg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png 1272w, https://substackcdn.com/image/fetch/$s_!IQFg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd05da0-817f-4ee3-9613-666a70162405_2645x1710.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Memory and allocation</h4><p>Python is, in many respects, fast enough for the logic layer of mid-frequency strategies. The obstacles are not the language syntax, but the underlying machinery. The interpreter loop is optimized for flexibility, not predictability.</p><p>The specific obstacles are physical and architectural:</p><ol><li><p><strong>Python object overhead:</strong> Every <code>int</code> or <code>float</code> in a standard Python list is a full <code>PyObject</code> C-struct. This includes a reference count (<code>ob_refcnt</code>), a type pointer (<code>ob_type</code>), and the value itself. A list of integers is not a contiguous block of memory (like a C array); it is a contiguous block of <em>pointers </em>to objects scattered across the heap. Iterating over this list requires double dereferencing (pointer to list &#8594; pointer to object &#8594; value) and destroys locality of reference. This causes the CPU to stall constantly waiting for data to arrive from main memory.</p></li><li><p><strong>Implicit allocations:</strong> High-level NumPy expressions optimize for readability, not memory reuse. The expression <code>C = A + B</code> allocates a new buffer for <code>C</code> every time it is executed. In a loop running 1,000 times a second, this creates 1,000 allocations and 1,000 deallocations per second. This places immense pressure on <code>malloc</code>/<code>free</code>, increasing the likelihood of locking contention in the memory allocator (glibc malloc has per-arena locks). Even worse, it churns the CPU cache, as new memory is fundamentally <em>cold</em> memory.</p></li><li><p><strong>Layout mismatches:</strong> We often compute feature vectors (columnar operations: mean of price, standard deviation of size), but market data arrives as discrete events (row-based: Update for symbol X). Converting between these layouts at high frequency creates friction. Naively appending rows to a NumPy array is an <em>O</em>(<em>N</em>) operation (copying the whole array), which is catastrophic for latency. Storing data as a list of rows creates the cache thrashing issues mentioned earlier. We need a hybrid approach that allows row-based updates into column-based storage without reallocation.</p></li><li><p><strong>The GIL and GC:</strong> The Global Interpreter Lock (GIL) ensures thread safety by allowing only one thread to execute Python bytecodes at a time, effectively serializing execution and preventing true parallelism on multicore systems. More dangerously, the Garbage Collector pauses execution to mark and sweep objects. These pauses are not scheduled; they occur when the allocation threshold is breached&#8212;which, per Murphy&#8217;s Law, is always during the busiest market interval because that&#8217;s when you are generating the most data. The duration of a GC pause is proportional to the number of <em>live</em> objects, meaning as your internal state grows (more orders, more signals), your latency spikes get worse.</p></li></ol><p>The challenge, therefore, is to build data structures in Python that minimize three physical costs:</p><ul><li><p><strong>Allocator involvement:</strong> We want <em>zero</em> allocations in the hot path. All memory must be claimed at startup. We want to treat Python as a static memory language within the critical section.</p></li><li><p><strong>Memory bandwidth waste:</strong> We want to move only the bits we read. We cannot afford to load 64 bytes of cache line to read 1 byte of data. We need dense, packed representations.</p></li><li><p><strong>Copy operations:</strong> We want to mutate data in place or use pre-existing buffers. We need to manage the lifecycle of our buffers explicitly, rather than relying on the interpreter to clean up after us.</p></li></ul><p>This requires a departure from <em>Pythonic</em> code (idiomatic, concise) and an embrace of <em>systems</em> thinking within Python (explicit, memory-aware). It means writing code that looks more like C or Fortran, wrapped in Python syntax.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qGHI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qGHI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png 424w, https://substackcdn.com/image/fetch/$s_!qGHI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png 848w, https://substackcdn.com/image/fetch/$s_!qGHI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png 1272w, https://substackcdn.com/image/fetch/$s_!qGHI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!qGHI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png" width="1456" height="848" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:848,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:418760,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qGHI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png 424w, https://substackcdn.com/image/fetch/$s_!qGHI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png 848w, https://substackcdn.com/image/fetch/$s_!qGHI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png 1272w, https://substackcdn.com/image/fetch/$s_!qGHI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6020565-2182-4cf6-ae73-3a4c029d7c71_1721x1002.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Core optimization</h3><p>The transition from theoretical latency minimization to practical, burst-resilient engineering requires a fundamental inversion of priorities. </p><div class="callout-block" data-callout="true"><p>In research, we optimize for throughput, expressiveness, and ease of debugging. In production execution, we must optimize for <em>determinism, bandwidth efficiency, and cache locality. </em></p></div><p>The optimizations detailed below&#8212;risk budgeting, bit-level encoding, memory layout, and allocation discipline&#8212;constitute the <em>core optimization</em> phase. These are not micro-optimizations but structural changes required to prevent the Python interpreter and the OS kernel from becoming the bottlenecks. </p><h4><strong>Tail-latency budgeting as a first-class risk metric (quantile engineering)</strong></h4><p>We must stop optimizing for the <em>happy path</em> (steady state) and optimize strictly for the <em>burst path</em> (regime change).</p><p>A trading system is effectively a stochastic processing network. While textbook queueing theory often models these as<em> M</em>/<em>M</em>/<em>1</em> queues (Poisson arrivals, exponential service times), real financial data is highly <em>clustered.</em> Market data arrival times follow <em>Hawkes processes</em>, where the arrival of one event increases the probability of subsequent events.</p><p>During a microburst (e.g., a stop-loss cascade or an aggressive sweep of the order book), the instantaneous arrival rate &#955;(<em>t</em>) can exceed the steady-state mean by 100x or 1000x for durations of 10 to 500 milliseconds. This is the <em>critical window.</em></p><p>Utilization is defined as &#961; = &#955;/&#956;. As &#961; &#8594; 1, the waiting time <em>W</em> does not grow linearly; it explodes asymptotically toward infinity:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;E[W] = \\frac{1}{\\mu - \\lambda}&quot;,&quot;id&quot;:&quot;ZRGPWGPUQC&quot;}" data-component-name="LatexBlockToDOM"></div><p>However, this equation assumes infinite buffer capacity and steady states. In a low-latency context, we care about the<em> sojourn time probability</em>: <em>P</em>(<em>W</em> &gt; <em>t</em>). When &#955;(<em>t</em>) &gt; &#956; (instantaneous overload), the queue builds up linearly. If our service rate &#956; is effectively lowered by events like GC, OS context switches, or any other weird fault, we face a double catastrophe: the input rate is maximizing exactly when the service rate drops to zero. This leads to <em>convex failure</em>, where a small degradation in code performance leads to a disproportionate, catastrophic increase in queue latency.</p><p>To manage this, we treat rare latency spikes (GC, OS paging, buffer resizing, lock contention) as <em>system failures,</em> equivalent to a network disconnect or a hardware fault. The distribution of Latency <em>L</em> can be decomposed into a baseline component <em>B</em> and a jump component <em>J</em>:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;L=B+J&quot;,&quot;id&quot;:&quot;UGYUXRVAWO&quot;}" data-component-name="LatexBlockToDOM"></div><ul><li><p><em>B</em><strong> (Baseline):</strong> The deterministic cost of instruction execution, memory access, and serialization. This is Gaussian-like, highly predictable, and easy to measure. Optimizing <em>B</em> involves algorithmic improvements and better CPU pipelining.</p></li><li><p><em>J</em><strong> (Jump):</strong> A stochastic process driven by allocation events, kernel interrupts, GIL contention, or Lower-Level Cache (LLC) thrashing. This component follows a <em>fat tail</em> distribution (Power Law).</p></li></ul><p>If <em>P</em>(<em>J</em> &gt; 0) &#8776; 0.001, then <em>Q</em><sub>99.9</sub>(<em>L</em>) is dominated entirely by <em>J</em>. Reducing <em>B</em> by 10% (e.g., optimizing a math formula to save 200ns) yields negligible benefit if <em>J</em> introduces a 5ms delay. Therefore,<em> we do not optimize B; we eliminate the existence of J</em><strong>.</strong> The goal is to truncate the tail of the distribution, converting the latency profile from a Power Law to a Normal distribution.</p><p><strong>Protocol:</strong></p><ol><li><p><strong>Preallocate everything (static heap):</strong> The heap topology should be static after initialization. All buffers used for calculation, state tracking, and order generation must be allocated at the start of the trading day. Using <code>append()</code> on a list, extending a deque, or resizing a NumPy array is a firing offense in the critical path. We use <em>Object Pools</em> or <em>Slab Allocation</em> patterns to reuse objects rather than creating new ones.</p></li><li><p><strong>Disable GC:</strong> <code>gc.disable()</code> should be called during trading hours. The standard Python GC is a collector (Generation 0 collections are fast, but Generation 2 collections can take tens of milliseconds). Depending on the requirements even a 2ms pause is an eternity. If your memory usage grows monotonically (leaks) without GC, your code is functionally broken. Fix the leak by managing object lifecycles explicitly; don&#8217;t rely on the GC crutch.</p></li><li><p><strong>High-res metrics:</strong> Measure latency per-stage at the 99.9th and 99.99th percentile using high-resolution timers (<code>time.perf_counter_ns</code>). Averages hide the truth; the mean is a vanity metric. If your p50 is 20us but your p99 is 2ms, you have an allocation problem. Instruments should be always-on but low-overhead (writing to preallocated circular buffers in memory, not disk).</p></li><li><p><strong>Process isolation:</strong> While not strictly a data structure, ensuring your Python process runs on an isolated core (<code>isolcpus</code>, <code>taskset</code>) prevents the OS scheduler from preempting your calculation loop to run a background daemon (like a logging agent or systemd cron), ensuring &#956; remains constant and deterministic.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GqWQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GqWQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png 424w, https://substackcdn.com/image/fetch/$s_!GqWQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png 848w, https://substackcdn.com/image/fetch/$s_!GqWQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png 1272w, https://substackcdn.com/image/fetch/$s_!GqWQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!GqWQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png" width="989" height="590" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:590,&quot;width&quot;:989,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GqWQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png 424w, https://substackcdn.com/image/fetch/$s_!GqWQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png 848w, https://substackcdn.com/image/fetch/$s_!GqWQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png 1272w, https://substackcdn.com/image/fetch/$s_!GqWQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56c04d3a-0b21-4550-9dd4-393148dfe499_989x590.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4><strong>State and flag encoding under memory-bandwidth constraints</strong></h4><p>Compactness is not about saving RAM (capacity); it is about saving <em>Memory Bandwidth</em> (throughput) and maximizing<em> L1 Cache Residency.</em></p><p>In a pipeline tracking 5,000 instruments, checking a boolean flag (e.g., <code>is_tradable</code>, <code>is_halted</code>, <code>risk_check_passed</code>) implies a memory read. Standard Python/NumPy booleans (<code>np.bool_</code>) are 1 byte (8 bits).</p><ul><li><p><strong>Byte-array footprint:</strong> <em>N</em> bytes.</p></li><li><p><strong>Bit-packed footprint:</strong> <em>N</em>/8 bytes.</p></li></ul><p>For a universe of 5,000 symbols, a byte array takes ~5KB, which fits in L1 cache. However, typically we track dozens of states per symbol. If you have 20 different flags per symbol, that&#8217;s 100KB, pushing data into L2 or L3 cache. By bit-packing, 100KB becomes ~12KB, fitting entirely in L1.</p><p>More importantly, CPU cache lines are typically 64 bytes.</p><ul><li><p><strong>Byte Array:</strong> Loading a cache line gives you flags for <em>64 symbols</em>.</p></li><li><p><strong>Bitset:</strong> Loading a cache line gives you flags for <em>512 symbols.</em></p></li></ul><p>This<em> 8x improvement in symbols per cache miss </em>is the single biggest optimization for iteration speed. When you scan a universe of symbols to filter for tradable opportunities, the bottleneck is almost always memory latency (waiting for data to arrive from RAM). Bitsets reduce this wait time by 87.5%.</p><p>Furthermore, bitsets allow for <em>branchless logic</em>. Standard boolean checks often involve <code>if</code> statements, which rely on the CPU&#8217;s branch predictor. If the state is random (e.g., 50% of symbols are halted), the branch predictor fails, causing pipeline flushes (expensive). With bitsets, logic becomes bitwise arithmetic (<code>&amp;</code>, <code>|</code>, <code>^</code>, <code>~</code>), which executes in the ALU without branching.<br>We utilize a <code>uint64</code> array as a bitset. This allows checking/setting 64 flags with a single memory load and register-level arithmetic.</p><pre><code>import numpy as np

class Bitset64:
    __slots__ = ("n", "words")

    def __init__(self, n_bits: int):
        self.n = int(n_bits)
        # Allocate enough 64-bit integers to hold n_bits. 
        # The +63 // 64 is a fast integer ceiling division.
        self.words = np.zeros((self.n + 63) // 64, dtype=np.uint64)

    def get(self, i: int) -&gt; bool:
        # No branching, just bit arithmetic.
        # i &gt;&gt; 6 is equivalent to i // 64 but faster.
        # i &amp; 63 is equivalent to i % 64 but faster.
        w = i &gt;&gt; 6
        b = i &amp; 63
        # Extract the specific bit.
        return bool((self.words[w] &gt;&gt; np.uint64(b)) &amp; np.uint64(1))

    def set(self, i: int) -&gt; None:
        w = i &gt;&gt; 6
        b = i &amp; 63
        self.words[w] |= (np.uint64(1) &lt;&lt; np.uint64(b))
        
    def clear(self, i: int) -&gt; None:
        w = i &gt;&gt; 6
        b = i &amp; 63
        self.words[w] &amp;= ~(np.uint64(1) &lt;&lt; np.uint64(b))

    def check_all_safe(self, mask_indices: np.ndarray) -&gt; bool:
        # Bulk Logic Example: "Are all these indices safe?"
        # Naive approach: Loop and check bit-by-bit (Slow, Python loop overhead)
        # Optimized approach: Vectorized bitwise operations
        
        # 1. Convert indices to word/bit coordinates
        words_idx = mask_indices &gt;&gt; 6
        bits_idx = mask_indices &amp; 63
        
        # 2. Extract specific bits
        # Note: This is still somewhat slow in pure Python due to array creation.
        # In a real hotpath, we would pass 'words' to a Numba function.
        # However, purely in numpy, we can do:
        values = (self.words[words_idx] &gt;&gt; bits_idx.astype(np.uint64)) &amp; 1
        return np.all(values)

    def intersection_count(self, other: 'Bitset64') -&gt; int:
        # SIMD-like capability in Python:
        # Count bits set in BOTH bitsets instantly
        # POPCNT instruction is used implicitly by optimized numpy/C builds
        common = np.bitwise_and(self.words, other.words)
        # Simple popcount (this works, though specific numpy versions have better ways)
        # In production, use gmpy2 or a small C-extension for popcount
        return int(sum(bin(x).count('1') for x in common))</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ECF9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ECF9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png 424w, https://substackcdn.com/image/fetch/$s_!ECF9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png 848w, https://substackcdn.com/image/fetch/$s_!ECF9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png 1272w, https://substackcdn.com/image/fetch/$s_!ECF9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!ECF9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png" width="722" height="483" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:483,&quot;width&quot;:722,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:37420,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ECF9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png 424w, https://substackcdn.com/image/fetch/$s_!ECF9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png 848w, https://substackcdn.com/image/fetch/$s_!ECF9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png 1272w, https://substackcdn.com/image/fetch/$s_!ECF9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8de1c99-ad66-4f5d-93da-0bc052cc7f61_722x483.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This structure is key for low latency flags like <code>is_halted</code>, <code>has_open_orders</code>, <code>risk_limit_breached</code>, or <code>short_locate_available</code>.</p><h4>SoA vs AoS for feature pipelines</h4><p>The fastest record to process is the one you do not load into the cache. <em>Cache pollution</em> is the enemy.</p><p>There are two primary memory layouts:</p><ol><li><p><strong>Array of Structures (AoS):</strong> <code>[{price, size, time}, {price, size, time}]</code>. This is <em>object oriented</em>. Ideally suited for accessing <em>one</em> complete record randomly.</p></li><li><p><strong>Structure of Arrays (SoA):</strong> <code>Prices=[...], Sizes=[...], Times=[...]</code>. This is <em>data oriented</em>. Ideally suited for accessing <em>one</em> field across <em>many</em> records.</p></li></ol><p>Trading algorithms are typically columnar (<em>vectorized</em>). We compute returns based on prices. We compute imbalances based on sizes. If we use AoS (e.g., NumPy structured arrays or lists of objects), calculating a moving average of price requires loading the <code>size</code> and <code>time</code> fields into the cache hierarchy, only to discard them.</p><p><em>How does AoS fail!?</em><br>The CPU Hardware Prefetcher is designed to detect linear access patterns (<em>strides</em>).</p><ul><li><p><strong>SoA access:</strong> Address X, X+8, X+16 &#8230; (dense). The prefetcher easily predicts this and loads data into L1 before the CPU asks for it.</p></li><li><p><strong>AoS access:</strong> Address X, X+32, X+64 &#8230; (Strided). Depending on the stride size and the complexity of the struct, the prefetcher may fail to engage or saturate the &#8220;pending request&#8221; queue, leading to pipeline stalls.</p></li></ul><p>Let <em>B<sub>AoS</sub></em> be the total record size in bytes, and <em>B<sub>used</sub></em> be the bytes actually required for the calculation. The memory traffic efficiency ratio is:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot; \\eta = \\frac{B_{used}}{B_{AoS}} &quot;,&quot;id&quot;:&quot;PBUFZKECAX&quot;}" data-component-name="LatexBlockToDOM"></div><p>If a record is 32 bytes (Price: 8, Size: 8, Time: 8, ID: 8) but we only need the 8-byte price for a signal, &#951;=0.25. We are wasting 75% of our memory bandwidth. Using SoA, &#951;&#8594;1.</p><p>Furthermore, SoA is naturally aligned for <em>vectorization </em>(SIMD). Even if you don&#8217;t write AVX2 assembly, libraries like NumPy (backed by BLAS/MKL) can operate on contiguous arrays using 256-bit or 512-bit registers, processing 4 or 8 doubles simultaneously. This is impossible with AoS layouts due to memory gaps.<br>We implement a preallocated SoA container. Note the use of <code>np.copyto</code> to enforce data movement without intermediate object creation. We also treat this as a primitive append-only store, which in a real system would be wrapped with circular buffer logic.</p><pre><code>class TickSoA:
    __slots__ = ("cap", "n", "ts", "px", "qty", "side")

    def __init__(self, capacity: int):
        self.cap = int(capacity)
        self.n = 0
        # Preallocate distinct arrays for each field.
        # This ensures that 'px' is a contiguous block of doubles in RAM.
        # This contiguity is required for SIMD optimization.
        self.ts = np.empty(self.cap, dtype=np.uint64)
        self.px = np.empty(self.cap, dtype=np.float64)
        self.qty = np.empty(self.cap, dtype=np.float32)
        # Side encoded as int8 (0=Bid, 1=Ask) to save space compared to strings/objects
        self.side = np.empty(self.cap, dtype=np.int8)

    def append_many(self, ts, px, qty, side):
        k = len(ts)
        i0 = self.n
        i1 = i0 + k
        if i1 &gt; self.cap:
             raise OverflowError("TickSoA capacity exceeded")
        
        # Critical: Use copyto with 'no' casting to prevent implicit allocations.
        # Standard slicing assignment (self.ts[i0:i1] = ts) can sometimes trigger
        # temporary copies if dtypes don't match exactly or if 'ts' is a list.
        # copyto is explicit and safer.
        np.copyto(self.ts[i0:i1], ts, casting="no")
        np.copyto(self.px[i0:i1], px, casting="no")
        np.copyto(self.qty[i0:i1], qty, casting="no")
        np.copyto(self.side[i0:i1], side, casting="no")
        self.n = i1
    
    def get_vwap(self, start_idx, end_idx):
        # Example of how SoA enables vectorized math
        # Slice views (no copy)
        p = self.px[start_idx:end_idx]
        q = self.qty[start_idx:end_idx]
        
        # In AoS, this would require a Python loop or list comprehension (slow)
        # In SoA, this maps to optimized C loops or BLAS calls
        # Note: We assume a preallocated workspace would be passed in for true zero-alloc
        return np.dot(p, q) / np.sum(q)</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!45lT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!45lT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png 424w, https://substackcdn.com/image/fetch/$s_!45lT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png 848w, https://substackcdn.com/image/fetch/$s_!45lT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png 1272w, https://substackcdn.com/image/fetch/$s_!45lT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!45lT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png" width="687" height="479" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:479,&quot;width&quot;:687,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:39366,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!45lT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png 424w, https://substackcdn.com/image/fetch/$s_!45lT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png 848w, https://substackcdn.com/image/fetch/$s_!45lT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png 1272w, https://substackcdn.com/image/fetch/$s_!45lT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33233c13-c53c-4573-8256-68d1e9c0f71d_687x479.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Standard NumPy fancy indexing (<code>prices[indices]</code>) is a <em>latency trap</em> because it silently forces the operating system to allocate a brand new array and triggers a full memory copy on every single execution, thrashing the heap and waking up the Garbage Collector. By switching to <code>np.take</code> with the <code>out=</code> parameter, you bypass the allocator entirely, writing directly into a pre-warmed, reusable memory buffer that stays hot in your CPU's L1/L2 cache, effectively transforming a complex, non-deterministic resource request into a simple, predictable, and significantly faster memory move.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lF1e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" width="1456" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1158490,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3&quot;,&quot;text&quot;:&quot;Send me a copy&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3"><span>Send me a copy</span></a></p><h4>Preallocation and <code>out=</code></h4><p>Allocations are tail-risk generators. Every <code>malloc</code> call is a gamble on the heap&#8217;s fragmentation state.</p><p>In the high-level NumPy expression <code>Y = A * B + C</code>, the interpreter typically performs the following sequence:</p><ol><li><p>Allocate a temporary array <code>temp1</code> to hold the result of <code>A * B</code>.</p></li><li><p>Allocate a target array <code>Y</code> to hold <code>temp1 + C</code>.</p></li><li><p>Deallocate <code>temp1</code>.</p></li></ol><p>This <em>allocator churn</em> has multiple deleterious effects:</p><ul><li><p><strong>Heap fragmentation:</strong> Creating and destroying unevenly sized arrays creates <em>holes </em>in the heap, making future allocations slower as the allocator searches for contiguous blocks (First-Fit vs Best-Fit algorithm latency).</p></li><li><p><strong>Cold memory access:</strong> Newly allocated memory is <em>cold</em>. It is not in L1/L2 cache. It might not even be backed by physical RAM yet. You pay the exorbitant cost of fetching it from main RAM or triggering the OS kernel&#8217;s Page Fault Handler. Reused memory is likely <em>hot</em> in L2/L3 cache.</p></li><li><p><strong>GC pressure:</strong> Even if <code>free()</code> is fast, the Python object wrapper overhead triggers the GC reference counting logic. High churn pushes the system toward the GC threshold.</p></li></ul><p>We use a <code>Workspace</code> class that holds reusable buffers (scratchpads). We replace operator overloading (<code>+</code>, <code>*</code>) with explicit functional calls using the <code>out</code> parameter. This is the <em>zero-copy pledge:</em> We pledge to the hardware that we will never ask for new memory during the trading session.</p><pre><code>class Workspace:
    __slots__ = ("tmp1", "tmp2", "result")

    def __init__(self, shape):
        # These buffers are allocated ONCE at startup and live forever.
        # They act as our "Static Heap".
        self.tmp1 = np.empty(shape, dtype=np.float64)
        self.tmp2 = np.empty(shape, dtype=np.float64)
        self.result = np.empty(shape, dtype=np.float64)

    def calc_weighted_mid(self, bid_px, bid_sz, ask_px, ask_sz):
        # Formula: (bid_px * ask_sz + ask_px * bid_sz) / (bid_sz + ask_sz)
        
        # Step 1: Numerator parts - Zero Allocation
        # Note: We reuse tmp1 and tmp2. The data in them is overwritten.
        # We explicitly manage the "liveness" of the data in these buffers.
        np.multiply(bid_px, ask_sz, out=self.tmp1)
        np.multiply(ask_px, bid_sz, out=self.tmp2)
        
        # Accumulate in tmp1. We don't need a new buffer for the sum.
        np.add(self.tmp1, self.tmp2, out=self.tmp1) 
        
        # Step 2: Denominator - Zero Allocation
        # We reuse tmp2 because we are done with the 'ask_px * bid_sz' term.
        np.add(bid_sz, ask_sz, out=self.tmp2)
        
        # Step 3: Division
        # Use 'where' to handle zero division safely without exceptions.
        # Exceptions are extremely slow (unwinding the stack).
        # This keeps the pipeline flowing even with bad data (safe defaults).
        # Note: The 'out' parameter ensures results go into our pre-allocated buffer.
        np.divide(self.tmp1, self.tmp2, out=self.result, where=self.tmp2 &gt; 0)
        
        # Important: In a real system, we might handle the 0-denominator case
        # by filling NaNs or previous values, depending on logic.
        
        return self.result</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Xb_D!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Xb_D!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png 424w, https://substackcdn.com/image/fetch/$s_!Xb_D!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png 848w, https://substackcdn.com/image/fetch/$s_!Xb_D!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png 1272w, https://substackcdn.com/image/fetch/$s_!Xb_D!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Xb_D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png" width="689" height="479" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/530296d7-9472-4b49-aae1-b64c7d867764_689x479.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:479,&quot;width&quot;:689,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:41475,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Xb_D!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png 424w, https://substackcdn.com/image/fetch/$s_!Xb_D!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png 848w, https://substackcdn.com/image/fetch/$s_!Xb_D!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png 1272w, https://substackcdn.com/image/fetch/$s_!Xb_D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F530296d7-9472-4b49-aae1-b64c7d867764_689x479.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This approach creates <em>zero </em>new array objects during execution. It is the only way to guarantee the GC stays asleep and that we maintain <em>temporal locality</em> (reusing the same physical RAM addresses for every tick).</p><h4>Queueing primitives</h4><p>Queues are not just plumbing; they define the latency distribution topology of your entire application.</p><p>Dynamic lists (<code>append</code>/<code>pop</code>) have <em>O</em>(1) <em>amortized</em> cost, but <em>O</em>(<em>N</em>) <em>worst-case</em> cost during resizing (when the underlying array doubles in size). In low latency environments, <em>amortized</em> is a dirty word. We cannot afford a worst-case resizing event during a market burst; that 5ms pause is when the market moves against us, transforming a profitable trade into an adverse selection loss. We require fixed-capacity ring buffers (circular queues).</p><ol><li><p><strong>Arithmetic optimization: </strong>Standard ring buffers use the module operator <code>%</code> to wrap indices. Division (and by extension, module) is computationally expensive. On modern x86 architectures, a <code>DIV</code> instruction can take 20-60 CPU cycles and disrupts the instruction pipeline. If we constrain the capacity <em>N</em> to be a power of two, we can replace module with a bitwise AND operation. This is a single-cycle operation, immune to branch misprediction penalties related to division checks. It allows the CPU to speculatively execute the next instructions without waiting for the arithmetic unit.</p></li><li><p><strong>Batch processing:</strong><br>In Python, function calls are expensive (~100-200ns overhead per call). Pushing items one by one into a ring buffer is inefficient; the loop overhead dominates the actual data movement. We need <em>bulk operations</em> (<code>push_many</code>) that use C-level memory copying (<code>np.copyto</code> or <code>memcpy</code>) to move chunks of data. This amortizes the Python overhead over hundreds of events.</p></li></ol><p>Let&#8217;s check it in the next implementation.</p><pre><code>class RingBuffer:
    __slots__ = ("buf", "cap", "mask", "head", "tail")

    def __init__(self, capacity_pow2, dtype=np.float64):
        self.cap = capacity_pow2
        # Verify power of two using bitwise check
        # (x &amp; (x-1)) == 0 is true only for powers of two.
        if (self.cap &amp; (self.cap - 1)) != 0:
            raise ValueError("Capacity must be power of two")
        self.mask = self.cap - 1
        self.buf = np.empty(self.cap, dtype=dtype)
        self.head = 0 # Write pointer (monotonic increasing, uint64)
        self.tail = 0 # Read pointer (monotonic increasing, uint64)

    def push(self, val):
        # Bitwise masking instead of modulo.
        # This is branchless and pipelined.
        idx = self.head &amp; self.mask
        self.buf[idx] = val
        self.head += 1
    
    def push_many(self, values):
        """
        Zero-loop bulk insertion. Handles the wrap-around logic
        using at most two copy operations (two memcpy calls).
        """
        n = len(values)
        if n == 0: return
        
        # Calculate write positions using bitmask
        start_idx = self.head &amp; self.mask
        end_idx = start_idx + n
        
        if end_idx &lt;= self.cap:
            # Case 1: No wrap-around. Single contiguous copy.
            self.buf[start_idx:end_idx] = values
        else:
            # Case 2: Wrap-around required. 
            # We hit the end of the buffer and must loop back to 0.
            # Split the copy: Part A (to end) + Part B (from start)
            first_chunk = self.cap - start_idx
            self.buf[start_idx:] = values[:first_chunk]
            self.buf[:n - first_chunk] = values[first_chunk:]
            
        self.head += n
        
    def get_last(self, lookback):
        # Retrieve item at head - 1 - lookback
        # This allows O(1) time-series access without copying history.
        # Works correctly with unsigned integer wrap-around logic.
        idx = (self.head - 1 - lookback) &amp; self.mask
        return self.buf[idx]</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zpm6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zpm6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png 424w, https://substackcdn.com/image/fetch/$s_!Zpm6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png 848w, https://substackcdn.com/image/fetch/$s_!Zpm6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png 1272w, https://substackcdn.com/image/fetch/$s_!Zpm6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zpm6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png" width="699" height="478" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:478,&quot;width&quot;:699,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:36557,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zpm6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png 424w, https://substackcdn.com/image/fetch/$s_!Zpm6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png 848w, https://substackcdn.com/image/fetch/$s_!Zpm6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png 1272w, https://substackcdn.com/image/fetch/$s_!Zpm6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23bea3dd-9315-467a-baa6-9eccbdecb9ac_699x478.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This structure provides <em>deterministic insertion time</em>, regardless of queue depth, and maximizes memory bandwidth utilization via vectorized copies.</p><h4>Zero-copy paths and multiprocess topologies</h4><p>Serialization is a tax on your latency budget.</p><p>Python&#8217;s Global Interpreter Lock (GIL) forces us to use <code>multiprocessing</code> for true parallelism (e.g., dedicating Core 1 to feed handling and Core 2 to strategy). However, passing Python objects between processes via <code>multiprocessing.Queue</code> is deceptive. Under the hood, it uses <em>pickling</em>.</p><p>The cost of pickling can be very high:</p><ol><li><p><strong>Introspection:</strong> Python must walk the object graph to determine types.</p></li><li><p><strong>Allocation:</strong> Bytes are allocated for the serialized stream.</p></li><li><p><strong>Copy:</strong> Data is copied into that stream.</p></li><li><p><strong>Syscall:</strong> Bytes are sent over a pipe/socket.</p></li><li><p><strong>Unpickling:</strong> The receiver allocates new objects and reconstructs the graph.</p></li></ol><p>This is slow, CPU-intensive, and generates massive GC pressure.</p><p>We must use <em>shared memory</em> buffers where the operating system maps the same physical RAM frame to the virtual address space of multiple processes. This allows us to treat Inter-Process Communication as a simple memory read.<br>On the other hand, to share the data is easy; to synchronize access without locks (which cause context switches) is the hard part. We typically use a lock-free topology:</p><ul><li><p><strong>Writer (feed handler):</strong> Updates the data array, then updates an atomic <code>head</code> counter.</p></li><li><p><strong>Reader (strategy):</strong> Polls the <code>head</code> counter. If <code>tail &lt; head</code>, it processes the data from <code>tail</code> to <code>head</code>, then updates its local <code>tail</code>.</p></li><li><p><strong>Memory barriers:</strong> In strict low-latency C++, we would insert memory fences. In Python, the GIL and OS-level operations often provide implicit ordering, but using <code>multiprocessing.Value</code> ensures atomicity.</p></li></ul><p>We use <code>multiprocessing.shared_memory</code> to create a raw byte buffer, then wrap it with a NumPy array. This allows Process A to write and Process B to read with <em>zero data copying.</em></p><pre><code>import multiprocessing
from multiprocessing import shared_memory
from multiprocessing import Value
import numpy as np
import ctypes

def create_shared_array(name, shape, dtype):
    """
    Allocates a block of Shared Memory and views it as a NumPy array.
    This memory persists until explicitly unlinked.
    """
    d = np.dtype(dtype)
    size = int(np.prod(shape)) * d.itemsize
    # Create shared memory block (shm_open + ftruncate + mmap)
    shm = shared_memory.SharedMemory(create=True, size=size, name=name)
    # Create numpy array backed by shared memory buffer
    return np.ndarray(shape, dtype=dtype, buffer=shm.buf)

def attach_shared_array(name, shape, dtype):
    """
    Attaches to an existing block of Shared Memory.
    No data is copied; we are mapping the existing pages.
    """
    shm = shared_memory.SharedMemory(name=name)
    return np.ndarray(shape, dtype=dtype, buffer=shm.buf)

class SharedRingBuffer:
    def __init__(self, name, capacity_pow2, dtype, is_writer=False):
        self.name = name
        self.cap = capacity_pow2
        self.mask = self.cap - 1
        
        # Shared Index Pointers (Atomic C-types)
        # In a real implementation, we store these in a separate small shared memory block
        # so both processes can see the 'head'.
        # For this example, we assume `head_ptr` is a shared Value passed in.
        
        if is_writer:
            self.array = create_shared_array(name, (self.cap,), dtype)
        else:
            self.array = attach_shared_array(name, (self.cap,), dtype)
            
    def write_updates(self, data, head_ptr):
        # Direct memory write to shared buffer
        # ... (Implementation similar to RingBuffer.push_many)
        # Update Atomic Counter
        head_ptr.value += len(data)</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!k1mA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!k1mA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png 424w, https://substackcdn.com/image/fetch/$s_!k1mA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png 848w, https://substackcdn.com/image/fetch/$s_!k1mA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png 1272w, https://substackcdn.com/image/fetch/$s_!k1mA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!k1mA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png" width="690" height="483" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:483,&quot;width&quot;:690,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:33417,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!k1mA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png 424w, https://substackcdn.com/image/fetch/$s_!k1mA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png 848w, https://substackcdn.com/image/fetch/$s_!k1mA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png 1272w, https://substackcdn.com/image/fetch/$s_!k1mA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75b770cc-bf5e-4c6f-96c3-8563a89811cf_690x483.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Combined with the <code>RingBuffer</code> logic, this creates a lock-free, zero-copy IPC mechanism. The Feed Handler writes bytes directly to RAM, and the Strategy reads those bytes. No serialization. No Pickling. No GC. The latency of passing a message is effectively the latency of a cache coherence update between CPU cores (~50-100ns).</p><p>Okay! Let&#8217;s see what this classes actually do!</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!njMj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!njMj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png 424w, https://substackcdn.com/image/fetch/$s_!njMj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png 848w, https://substackcdn.com/image/fetch/$s_!njMj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png 1272w, https://substackcdn.com/image/fetch/$s_!njMj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!njMj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png" width="1149" height="457" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:457,&quot;width&quot;:1149,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:51008,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/182763608?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!njMj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png 424w, https://substackcdn.com/image/fetch/$s_!njMj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png 848w, https://substackcdn.com/image/fetch/$s_!njMj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png 1272w, https://substackcdn.com/image/fetch/$s_!njMj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd431fb95-4d4c-47b2-bc5f-fd94ca0d862c_1149x457.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p><strong>Bitset vs bool array (N = 1,000,000):</strong></p><ol><li><p><strong>Latency:</strong> <code>bool array = 3.632 &#181;s</code> vs <code>bitset = 14.885 &#181;s</code> &#8594; the bitset is <strong>~4.1&#215; slower </strong>(<strong>~0.24&#215; </strong><em>speedup</em>).</p></li><li><p><strong>Memory:</strong> not printed here, but by design a bitset is typically <strong>~8&#215; </strong><em>smaller </em>than a <code>bool</code> mask (&#8776;1 bit/flag vs 1 byte/flag).</p></li></ol><p>This is the classic trade: <em>bitsets compress memory</em>, but they often <em>lose on latency </em>if your benchmark involves <em>extracting/setting individual bits</em> or doing non-bulk access patterns.</p><ol><li><p>If you need <em>fast elementwise masks in NumPy</em> &#8594; keep <code>bool</code> (it&#8217;s already highly optimized).</p></li><li><p>If you need<em> tiny memory + bulk bitwise algebra</em> &#8594; only then a bitset can win (operate on <code>uint64</code> blocks with AND/OR/XOR + popcount-style counting), <em>not</em> per-bit logic in Python.</p></li></ol></li></ol><ol start="2"><li><p><strong>SoA vs AoS (VWAP, N = 10,000):</strong></p><ol><li><p><strong>Latency:</strong> <code>list[dict] = 3435.451 &#181;s</code> vs <code>SoA = 14.511 &#181;s</code></p></li><li><p><strong>Speedup:</strong> <strong>236.7&#215;</strong> (this is the<em> real structural win</em>).</p></li></ol><p>You removed:</p><ol><li><p>Python object overhead (<code>dict</code> lookups, dynamic typing),</p></li><li><p>pointer chasing (bad cache locality),</p></li><li><p>scattered memory access,</p></li></ol><p>&#8230;and replaced it with <em>contiguous numeric arrays</em> that NumPy can process at <em>C speed. </em>For trading workloads (VWAP, filters, signals, rolling stats): <strong>SoA is the default</strong> unless you truly need heterogeneous records.</p></li><li><p><strong>Workspace vs Implicit allocation (N = 100,000):</strong></p><ol><li><p><strong>Latency:</strong> <code>implicit = 603.739 &#181;s</code> vs <code>workspace = 880.466 &#181;s</code></p></li><li><p><strong>Speedup:</strong> <strong>0.7&#215;</strong> &#8594; workspace is <strong>~1.46&#215; </strong><em>slower </em>(regression).</p></li></ol><p>This usually means the workspace path is paying extra overhead that dominates any allocation savings, e.g.:</p><ol><li><p>extra passes / copies (<code>copyto</code>, <code>astype</code>, unnecessary temporaries),</p></li><li><p>not using <code>out=</code> effectively (so you still allocate),</p></li><li><p>more Python-level steps than the implicit version,</p></li><li><p>poor memory layout / cache behavior in the workspace pipeline.</p></li></ol><p>Workspace only helps when it truly eliminates temporaries via <code>out=</code> and reduces passes. If your workspace version does <em>more work</em>, it will lose&#8212;even if it allocates less.</p></li><li><p><strong>Ring buffer (Bulk push N = 1000):</strong></p><ol><li><p><strong>Latency:</strong> <code>deque.extend = 53.146 &#181;s</code> vs <code>ring push = 0.904 &#181;s</code></p></li><li><p><strong>Speedup:</strong> <strong>58.8&#215;</strong> (<em>massive win</em>).</p></li></ol><p>That&#8217;s what you get when you move from:</p><ol><li><p>a Python-container approach (even <code>deque</code> still involves object-level mechanics)</p></li><li><p>to: a <em>contiguous buffer + index arithmetic </em>(predictable, cache-friendly).</p></li></ol><p>This is <em>exactly</em> the kind of structure you want for <em>tick buffers, sliding windows, micro-batching,</em> and <em>latest-N</em> state in execution systems.</p></li><li><p><strong>Shared memory vs pickling (N = 5000 floats):</strong></p><ol><li><p><strong>Latency:</strong> <code>pickle/queue = 11.407 &#181;s</code> vs <code>sharedmem = 3.413 &#181;s</code></p></li><li><p><strong>Speedup:</strong> <strong>3.3&#215;</strong> (<em>solid win</em>).</p></li></ol><p>Pickle/Queue pays:</p><ol><li><p>serialization/deserialization,</p></li><li><p>extra copies,</p></li><li><p>Python object overhead.</p></li></ol><p>Shared memory reduces this toward <em>zero-copy data movement</em> (you mostly pay synchronization + view creation).</p><p>If you&#8217;re moving numeric arrays between processes at low latency, <em>shared memory wins</em>, but you must keep synchronization minimal and memory layout stable.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KKRX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 424w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 848w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1272w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png" width="1456" height="271" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:271,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:381253,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!KKRX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 424w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 848w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1272w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Alright crew &#8212; strong session in the books. Until the next bell rings: keep your execution sharp, your sizing honest, and your process louder than your emotions. May volatility work <em>for</em> you, losses stay contained, and winners compound quietly in the background. &#129504;&#128200;</p><p>PS: <em><strong>Help me solve this dilemma!</strong></em></p><div class="poll-embed" data-attrs="{&quot;id&quot;:425032}" data-component-name="PollToDOM"></div><div><hr></div><p>This is an<strong> invitation-only access</strong> to our <strong>QUANT COMMUNITY</strong>, so we verify numbers to avoid spammers and scammers. Feel free to join or decline at any time. <em>Tap the WhatsApp icon below to join</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://chat.whatsapp.com/LsXSHJxfp9l7tOd7OO6PQL" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nbxE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 424w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 848w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1272w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png" width="1280" height="352" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:352,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:87001,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://chat.whatsapp.com/LsXSHJxfp9l7tOd7OO6PQL&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/181419543?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!nbxE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 424w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 848w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1272w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Appendix</strong></p><p>Full script</p><pre><code>import numpy as np
import time
import gc
import sys
import multiprocessing
from multiprocessing import shared_memory, Value, Queue
from collections import deque
import pickle


class Bitset64:
    __slots__ = ("n", "words")
    def __init__(self, n_bits: int):
        self.n = int(n_bits)
        self.words = np.zeros((self.n + 63) // 64, dtype=np.uint64)

    def set(self, i: int) -&gt; None:
        w = i &gt;&gt; 6
        b = i &amp; 63
        self.words[w] |= (np.uint64(1) &lt;&lt; np.uint64(b))
        
    def check_all_safe(self, mask_indices: np.ndarray) -&gt; bool:
        words_idx = mask_indices &gt;&gt; 6
        bits_idx = mask_indices &amp; 63
        values = (self.words[words_idx] &gt;&gt; bits_idx.astype(np.uint64)) &amp; 1
        return np.all(values)

class TickSoA:
    __slots__ = ("cap", "n", "ts", "px", "qty", "side")
    def __init__(self, capacity: int):
        self.cap = int(capacity)
        self.n = 0
        self.ts = np.empty(self.cap, dtype=np.uint64)
        self.px = np.empty(self.cap, dtype=np.float64)
        self.qty = np.empty(self.cap, dtype=np.float32)
        self.side = np.empty(self.cap, dtype=np.int8)

    def append_many(self, ts, px, qty, side):
        k = len(ts)
        i0 = self.n
        i1 = i0 + k
        if i1 &gt; self.cap: raise OverflowError("TickSoA capacity exceeded")
        np.copyto(self.ts[i0:i1], ts, casting="no")
        np.copyto(self.px[i0:i1], px, casting="no")
        np.copyto(self.qty[i0:i1], qty, casting="no")
        np.copyto(self.side[i0:i1], side, casting="no")
        self.n = i1
    
    def get_vwap(self, start_idx, end_idx):
        p = self.px[start_idx:end_idx]
        q = self.qty[start_idx:end_idx]
        return np.dot(p, q) / np.sum(q)

class Workspace:
    __slots__ = ("tmp1", "tmp2", "result")
    def __init__(self, shape):
        self.tmp1 = np.empty(shape, dtype=np.float64)
        self.tmp2 = np.empty(shape, dtype=np.float64)
        self.result = np.empty(shape, dtype=np.float64)

    def calc_weighted_mid(self, bid_px, bid_sz, ask_px, ask_sz):
        np.multiply(bid_px, ask_sz, out=self.tmp1)
        np.multiply(ask_px, bid_sz, out=self.tmp2)
        np.add(self.tmp1, self.tmp2, out=self.tmp1) 
        np.add(bid_sz, ask_sz, out=self.tmp2)
        np.divide(self.tmp1, self.tmp2, out=self.result, where=self.tmp2 &gt; 0)
        return self.result

class RingBuffer:
    __slots__ = ("buf", "cap", "mask", "head")
    def __init__(self, capacity_pow2, dtype=np.float64):
        self.cap = capacity_pow2
        if (self.cap &amp; (self.cap - 1)) != 0: raise ValueError("Power of 2 required")
        self.mask = self.cap - 1
        self.buf = np.empty(self.cap, dtype=dtype)
        self.head = 0

    def push_many(self, values):
        n = len(values)
        if n == 0: return
        start_idx = self.head &amp; self.mask
        end_idx = start_idx + n
        
        if end_idx &lt;= self.cap:
            self.buf[start_idx:end_idx] = values
        else:
            first = self.cap - start_idx
            self.buf[start_idx:] = values[:first]
            self.buf[:n - first] = values[first:]
        self.head += n

def create_shared_array(name, shape, dtype):
    d = np.dtype(dtype)
    size = int(np.prod(shape)) * d.itemsize
    try:
        shm = shared_memory.SharedMemory(create=True, size=size, name=name)
    except FileExistsError:
        shm = shared_memory.SharedMemory(name=name)
    return np.ndarray(shape, dtype=dtype, buffer=shm.buf), shm

class SharedRingBuffer:
    def __init__(self, name, capacity_pow2, dtype):
        self.name = name
        self.cap = capacity_pow2
        self.mask = self.cap - 1
        # In benchmarking we just own the creation
        self.array, self.shm = create_shared_array(name, (self.cap,), dtype)
            
    def write_updates(self, data, head_ptr):
        n = len(data)
        head = head_ptr.value
        start_idx = head &amp; self.mask
        end_idx = start_idx + n
        
        if end_idx &lt;= self.cap:
             self.array[start_idx:end_idx] = data
        else:
             first = self.cap - start_idx
             self.array[start_idx:] = data[:first]
             self.array[:n - first] = data[first:]
        head_ptr.value += n

    def cleanup(self):
        self.shm.close()
        self.shm.unlink()

# BENCHMARK ENGINE
def run_benchmark(name, loops, func, *args):
    # Warmup
    func(*args)
    gc.collect()
    gc.disable()
    start = time.perf_counter_ns()
    for _ in range(loops):
        func(*args)
    end = time.perf_counter_ns()
    gc.enable()
    avg_ns = (end - start) / loops
    print(f"[{name:&lt;30}] Avg Latency: {avg_ns/1000:.3f} us")
    return avg_ns

def test_bitset():
    print("\nBitset vs Boolean Array (N=1M) ---")
    N = 1_000_000
    indices = np.random.randint(0, N, 1000)
    bool_arr = np.random.choice([True, False], size=N)
    bitset = Bitset64(N)
    for i in indices: bitset.set(i)

    t1 = run_benchmark("Naive (Bool Array)", 1000, lambda: bool_arr[indices].all())
    t2 = run_benchmark("Optimized (Bitset)", 1000, lambda: bitset.check_all_safe(indices))
    print(f"Speedup: {t1/t2:.1f}x")

def test_soa():
    print("\nSoA vs AoS (N=10k VWAP) ---")
    N = 10_000
    ts = np.arange(N, dtype=np.uint64)
    px = np.random.rand(N)
    qty = np.random.rand(N).astype(np.float32)
    side = np.zeros(N, dtype=np.int8)
    
    aos = [{'px': px[i], 'qty': qty[i]} for i in range(N)]
    soa = TickSoA(N*2)
    soa.append_many(ts, px, qty, side)

    def naive_vwap():
        tot_pv = 0.0; tot_q = 0.0
        for r in aos:
            tot_pv += r['px'] * r['qty']
            tot_q += r['qty']
        return tot_pv/tot_q

    t1 = run_benchmark("Naive (List[Dict])", 200, naive_vwap)
    t2 = run_benchmark("Optimized (SoA)", 200, lambda: soa.get_vwap(0, N))
    print(f"Speedup: {t1/t2:.1f}x")

def test_allocation():
    print("\nWorkspace vs Implicit Alloc (N=100k) ---")
    N = 100_000
    a, b, c, d = [np.random.rand(N) for _ in range(4)]
    ws = Workspace(N)

    def naive_math():
        num = (a * b) + (c * d)
        den = (b + d)
        return np.divide(num, den, where=den&gt;0)

    t1 = run_benchmark("Naive (Implicit)", 500, naive_math)
    t2 = run_benchmark("Optimized (Workspace)", 500, lambda: ws.calc_weighted_mid(a, b, c, d))
    print(f"Speedup: {t1/t2:.1f}x")

def test_ringbuffer():
    print("\n Ring Buffer (Bulk Push N=1000) ---")
    # Scenario: Ingesting a batch of 1000 updates
    N_BATCH = 1000
    data = np.random.rand(N_BATCH)
    
    # Naive: Deque extend
    d = deque(maxlen=65536)
    
    # Optimized: Power-of-Two Ring
    rb = RingBuffer(65536)

    def naive_deque():
        d.extend(data)

    def optimized_ring():
        rb.push_many(data)

    t1 = run_benchmark("Naive (Deque extend)", 5000, naive_deque)
    t2 = run_benchmark("Optimized (Ring Push)", 5000, optimized_ring)
    print(f"Speedup: {t1/t2:.1f}x")

def test_ipc():
    print("\nShared Memory vs Pickling (N=5000 floats) ---")
    # Scenario: Sending 5000 floats to another process
    # We measure the COST TO SEND (Write latency)
    N_MSG = 5000
    data = np.random.rand(N_MSG)
    
    # Naive: Multiprocessing Queue
    # Note: We dump to pickle to simulate queue overhead without OS pipe noise
    # purely to measure CPU cost of serialization.
    # Real Q.put() is even slower due to locking/pipe.
    
    # Optimized: Shared Ring Buffer
    shm_rb = SharedRingBuffer("bench_ipc", 65536, np.float64)
    head_ptr = Value('i', 0)

    def naive_pickle():
        # Cost to serialize
        _ = pickle.dumps(data)

    def optimized_shm():
        shm_rb.write_updates(data, head_ptr)

    t1 = run_benchmark("Naive (Pickle/Queue)", 2000, naive_pickle)
    t2 = run_benchmark("Optimized (SharedMem)", 2000, optimized_shm)
    print(f"Speedup: {t1/t2:.1f}x")
    
    shm_rb.cleanup()

if __name__ == "__main__":
    print(f"Benchmarking Environment: Python {sys.version.split()[0]} | NumPy {np.__version__}")
    test_bitset()
    test_soa()
    test_allocation()
    test_ringbuffer()
    test_ipc()</code></pre>]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Switch-off: Conformal-RANSAC kill switch ]]></title><description><![CDATA[Conformal Prediction coupled with RANSAC to switch-off portfolios]]></description><link>https://www.quantbeckman.com/p/with-code-switch-off-conformal-ransac</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-switch-off-conformal-ransac</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Mon, 15 Dec 2025 13:52:12 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>The trend validation trap.</p></li><li><p>Risk and model limitations.</p></li><li><p>What are the obstacles to face when developing this kind of algorithms?</p></li><li><p>Theoretical foundation.</p><ol><li><p>Point 1: The linear necessity and the failure of OLS.</p></li><li><p>Point 2: The RANSAC algorithm construction.</p></li><li><p>Point 3: The split-Conformal Prediction framework.</p></li><li><p>Point 4: The strict positivity logic</p></li></ol></li><li><p>Operationalization.</p><ol><li><p>Engineering the Conformal-RANSAC.</p></li></ol></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read the newsletter index</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Introduction</h4><p>In systematic portfolio management, the question that matters isn&#8217;t whether a strategy has been profitable in a backtest or over a recent window&#8212;it&#8217;s whether the process generating that profit still deserves capital <em>right now</em>. Industry practice tends to compress that decision into a single, comforting statistic: fit a straight line to the equity curve and ask whether the estimated slope is positive with <em>enough</em> confidence. On paper, this is clean. In production, it is dangerously brittle. A PnL curve is not a textbook signal but an object shaped by microstructure effects, intermittent liquidity, and rare events that are not rare enough. Under those conditions, the usual regression tools don&#8217;t merely lose precision&#8212;they can invert the meaning of evidence, mistaking a single lucky outlier for a restored edge or treating a brief but violent drawdown as proof of structural decay.</p><p>This is the trend validation trap: we want a kill-switch that enforces a strict positivity constraint on performance, because capital should not sit inside a system that is not demonstrably compounding. Yet the standard method used to measure <em>trend</em> is built on squared-error minimization, which amplifies precisely the observations we should distrust most. The consequence is a conflict between operational necessity and statistical fragility. If we keep the rule but rely on OLS, we risk zombie strategies surviving on the back of one exceptional trade, and healthy strategies getting shut down due to a transient tail event or a bad print. If we abandon the rule, we drift back into discretionary tolerance bands and post-hoc rationalizations&#8212;exactly the failure mode a kill-switch is meant to prevent.</p><p>The framework developed here resolves that conflict by reframing the problem from &#8220;fit the data&#8221; to &#8220;validate the structure.&#8221; It is based on several papers that you can find here:</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!mH_-!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9bd27f9-6bfb-44a9-8b32-740318ffa2ce_432x613.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Conformalize Quantile Regression</div><div class="file-embed-details-h2">1.36MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/d434909a-589a-402c-a66b-5abb6d766da6.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/d434909a-589a-402c-a66b-5abb6d766da6.pdf"><span class="file-embed-button-text">Download</span></a></div></div><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!fTlV!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f76246-62df-4dc6-b1fa-4e26e1a132c9_432x613.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Adaptive Conformal Inference </div><div class="file-embed-details-h2">825KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/695c68ec-6c2c-45c7-99fa-be0dca2adb91.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/695c68ec-6c2c-45c7-99fa-be0dca2adb91.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>First, we replace the regression objective with a robustness principle: RANSAC searches for the dominant linear drift supported by the majority of observations and treats the tails as contamination rather than evidence. Second, we move from asymptotic confidence language to finite-sample guarantees by calibrating uncertainty with split conformal prediction, producing a dynamic lower bound that behaves like a probabilistic trailing stop. Finally, we weld these components into a strict logic gate: capital is allocated only if the robust drift is positive <em>and</em> current performance remains inside a statistically valid corridor around that drift. The result is not a prettier line through the equity curve; it is a geometric protocol for deciding when a strategy has earned the right to continue trading, and when the burden of proof has not been met.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!k4ob!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!k4ob!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!k4ob!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!k4ob!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!k4ob!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!k4ob!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1649773,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/181419543?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!k4ob!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!k4ob!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!k4ob!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!k4ob!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f4e4271-7a96-4372-84e3-11a2c94ecc34_1536x1024.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>The trend validation trap</h4><p>In quantitative portfolio management, the most fundamental question we ask of any active strategy is deceptively simple: </p><blockquote><p><em>Is it working?</em> </p></blockquote><p>Operationally, this translates to a statistical hypothesis test: </p><blockquote><p><em>Is the PnL trend strictly positive with high statistical confidence?</em></p></blockquote><p>The initial dilemma is the <em>trend validation trap</em>. In the industry, we typically answer this question using Ordinary Least Squares (OLS) regression on the equity curve. We fit a linear model </p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;y_t = \\beta t + \\alpha + \\epsilon_t&quot;,&quot;id&quot;:&quot;LPOSCECKRA&quot;}" data-component-name="LatexBlockToDOM"></div><p>and check if the slope coefficient &#946; is positive. However, OLS is notoriously fragile in the context of financial time series. It operates by minimizing the sum of squared residuals. This quadratic loss function grants disproportionate influence to outliers&#8212;observations that lie far from the mean trend.</p><p>A single large outlier&#8212;a <em>lucky</em> trade driven by an exogenous liquidity event or a data artifact&#8212;can act as a high-leverage point, skewing the regression slope &#946; significantly upwards. A strategy might exhibit a positive OLS slope solely due to one massive outlier, while bleeding capital on 95% of its trades (negative median PnL). Conversely, a healthy strategy might show a negative OLS slope during a short, sharp drawdown that is merely noise within a heavy-tailed distribution.</p><p>We face a critical conflict: we need a kill-switch that enforces a <strong>strict positivity constraint</strong> on the PnL trend, effectively demanding that the strategy demonstrates consistent alpha generation. Yet, we cannot rely on standard regression tools to measure that trend because they conflate variance with structural performance. The dilemma is how to distinguish between a strategy that is truly broken (structural decay of edge) and one that is merely suffering from heavy-tailed noise, without succumbing to the non-robustness of squared-error minimization.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><h4>Risk and model limitations</h4><p>To solve this, we introduce a robust approach: <em>Conformal Prediction coupled with RANSAC (Random Sample Consensus) Regression.</em></p><p>What is RANSAC?</p><blockquote><p>RANSAC fundamentally inverts the philosophy of regression. While OLS attempts to model the entire dataset by minimizing average error, RANSAC assumes the dataset is a mixture of two distinct generating processes: &#8220;inliers&#8221; (the true signal/trend) and &#8220;outliers&#8221; (noise that should be ignored). It does not fit the data; it finds the data that fits the model.</p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5SYR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5SYR!,w_424,c_limit,f_webp,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif 424w, https://substackcdn.com/image/fetch/$s_!5SYR!,w_848,c_limit,f_webp,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif 848w, https://substackcdn.com/image/fetch/$s_!5SYR!,w_1272,c_limit,f_webp,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif 1272w, https://substackcdn.com/image/fetch/$s_!5SYR!,w_1456,c_limit,f_webp,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!5SYR!,w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif" width="864" height="576" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:576,&quot;width&quot;:864,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5SYR!,w_424,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif 424w, https://substackcdn.com/image/fetch/$s_!5SYR!,w_848,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif 848w, https://substackcdn.com/image/fetch/$s_!5SYR!,w_1272,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif 1272w, https://substackcdn.com/image/fetch/$s_!5SYR!,w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb3032de-3590-445d-8d5f-067ec9de1a2d_864x576.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>It operates via an iterative <em>hypothesize-and-verify</em> loop:</p><ol><li><p><strong>Hypothesize:</strong> The algorithm randomly selects a minimal subset of data points (e.g., just two points) to analytically define a candidate linear model.</p></li><li><p><strong>Verify:</strong> It tests this candidate trend against the entire dataset. It counts how many points fall within a strict tolerance band &#177;&#948; of the candidate line. These points form the <em>Consensus Set.</em></p></li><li><p><strong>Optimize:</strong> It repeats this process hundreds or thousands of times, ultimately selecting the model that maximizes the cardinality of the Consensus Set (i.e., the line that explains the largest number of trades).</p></li></ol><p>In the context of a PnL curve, RANSAC effectively asks: <em>&#8220;Is there a consistent, positive slope that explains the majority of the trades, if we ignore the shock events?&#8221;</em> This makes it a lie detector for &#8220;fake alpha&#8221;&#8212;it rejects positive trends driven solely by outliers because those outliers will not form a large enough consensus set to validate the slope.</p><p>However, implementing this in production introduces specific operational and statistical risks:</p><ol><li><p><strong>The inlier bias and regime aliasing</strong><br>RANSAC works by finding the subset of data that fits a linear model best. In a trading context, there is a risk that the algorithm identifies a <em>healthy</em> subset of trades as inliers and dismisses the losses as outliers. This is<em> regime aliasing</em>. If a strategy is losing money consistently but has a few winning streaks, RANSAC might latch onto the winning streaks as the <em>true</em> trend and ignore the catastrophic losses as noise, provided the winning streaks are linear and numerous enough. The definition of the <code>residual_threshold</code> becomes the arbiter of reality; set it too tight, and the model overfits to noise; set it too loose, and it accepts broken trends.</p></li><li><p><strong>The conformal coverage gap</strong><br>We use <em>split Conformal Prediction </em>to build a dynamic lower bound (a trailing stop based on probability). We calibrate a quantile <em>q</em> such that <em>P</em>(<em>Y</em><sub>new</sub>&#8712;&#264;) &#8805; 1-&#945;. This provides finite-sample validity, unlike asymptotic confidence intervals. However, the risk lies in the <em>exchangeability assumption</em>. Conformal prediction guarantees coverage only if the calibration residuals are exchangeable with the test residuals. In finance, volatility clustering violates this. A kill-switch calibrated during a low-volatility regime might fail to cover the residuals during a volatility explosion, triggering a false kill signal just when the strategy needs <em>breathing room.</em> The calibration window must be representative of the current volatility regime, or the coverage guarantee collapses.</p></li><li><p><strong>The lope-level conflict</strong><br>Our proposed kill-switch logic uses a dual condition that establishes a hierarchy of failure:</p><ol><li><p><strong>Slope condition:</strong> The robust trend must be positive (&#946;<sub>RANSAC</sub>&gt; 0). This ensures the strategy is generating alpha directionally.</p></li><li><p><strong>Level condition:</strong> The current PnL must be above the conformal lower bound (PnL<sub>t</sub> &gt; &#375;<sub>t</sub> - q). This ensures the strategy has not realized a drawdown that is statistically improbable given its valid trend.</p></li></ol><p>This creates a conflict in <em>turnaround</em> scenarios. A strategy can have a positive slope (recovering vigorously) but still be below the lower bound (deep in a drawdown hole). Conversely, it can be above the lower bound (shallow drawdown) but have a negative slope (beginning to rot). The risk involves defining the operational precedence: do we kill a strategy that is recovering but statistically <em>too low</em>, or do we keep a strategy that is high but statistically <em>rotting</em>? The model defaults to <em>kill if either fails,</em> which is a conservative, capital-preservation stance.</p></li></ol><p>If you want to go deeper into Conformal Prediction, check out this <em>Quant Lecture</em>:</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;1167e6d8-f0e9-4725-87ae-2b09d52086b7&quot;,&quot;caption&quot;:&quot;Why parametric assumptions blind your models&#8212;and how distribution-free methods keep you adaptive&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;[Quant Lecture] Distribution Free&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:311582722,&quot;name&quot;:&quot;Quant Beckman&quot;,&quot;bio&quot;:&quot;A quant research lab in newsletter form. Turning mathematics into automated trading systems and systematic portfolios. &quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/29f90c76-3fd2-4648-8b68-02caa8de3e77_800x800.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:100}],&quot;post_date&quot;:&quot;2025-04-18T14:29:10.969Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png&quot;,&quot;cover_image_alt&quot;:null,&quot;cover_image_crop&quot;:null,&quot;canonical_url&quot;:&quot;https://www.quantbeckman.com/p/lecture-6-probability-for-algorithmic&quot;,&quot;section_name&quot;:&quot;Quant Lectures&quot;,&quot;video_upload_id&quot;:null,&quot;id&quot;:161610600,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:4,&quot;comment_count&quot;:2,&quot;publication_id&quot;:3833331,&quot;publication_name&quot;:&quot;Trading the Breaking&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>The core risk crystallizes during a <em>false Dawn.</em> Imagine a Momentum strategy that has suffered a significant 10% drawdown over two months. Suddenly, it prints a series of highly profitable trades due to a short-term volatility spike or a favorable macro announcement.</p><p>An OLS regression would immediately react to these large positive values, flipping the slope &#946; from negative to positive, signaling &#8220;<em>all clear</em>.&#8221; A trader looking at the raw PnL would see a sharp uptick and feel relief. However, the RANSAC algorithm, robust to outliers, might reject these new profits as anomalies if they don&#8217;t fit the dominant (negative) trend established by the majority of trades in the window. To RANSAC, this is just high-magnitude noise, not a structural return to form.</p><p>Simultaneously, the Conformal interval, widened by the recent volatility of the drawdown, might still position the current PnL below the critical threshold defined by the calibration set. The pivotal event is this disagreement: The trader sees profits; the  model sees noise. The decision to halt trading here is painful but necessary if we adhere to the philosophy that <em>only robust trends constitute valid alpha</em>. We need a mechanism that enforces this by rejecting the false dawn until the <em>structure</em> of the PnL proves it has healed, preventing re-entry into a strategy that is merely thrashing before failing completely.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6qN-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6qN-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png 424w, https://substackcdn.com/image/fetch/$s_!6qN-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png 848w, https://substackcdn.com/image/fetch/$s_!6qN-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png 1272w, https://substackcdn.com/image/fetch/$s_!6qN-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!6qN-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png" width="989" height="690" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:690,&quot;width&quot;:989,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6qN-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png 424w, https://substackcdn.com/image/fetch/$s_!6qN-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png 848w, https://substackcdn.com/image/fetch/$s_!6qN-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png 1272w, https://substackcdn.com/image/fetch/$s_!6qN-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22621367-0530-412e-8c1c-4d4ad55b62b7_989x690.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>What are the obstacles to face when developing this kind of algorithms?</h4><p>The challenge is to construct a <em>robust envelope</em> around our PnL. We are moving away from simple <em>lines in the sand</em> (e.g., &#8220;stop at -$10k&#8221; or &#8220;stop at -15% MDD&#8221;) towards a dynamic, probabilistic tunnel that adapts to the strategy&#8217;s behavior.</p><p>We treat the PnL curve not as a deterministic financial accounting record, but as a noisy signal emitted by a stochastic physical process. Our goal is to extract the trajectory of the <em>true</em> process (via RANSAC) and bound its uncertainty (via Conformal Prediction). This shifts the burden of proof: </p><blockquote><p>The strategy is assumed to be broken (signal <code>False</code>) unless it can prove it has a positive robust slope <em>and</em> is performing within its probabilistic bounds. The envelope acts as a dynamic hypothesis test performed at every time step, validating both the direction (slope) and the magnitude (level) of the performance.</p></blockquote><p>In this terms there are 3 main obstacles we need to overcome:</p><ol><li><p><strong>The RANSAC stochasticity:</strong><br>RANSAC is intrinsically non-deterministic. It relies on random sub-sampling to find the best fit. In a production environment, running the same check twice on the same data could theoretically yield different results (kill vs. keep) if the <code>max_trials</code> parameter is too low or the data is highly ambiguous (low signal-to-noise ratio). We face the obstacle of stabilizing a randomized algorithm for a deterministic decision process. This requires setting of the random seed and ensuring <code>max_trials</code> is sufficient to reach a 99.99% probability of finding the optimal consensus set.</p></li><li><p><strong>The calibration window dilemma:</strong><br>How much history do we feed the model? The length of the calibration window determines the <em>memory</em> of the volatility estimate. If we use <code>min_history=50</code> trades, the conformal bounds are jumpy and loose, reacting wildly to recent volatility. If we use <code>min_history=500</code>, the bounds are tight but the model has massive inertia&#8212;it will be slow to react to a sudden strategy decay. Balancing the <em>responsiveness</em> of the RANSAC fit with the <em>stability</em> of the Conformal calibration is the central tuning challenge. We must find a window size that captures the relevant regime without introducing excessive lag.</p></li><li><p><strong>The strict positivity constraint:</strong><br>The logic requires <code>line_positive = (slope &gt; 0) AND (trend_prediction &gt; 0)</code>. This is a harsh constraint. A flat strategy (&#946;&#8776;0) is treated the same as a losing strategy. In a zero-interest-rate environment, holding a flat strategy might be acceptable. In a high-opportunity-cost environment, it is unacceptable. The obstacle is cultural: convincing stakeholders that a <em>flat</em> strategy is a <em>broken</em> strategy because it consumes capital, risk budget, and operational bandwidth without contributing to the robust positive trend. If you aren&#8217;t trending up robustly, you are out.</p></li></ol><h4>Theoretical foundation</h4><p>To establish the necessity of the proposed framework, we must first deconstruct the implicit geometric assumptions embedded within traditional alpha validation. The <em>structure</em> of a profitable strategy is often simplified into a linear drift term &#946;, estimated via squared-error minimization; yet, this simplification collapses under the heavy-tailed financial time series that distort the true trajectory.</p><p><strong>Point 1: The linear necessity and the failure of OLS</strong></p><p>We begin by re-evaluating the geometry of alpha. In its simplest, most reductive form, alpha manifests as a positive deterministic drift on the stochastic equity curve. We model the PnL <em>y<sub>t</sub></em> at time index <em>t</em> as a linear process corrupted by noise:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;y_t = \\beta t + \\alpha + \\epsilon_t&quot;,&quot;id&quot;:&quot;GNNOSKIIAN&quot;}" data-component-name="LatexBlockToDOM"></div><p>Here, &#946; represents the <em>edge</em> (dollars per unit time), and &#949;<em><sub>t</sub></em> represents the variance of returns. Standard industry practice relies on Ordinary Least Squares (OLS) regression to estimate this edge. The OLS framework is seductive because it relies on the Gauss-Markov theorem, which guarantees that the OLS estimator is the <em>Best Linear Unbiased Estimator</em>&#8212;but only under a strict set of assumptions: </p><blockquote><p>That the noise terms &#949;<em><sub>t</sub></em> are independent, identically distributed, and drawn from a Gaussian distribution <em>N</em>(0, &#963;<sup>2</sup>) with constant variance.</p></blockquote><p>Under these idealized conditions, the optimal estimator for &#946; is found by minimizing the sum of squared residuals (<em>L</em><sub>2</sub> norm):</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\hat{\\beta}_{OLS} = \\arg \\min_{\\beta} \\sum_{t=1}^N (y_t - (\\beta t + \\alpha))^2&quot;,&quot;id&quot;:&quot;PNTRBUDQGM&quot;}" data-component-name="LatexBlockToDOM"></div><p>However, financial markets are hostile environments that routinely violate every single Gauss-Markov assumption. </p><p>The fatal flaw of OLS lies in its cost function: <em>the quadratic penalty</em><strong> </strong>(<em>x</em><sup>2</sup>). By squaring the residuals, OLS grants disproportionate, exponential influence to outliers. Outside of the markets, large outliers are exponentially rare (3&#963; events happen 0.3% of the time). In financial markets, outliers and spikes happen in a regular basis. When OLS encounters those outliers, it treats them as important points to consider in the model.<br>This sensitivity is formalized by the concept of the <em>breakdown point</em> in robust statistics. The breakdown point is the smallest fraction of contamination (arbitrarily bad data) that can cause the estimator to produce an arbitrarily wrong result.</p><ul><li><p><strong>The mean </strong>(and OLS)<strong>:</strong> Has a breakdown point of 0%. As <em>N </em>&#8614; &#8734;, the fraction 1/<em>N</em> &#8614; 0. A single leverage point&#8212;a data artifact or a liquidity gap&#8212;can pull the regression line infinitely far from the true trend of the bulk data.</p></li><li><p><strong>The mechanism:</strong> To minimize the total squared error, the regression line mechanically rotates or tilts toward the outlier. It sacrifices the fit on 99% of the normal data points just to reduce the massive squared residual of that one outlier.</p></li></ul><p>Operationally, this mathematical pitfall creates two distinct, capital-destroying failure modes in a kill-switch system:</p><ol><li><p><strong>The lucky survivor </strong>(aka the zombie strategy):<br>Consider a strategy that has structurally lost its edge. The <em>true</em> drift &#946; is negative; it is slowly bleeding capital. However, during a market dislocation, the strategy executes a trade into a liquidity vacuum or benefits from a data error, recording a massive, phantom profit (a positive outlier).</p><ul><li><p><em>The OLS reaction:</em> The regression line, anchored by this positive outlier, tilts aggressively upward. The calculated slope flips from negative to positive.</p></li><li><p><em>The consequence:</em> The kill-switch fails to trigger. We interpret the noise as signal. We continue allocating capital to a decaying zombie strategy, paying execution costs and opportunity costs, blinded by a single lucky event that masks the structural rot.</p></li></ul></li><li><p><strong>The bad tick kill </strong>(aka the flash crash false alarm):<br>Consider a robust, compounding strategy with a healthy positive drift. Suddenly, a microstructure glitch or a <em>fat finger</em> error causes a momentary price dislocation. The strategy is marked-to-market at a massive loss for a single timestamp, creating a deep, artificial drawdown spike (a negative outlier) before snapping back.</p><ul><li><p><em>The OLS reaction:</em> To accommodate this massive squared error, the regression line tilts downward, potentially driving below zero.</p></li><li><p><em>The consequence:</em> The kill-switch triggers falsely. We are forced to liquidate a winning strategy. When the price snaps back later, we are left flat, having realized a loss that was merely a data artifact. Re-entering the position incurs spread costs and slippage, permanently degrading the strategy&#8217;s Sharpe ratio.</p></li></ul></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!peI_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!peI_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png 424w, https://substackcdn.com/image/fetch/$s_!peI_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png 848w, https://substackcdn.com/image/fetch/$s_!peI_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png 1272w, https://substackcdn.com/image/fetch/$s_!peI_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!peI_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png" width="1456" height="860" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:860,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:67818,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/181419543?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!peI_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png 424w, https://substackcdn.com/image/fetch/$s_!peI_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png 848w, https://substackcdn.com/image/fetch/$s_!peI_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png 1272w, https://substackcdn.com/image/fetch/$s_!peI_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9ee1467-1733-4569-bccd-0f0e9202a4a5_1652x976.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We require a method that is invariant to these eccentricities&#8212;a robust estimator with a high breakdown point that ignores the tails to focus on the structure.</p><p><strong>Point 2: The RANSAC algorithm construction</strong></p><p>We adopt the RANSAC (Random Sample Consensus) algorithm. RANSAC fundamentally transforms the regression problem from an <em>optimization</em> task (minimizing global error) into a <em>search</em> task (maximizing local consensus). It operates on the assumption that the data consists of <em>inliers</em> (points that can be explained by a model) and <em>outliers</em> (points that cannot).</p><p>Instead of trying to fit all points simultaneously, RANSAC iterates through a <em>hypothesize-and-verify</em> loop:</p><ol><li><p><strong>Hypothesize </strong>(<em>minimal sampling</em>): The algorithm randomly selects the smallest possible subset of data points required to analytically determine the model parameters. For a linear model in 2D space (time vs. PnL), this is exactly 2 points. These two points define a candidate line <em>L<sub>k</sub></em>.</p></li><li><p><strong>Verify </strong>(<em>consensus building</em>): The algorithm tests this candidate line <em>L<sub>k</sub></em> against the <em>entire</em> dataset. It computes the residuals <em>r<sub>i</sub></em> for all data points <em>i</em>=1 &#8230; <em>N</em>. A point is classified as an inlier if its residual falls within a strict tolerance band &#948; (the <code>residual_threshold</code>):</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\mathbb{I}_{inlier}(i) = \\begin{cases} 1 &amp; \\text{if } |y_i - \\hat{y}_i| < \\delta \\\\ 0 &amp; \\text{text{otherwise}} \\end{cases}&quot;,&quot;id&quot;:&quot;BFKIJWDWDV&quot;}" data-component-name="LatexBlockToDOM"></div><p>Crucially, in our implementation, &#948; is not arbitrary. It is derived robustly using the Median Absolute Deviation of the residuals, ensuring the threshold scales dynamically with the strategy&#8217;s baseline volatility.</p></li><li><p><strong>Score and update:</strong> The score of the model is simply the cardinality of the consensus set (the count of inliers).</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\text{Score}(L_k) = \\sum_{i=1}^N \\mathbb{I}_{inlier}(i)&quot;,&quot;id&quot;:&quot;HQPGWWXCZX&quot;}" data-component-name="LatexBlockToDOM"></div><p>We store the model with the highest score found so far.</p></li><li><p><strong>Optimize </strong>(<em>refinement</em>): After iterating for <em>K</em> trials, we take the best consensus set found (e.g., 90% of the data) and re-run a standard regression <em>only on those inliers</em>. This polishes the fit, removing the jitter introduced by the minimal sampling in step 1.</p></li></ol><p>The number of iterations <em>K</em> (<code>max_trials</code>) is determined probabilistically. If <em>w</em> is the probability that any given data point is an inlier, and we want a probability <em>p</em> (e.g., 99%) that at least one of our random samples consists entirely of inliers (yielding a &#8220;clean&#8221; model), we need:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;K \\geq \\frac{\\log(1-p)}{\\log(1-w^n)}&quot;,&quot;id&quot;:&quot;ZUBTNLPNCV&quot;}" data-component-name="LatexBlockToDOM"></div><p>where <em>n</em>=2 for a line. Even with substantial contamination (e.g., 20% outliers), RANSAC converges rapidly to the true structure of the PnL, identifying the steady accumulation of alpha while ignoring the shock events that confuse OLS.Let&#8217;s implement this class:</p><pre><code># RANSAC linear regression
@dataclass
class RANSACLinearRegressor:
    &#8220;&#8221;&#8220;Simple RANSAC linear regressor implemented with NumPy only.

    Parameters
    ----------
    max_trials : int
        Maximum number of RANSAC iterations.
    min_samples : int or None
        Minimum number of samples used to estimate a candidate model at each trial.
        If None, it defaults to n_features + 1.
    residual_threshold : float or None
        Threshold for classifying inliers. If None, it is estimated robustly.
    stop_inlier_ratio : float
        Early stopping if the best model reaches this inlier ratio.
    random_state : int or None
        Seed for the random number generator.

    Attributes (after fitting)
    --------------------------
    coef_ : np.ndarray, shape (n_features,)
        Linear coefficients of the robust model.
    intercept_ : float
        Intercept term of the robust model.
    inlier_mask_ : np.ndarray of bool, shape (n_samples,)
        Boolean mask indicating which points are inliers.
    &#8220;&#8221;&#8220;

    max_trials: int = 100
    min_samples: Optional[int] = None
    residual_threshold: Optional[float] = None
    stop_inlier_ratio: float = 0.9
    random_state: Optional[int] = None

    def fit(self, X: np.ndarray, y: np.ndarray) -&gt; &#8220;RANSACLinearRegressor&#8221;:
        X = ensure_2d(X)
        y = ensure_1d(y)

        n_samples, n_features = X.shape
        if n_samples != y.shape[0]:
            raise ValueError(&#8221;X and y must have the same number of samples.&#8221;)

        if self.min_samples is None:
            min_samples = n_features + 1
        else:
            min_samples = int(self.min_samples)

        if min_samples &lt; n_features + 1:
            raise ValueError(
                &#8220;min_samples must be at least n_features + 1 for linear regression.&#8221;
            )
        if min_samples &gt; n_samples:
            raise ValueError(
                &#8220;min_samples cannot be greater than the total number of samples.&#8221;
            )

        rng = np.random.RandomState(self.random_state)

        # If residual_threshold is not set, estimate it robustly from a global LS fit.
        if self.residual_threshold is None:
            coef_ls, intercept_ls = linear_fit_with_intercept(X, y)
            y_pred_ls = X.dot(coef_ls) + intercept_ls
            residuals_ls = y - y_pred_ls
            median = np.median(residuals_ls)
            mad = np.median(np.abs(residuals_ls - median))
            # 1.4826 makes MAD comparable to sigma under normality.
            scale = 1.4826 * mad if mad &gt; 0 else np.std(residuals_ls)
            if scale &lt;= 0.0 or not np.isfinite(scale):
                # Fallback to a small positive scale to avoid issues.
                scale = max(1.0, np.std(y) * 0.1)
            residual_threshold = 2.5 * scale
        else:
            residual_threshold = float(self.residual_threshold)

        best_inlier_mask = None
        best_inlier_count = 0
        best_coef = None
        best_intercept = None

        for _ in range(self.max_trials):
            # Draw a random minimal subset
            subset_idx = rng.choice(n_samples, min_samples, replace=False)
            X_sub = X[subset_idx]
            y_sub = y[subset_idx]

            # Fit candidate model
            try:
                coef_cand, intercept_cand = linear_fit_with_intercept(X_sub, y_sub)
            except np.linalg.LinAlgError:
                # Degenerate subset; skip this trial.
                continue

            # Compute residuals on all points
            y_pred_all = X.dot(coef_cand) + intercept_cand
            residuals = np.abs(y - y_pred_all)

            inlier_mask = residuals &lt;= residual_threshold
            inlier_count = int(inlier_mask.sum())

            # Update best solution
            if inlier_count &gt; best_inlier_count:
                X_inliers = X[inlier_mask]
                y_inliers = y[inlier_mask]

                try:
                    coef_refined, intercept_refined = linear_fit_with_intercept(
                        X_inliers, y_inliers
                    )
                except np.linalg.LinAlgError:
                    coef_refined, intercept_refined = coef_cand, intercept_cand

                best_inlier_mask = inlier_mask
                best_inlier_count = inlier_count
                best_coef = coef_refined
                best_intercept = intercept_refined

                if best_inlier_count / n_samples &gt;= self.stop_inlier_ratio:
                    break

        # Fallback: no robust solution, use plain least squares
        if best_inlier_mask is None:
            best_coef, best_intercept = linear_fit_with_intercept(X, y)
            best_inlier_mask = np.ones(n_samples, dtype=bool)

        self.coef_ = best_coef
        self.intercept_ = float(best_intercept)
        self.inlier_mask_ = best_inlier_mask
        return self

    def predict(self, X: np.ndarray) -&gt; np.ndarray:
        if not hasattr(self, &#8220;coef_&#8221;):
            raise RuntimeError(&#8221;The RANSAC model is not fitted yet.&#8221;)
        X = ensure_2d(X)
        return X.dot(self.coef_) + self.intercept_</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lF1e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" width="1456" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1158490,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3&quot;,&quot;text&quot;:&quot;Send me a copy&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3"><span>Send me a copy</span></a></p><p><strong>Point 4: The strict positivity logic</strong></p><p>The core innovation in our <code>ConformalRansacPnL</code> class is the integration of these two concepts into a <em>strict positivity kill switch</em>. We enforce a binary logic gate that is far more rigorous than a simple trailing stop or OLS check.</p><p>The logic is formally defined as:</p><pre><code><code>line_positive = (slope &gt; 0.0) and all_preds_positive

current_pnl = float(pnl[test_idx])
current_lower = float(lower_all[test_idx])

# Raw &#8220;above lower bound&#8221; info for every point
above_lower_raw = pnl &gt; lower_all

# Final signal series:
# - if line not positive =&gt; everything False
# - else =&gt; True where PnL &gt; lower bound
if not line_positive:
    signal_series = np.zeros_like(pnl, dtype=bool)
else:
    signal_series = above_lower_raw.copy()</code></code></pre><p>This implies a hierarchical, two-stage filter:</p><ol><li><p><strong>Directional validation </strong>(<em>the structure&#8594;zombie trap</em>):<br>First, the strategy must have a positive drift (&#946;<em><sub>robust</sub></em> &gt; 0). But we go further: <code>all_trend_predictions &gt; 0</code>. This condition mandates that the robust trend line itself must reside entirely in positive PnL territory over the analysis window.</p></li><li><p><strong>Distributional validation</strong> (<em>the state&#8594;volatility drift</em>):<br>Even if the trend is positive, the current performance state must be valid. If <code>current_PnL &lt; lower_conformal_bound</code>, the signal is <code>False</code>.</p></li></ol><p>This dual condition ensures we only allocate capital to strategies that are both <em>structurally sound</em> (positive RANSAC consensus) and <em>statistically controlled</em> (within Conformal bounds).</p><h4>Operationalization </h4><p>Implementing the theoretical RANSAC model in daily trading loop requires careful engineering. We cannot simply import heavy external libraries like <code>scikit-learn</code> inside a latency-sensitive block; the overhead of object instantiation, dependency management, and unnecessary abstraction layers is prohibitive. Furthermore, <em>black box</em> implementations often obscure the critical fallback logic required when data becomes degenerate.</p><p><strong>Engineering the Conformal-RANSAC</strong></p>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-switch-off-conformal-ransac">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Switch-off: Robust changepoint protocol ]]></title><description><![CDATA[A robust changepoint-based switch-off protocol for algorithmic trading]]></description><link>https://www.quantbeckman.com/p/with-code-switch-off-robust-changepoint</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-switch-off-robust-changepoint</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Mon, 01 Dec 2025 12:33:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>The drift in algorithm performance.</p></li><li><p>Taxonomy of performance drift.</p></li><li><p>Model risks and limitations.</p></li><li><p>Robust estimation and the biweight loss.</p><ol><li><p>M-estimators and the geometry of influence.</p></li><li><p>Tukey&#8217;s Biweight (bisquare) loss.</p></li><li><p>Iteratively Reweighted Least Squares (IRLS).</p></li></ol></li><li><p>Regime segmentation via penalized dynamic programming.</p></li><li><p>The PELT Algorithm (Pruned Exact Linear Time).</p><ol><li><p>The pruning inequality.</p></li><li><p>Phase 1: Cost Matrix precomputation.</p></li><li><p>Phase 2: The PELT solver.</p></li></ol></li><li><p>Reconstruction and visualizing the drift.</p><ol><li><p>Logic A: Relative decay (alpha erosion).</p></li><li><p>Logic B: Absolute failure (negative expectancy).</p></li></ol></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read the newsletter index</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Introduction</h4><p>Every live strategy is a slowly degrading asset. Not because <em>the model was bad,</em> but because the market keeps changing. </p><p>When we deploy an algorithm, we behave as if the distribution that generated its backtest will hang around just long enough for us to harvest the edge. We roll windows, we recalibrate, we monitor drawdowns and Sharpe ratios&#8212;all under an implicit truce: whatever drives our PnL won&#8217;t change too fast. In practice, that truce is routinely violated.</p><p>Edges are conditional stories about the world. Microstructure, liquidity, crowding, policy, fees, latency, and even hardware shape the conditional law that turns a set of signals into returns. Those conditions drift. Sometimes slowly, as competitors discover the same inefficiency and squeeze the juice out of it. Sometimes brutally, as a change in market structure or regulation flips the sign of the trade overnight. From the PnL point of view, both scenarios look like one thing: a strategy that once worked&#8230; now doesn&#8217;t.</p><p>The core operational problem is simple to state and hard to solve:</p><blockquote><p><strong>When does a drawdown stop being noise and become a structural break?</strong></p></blockquote><p>Look at it from the supervisor&#8217;s perspective. You observe a noisy sequence of PnL: good days, bad days, occasional 5&#8211;10&#963; events courtesy of fat tails and slippage. Somewhere inside that, the expected value of the strategy can jump from strongly positive to mildly positive, from mildly positive to mediocrity, or from mediocrity to outright negative. Your job is to distinguish those regime changes from random turbulence&#8212;fast enough to avoid ruin, slow enough to avoid killing healthy engines.</p><p>Standard risk tools are not built for this. Sharpe ratio, volatility targeting, VaR and its cousins are all about dispersion. They tell you <em>how wild</em> the ride is, not <em>whether the underlying game has changed</em>. In fat-tailed, heteroscedastic PnL, a violent loss is weak evidence of anything. A single execution error or fat-finger trade can destroy your mean while telling you nothing about the underlying alpha. Naive responses&#8212;Z-score kill switches, rolling-window Sharpe cutoffs, 3&#963; <em>and you&#8217;re out</em>&#8212;confuse outliers with structural decay.</p><p>At the same time, humans are terrible at making this call in real time. Six months of research sunk into a model, a couple of decent years in production, a rough patch that <em>might</em> be noise or <em>might</em> be decay. The quant knows the theory, the PM sees the drawdown, the committee wants <em>just a bit more evidence</em>. So the strategy stays on. One more week. One more month. One more quarter. By the time everyone agrees it&#8217;s dead, the damage is already done.</p><p>This article is about replacing that improvisation with a protocol. The implementation was inspired by this paper</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!Fe8X!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f0b6ad7-040b-4278-80c4-4c5e7a8ba252_756x1015.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Changepoint Detection in the Presence of Outliers</div><div class="file-embed-details-h2">1.08MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/731e28ef-e376-4949-8389-aca07fc70f9f.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/731e28ef-e376-4949-8389-aca07fc70f9f.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>Although several modifications were made for our purpose (switch-off tool). Therefore, it is not exactly the same nor does it serve to detect the same thing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jShe!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jShe!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!jShe!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!jShe!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!jShe!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!jShe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1618796,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/180103121?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jShe!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!jShe!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!jShe!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!jShe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f32df81-b1e5-4952-bedc-85d99525729e_1536x1024.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>The drift in algorithm performance</h4><p>A pervasive working assumption in quantitative trading is not full stationarity, but <em>some degree of structural persistence</em>. When we calibrate a model, we estimate parameters &#952; on a historical sample <em>under the practical assumption that the conditional law driving decisions will not change too fast:</em></p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P{t+1}(X, y) \\approx P_{t}(X, y) \\approx \\widehat P_{\\text{sample}}(X, y)&quot;,&quot;id&quot;:&quot;XOYCFQOAQJ&quot;}" data-component-name="LatexBlockToDOM"></div><p>at least in the parts of the state space that matter for our strategy. Even when we use rolling windows, online learning, or adaptive rebalancing, we are implicitly assuming that <em>concept drift is slower than our learning and turnover cycle</em>&#8212;that we can track the moving target before we hit a ruin boundary.</p><p>In the markets this is, at best, an approximation and often a dangerous one. The return-generating mechanism is subject to <em>regime shifts, structural breaks, policy interventions, technological change, and competition for the same signals</em>. Alpha is not literally defined as a disequilibrium, but operationally it <em>behaves</em> like one: </p><blockquote><p>It is a predictable deviation from what competing capital currently prices in. In a frictionless, unconstrained market, any such predictable excess return is self-destructive: as capital scales into the trade, the pattern is arbitraged away, its Sharpe collapses, and its behaviour migrates towards noise.</p></blockquote><p>This means that <strong>alpha decay is not a bug in the research process but the default equilibrium outcome.</strong> A deployed strategy is engaged in a race between three clocks:</p><ol><li><p>The <strong>decay time</strong> of the inefficiency as others discover and crowd it.</p></li><li><p>The <strong>adaptation time</strong> of our models and infrastructure.</p></li><li><p>The <strong>risk horizon</strong> over which drawdowns become existential.</p></li></ol><p>Stationarity, in this light, is not a realistic global assumption but a <em>local, temporary truce</em> between these clocks. Robust design does not assume that <em>P</em>(<em>X</em>, <em>y</em>) is stationary; it assumes that <em>edges have a finite half-life,</em> and builds everything&#8212;the research loop, deployment, and switch-off logic&#8212;around surviving long after the original pattern has started to die.</p><p>Empirical observation across multiple asset classes&#8212;from market making to low-frequency statistical arbitrage&#8212;suggests that strategies do not fade linearly. Instead, they exhibit distinct, abrupt <em>regimes</em>. A strategy typically oscillates between:</p><ol><li><p><strong>Healthy regime:</strong> Positive expectancy (<em>E</em>[<em>r</em>] &gt; 0), stable variance (&#963;<sup>2</sup> &#8776; constant), and high information ratio.</p></li><li><p><strong>Degraded regime:</strong> Zero or negative expectancy, often accompanied by <em>volatility clustering</em> where variance spikes due to liquidity constraints, crowding, or breakdown in the correlation structure.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HsPO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HsPO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png 424w, https://substackcdn.com/image/fetch/$s_!HsPO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png 848w, https://substackcdn.com/image/fetch/$s_!HsPO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png 1272w, https://substackcdn.com/image/fetch/$s_!HsPO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!HsPO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png" width="1187" height="390" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:390,&quot;width&quot;:1187,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!HsPO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png 424w, https://substackcdn.com/image/fetch/$s_!HsPO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png 848w, https://substackcdn.com/image/fetch/$s_!HsPO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png 1272w, https://substackcdn.com/image/fetch/$s_!HsPO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040604c8-65ee-49e3-b455-47dc4ea5d259_1187x390.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Comparison of both regimes</figcaption></figure></div><p>The trader&#8217;s dilemma arises when the realized PnL sequence <em>R</em> = {<em>r</em><sub>1</sub>, <em>r</em><sub>2</sub>, &#8230;, <em>r</em><sub>t</sub>} deviates from the expected value <em>E</em>[<em>R</em>]. The operator is essentially solving a high-stakes signal classification problem in real-time, often under immense psychological pressure and incomplete information:</p><ol><li><p><strong>Hypothesis </strong><em>H</em><sub>0</sub><strong> </strong>(null hypothesis -<strong> noise</strong>)<strong>:</strong> The deviation is merely sampling variance. The strategy&#8217;s edge is intact (<em>E</em>[<em>r</em>] &gt; 0). The drawdown is a statistical artifact of the variance, not a bias in the mean. <em>Action: Do nothing.</em></p></li><li><p><strong>Hypothesis </strong><em>H</em><sub>1</sub><strong> </strong>(alternative hypothesis -<strong> broken</strong>)<strong>:</strong> The deviation is structural. The strategy&#8217;s edge has evaporated (<em>E</em>[<em>r</em>] &#8804; 0). The drawdown is a function of a new, adverse mean. <em>Action: Halt trading.</em></p></li></ol><p>Because financial time series exhibit<em> high kurtosis </em>(fat tails)<em> </em>and <em>heteroscedasticity</em> (changing variance), a large negative return <em>r</em><sub>t</sub> provides surprisingly weak evidence for <em>H</em><sub>1</sub> in isolation. A standard 3&#963; event&#8212;which happens once every 740 years in a Gaussian world&#8212;might occur every month in a live trading strategy. Consequently, standard variance-based risk metrics (like Value at Risk, Sharpe Ratio, or volatility targeting) usually fail in this specific context because they measure the <em>magnitude</em> of the dispersion, not the <em>shift</em> in the underlying data generating process.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><h4>Taxonomy of performance drift</h4><p>To correctly diagnose the failure of a strategy, we must move beyond generic labels like <em>drawdown</em> or<em> bad luck</em> and categorize the statistical nature of the failure. We define two primary categories of drift that necessitate a switch-off protocol.</p><p>In the previous article, we discussed the different types of drift from a time series perspective, in our case, PnL. Take a look:</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;85b2cb78-36b3-481e-8778-7e673370b8d8&quot;,&quot;caption&quot;:&quot;&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;[WITH CODE] Switch-Off: Bayesian online changepoint detection&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:311582722,&quot;name&quot;:&quot;Quant Beckman&quot;,&quot;bio&quot;:&quot;A quant research lab in newsletter form. Turning mathematics into automated trading systems and systematic portfolios. &quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/29f90c76-3fd2-4648-8b68-02caa8de3e77_800x800.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:100}],&quot;post_date&quot;:&quot;2025-11-17T07:44:08.469Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png&quot;,&quot;cover_image_alt&quot;:null,&quot;cover_image_crop&quot;:null,&quot;canonical_url&quot;:&quot;https://www.quantbeckman.com/p/with-code-switch-off-bayesian-online&quot;,&quot;section_name&quot;:&quot;Alpha Lab&quot;,&quot;video_upload_id&quot;:null,&quot;id&quot;:178589813,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:10,&quot;comment_count&quot;:3,&quot;publication_id&quot;:3833331,&quot;publication_name&quot;:&quot;Trading the Breaking&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><ol><li><p><strong>Covariate drift &#8594; environmental shift.</strong></p><p>Covariate drift occurs when the distribution of the inputs <em>P</em>(<em>X</em>) changes while the conditional probability of the target <em>P</em>(<em>y</em>|<em>X</em>) remains constant.</p><ul><li><p>The market environment shifts, starving the model of its necessary preconditions. Consider a Mean Reversion strategy trained on high-volatility regimes (X<sub>train</sub> &#8764; <em>N</em>(0, &#963;<sub>high</sub>). If the market enters a prolonged period of low volatility (central bank pinning, summer doldrums), the distribution of spread opportunities <em>P</em>(<em>X</em>) collapses.</p></li><li><p>The strategy&#8217;s logic remains mathematically sound&#8212;if a spread <em>did</em> open, it would likely close&#8212;but the opportunities simply do not arise. The PnL flatlines. However, because the strategy effectively pays <em>rent</em> (server costs, data fees, minimum commissions, and slippage on desperate fills), the net PnL begins to bleed slowly.</p></li><li><p>This is often termed <em>regime mismatch</em>. While the alpha is not strictly broken, the capital is inefficiently deployed. The correct response may be to <em>hibernate</em> the strategy rather than kill it, but from a PnL perspective, it must be switched off to preserve Sharpe.</p></li></ul></li><li><p><strong>Concept drift &#8594; alpha decay</strong></p><p>Concept drift occurs when the relationship between the signal and the target changes, i.e., <em>P</em>(<em>y</em>|<em>X</em>) shifts. This is the most dangerous form of drift.</p><ul><li><p>The market structure evolves, or the signal becomes crowded.</p><ul><li><p><em>Example 1 (</em><strong>crowding</strong><em>):</em> A proprietary signal based on order book imbalance worked when you were the only user. Now, five hedge funds are using the same signal. The predictive power <em>X</em> is still visible, but by the time you execute, the price impact of competitors has erased the profit margin. <em>P</em>(<em>y</em>&gt;0|<em>X</em>) drops to near zero.</p></li><li><p><em>Example 2 (</em><strong>microstructure</strong><em>):</em> A strategy relies on a latency arbitrage between two exchanges. The exchange updates its matching engine, reducing the latency delta. The physical reality of the arb disappears.</p></li></ul></li><li><p>The strategy continues to take trades that <em>look</em> historically valid, but the expected return of those trades shifts from positive to negative. The PnL exhibits a persistent negative trend (drift) that is statistically distinguishable from zero.</p></li><li><p>This is a <em>structural break</em>. The alpha is dead. No amount of parameter retuning will fix a broken thesis. This requires immediate, permanent cessation of trading.</p></li></ul></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!xim7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xim7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png 424w, https://substackcdn.com/image/fetch/$s_!xim7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png 848w, https://substackcdn.com/image/fetch/$s_!xim7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png 1272w, https://substackcdn.com/image/fetch/$s_!xim7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!xim7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png" width="886" height="346" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2d17760d-1523-4271-877c-318af13ef2b3_886x346.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:346,&quot;width&quot;:886,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:45508,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/180103121?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!xim7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png 424w, https://substackcdn.com/image/fetch/$s_!xim7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png 848w, https://substackcdn.com/image/fetch/$s_!xim7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png 1272w, https://substackcdn.com/image/fetch/$s_!xim7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d17760d-1523-4271-877c-318af13ef2b3_886x346.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The speed of the drift determines the difficulty of detection.</p><ul><li><p><strong>Gradual drift </strong>(<em>the boiled frog</em>): Typical of alpha decay due to competition. The Sharpe ratio erodes from 2.0 to 1.5 to 1.0 over a period of months. This is the hardest to detect because each incremental drop is masked by the daily noise. The operator rationalizes it as <em>a slightly tough month</em>, failing to integrate the long-term trend.</p></li><li><p><strong>Sudden drift </strong>(<em>the cliff edge</em>)<strong>:</strong> Typical of structural breaks, such as a regulatory ban on a specific order type, a change in the maker-taker fee model, or a shift in the tick size. The mean PnL jumps instantaneously from &#956;<sub>positive</sub> to &#956;<sub>negative</sub>. While the magnitude is larger, the <em>suddenness</em> makes it easier to detect with changepoint algorithms, provided they are robust to outliers.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IDDn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IDDn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png 424w, https://substackcdn.com/image/fetch/$s_!IDDn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png 848w, https://substackcdn.com/image/fetch/$s_!IDDn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png 1272w, https://substackcdn.com/image/fetch/$s_!IDDn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!IDDn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png" width="896" height="326" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aefe7198-8164-4af3-a847-503300498e06_896x326.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:326,&quot;width&quot;:896,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:358046,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/180103121?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IDDn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png 424w, https://substackcdn.com/image/fetch/$s_!IDDn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png 848w, https://substackcdn.com/image/fetch/$s_!IDDn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png 1272w, https://substackcdn.com/image/fetch/$s_!IDDn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faefe7198-8164-4af3-a847-503300498e06_896x326.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Our Robust Changepoint Detection model (RCD) is specifically designed to distinguish these structural drifts from mere volatility expansion.</p><p>In the absence of a rigorous framework, the decision to stop is psychological rather than statistical. This leads to the <strong>sunk cost</strong> behavior pattern, well-documented in behavioral finance but particularly acute in quantitative R&amp;D. A quant team may spend 6 months developing a model. When it degrades, the emotional cost of admitting the 6 months of work is now obsolete is incredibly high.</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!NsP3!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7008a281-6f2b-463f-a499-9bbfead835f4_441x661.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Evaluating The Sunk Cost Effect</div><div class="file-embed-details-h2">20.1MB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/14be375b-88c9-4e0a-b951-7ee5677940fa.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/14be375b-88c9-4e0a-b951-7ee5677940fa.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>The quant observes a degradation in performance but delays the switch-off decision, waiting for <em>one more data point</em> to confirm the trend. This one more data point is a statistical fallacy; in a noisy time series (Signal-to-Noise &lt;&lt; 1), a single point contributes almost zero information gain regarding the regime hypothesis.</p><p>This delay is the pivotal source of excess loss in quantitative funds. The conflict is between the human desire for continuity (and the ego associated with the model&#8217;s creation) and the statistical reality of the data. We resolve this conflict by removing the human operator from the decision loop entirely. The pivotal event is the <em>automated execution</em> of the switch-off signal, driven purely by the posterior probability of a regime change. The <em>supervisor</em> code becomes the final arbiter of the strategy&#8217;s life, acting as a meta-strategy that trades the portfolio of sub-strategies.</p><h4>Model risks and limitations</h4><p>While the RCD protocol significantly reduces the risk of ruin compared to naive stop-loss heuristics, it is not a panacea. It introduces a secondary layer of <em>model risk</em>. We are effectively trading one set of assumptions (market stationarity) for another (regime stationarity and independence). Understanding the boundaries of this model is as critical as understanding the strategy it supervises.</p><p>The standard formulation of the PELT algorithm, as implemented in this protocol, assumes that the residuals within a regime are Independent and Identically Distributed (i.i.d): &#1013;<sub>t</sub> &#8764; <em>N</em>(0, &#963;).</p><ul><li><p>Algorithmic trading PnL is rarely i.i.d.</p><ul><li><p><em>Mean Reversion Strategies</em> often exhibit negative autocorrelation.</p></li><li><p><em>Trend Following Strategies</em> often exhibit positive autocorrelation (streaks/momentum).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!f_fN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92893fa-93e4-4731-b97a-0916d893d08a_937x349.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!f_fN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92893fa-93e4-4731-b97a-0916d893d08a_937x349.png 424w, https://substackcdn.com/image/fetch/$s_!f_fN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92893fa-93e4-4731-b97a-0916d893d08a_937x349.png 848w, https://substackcdn.com/image/fetch/$s_!f_fN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92893fa-93e4-4731-b97a-0916d893d08a_937x349.png 1272w, https://substackcdn.com/image/fetch/$s_!f_fN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92893fa-93e4-4731-b97a-0916d893d08a_937x349.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!f_fN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92893fa-93e4-4731-b97a-0916d893d08a_937x349.png" width="937" height="349" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e92893fa-93e4-4731-b97a-0916d893d08a_937x349.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:349,&quot;width&quot;:937,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!f_fN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92893fa-93e4-4731-b97a-0916d893d08a_937x349.png 424w, https://substackcdn.com/image/fetch/$s_!f_fN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92893fa-93e4-4731-b97a-0916d893d08a_937x349.png 848w, https://substackcdn.com/image/fetch/$s_!f_fN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92893fa-93e4-4731-b97a-0916d893d08a_937x349.png 1272w, https://substackcdn.com/image/fetch/$s_!f_fN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92893fa-93e4-4731-b97a-0916d893d08a_937x349.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li></ul></li><li><p>Positive autocorrelation reduces the <em>effective sample size</em>. If a strategy has a <em>lucky streak</em> (autocorrelated wins), the PELT algorithm may interpret this cluster of wins as a distinct high alpha regime. When the streak inevitably reverts to the mean, the algorithm detects a regime shift downwards. This leads to<em> over-segmentation.</em> We detect shifts that are merely artifacts of the serial dependence, leading to potential Type I errors (premature switch-off).</p></li><li><p>For highly autocorrelated strategies, the PnL series should ideally be pre-whitened or the penalty parameter must be inflated to account for the reduced degrees of freedom.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!be8K!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!be8K!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png 424w, https://substackcdn.com/image/fetch/$s_!be8K!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png 848w, https://substackcdn.com/image/fetch/$s_!be8K!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png 1272w, https://substackcdn.com/image/fetch/$s_!be8K!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!be8K!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png" width="798" height="326" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:326,&quot;width&quot;:798,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!be8K!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png 424w, https://substackcdn.com/image/fetch/$s_!be8K!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png 848w, https://substackcdn.com/image/fetch/$s_!be8K!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png 1272w, https://substackcdn.com/image/fetch/$s_!be8K!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8cf296a-1c19-47db-8d35-87e2baa5f8c7_798x326.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Besides, the system&#8217;s decision boundary is heavily dependent on two hyperparameters: the penalty and the robustness scale <em>K</em>. We typically calibrate <em>K</em> based on a long-term estimate of volatility. However, volatility itself is regime-dependent.</p><ul><li><p>If the market transitions from a low-vol to a high-vol regime <em>without</em> a change in expectancy (Signal-to-Noise drops, but Mean is stable), a static <em>K</em> may interpret the new normal variance as a series of outliers. The biweight loss function will &#8220;clip&#8221; these valid data points, causing the estimator to become unresponsive or erratic.</p></li><li><p>The current implementation assumes a relatively homoscedastic noise structure <em>within</em> regimes or relies on the Biweight to simply ignore heteroscedasticity. It does not explicitly model changing volatility regimes as distinct from mean regimes.</p></li></ul><p>The protocol provided is explicitly a switch-off logic. It acts as a circuit breaker. It does not provides a signal for re-entry.</p><ul><li><p>If the supervisor switches off a strategy due to a detected negative drift (Type II Drift), and the strategy subsequently recovers in a simulation-trading environment, the algorithm offers no criteria for reactivation.</p></li><li><p>There is a significant risk of<em> missing the recovery</em>. Strategies often decay due to temporary crowding and recover when competitors exit (the &#8220;ecological cycle&#8221; of alpha). By automating the exit but leaving the re-entry to human discretion, we introduce a new behavioral bias: the fear of re-engaging with a failed strategy. This asymmetry can lead to under-allocation during the strategy&#8217;s renaissance.</p></li></ul><h4>Robust estimation and the biweight loss</h4><p>In the domain of financial time series, the Ordinary Least Squares estimator is dangerously fragile. While it is the Maximum Likelihood Estimator for Gaussian distributions, its performance degrades catastrophically in the presence of heavy-tailed noise.</p><p>To quantify this fragility, we utilize the statistical concept of the <em>breakdown point</em>. The breakdown point of an estimator is the smallest fraction of observations that need to be replaced by arbitrary values to cause the estimator to take on an arbitrarily large value (i.e., to become useless).</p><ul><li><p>The breakdown point of the arithmetic mean is 1/n (asymptotically 0). This implies that a single data point, if sufficiently large, can pull the sample mean to infinity.</p></li><li><p>The breakdown point of the median is 0.5 (50%). Half the data must be corrupted before the median becomes unreliable.</p></li></ul><p>Consider a strategy with a Sharpe ratio of 2.0. In a typical window of 100 trades, it generates returns clustered tightly around a mean of +0.05R. Our <em>scenario</em> would be something like: A single execution error results in a -50R loss.</p><ul><li><p><em>Result (mean):</em> The sample mean for the window instantly drops to negative territory. A variance-based switch-off logic (like a Z-score) immediately triggers.</p></li><li><p><em>Result (robust):</em> A robust estimator identifies the -50R event as distinct from the data generating process of the alpha. It isolates the outlier, discards it, and correctly identifies that the remaining 99 trades still exhibit a +0.05R expectancy.</p></li></ul><p>The switch-off logic must fundamentally distinguish between <em>execution failure</em> (an outlier) and<em> alpha decay</em> (a shift in the central tendency). The sample mean conflates these two errors.</p><p><strong>M-estimators and the geometry of influence</strong></p><p>We employ <em>M-estimators</em>, a broad class of estimators obtained as the minima of sums of functions of the data. We minimize an objective function of the form:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\sum_{i=1}^n \\rho(y_i - \\theta)&quot;,&quot;id&quot;:&quot;YMTTADNSNG&quot;}" data-component-name="LatexBlockToDOM"></div><p>where &#961; is a symmetric, positive-definite loss function. The choice of &#961; dictates the geometry of the risk surface and how the model reacts to deviations.</p><p>The sensitivity of the estimator to an outlier at value <em>x</em> is described by its influence function, which is proportional to the derivative of the loss function, &#936;(<em>x</em>) = &#961;&#8217;(<em>x</em>).</p><ol><li><p><strong>Ordinary Least Squares:</strong> &#961;(<em>x</em>) = <em>x</em><sup>2</sup>.</p><ul><li><p>&#936;(<em>x</em>) = 2<em>x</em>.</p></li><li><p>The influence of an error grows linearly with its magnitude. A 10&#963; event has 10 times the <em>pull</em> of a 1&#963; event. This is unacceptable for fat-tailed markets.</p></li></ul></li><li><p><strong>Huber Loss:</strong> &#961;(<em>x</em>) is quadratic for small <em>x</em> and linear for large <em>x</em>.</p><ul><li><p>&#936;(<em>x</em>) = c &#183; sgn(<em>x</em>) for |x|&gt;<em>c</em>.</p></li><li><p>The influence of an outlier is capped (constant). While better than OLS, it still allows extreme outliers to exert <em>some</em> influence on the location estimate. In a regime switch-off context, even a constant pull from a massive outlier can bias the decision.</p></li></ul></li><li><p><strong>Tukey&#8217;s Biweight:</strong></p><ul><li><p>&#936;(<em>x</em>) &#8594; 0 as |x| &#8594; &#8734;.</p></li><li><p>This is a <em>redescending M-estimator.</em> Once a data point is identified as being &#8220;too far&#8221; (beyond a threshold <em>K</em>), its influence completely vanishes. The estimator effectively performs a soft trim, ignoring the data point entirely. This is the property required for a reliable switch-off mechanism.</p></li></ul></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3csp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3csp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png 424w, https://substackcdn.com/image/fetch/$s_!3csp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png 848w, https://substackcdn.com/image/fetch/$s_!3csp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png 1272w, https://substackcdn.com/image/fetch/$s_!3csp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!3csp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png" width="1456" height="903" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:903,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:107234,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/180103121?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3csp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png 424w, https://substackcdn.com/image/fetch/$s_!3csp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png 848w, https://substackcdn.com/image/fetch/$s_!3csp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png 1272w, https://substackcdn.com/image/fetch/$s_!3csp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90f0a058-9800-4f3f-aaec-c4da1ec0ecfd_1580x980.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Tukey&#8217;s Biweight (bisquare) loss</strong></p><p>To achieve maximum resistance to toxic flow and outliers, we utilize Tukey&#8217;s Biweight loss function.</p><p>The Biweight loss function is defined as:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\rho(x) = \\begin{cases} \\frac{K^2}{6} \\left[ 1 - \\left( 1 - \\left(\\frac{x}{K}\\right)^2 \\right)^3 \\right] &amp; \\text{if } |x| \\le K \\\\\\frac{K^2}{6} &amp; \\text{if } |x| > K\\end{cases}&quot;,&quot;id&quot;:&quot;WVEISZOYIP&quot;}" data-component-name="LatexBlockToDOM"></div><p>The corresponding influence function &#936;(<em>x</em>) is:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\psi(x) = \\begin{cases} x \\left( 1 - \\left(\\frac{x}{K}\\right)^2 \\right)^2 &amp; \\text{if } |x| \\le K \\\\0 &amp; \\text{if } |x| > K\\end{cases}&quot;,&quot;id&quot;:&quot;PBKBEIVGIR&quot;}" data-component-name="LatexBlockToDOM"></div><p>Crucially, &#936;(<em>x</em>) = 0 for |<em>x</em>| &gt; <em>K</em>.<br>The parameter <em>K</em> is not arbitrary; it represents the <em>scale of robustness.</em> It defines the boundary between noise (which affects the mean) and structural breaks/outliers (which should be ignored).</p><ul><li><p>Typically, <em>K</em> is calibrated to <em>c</em> x <em>Scale</em>, where Scale is often the Median Absolute Deviation.</p></li><li><p>A common default is <em>c</em>=4.685 for asymptotic efficiency at the Normal distribution, but in trading, we often tighten this to c &#8776; 2.5 to be more aggressive in rejecting outliers.</p></li></ul><p><strong>Iteratively Reweighted Least Squares (IRLS)</strong></p><p>The Biweight loss function is non-convex. This means there is no closed-form solution (like the normal equations for OLS). We must solve for the location parameter &#952; iteratively.</p><p>We utilize the IRLS method. The logic proceeds as follows:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lmXR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lmXR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png 424w, https://substackcdn.com/image/fetch/$s_!lmXR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png 848w, https://substackcdn.com/image/fetch/$s_!lmXR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png 1272w, https://substackcdn.com/image/fetch/$s_!lmXR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!lmXR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png" width="885" height="631" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:631,&quot;width&quot;:885,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:73782,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/180103121?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lmXR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png 424w, https://substackcdn.com/image/fetch/$s_!lmXR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png 848w, https://substackcdn.com/image/fetch/$s_!lmXR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png 1272w, https://substackcdn.com/image/fetch/$s_!lmXR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7b8cdb8-222d-4e94-b9df-cd19a337a125_885x631.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>For computational simplicity and speed in our Python implementation (specifically to avoid complex iterative solvers in the inner loop of the dynamic programming step), we approximate the Biweight behavior using a truncated quadratic loss. This effectively clips the influence of any data point beyond distance <em>K</em> from the center.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\rho_{Trunc}(r) = \\min(r^2, K^2)&quot;,&quot;id&quot;:&quot;ICFXNIOTTP&quot;}" data-component-name="LatexBlockToDOM"></div><p>This function preserves the core property we need: it is quadratic near zero (efficient for Gaussian noise in normal regimes) but constant for large |<em>r</em>| (robust to outliers).</p><p>We implement a simplified IRLS solver. Note the specific handling of the <em>mask </em>which serves as a binary weight (0 or 1) rather than the smooth polynomial weight of the full Biweight. This <em>hard rejection</em> is computationally faster and often preferred in high-noise signal processing.</p><pre><code>def robust_mean_biweight(y, K, max_iter=50, tol=1e-6):
    &#8220;&#8221;&#8220;
    Approximate minimiser of sum_i min((y_i - theta)^2, K^2)
    using a simple iterative truncated mean.

    Parameters
    ----------
    y : array_like
        PnL values in one segment.
    K : float
        Biweight threshold. Residuals with |y_i - theta| &gt;= K are saturated.
    max_iter : int
        Maximum iterations.
    tol : float
        Convergence tolerance.

    Returns
    -------
    theta_hat : float
        Robust mean estimate for the segment.
    &#8220;&#8221;&#8220;
    y = np.asarray(y, dtype=float)
    if y.size == 0:
        return np.nan

    # Start from median for robustness
    theta = np.median(y)

    for _ in range(max_iter):
        r = y - theta
        mask = np.abs(r) &lt; K  # inliers
        if not np.any(mask):
            # everything is considered an outlier at this theta
            return theta

        theta_new = y[mask].mean()
        if abs(theta_new - theta) &lt; tol:
            return theta_new
        theta = theta_new

    return theta


def biweight_segment_cost(y, K):
    &#8220;&#8221;&#8220;
    Biweight segment cost and corresponding robust mean.

    Cost: sum_i min((y_i - theta)^2, K^2), theta via robust_mean_biweight.
    &#8220;&#8221;&#8220;
    y = np.asarray(y, dtype=float)
    theta = robust_mean_biweight(y, K)
    r = y - theta
    loss = np.where(np.abs(r) &lt; K, r**2, K**2)
    return float(loss.sum()), theta</code></pre><p>It is critical to note that the Biweight loss function is <em>non-convex.</em> Unlike OLS (a simple bowl shape), the Biweight loss surface has multiple local minima.</p><p>If we initialize the IRLS algorithm with an arbitrary value (e.g., zero, or the previous window&#8217;s mean), the solver might get <em>stuck</em> in a local minimum that corresponds to a cluster of outliers rather than the main data distribution.</p><p>This is why the code explicitly initializes <code>theta = np.median(y)</code>. The median is guaranteed to be <em>close enough</em> to the global minimum to place the solver within the basin of attraction of the true robust mean. </p><h4>Regime segmentation via penalized dynamic programming</h4><p>We have shifted our perspective from viewing PnL as a continuous random walk to viewing it as a discrete sequence of distinct structural regimes. This discretization is a fundamental assertion about the nature of market dynamics. </p><p>Let <em>y</em><sub>1:n</sub> be the sequence of realized PnL returns. We assume the existence of a set of <em>m</em> changepoints &#964;<sub>1</sub>, &#8230;, &#964;<sub>m</sub> that partition the historical data into <em>m</em>+1 discrete segments. Within the <em>j</em>-th segment, defined by the interval (&#964;<sub>j-1</sub>, &#964;<sub>j</sub>], the data is modeled as independent and identically distributed (i.i.d.) with a specific location parameter &#952;<sub>j</sub> and scale parameter &#963;<sub>j</sub>.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;y_t = \\theta_j + \\epsilon_t, \\quad \\text{for } \\tau_{j-1} < t \\le \\tau_j&quot;,&quot;id&quot;:&quot;GBUPTUYEUT&quot;}" data-component-name="LatexBlockToDOM"></div><p>Our operational goal is to identify the optimal set of changepoints. This is fundamentally an optimization problem where we must balance two competing forces: <strong>Fidelity</strong> (how well the model fits the data) and <strong>parsimony</strong> (the simplicity of the model).</p><p>Let <em>C</em>(<em>y<sub>s</sub></em><sub>:</sub><em><sub>e</sub></em>) denote the aggregate robust cost&#8212;calculated as the summation of individual biweight loss contributions&#8212;for a candidate market regime. This segment is rigorously defined by the temporal interval initiating at index <em>s</em> (representing the hypothesized genesis of a new statistical environment) and extending through to index <em>e</em>. In this context, <em>s</em> serves as the critical boundary condition: it is the candidate time-step where the algorithm postulates that the prior market dynamics have structurally decoupled from the future. At index <em>s</em>, the joint distribution <em>P</em>(<em>X</em><sub>s-1</sub>, <em>y</em><sub>s-1</sub>) is assumed to diverge from <em>P</em>(<em>X</em><sub>s</sub>, <em>y</em><sub>s</sub>), necessitating the estimation of a new location parameter &#952; for the subsequent window.</p><p>We seek to minimize the following penalized objective function:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\min_{\\tau, m} \\left[ \\sum_{j=1}^{m+1} C(y_{\\tau_{j-1}:\\tau_j}) + \\beta m \\right]&quot;,&quot;id&quot;:&quot;EUMBZSJKSB&quot;}" data-component-name="LatexBlockToDOM"></div><p>Here, &#946; is the <em>penalty term</em>, functioning as a regularizer similar to the role of &#955; in Ridge Regression or the penalty in the Akaike Information Criterion.</p><ul><li><p><strong>The Fit (</strong><em>C</em><strong>):</strong> This term pushes the model to add more changepoints. In the limit, if we assign a distinct regime to every single data point, the cost <em>C</em> drops to zero (perfect fit).</p></li><li><p><strong>The Regularizer (</strong>&#946;<strong>):</strong> This term penalizes complexity. It imposes a <em>cost</em> for declaring a regime change.</p></li></ul><p>The optimization landscape is defined by the interaction of these terms:</p><ul><li><p>If &#946; &#8594; 0, the cost function is dominated by the fit. The optimal solution converges to a changepoint at every time step <em>t</em>, modeling noise as structure (overfitting).</p></li><li><p>If &#946; &#8594; &#8734;, the penalty dominates. The optimal solution collapses to zero changepoints, modeling the entire history as a single stationary regime (underfitting).</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lF1e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" width="1456" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1158490,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3&quot;,&quot;text&quot;:&quot;Send me a copy&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3"><span>Send me a copy</span></a></p><h4>The PELT Algorithm (Pruned Exact Linear Time)</h4><p>Finding the global minimum of the objective function via brute force is computationally intractable. For a time series of length <em>n</em>, there are 2<sup>n-1</sup> possible segmentations.</p><ul><li><p>For <em>n</em>=100 trades, this is 2<sup>99</sup> combinations&#8212;a number exceeding the estimated number of atoms in the universe.</p></li><li><p>Consequently, heuristic methods (like sliding windows) are often used, but they fail to guarantee global optimality and are sensitive to window size.</p></li></ul><p>However, the problem exhibits <em>optimal Substructure</em>: the optimal segmentation of the entire history <em>y</em><sub>1:t</sub> contains within it the optimal segmentation of a sub-problem ending at some previous time <em>s</em>. This allows us to utilize <em>Dynamic Programming</em> (DP).</p><p>Here is where the <em>Bellman&#8217;s Principle of Optimality</em> comes into action. We define <em>F</em>(<em>t</em>) as the minimum cost of segmenting the data from the start up to time <em>t</em>. The recurrence relation (Bellman Equation) is: </p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;F(t) = \\min_{0 \\le s < t} \\left\\{ F(s) + C\\bigl(y_{s+1:t}\\bigr) + \\beta \\right\\} \n&quot;,&quot;id&quot;:&quot;JSMJXYIYMD&quot;}" data-component-name="LatexBlockToDOM"></div><p>To find the best cost at time <em>t</em>, we look back at all possible previous changepoints <em>s</em>. For each <em>s</em>, we take the already-computed optimal cost up to <em>s</em> (<em>F</em>(<em>s</em>)), add the cost of the new segment formed from <em>s</em>+1 to <em>t</em> (<em>C</em>(<em>y<sub>s</sub></em><sub>+1:</sub><em><sub>t</sub>)</em>), and add the penalty for creating this new segment (&#946;). We choose the <em>s</em> that minimizes this sum.</p><p>Standard Dynamic Programming is <em>O</em>(<em>n</em><sup>2</sup>) because for each time step <em>t</em>, we must iterate through all previous <em>s</em> &lt; <em>t</em>. For high-frequency data or long histories (<em>n</em> &gt; 10,000), quadratic complexity is too slow.</p><p>We utilize the <em>PELT</em> method. PELT improves upon standard DP by mathematically proving that certain past indices <em>s</em> can never be the optimal changepoint for <em>any</em> future time <em>T</em> &gt; <em>t</em>.</p><p><strong>The pruning inequality</strong><br>If there exists a time <em>s</em> &lt; <em>t</em> such that:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;F(s) + C\\bigl(y_{s+1:t}\\bigr) \\ge F(t)&quot;,&quot;id&quot;:&quot;XHJKZXUGLL&quot;}" data-component-name="LatexBlockToDOM"></div><p>Then, under mild assumptions about the cost function (sub-additivity), <em>s</em> can be permanently discarded from the search space for all future iterations.</p><ul><li><p>Intuitively, If the cost to reach time <em>t</em> via a split at <em>s</em> is already <em>worse</em> than the optimal cost to reach <em>t</em> (even before adding the penalty &#946;), then splitting at <em>s</em> will never help us in the future.</p></li><li><p>This pruning reduces the number of candidate <em>s</em> values at each step. For many change-point models, the number of candidates remains constant rather than growing linearly with <em>t</em>, resulting in a total complexity of <em>O</em>(<em>n</em>)<strong> </strong>(linear time).</p></li></ul><p>The implementation is bifurcated into two distinct computational phases: Precomputing the Cost Matrix and The PELT Solver.</p><p><strong>Phase 1: Cost Matrix precomputation</strong><br>We first compute the biweight cost for all possible contiguous segments. While precomputing the full matrix is technically <em>O</em>(<em>n</em><sup>2</sup>), in the Python environment, vectorizing this operation is often significantly faster for moderate <em>N</em> (<em>N</em> &lt; 2000) than recomputing costs iteratively inside the DP loop (which incurs Python interpreter overhead). For larger datasets, one would implement the cost calculation strictly inside the loop with Numba or Cython.</p><pre><code>def precompute_cost_matrix_l2(y):
    &#8220;&#8221;&#8220;
    Precompute L2 (squared error) segment costs for all [s:e] segments.

    Cost(s,e) = min_theta sum_{i=s}^e (y_i - theta)^2
              = SSE around the segment mean.

    Uses cumulative sums for O(1) per segment.
    &#8220;&#8221;&#8220;
    y = np.asarray(y, dtype=float)
    n = y.size

    S1 = np.zeros(n + 1)
    S2 = np.zeros(n + 1)
    S1[1:] = np.cumsum(y)
    S2[1:] = np.cumsum(y**2)

    cost = np.zeros((n, n), dtype=float)
    for s in range(n):
        for e in range(s, n):
            m = e - s + 1
            sum_y = S1[e + 1] - S1[s]
            sum_y2 = S2[e + 1] - S2[s]
            mean = sum_y / m
            # SSE = sum(y^2) - 2*mean*sum(y) + m*mean^2
            cost[s, e] = sum_y2 - 2 * mean * sum_y + m * mean**2
    return cost


def precompute_cost_matrix_biweight(y, K):
    &#8220;&#8221;&#8220;
    Precompute Biweight segment costs for all [s:e] segments.

    For each segment [s:e]:
      1) Estimate robust mean via robust_mean_biweight.
      2) Compute sum_i min((y_i - theta)^2, K^2).

    Complexity is O(n^3) in the worst case, fine for moderate n and
    for a kill-switch that runs e.g. once per day.
    &#8220;&#8221;&#8220;
    y = np.asarray(y, dtype=float)
    n = y.size
    cost = np.zeros((n, n), dtype=float)

    for s in range(n):
        for e in range(s, n):
            seg = y[s:e + 1]
            c, _ = biweight_segment_cost(seg, K)
            cost[s, e] = c

    return cost</code></pre><p><strong>Phase 2: The PELT solver</strong><br>This function implements the Dynamic Programming logic. Note the explicit handling of the path reconstruction via the <code>cps</code> (changepoints) list.</p><pre><code>def pelt_from_cost_matrix(cost_mat, penalty, min_seg_len=1):
    &#8220;&#8221;&#8220;
    Penalised dynamic programming segmentation using a precomputed cost matrix.

    Minimises:
        sum_{segments} cost(segment) + penalty * (#changepoints)

    F[t]   = optimal cost for y[0:t]
    cps[t] = list of segment endpoints up to t (starting at 0, ending at t)

    Parameters
    ----------
    cost_mat : np.ndarray
        Precomputed segment costs, shape (n, n), cost_mat[s, e] = cost of y[s:e+1]
    penalty : float
        Penalty for each changepoint.
    min_seg_len : int
        Minimum length per segment.

    Returns
    -------
    changepoints : list[int]
        Internal changepoints (excluding 0 and n).
    total_cost : float
        Total penalised cost of the optimal segmentation.
    F : np.ndarray
        DP table of optimal costs up to each t.
    cps : list[list[int]]
        Segment endpoints up to each t.
    &#8220;&#8221;&#8220;
    n = cost_mat.shape[0]
    F = np.full(n + 1, math.inf, dtype=float)
    F[0] = -penalty  # first segment pays one penalty

    cps = [[] for _ in range(n + 1)]
    cps[0] = [0]

    for t in range(1, n + 1):
        best_cost = math.inf
        best_s = None

        for s in range(0, t):
            seg_len = t - s
            if seg_len &lt; min_seg_len or not math.isfinite(F[s]):
                continue

            seg_cost = cost_mat[s, t - 1]
            val = F[s] + seg_cost + penalty

            if val &lt; best_cost:
                best_cost = val
                best_s = s

        if best_s is not None:
            F[t] = best_cost
            cps[t] = cps[best_s] + [t]

    if not math.isfinite(F[n]):
        raise RuntimeError(
            &#8220;No valid segmentation found. &#8220;
            &#8220;Try reducing &#8216;min_seg_len&#8217; or the penalty.&#8221;
        )

    ends = cps[n]          # e.g. [0, 70, 150, 240]
    changepoints = ends[1:-1]  # internal cps only

    return changepoints, F[n], F, cps</code></pre><p>This algorithm guarantees the global optimum for the given cost function and penalty. It is deterministic and repeatable. Unlike stochastic methods (like MCMC or Particle Filtering), PELT will always return the exact same segmentation for the same dataset and parameters, which is a critical requirement for auditable algorithmic trading systems.</p><h4>Reconstruction and visualizing the drift</h4><p>The raw output of the PELT algorithm is mathematically precise but operationally abstract: it is merely a tuple of integer indices <em>T</em> = {<em>&#964;</em><sub>1</sub>, <em>&#964;</em><sub>2</sub>, &#8230;, &#964;<sub>t</sub>} representing the boundaries where the cost function was minimized. While this minimizes the objective function, it does not essentially tell the human operator <em>what</em> happened, only <em>when</em> it happened. To bridge the gap between statistical detection and actionable intelligence, we must perform a signal reconstruction step.</p><p>This process involves projecting the original, noisy PnL vector <em>y</em>&#8712;R<sup>n</sup> onto the subspace of piecewise-constant functions defined by the changepoints <em>T</em>. We construct a reconstruction vector &#375; where &#375;<sub>t</sub> = &#952;<sub>j</sub> for all <em>t</em> in the interval (<em>&#964;</em><sub>1-1</sub>,<em>&#964;</em><sub>j</sub>].</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\hat{y}_t = \\text{argmin}_{\\theta} \\sum_{k=\\tau_{j-1}+1}^{\\tau_j} \\rho(y_k - \\theta)&quot;,&quot;id&quot;:&quot;UCWUGPTCBI&quot;}" data-component-name="LatexBlockToDOM"></div><p>This reconstruction effectively acts as a non-linear low-pass filter. It strips away the fat-tailed noise to reveal the underlying skeletal structure of the strategy&#8217;s expected return. The human eye is notoriously poor at integrating noisy time series to estimate means; it is prone to recency bias and pattern pareidolia. </p><p>The following function transforms the raw changepoint indices into a sequence of robust means. It iterates through the identified segments and re-applies the robust M-estimator to each window locally.</p><pre><code>def reconstruct_piecewise_constant(y, changepoints, mean_kind=&#8221;biweight&#8221;, K=None):
    &#8220;&#8221;&#8220;
    Build piecewise-constant fit from segmentation.

    Parameters
    ----------
    y : array_like
        Original series.
    changepoints : list[int]
        Internal changepoints.
    mean_kind : {&#8221;biweight&#8221;, &#8220;l2&#8221;}
        Mean type.
    K : float
        Biweight threshold if mean_kind=&#8221;biweight&#8221;.

    Returns
    -------
    np.ndarray
        Piecewise-constant fitted series.
    &#8220;&#8221;&#8220;
    y = np.asarray(y, dtype=float)
    n = len(y)
    y_hat = np.zeros_like(y)

    segs = get_segments(n, changepoints)
    for s, e in segs:
        seg = y[s:e]
        if mean_kind == &#8220;biweight&#8221;:
            if K is None:
                raise ValueError(&#8221;K must be provided for biweight mean.&#8221;)
            theta = robust_mean_biweight(seg, K)
        elif mean_kind == &#8220;l2&#8221;:
            theta = seg.mean()
        else:
            raise ValueError(f&#8221;Unknown mean_kind: {mean_kind}&#8221;)
        y_hat[s:e] = theta

    return y_hat</code></pre><p>When we plot the noisy scatter of daily returns against the step function&#8212;like we did below&#8212;the trend becomes undeniable. If the step function slopes downward over the last two regimes, we have rigorous, visual, and statistical evidence of decay.</p><p>The Supervisor module sits above the trading strategy. Its output cannot be a probability or a <em>warning level.</em> Operational risk management requires decisive action. Therefore, the output must be a definitive binary state: <code>True</code> (keep running) or <code>False</code> (kill). Ambiguity is the enemy of survival; if the system outputs &#8220;maybe,&#8221; the human operator will invariably default to &#8220;wait and hope,&#8221; reintroducing the very bias we aim to eliminate.</p><p>We define two distinct, mutually exclusive failure modes that trigger the <code>False</code> state. Each corresponds to a different economic reality of algorithmic decay.</p><ol><li><p><strong>Logic A: Relative decay (alpha erosion)</strong></p><p>This condition addresses the phenomenon often colloquially referred to as the <em>Boiled Frog</em> scenario. In this state, the strategy is not necessarily losing money in absolute terms&#8212;the mean may still be slightly positive. However, it is performing significantly worse than its peak historical regime.</p><ol><li><p>This decay usually signals that the alpha is being competed away. Other market participants have discovered the signal, or the market capacity for this specific inefficiency has been reached. While the strategy is not yet <em>toxic</em>, the reward-to-risk profile has shifted unfavorably. Continuing to deploy the same amount of capital for a fraction of the return leverages the tail risk without the compensatory upside.</p></li><li><p>Formal condition:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\theta_{current} < \\alpha \\cdot \\theta_{best}&quot;,&quot;id&quot;:&quot;ZZUCAMIOGJ&quot;}" data-component-name="LatexBlockToDOM"></div></li></ol><p>Where &#945; is a tolerance factor (e.g., 0.5) and &#952;<sub>best</sub> = max(&#952;<sub>1</sub>, &#8230;, &#952;<sub>m-1</sub>). If the current edge is less than 50% of the best historical edge, we kill it. We do not want to deploy capital for mediocre returns that do not justify the operational and tail risks inherent in any active strategy.</p></li><li><p><strong>Logic B: Absolute failure (negative expectancy)</strong></p><p>This condition handles the <em>bleeder</em> scenario. The strategy is strictly losing money in the current regime. This is not a variance event; the <em>expectation</em> of the PnL distribution has shifted below zero.</p><ol><li><p>This represents a structural break in the hypothesis. The correlation structure the strategy relied upon has inverted, the advantage is gone, or a new fee structure has rendered the edge negative.</p></li><li><p>Formal condition:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\theta_{current} < \\epsilon&quot;,&quot;id&quot;:&quot;LTVZOUFUJN&quot;}" data-component-name="LatexBlockToDOM"></div></li></ol></li></ol><p>Where &#949; is a threshold. Typically &#949; = 0, but it can be set to a slightly negative number (e.g., -0.05R) to account for friction, slippage, or very short-term noise that the biweight loss didn&#8217;t fully filter. Setting &#949; slightly below zero prevents <em>flickering</em> (rapid on/off switching) when the strategy is hovering exactly at breakeven.</p><p>The following implementation encapsulates these two logic gates into a single decision function.</p><pre><code>def switch_off_decision(
    y,
    changepoints,
    mean_kind=&#8221;biweight&#8221;,
    K=None,
    rel_drop=0.5,
    abs_threshold=0.0,
    min_bad_len=10,
):
    &#8220;&#8221;&#8220;
    Decide whether to switch off the algo based on PnL regimes.

    Inputs
    ------
    y : array_like
        Per-period PnL series.
    changepoints : list[int]
        Internal changepoints from PELT.
    mean_kind : {&#8221;biweight&#8221;, &#8220;l2&#8221;}
        Type of mean for regime quality.
    K : float
        Biweight threshold if mean_kind=&#8221;biweight&#8221;.
    rel_drop : float
        Relative drop threshold. If last mean &lt;= rel_drop * best_prev_mean
        and best_prev_mean &gt; 0, we treat it as degradation.
        Example: rel_drop=0.5 &#8594; drop by 50% or more.
    abs_threshold : float
        Absolute threshold for &#8220;bad&#8221; mean. Default 0.0 (non-positive mean).
    min_bad_len : int
        Minimum length of the last regime before we react.
        Avoids switching off on tiny patches.

    Returns
    -------
    decision : dict
        {
          &#8220;switch_off&#8221;: bool,
          &#8220;reason&#8221;: str,
          &#8220;segments&#8221;: list[(s,e)],
          &#8220;means&#8221;: np.ndarray,
          &#8220;lengths&#8221;: np.ndarray,
          &#8220;last_mean&#8221;: float,
          &#8220;best_prev_mean&#8221;: float
        }
    &#8220;&#8221;&#8220;
    y = np.asarray(y, dtype=float)
    n = len(y)
    if n == 0:
        return {&#8221;switch_off&#8221;: False, &#8220;reason&#8221;: &#8220;empty series&#8221;}

    segs = get_segments(n, changepoints)
    if len(segs) == 1:
        return {&#8221;switch_off&#8221;: False, &#8220;reason&#8221;: &#8220;only one regime detected&#8221;}

    means = segment_means(y, segs, mean_kind=mean_kind, K=K)
    lengths = np.array([e - s for (s, e) in segs])

    last_mean = means[-1]
    last_len = lengths[-1]
    best_prev_mean = means[:-1].max()

    cond_len = last_len &gt;= min_bad_len
    cond_rel = (best_prev_mean &gt; 0) and (last_mean &lt;= rel_drop * best_prev_mean)
    cond_abs = last_mean &lt;= abs_threshold

    switch_off = cond_len and (cond_rel or cond_abs)

    # Human-readable reason
    if not cond_len:
        reason = f&#8221;Current regime too short (len={last_len}, min_bad_len={min_bad_len}).&#8221;
    elif cond_rel and cond_abs:
        reason = (
            f&#8221;Last mean {last_mean:.4f} &lt;= abs_threshold {abs_threshold:.4f} &#8220;
            f&#8221;and &lt;= {rel_drop:.2f} * best_prev_mean {best_prev_mean:.4f}.&#8221;
        )
    elif cond_rel:
        reason = (
            f&#8221;Last mean {last_mean:.4f} &lt;= {rel_drop:.2f} * best_prev_mean &#8220;
            f&#8221;{best_prev_mean:.4f}.&#8221;
        )
    elif cond_abs:
        reason = (
            f&#8221;Last mean {last_mean:.4f} &lt;= abs_threshold {abs_threshold:.4f}.&#8221;
        )
    else:
        reason = &#8220;No degradation condition met.&#8221;

    return {
        &#8220;switch_off&#8221;: bool(switch_off),
        &#8220;reason&#8221;: reason,
        &#8220;segments&#8221;: segs,
        &#8220;means&#8221;: means,
        &#8220;lengths&#8221;: lengths,
        &#8220;last_mean&#8221;: float(last_mean),
        &#8220;best_prev_mean&#8221;: float(best_prev_mean)}</code></pre><p>Okay, it&#8217;s time to check what this script can offer.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!xdHt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xdHt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png 424w, https://substackcdn.com/image/fetch/$s_!xdHt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png 848w, https://substackcdn.com/image/fetch/$s_!xdHt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png 1272w, https://substackcdn.com/image/fetch/$s_!xdHt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!xdHt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png" width="989" height="788" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:788,&quot;width&quot;:989,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!xdHt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png 424w, https://substackcdn.com/image/fetch/$s_!xdHt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png 848w, https://substackcdn.com/image/fetch/$s_!xdHt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png 1272w, https://substackcdn.com/image/fetch/$s_!xdHt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc8e025-5601-44d6-bef6-9067a0dc5415_989x788.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We see that the dynamics are very similar to those of the previous prototype. As soon as it detects a structural break in the PnL, the warning light is activated. When the Supervisor function returns <code>switch_off=True</code>, the resolution must be immediate, automated, and dispassionate. There is no appeal process in the algorithm. The pivotal event occurs in three stages:</p><ol><li><p><strong>Halt entry generation </strong>(<em>the kill)</em>: The algorithm stops emitting new entry signals immediately. This is the primary kill switch. It stops the bleeding. Note that this is distinct from &#8220;liquidating the portfolio.&#8221;</p></li><li><p><strong>Liquidation logic:</strong> Existing positions are managed according to the <em>standard</em> exit logic of the strategy. We do <em>not</em> panic liquidate at market unless the detected negative drift is extreme (e.g., mean &lt; -2.0 sigma, indicating a critical malfunction). Panic liquidation often incurs slippage that exceeds the expected loss of holding the position.</p></li><li><p><strong>Quarantine and review:</strong> The strategy is moved to a <em>quarantine</em> state in the portfolio management system. It requires human review. The quant must determine: Was this a temporary market anomaly? Or has the alpha permanently decayed? The strategy can only be reactivated after a recalibration or a code update; it is never simply <em>turned back on</em> without intervention.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KKRX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 424w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 848w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1272w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png" width="1456" height="271" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:271,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:381253,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!KKRX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 424w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 848w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1272w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Cool guys! Good job today. Until then&#8212;may your signals whisper truth, your strategies hold their ground, and your equity curve climb with the quiet relentlessness of a tide. Stay sharp, stay bold, stay unshakeable &#128200;</p><div><hr></div><p>This is an<strong> invitation-only access</strong> to our <strong>QUANT COMMUNITY</strong>, so we verify numbers to avoid spammers and scammers. Feel free to join or decline at any time. <em>Tap the WhatsApp icon below to join</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://chat.whatsapp.com/LsXSHJxfp9l7tOd7OO6PQL" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nbxE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 424w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 848w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1272w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png" width="1280" height="352" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:352,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:87001,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://chat.whatsapp.com/LsXSHJxfp9l7tOd7OO6PQL&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/181419543?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!nbxE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 424w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 848w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1272w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Appendix</strong></p><p>Full code:</p>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-switch-off-robust-changepoint">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Switch-Off: Bayesian online changepoint detection ]]></title><description><![CDATA[A probabilistic framework for strategy de-allocation]]></description><link>https://www.quantbeckman.com/p/with-code-switch-off-bayesian-online</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-switch-off-bayesian-online</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Mon, 17 Nov 2025 07:44:08 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>Risks and limitations of traditional and model-based de-allocation.</p></li><li><p>Probabilistic kill switch.</p><ol><li><p>Point 1: The non-stationarity fallacy.</p></li><li><p>Point 2: Automating priors with heuristics.</p></li><li><p>Point 3: The engine (the recursive update).</p></li><li><p>Point 4: Numerical stability via log-space.</p></li><li><p>Point 5: The N-IG / Student-t likelihood.</p></li><li><p>Point 6: State-space pruning.</p></li><li><p>Point 7: The dual-trigger system.</p></li></ol></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read full story</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Introduction</h4><p>Every quantitative trading operation, from a single retail algorithmist to a multi-billion dollar systematic fund, faces an identical, recurring, and fundamental dilemma: </p><blockquote><p><em><strong>When to deactivate a failing strategy.</strong></em></p></blockquote><p>This problem is, in many ways, the <em>only</em> problem that matters for long-term survival. Alpha is finite. All strategies eventually decay. The persistence of a quant fund is not determined by its ability to <em>find</em> alpha, but by its efficiency in <em>discarding</em> dead alpha.</p><p>A strategy is designed, backtested, and deployed, exhibiting positive expectancy. Its cumulative P&amp;L curve moves, as expected, from the lower-left to the upper-right, exhibiting stochastic variance around its mean. Then, one day, the curve flattens. It enters a drawdown. The drawdown deepens. This is the moment of truth.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rVBo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rVBo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png 424w, https://substackcdn.com/image/fetch/$s_!rVBo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png 848w, https://substackcdn.com/image/fetch/$s_!rVBo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png 1272w, https://substackcdn.com/image/fetch/$s_!rVBo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!rVBo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png" width="1021" height="564" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:564,&quot;width&quot;:1021,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:145759,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/178589813?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rVBo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png 424w, https://substackcdn.com/image/fetch/$s_!rVBo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png 848w, https://substackcdn.com/image/fetch/$s_!rVBo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png 1272w, https://substackcdn.com/image/fetch/$s_!rVBo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa20e058c-7d8d-4d2c-8bdb-30f118e0cc9b_1021x564.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This event, the drawdown, creates an immediate and critical decision point. The operator must distinguish between two mutually exclusive states of the world:</p><ol><li><p><strong>State 1: Stochastic noise.</strong> The generative process of returns, <em>G</em>(<em>x<sub>t</sub></em>|&#952;<sub>1</sub>), remains unchanged. The strategy is operating within its expected parameters, where &#952;<sub>1</sub> includes a positive mean return (&#956;&gt;0). It has simply encountered a statistically normal, albeit painful, period of adverse variance. This is an expected excursion into the left tail of the P&amp;L distribution. The correct action is to <em><strong>do nothing</strong></em>. Intervening would be a catastrophic, unforced error&#8212;crystallizing a temporary loss, violating the strategy&#8217;s core assumptions, and missing the inevitable (if profitable) reversion to the mean.</p></li><li><p><strong>State 2: Structural decay.</strong> The underlying market dynamics that generated the strategy&#8217;s alpha have fundamentally and irrevocably changed. The generative process has shifted from <em>G</em>(<em>x<sub>t</sub></em>|&#952;<sub>1</sub>) to <em>G</em>(<em>x<sub>t</sub></em>|&#952;<sub>2</sub>), where the new parameters &#952;<sub>2</sub> imply a zero or negative mean return (&#956;&#8804;0). The correct action is to <em><strong>intervene immediately</strong></em>, set position sizing to zero, and halt all new signal execution. Every <em>new</em> trade taken under this regime is a new, independent bet with a negative expected value.</p></li></ol><p>The cost of mis-identification is asymmetric and severe.</p><ul><li><p><strong>Cost of false positive (Type I error):</strong> Intervening on noise. You kill a profitable strategy. You crystallize a temporary loss and, more importantly, you miss the profitable recovery. This is a whipsaw. It&#8217;s an unforced error that destroys the geometric compounding of your capital. You have paid the cost of the drawdown without collecting the reward of the recovery.</p></li><li><p><strong>Cost of false negative (Type II error):</strong> Failing to intervene on structural decay. This is an existential risk. It is the act of <em>inertia</em>&#8212;continuing to deploy capital into a statistically broken model. You are now systematically paying spread and slippage to execute a negative-expectancy strategy. You are not waiting for a recovery; you are actively digging a hole. This is how funds blow up. They transform a small, manageable drawdown from a broken strategy into a catastrophic, unrecoverable total loss.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!G6TX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!G6TX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png 424w, https://substackcdn.com/image/fetch/$s_!G6TX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png 848w, https://substackcdn.com/image/fetch/$s_!G6TX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png 1272w, https://substackcdn.com/image/fetch/$s_!G6TX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!G6TX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png" width="1452" height="847" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:847,&quot;width&quot;:1452,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:236207,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/178589813?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!G6TX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png 424w, https://substackcdn.com/image/fetch/$s_!G6TX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png 848w, https://substackcdn.com/image/fetch/$s_!G6TX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png 1272w, https://substackcdn.com/image/fetch/$s_!G6TX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ab0b4e9-4baa-4941-b039-8ab6f6dbb7a6_1452x847.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You can check here the original paper from which we have taken the implementation that we are going to develop:</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!XS8m!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbab88bac-2415-4ef7-af28-f58581aa6afe_443x552.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Bayesian online changepoint detection</div><div class="file-embed-details-h2">249KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/2a942864-5d51-4a53-ab5c-a124bc029a73.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/2a942864-5d51-4a53-ab5c-a124bc029a73.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>In this article we treat that decision explicitly as a <strong>probabilistic</strong> problem. Quants never observes &#8220;truth&#8221; directly; they only ever see a noisy, path&#8211;dependent P&amp;L stream. Any rule that pretends otherwise&#8212;whether a hard 20% drawdown stop, a three-month underperformance review, or a hand-wavy committee meeting&#8212;is implicitly running a  probabilistic model, usually a very bad one. The goal is to replace that collection of ad-hoc, inconsistent heuristics with a single, coherent decision rule that states, in clear numbers, <em>how likely it is that the strategy has broken</em> and <em>what to do about it</em>.</p><p>Legacy de-allocation frameworks tend to fall into two pathological extremes. On one side are purely heuristic rules: </p><ol><li><p>Fixed drawdown limits, lookback Sharpe thresholds, rolling t-tests, or &#8220;if it feels wrong, turn it off.&#8221; These are simple to implement but blind to the true data-generating process; they confuse path properties of the equity curve with properties of the underlying returns. </p></li><li><p>On the other side are textbook Bayesian changepoint detectors, which assume idealised distributions, expose a large number of tunable hyperparameters, and often require full re-runs over the entire history. In theory they are elegant; in practice they are brittle, slow, and dangerously sensitive to mis-specification.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!C67L!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!C67L!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!C67L!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!C67L!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!C67L!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!C67L!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1618829,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/178589813?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!C67L!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!C67L!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!C67L!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!C67L!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb98bec9b-868d-4c73-a8f0-f8d3d2e92c99_1536x1024.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4><strong>Risks and limitations of traditional and model-based de-allocation</strong></h4><p>Traditional risk management methods are woefully inadequate for this dilemma because they are deterministic, lagging, and non-probabilistic. They are relics of a discretionary world, not tools for a systematic one.</p><ul><li><p><strong>Fixed-threshold mechanisms:</strong> A <em>2% drawdown stop-loss</em> is a primitive, lagging indicator. It is mathematically incoherent. It places a stop on the <em>path</em> (the cumulative P&amp;L), not the <em>process</em> (the returns). It does not differentiate between a 2% loss from a single, sharp volatility shock (which may be a high-expectancy mean-reversion opportunity) and a 2% loss from a 1,000-trade <em>slow bleed</em> (which signifies certain alpha decay). This method functions by confirming a large loss has <em>already occurred</em>, guaranteeing the de-allocation is executed at the point of maximum pain. It is a purely reactive measure that carries no predictive power.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gwu5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gwu5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png 424w, https://substackcdn.com/image/fetch/$s_!gwu5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png 848w, https://substackcdn.com/image/fetch/$s_!gwu5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png 1272w, https://substackcdn.com/image/fetch/$s_!gwu5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!gwu5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png" width="752" height="626" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:626,&quot;width&quot;:752,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:55353,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/178589813?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gwu5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png 424w, https://substackcdn.com/image/fetch/$s_!gwu5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png 848w, https://substackcdn.com/image/fetch/$s_!gwu5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png 1272w, https://substackcdn.com/image/fetch/$s_!gwu5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab1ccdb1-98fe-4695-bfd7-ddc8915a2e84_752x626.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li><li><p><strong>Discretionary intervention:</strong> A <em>portfolio manager review</em> is un-quantitative. It is a surrender of the entire systematic premise. It injects a portfolio of well-documented human biases directly into the risk model:</p><ul><li><p><em>The sunk-cost fallacy</em>: &#8221;I&#8217;ve lost so much, I can&#8217;t stop now&#8221;.</p></li><li><p><em>Emotional attachment</em>: &#8221;This was my best strategy, it <em>must</em> come back&#8221;.</p></li><li><p><em>The gambler&#8217;s fallacy</em>: &#8221;It&#8217;s lost 10 times in a row, it <em>must</em> be due for a win&#8221;. This is not risk management; it is gambling.</p></li></ul></li></ul><p>An interesting alternative is a model-based approach, as described in the Adams &amp; MacKay paper, <em>Bayesian Online Changepoint Detection</em>. However, this, too, is not a silver bullet. A naive implementation introduces its own, more sophisticated set of risks.</p><ol><li><p><strong>Model mis-specification risk:</strong> This is the primary and most dangerous risk. It is the assumption of an incorrect likelihood model for the data-generating process: <em>Gaussian</em>. Depending on your approach and type of model, the P&amp;L could have different shapes. For example, a buy&amp;hold approach will <em>always</em> see a fat-tailed event as a structural break, guaranteeing it will signal a false positive and trigger a whipsaw precisely when volatility expands.</p></li><li><p><strong>Parameter sensitivity risk:</strong> A Bayesian model is, by definition, sensitive to its <em>priors</em>. This is a feature, not a bug, but it must be managed. The single most sensitive prior in BOCPD is the <em>hazard rate</em>, <em>H</em>, which is derived from the expected run length, &#955;. This parameter encodes our <em>a priori</em> belief about how often regimes should change.</p><ul><li><p>A high hazard rate (low &#955;) creates a <em>hyper-sensitive</em> model. It has a weak prior on stability and will over-fit to noise, signaling changepoints constantly.</p></li><li><p>A low hazard rate (high &#955;) creates a <em>sluggish, inertial</em> model. It has a very strong prior on stability. It will require devastating, statistically overwhelming losses to overcome this prior. This model <em>will</em> commit the Type II error (failure to intervene) if not tuned correctly.</p></li></ul></li><li><p><strong>Computational complexity risk:</strong> An online algorithm must, by definition, run in linear time with respect to the data, <em>O</em>(<em>T</em>). A naive, textbook implementation of the BOCPD recursive update requires updating <em>every</em> possible past run length at <em>every</em> new time step. The total work to process a stream of length <em>T</em> is 1+2+...+<em>T</em> =<em>T</em>(<em>T</em>+1)/2. This is <em>O</em>(<em>T</em><sup>2</sup>), or quadratic, complexity. </p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><h4>Probabilistic kill switch</h4><p>The pivotal event driving our entire investigation is the <em>structural break</em>&#8212;the unknown moment in time, &#964;, when the generative parameters of our P&amp;L stream, &#952;<sub>1</sub>, instantaneously shift to new parameters, &#952;<sub>2</sub>.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;x_t \\sim\\begin{cases}P(x_t | \\theta_1) &amp; \\text{for } t < \\tau \\\\P(x_t | \\theta_2) &amp; \\text{for } t \\ge \\tau\\end{cases}&quot;,&quot;id&quot;:&quot;JBJFCFMZRD&quot;}" data-component-name="LatexBlockToDOM"></div><p>The problem is the financial trade-off between the <em>cost of inertia</em> (type II error / false negative) and the <em>cost of whipsaw </em>(type I error / false positive).</p><p>We need a de-allocation method&#8212;a <em>kill switch</em>&#8212;to solve this situation. A simple, fixed-ruleset cannot. We require a system that:</p><ol><li><p>Operates in real-time, meaning <em>O</em>(<em>K &#183; T</em>) complexity, where <em>K&lt;&lt;T</em>.</p></li><li><p>Is mathematically robust to the known statistical properties of financial data (i.e., leptokurtosis).</p></li><li><p>Is computationally tractable over thousand of data points, meaning it must be numerically stable.</p></li><li><p>Does not provide a binary &#8220;yes/no&#8221; answer, but rather a full <em>posterior probability distribution</em> of a changepoint.</p></li></ol><p>This is the necessity. We must evolve our risk management from the primitive, deterministic question &#8220;<em>How much have I lost?</em>&#8221; to the key, probabilistic question:</p><blockquote><p><strong>What is the </strong><em><strong>probability</strong></em><strong>, given the data I&#8217;ve just seen, that the generative process of my returns has fundamentally broken?</strong></p></blockquote><p>We will now build this exact system from the ground up, based on the specific engineering and mathematical decisions in the <code>bocpd_kill_switch</code> method.</p><p>The idea is to feed a P&amp;L stream into a BOCPD model and receive a kill signal. The challenge is that doing this <em>correctly</em> requires solving a series of non-trivial mathematical and engineering problems. </p><p>We will now analyze, in exhaustive detail, the seven critical technical challenges that must be solved.</p><p><strong>Point 1: The non-stationarity fallacy</strong></p><p>The first, and most common, failure in any time-series analysis is mis-handling the input data. A junior quant, looking at a beautiful cumulative P&amp;L chart, will attempt to feed that chart directly into the model. This is a fatal mathematical error that invalidates the entire analysis before it even begins.</p><p>Our script&#8217;s first line of defense is not in the <code>BOCPDRunner</code> class, but in the <code>analyze_pnl_stream</code> wrapper function:</p><pre><code><code># THE SCRIPT&#8217;S FIRST ACTION:
returns = np.diff(cumulative_pnl, prepend=cumulative_pnl[0])</code></code></pre><p>Why is this line non-negotiable? The answer is <strong>stationarity</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YyY0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YyY0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png 424w, https://substackcdn.com/image/fetch/$s_!YyY0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png 848w, https://substackcdn.com/image/fetch/$s_!YyY0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png 1272w, https://substackcdn.com/image/fetch/$s_!YyY0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!YyY0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png" width="1015" height="501" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dd51a71e-a000-4025-a017-8e559b74952a_1015x501.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:501,&quot;width&quot;:1015,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:204767,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/178589813?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!YyY0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png 424w, https://substackcdn.com/image/fetch/$s_!YyY0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png 848w, https://substackcdn.com/image/fetch/$s_!YyY0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png 1272w, https://substackcdn.com/image/fetch/$s_!YyY0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd51a71e-a000-4025-a017-8e559b74952a_1015x501.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Non-stationary vs Stationary series</figcaption></figure></div><p>A stationary process is one whose statistical properties (mean, variance, autocorrelation) are constant over time. A cumulative P&amp;L curve is, by definition,<em> non-stationary</em>. It is an integrated process of order 1, or I(1). It is, in simple terms, a random walk (with, one hopes, a positive drift).</p><p>Let&#8217;s be precise. The properties of a random walk, are:</p><ul><li><p><strong>Time-dependent mean:</strong> The expected value is a <em>direct function of time</em>. It is not constant.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;E[PnL_t] = PnL_0 + \\mu \\cdot t&quot;,&quot;id&quot;:&quot;GZBGBEWKPH&quot;}" data-component-name="LatexBlockToDOM"></div></li><li><p><strong>Time-dependent variance:</strong> The variance also <em>grows linearly with time</em>. It is not constant.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;Var(PnL_t) = \\sigma^2 \\cdot t&quot;,&quot;id&quot;:&quot;HRDQUVQUAK&quot;}" data-component-name="LatexBlockToDOM"></div></li></ul><p>All standard statistical models, from ARIMA to GARCH to the generative model inside BOCPD, are built on the fundamental assumption that the data <em>within a single regime</em> is stationary (or can be modeled as such). They are designed to model a <em>process</em>, not an integrated <em>path</em>.</p><p>Modeling the cumulative P&amp;L is like trying to measure the wind&#8217;s speed by only looking at a bird&#8217;s final displacement. You are measuring the <em>accumulated result</em>, not the <em>generative process</em> that created it. Any statistical measurement you take from the path (like its mean or variance) is a meaningless artifact of <em>how long</em> you measured, not a property of the <em>process itself</em>.</p><p>The returns, <em>x<sub>t</sub></em> = PnL<em><sub>t</sub></em> - PnL<em><sub>t-1</sub></em>, are (or should be) our stationary, <em>I</em>(0) process. <em>This</em> is the generative process. The returns <em>x<sub>t</sub></em> are the sequence of steps in the random walk. This process <em>does</em> have stable properties:</p><ul><li><p>E[<em>x<sub>t</sub></em>] = &#956; (constant)</p></li><li><p>Var(<em>x<sub>t</sub></em>) = &#963;<sup>2</sup> (constant)</p></li></ul><blockquote><p>This insight breaks a fundamental misconception. We are <strong>not</strong> detecting when the P&amp;L curve flattens. That is a lagging, path-dependent observation. We are detecting when the statistical distribution of the <em>steps</em> changes.</p></blockquote><p>The alpha of our strategy is the mean of this distribution, E[<em>x<sub>t</sub></em>] = &#956;. When our strategy <em>breaks</em>, it means the generative process has shifted from <em>P</em><sub>1</sub>(<em>x<sub>t</sub></em> | &#956;<sub>1</sub> &gt; 0, &#963;<sub>1</sub>) to a new process <em>P</em><sub>2</sub>(<em>x<sub>t</sub></em> | &#956;<sub>2</sub> &gt; 0, &#963;<sub>2</sub>).</p><p>Therefore, the first critical action of our script is to differentiate the time series. All subsequent mathematics, every calculation, operates <em>only</em> on the <code>returns</code> array, <em>x<sub>t</sub></em>.</p><p><strong>Point 2: Automating priors with heuristics</strong></p><p>The second major obstacle is that a Bayesian model requires <em>priors</em>. A naive BOCPD script would require a user to specify at least 10 different parameters: &#945;<sub>0</sub>, &#946;<sub>0</sub>, &#954;<sub>0</sub>, &#956;<sub>0</sub> for the prior distribution, &#955; for the hazard rate, and various thresholds for the triggers.</p><p>A model with 10 knobs is not a tool but a research project. It creates a high risk of user error and data-dredging. The <code>bocpd_kill_switch</code> demonstrates a far more robust approach: <em>parameter automation via heuristics</em><strong>.</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!smDM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!smDM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png 424w, https://substackcdn.com/image/fetch/$s_!smDM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png 848w, https://substackcdn.com/image/fetch/$s_!smDM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png 1272w, https://substackcdn.com/image/fetch/$s_!smDM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!smDM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png" width="636" height="311" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:311,&quot;width&quot;:636,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:52389,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/178589813?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!smDM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png 424w, https://substackcdn.com/image/fetch/$s_!smDM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png 848w, https://substackcdn.com/image/fetch/$s_!smDM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png 1272w, https://substackcdn.com/image/fetch/$s_!smDM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39f95d74-f0bf-468f-828d-4102ead7f5dd_636x311.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The <code>analyze_pnl_stream</code> function derives its own parameters from the input data, making it a one-click diagnostic. This is the engineering wrapper that makes the science core usable.</p><pre><code><code># START OF PARAMETER AUTOMATION 
T = len(returns)

# Heuristic 1: burn_in_period
burn_in_period = max(30, int(T * 0.15))

# Heuristic 2: expected_run_length (Lambda)
expected_run_length = max(burn_in_period + 10, int(T / 3.0))

# Heuristic 3: kill_threshold (Shock)
kill_threshold = 0.5 # Fixed: Risk Preference

# Heuristic 4: l_min (Erosion threshold)
l_min = max(15, int(expected_run_length * 0.25))

# Heuristic 5: m_consecutive
m_consecutive = max(5, int(l_min * 0.3))
# END OF AUTOMATION </code></code></pre><p>Let&#8217;s analyze this logic in depth, as it is the secret sauce of the script.</p><p><strong>Heuristic 1: </strong><code>burn_in_period</code><br>We set aside the first 15% of the data (with a 30-tick minimum) to <em>learn</em> the initial parameters of the <em>first</em> regime. This data is passed to the <code>_init_priors</code> function:</p><pre><code><code>def _init_priors(self, burn_in_data):
    if len(burn_in_data) &lt; 2:
        self.prior_m0 = 0.0
        var_guess = 0.01
    else:
        self.prior_m0 = np.mean(burn_in_data)
        var_guess = np.var(burn_in_data)

    self.prior_k0 = 1.0  # Low confidence in prior mean
    self.prior_a0 = 1.0  # Low confidence in prior variance
    
    if var_guess == 0: var_guess = 0.0001
    self.prior_b0 = var_guess * self.prior_a0 # Set prior scale
</code></code></pre><p>This is a critical step. Instead of forcing the user to guess the initial mean and variance of their strategy, we <em>empirically measure</em> them from the <code>burn_in_data</code>. The model is told: </p><blockquote><p><em>Look at these first N ticks. This is what &#8216;normal&#8217; looks like. Your job is to tell me when the data stops looking like this.</em></p></blockquote><p>This makes the model data-driven and adaptive from the very first step. We set <code>prior_k0</code> and <code>prior_a0</code> to 1.0, which are uninformative priors, essentially telling the model, &#8220;<em>Here&#8217;s our initial guess for the mean and variance, but don&#8217;t be too confident in it; learn from the data.</em>&#8221;</p><p><strong>Heuristic 2: </strong><code>expected_run_length</code><strong> </strong>(Lambda,<strong> </strong>&#955;)<br>This is the single most sensitive parameter in the entire model. It defines the <em>hazard function</em>, <em>H</em>, which is the <em>a priori</em> probability of a changepoint at any given step. For a constant hazard rate, as used here, <em>H</em> = 1/&#955;.</p><ul><li><p>A <strong>low </strong>&#955; (e.g., 50) means a high hazard rate (<em>H</em> = 1/50 = 2%). We are telling the model &#8220;<em>I expect regimes to break, on average, every 50 ticks</em>.&#8221; The model will be <em>hyper-sensitive</em>, has a very weak prior on stability, and will over-fit to noise, signaling false changepoints.</p></li><li><p>A <strong>high </strong>&#955; (e.g., 2000) means a low hazard rate (H = 1/2000 = 0.05%). We are telling the model &#8220;<em>I have a very strong belief that regimes are stable for a long time.</em>&#8221; The model will be <em>sluggish</em> and inertial. It will require a catastrophic amount of evidence (i.e., massive losses) to overcome this strong prior.</p></li></ul><p>Choosing &#955; is a dark magic. Our heuristic, <code>expected_run_length = max(burn_in_period + 10, int(T / 3.0))</code>, is a the choice here&#8212;so feel free to tune this part. </p><blockquote><p>It states: <strong>&#8220;My agnostic, default assumption is that this P&amp;L stream will contain approximately 3 distinct regimes.&#8221;</strong> This scales the model&#8217;s sensitivity to the length of the data being analyzed. A 3-year daily backtest (T=750) will get a &#955; &#8776; 250, implying an average regime length of one year. A 1-day tick backtest (T=3000) will get a &#955; &#8776; 1000, implying a new regime every ~1000 ticks. This is a far more robust starting point than a fixed number.</p></blockquote><p><strong>Heuristics 4 &amp; 5: </strong><code>l_min</code><strong> and </strong><code>m_consecutive</code><br>These define our <em>erosion</em> trigger (which we will detail in Point 7). The key insight is that they are <em>not</em> fixed numbers. They are <em>proportional</em> to the <code>expected_run_length</code>.</p><ul><li><p><code>l_min = 0.25 * expected_run_length</code>: The erosion alarm sounds if the <em>current</em> regime&#8217;s expected lifespan (the ERL) falls below 25% of what we <em>a priori</em> defined as a <em>normal</em> lifespan.</p></li><li><p><code>m_consecutive = 0.3 * l_min</code>: The confirmation period&#8212;how long we wait in this &#8220;danger zone&#8221; before pulling the trigger&#8212;is also proportional, set to 30% of the danger threshold itself.</p></li></ul><p>This proportionality makes the trigger <em>time-scale invariant</em>. An Expected Run Length (ERL) of 25 on a swing-trading strategy (where &#955;=100) is just as alarming as an ERL of 250 on an HFT strategy (where &#955;=1000).</p><p><strong>Point 3: The engine (the recursive update)</strong></p><p>Now we enter the core of the <code>BOCPDRunner</code> class: the <code>update</code> function. This is the mathematical engine that propogates our beliefs forward in time.</p><p>The model maintains a vector of probabilities, <em>R<sub>t</sub></em>, where<em>R<sub>t</sub></em>(<em>i</em>) is the probability that the current run length <em>r<sub>t</sub></em> is equal to <em>i</em>.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;R_t(i) = P(r_t = i | x_{1:t})&quot;,&quot;id&quot;:&quot;NYLQZDOXDW&quot;}" data-component-name="LatexBlockToDOM"></div><p>To get the <em>next</em> step&#8217;s probability vector, <em>R<sub>t+1</sub></em>, we must compute two components for every possible previous run length <em>i</em>:</p><ol><li><p><strong>The growth probability:</strong> The probability that the regime <em>continues</em>. The run length  <em>r<sub>t+1</sub></em> will be i+1.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(\\text{Growth}) = P(r_t = i | x_{1:t}) \\cdot P(x_{t+1} | x_{t-i:t}) \\cdot (1-H(i))&quot;,&quot;id&quot;:&quot;UHWLDDHEZG&quot;}" data-component-name="LatexBlockToDOM"></div><ul><li><p><em>P</em>(<em>r<sub>t</sub></em>= i | <em>x</em><sub>1</sub><em><sub>:t</sub></em>) is our prior belief from the last step (the value of <code>log_R_t[i]</code>).</p></li><li><p><em>P</em>(<em>x<sub>t+1</sub></em> | ...) is the <em>predictive likelihood</em>: How likely is the new data point <em>x<sub>t+1</sub></em> given the data we&#8217;ve seen in this <em>i</em>-length run? This is the output of <code>_log_student_t_predictive</code>.</p></li><li><p>(1-<em>H</em>(<em>i</em>)) is the <em>prior probability of no changepoint</em> (our <code>log_neg_hazard</code>).</p></li></ul></li><li><p><strong>The changepoint probability:</strong> The probability that the regime <em>breaks</em>. The new run length <em>r<sub>t+1</sub></em> will be 0.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(\\text{Change}) = P(r_t = i | x_{1:t}) \\cdot P(x_{t+1} | x_{t-i:t}) \\cdot H(i)&quot;,&quot;id&quot;:&quot;QRGVOSEYWO&quot;}" data-component-name="LatexBlockToDOM"></div><ul><li><p><em>H</em>(<em>i</em>) is the <em>hazard rate</em>, the prior probability of a changepoint (our <code>log_hazard</code>).</p></li></ul></li></ol><p>The total probability of a changepoint, <em>P</em>(<em>r<sub>t+1</sub></em> = 0), is the sum of all changepoint probabilities from all possible previous run lengths:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(r_{t+1} = 0 | x_{1:t+1}) = \\sum_{i=0}^{t} P(r_t = i | x_{1:t}) \\cdot P(x_{t+1} | x_{t-i:t}) \\cdot H(i)&quot;,&quot;id&quot;:&quot;UBDFXVRFHI&quot;}" data-component-name="LatexBlockToDOM"></div><p>The new probability vector <em>R<sub>t+1</sub></em> is then formed by:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{align}\nR_{t+1}(0) &amp;= P(r_{t+1} = 0 \\mid \\ldots) \\\\\nR_{t+1}(i+1) &amp;= P(r_t = i \\mid \\ldots)\\cdot P(x_{t+1} \\mid \\ldots)\\cdot (1 - H(i))\n\\end{align}\n\n&quot;,&quot;id&quot;:&quot;TPTRHOUHPN&quot;}" data-component-name="LatexBlockToDOM"></div><p>Finally, we must normalize <em>R<sub>t+1</sub></em>so it sums to 1. This trellis of calculations, where each step&#8217;s probabilities are passed to the next, is the core of the algorithm. However, a naive implementation of this math <em>will not work</em>.</p><p><strong>Point 4: Numerical stability via log-space</strong></p><p>Why is the <code>bocpd_kill_switch</code> script filled with <code>np.log</code>, <code>np.exp</code>, <code>gammaln</code>, and <code>logsumexp</code>? Why not just multiply the probabilities as defined in Point 3?</p><blockquote><p>Answer: <em>Numerical underflow.</em></p></blockquote><p>Probabilities are small numbers, often &lt; 1.0. Multiplying many small numbers together creates <em>astronomically</em> small numbers.</p><ul><li><p><em>P</em>(<em>seq</em>) = 0.1 x 0.5 x 0.2 x 0.1 = 0.001<br>After just 100 ticks, a typical sequence probability might be 0.1<sup>100</sup> = 10<sup>-100</sup>.</p></li></ul><p>A standard 64-bit floating-point number in Python (<code>float</code>) cannot represent this. The machine precision bottoms out (around 10<sup>-308</sup>), and the number is rounded to <code>0.0</code>. The algorithm breaks. The entire probability vector <em>R<sub>t</sub></em> becomes <code>[0.0, 0.0, ..., 0.0]</code>. This isn&#8217;t a minor bug but a complete failure of the algorithm.</p><blockquote><p>The solution: <em>We perform all calculations in log-space.</em></p></blockquote><p>By applying the logarithm, we transform our numbers from the probability domain [0, 1] to the log-probability domain [-inf, 0]. This conversion exploits two key mathematical identities:</p><ul><li><p><strong>Multiplication becomes addition:</strong> log(<em>A&#183;B</em>)=log(<em>A</em>)+log(<em>B</em>)<br>This is computationally fast and numerically stable. Adding log(10<sup>-100</sup>) and log(10<sup>-50</sup>) gives -100&#183;log(10) - 50&#183;log(10) = -230 - 115 = -345. This is a perfectly reasonable <code>float</code> for a computer to store.</p></li><li><p><strong>Addition becomes </strong><code>logsumexp</code><strong>:</strong> log(A + B) = log(e<sup>log A</sup> + e<sup>log B</sup>)<br>This is the tricky part. We can&#8217;t just add log-probabilities. The <code>logsumexp</code> function is a numerically careful way to compute this by factoring out the maximum value: </p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\log(A+B) = \\log(e^{\\log A - \\log M} + e^{\\log B - \\log M}) + \\log M, \\text{ where } \\log M = \\max(\\log A, \\log B)&quot;,&quot;id&quot;:&quot;NDZJFJHAAY&quot;}" data-component-name="LatexBlockToDOM"></div></li></ul><p>Now, look at the <em>actual</em> implementation in the <code>update</code> function and compare it to the math in Point 3.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(\\text{Growth}) = P(r_t=i) \\cdot P(x_{t+1} | ...) \\cdot (1-H(i))\n&quot;,&quot;id&quot;:&quot;VKXWZOJEZI&quot;}" data-component-name="LatexBlockToDOM"></div><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(\\text{Change}) = \\sum_{i} P(r_t=i) \\cdot P(x_{t+1} | ...) \\cdot H(i)&quot;,&quot;id&quot;:&quot;CEVCXUPRNW&quot;}" data-component-name="LatexBlockToDOM"></div><p>The part of the code that is in charged of this is:</p><pre><code><code># log_R_t is log(P(r_t=i))
# log_pred_probs is log(P(x_{t+1} | ...))
# self.log_neg_hazard is log(1 - H(i))
# self.log_hazard is log(H(i))

# 1. Growth (Multiplication -&gt; Addition)
log_growth_probs = self.log_R_t + log_pred_probs + self.log_neg_hazard

# 2. Change (Sum of Products -&gt; logsumexp of sums)
log_cp_prob = logsumexp(self.log_R_t + log_pred_probs + self.log_hazard)

# ...

# 5. Normalization (Division -&gt; Subtraction)
# P(r_t) / sum(P(r_t)) -&gt; log(P(r_t)) - log(sum(P(r_t)))
# log(sum(P(r_t))) = log(sum(exp(log(P(r_t))))) = logsumexp(log_R_t)
log_norm_const = logsumexp(new_log_R_t)
self.log_R_t = new_log_R_t - log_norm_const</code></code></pre><p>This is a <em>direct</em> translation of the recursive Bayesian update into a numerically stable form. This section is not a minor detail. This log-space implementation is the <em>only</em> thing that allows the algorithm to run for more than a few hundred ticks. It is the difference between a textbook theory and a production-grade tool.</p><p><strong>Point 5: The N-IG / Student-t likelihood</strong></p><p>We have an engine. Now we must feed it. The most important part of the engine is the <code>log_pred_probs</code>&#8212;the<em> predictive likelihood function P</em>(<em>x<sub>t+1</sub></em>|<em>x<sub>t-1:t</sub></em>). This function answers the question: <em>How likely is this new data point, given the regime we are assuming we&#8217;re in?</em></p><p>The second, and equally fatal, mistake a quant can make is to assume a <em>Gaussian (Normal) distribution</em> for returns.</p><p>Financial returns are not Gaussian. They are leptokurtic. They have fat tails and excess kurtosis. A 4-sigma event, which a Gaussian model treats as a 1-in-3.5-million impossibility (P&#8776;3x10<sup>-5</sup>), is something a financial market produces every few years, or even months. It is <em>not</em> an impossible event; it is an <em>expected</em> (if infrequent) feature of the landscape.</p><p>If you use a Gaussian likelihood (<code>scipy.stats.norm.pdf</code>):</p><ol><li><p>A 4-sigma return <em>x<sub>t</sub></em> occurs.</p></li><li><p>The model calculates its likelihood: <em>P</em>(<em>x<sub>t</sub></em> | Gaussian regime)&#8776;3x10<sup>-5</sup>.</p></li><li><p>In log-space, this is a <em>massive</em> penalty: log(10<sup>-5</sup>) &#8776; -11.5.</p></li><li><p>This huge negative number is added to all <code>log_growth_probs</code>, effectively killing them.</p></li><li><p>The model concludes it is <em>impossible</em> for <em>x<sub>t</sub></em> to have come from the old regime. <em>P</em>(<em>x<sub>t+1</sub></em>=0) spikes to ~100%.</p></li><li><p>The model signals a <strong>false positive</strong>. It has mistaken a normal (but volatile) market day for a structural break.</p></li></ol><p>We must use a likelihood model that <em>expects</em> fat tails. Our script does this using a <strong>Normal-Inverse-Gamma (N-IG)</strong> prior.</p><p>This is a 4-parameter conjugate prior for a Gaussian process <em>with both unknown mean AND unknown variance</em>. This is key. We are not just modeling the mean (our alpha); we are modeling the mean, the variance (our risk), and our <em>uncertainty</em> about both.</p><p>The N-IG prior is defined by four hyperparameters (which we set in <code>_init_priors</code>):</p><ul><li><p><code>prior_m0</code> (&#956;<sub>0</sub>): The prior mean.</p></li><li><p><code>prior_k0</code> (&#954;<sub>0</sub>): Our confidence in the prior mean.</p></li><li><p><code>prior_a0</code> (&#945;<sub>0</sub>): The shape of the prior variance (an Inverse-Gamma dist).</p></li><li><p><code>prior_b0</code> (&#946;<sub>0</sub>): The scale of the prior variance.</p></li></ul><p>The mathematical magic is this: when you have a Gaussian likelihood with an N-IG prior, the <em>posterior predictive distribution</em> (the distribution of a <em>new</em> data point, after integrating out all uncertainty about the <em>unknown</em> &#956; and &#963;<sup>2</sup>) is <strong>not</strong> a Gaussian. It is a Student-t distribution.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rVEi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rVEi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png 424w, https://substackcdn.com/image/fetch/$s_!rVEi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png 848w, https://substackcdn.com/image/fetch/$s_!rVEi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png 1272w, https://substackcdn.com/image/fetch/$s_!rVEi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!rVEi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png" width="935" height="373" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:373,&quot;width&quot;:935,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;comparison of t and z distibutions&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="comparison of t and z distibutions" title="comparison of t and z distibutions" srcset="https://substackcdn.com/image/fetch/$s_!rVEi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png 424w, https://substackcdn.com/image/fetch/$s_!rVEi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png 848w, https://substackcdn.com/image/fetch/$s_!rVEi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png 1272w, https://substackcdn.com/image/fetch/$s_!rVEi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdd904e4-fb79-4fb1-9a98-f88a9f4c4a74_935x373.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The <code>_log_student_t_predictive</code> function is the log-PDF of this distribution:</p><pre><code><code>def _log_student_t_predictive(self, data_point):
    # ...
    # df = degrees of freedom
    df = 2 * self.alphas
    loc = self.mus
    scale_sq = self.betas * (self.kappas + 1) / ((self.alphas * self.kappas) + epsilon)
    # ...
    log_prob = (gammaln(df / 2.0 + 0.5) -
                gammaln(df / 2.0) -
                0.5 * np.log(df * np.pi * scale_sq) -
                (df / 2.0 + 0.5) * np.log(1.0 + (data_point - loc)**2 / (df * scale_sq)))
    
    return log_prob
</code></code></pre><p>Notice that the degrees of freedom <code>df</code> are not fixed, but are derived from <code>self.alphas</code>, which is <em>learned from the data</em>. As the model sees more data in a run (and <code>self.alphas</code> grows), the <code>df</code> increases, and the Student-t distribution <em>slowly converges to a Gaussian</em>. This is mathematically beautiful. It&#8217;s a model that says </p><blockquote><p><em>&#8220;When I have little data (low </em><code>df</code><em>), I assume fat tails. As I gain confidence from more data (high </em><code>df</code><em>), I will tighten my assumptions and converge towards a Normal distribution.&#8221;</em></p></blockquote><p>When this model (with a low <code>df</code>, e.g., <code>df=4</code>) sees a 4-sigma event, the t-distribution assigns it a low, but <em>not impossible</em>, probability (e.g., <em>P</em>=0.01). The log-likelihood is log(0.01)&#8776;-4.6.</p><ul><li><p>Gaussian penalty: -11.5</p></li><li><p>Student-t penalty: -4.6<br>The t-distribution&#8217;s penalty is <em>far</em> smaller. The model thinks: </p></li></ul><blockquote><p>&#8220;That was unlikely, but plausible. I will reduce my belief in this regime, but I will not discard it.&#8221; </p></blockquote><p>This prevents the false positive. The N-IG model is updated analytically in <em>O</em>(1) time, which is what the <code>update</code> function does:</p><pre><code><code># 6. Update sufficient statistics for each run length
new_alphas = self.alphas + 0.5
new_betas = self.betas + (self.kappas * (data_point - self.mus)**2) / (2.0 * (self.kappas + 1.0))
new_kappas = self.kappas + 1.0
new_mus = (self.kappas * self.mus + data_point) / new_kappas
</code></code></pre><p>This is the conjugate update. It is the learning part of the algorithm.</p><ul><li><p><code>new_kappas</code> and <code>new_alphas</code> grow with each data point, increasing our confidence (<code>df</code> goes up).</p></li><li><p><code>new_mus</code> becomes a weighted average of the prior mean and the data mean.</p></li><li><p><code>new_betas</code> accumulates the sum of squared errors, updating our variance estimate.</p></li></ul><p><strong>Point 6: State-space pruning</strong></p><p>We have a robust, numerically stable model. Now we have a new problem: <em>computational complexity.</em></p><p>Let&#8217;s follow the logic:</p><ul><li><p>At time <em>t</em>=100, our <code>log_R_t</code> vector has 101 elements (for <em>x<sub>t</sub></em>=0 to <em>x<sub>t</sub></em>=100). We also have 101 sets of N-IG parameters (<code>alphas</code>, <code>betas</code>, <code>kappas</code>, <code>mus</code>).</p></li><li><p>To compute <code>log_R_t</code> at <em>t</em>=101, we must update all 101 hypotheses.</p></li><li><p>At time <em>t</em>=<em>T</em>, our <code>log_R_t</code> vector has <em>T</em>+1 elements. We must run <em>T</em>+1 updates.</p></li><li><p>The total work to process a stream of length <em>T</em> is 1 + 2 + 3 + ... + <em>T</em> = <em>T</em>(<em>T</em>+1<em>)/2</em>.</p></li><li><p>The complexity is <em>O</em>(<em>T</em><sup>2</sup>), or quadratic.</p></li></ul><p>This is a <em>catastrophic</em> failure for a high-frequency context. A 10,000-tick backtest would require ~50,000,000 operations. A 100,000-tick backtest would require ~5,000,000,000 operations. The algorithm does not scale. It is not <em>online</em>. It&#8217;s not just a <em>speed</em> problem; it&#8217;s a <em>memory</em> problem. We&#8217;d have to store <em>T</em> copies of our 4 state-space vectors, an <em>O</em>(<em>T</em><sup>2</sup>) memory footprint.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vDlq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vDlq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png 424w, https://substackcdn.com/image/fetch/$s_!vDlq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png 848w, https://substackcdn.com/image/fetch/$s_!vDlq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png 1272w, https://substackcdn.com/image/fetch/$s_!vDlq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!vDlq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png" width="646" height="566" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:566,&quot;width&quot;:646,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:45840,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/178589813?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vDlq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png 424w, https://substackcdn.com/image/fetch/$s_!vDlq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png 848w, https://substackcdn.com/image/fetch/$s_!vDlq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png 1272w, https://substackcdn.com/image/fetch/$s_!vDlq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e853c2-c558-4501-83cc-c4f99b304e51_646x566.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The reason of the catastrohpic failure</figcaption></figure></div><p>If we are at <em>t</em>=1000, what is the probability that the <em>true</em> run length is <em>x<sub>t</sub></em>=5 It is astronomically small. What about <em>x<sub>t</sub></em>=998? Also astronomically small. The <em>true</em> posterior probability mass <em>P</em>(<em>x<sub>t</sub></em>| <em>x<sub>t</sub></em><sub>:1</sub>) is clustered around a few, plausible run lengths. We do not need to waste CPU cycles and RAM updating hypotheses that have a 10<sup>-50</sup> probability of being correct.</p><p>Our script implements this optimization aggressively in the <code>update</code> function:</p><pre><code><code># 8. Pruning: Discard states with vanishingly small probability
keep_indices = np.where(self.log_R_t &gt; self.prune_threshold)[0]

if len(keep_indices) == 0:
     # Failsafe: if pruning is too aggressive, keep at least the most likely state
    keep_indices = np.array([np.argmax(self.log_R_t)])

self.log_R_t = self.log_R_t[keep_indices]
self.alphas = self.alphas[keep_indices]
self.betas = self.betas[keep_indices]
self.kappas = self.kappas[keep_indices]
self.mus = self.mus[keep_indices]
</code></code></pre><p>The <code>self.prune_threshold</code> is set to <code>-10.0</code>. Since we are in log-space, this means log(<em>P</em>) = -10 &#8658; <em>P</em> = e<sup>-10</sup> &#8776; 0.000045.</p><p>We are <em>aggressively culling</em> any run-length hypothesis whose posterior probability falls below 0.0045%.</p><p>The result is that the number of active hypotheses, <em>K</em> (the length of <code>self.log_R_t</code>), does not grow to <em>T</em>. It stays at a small, roughly constant size, <em>K</em> &lt;&lt; <em>T</em> (e.g., <em>K</em> &#8776; 100-200, regardless of whether <em>T</em>=1,000 or <em>T</em>=10,000,000).</p><p>This optimization reduces the computational complexity. This is <em>linear time complexity</em>. The memory footprint is also reduced from O(T<sup>2</sup>) to <em>O</em>(<em>K&#183;T</em>). <em>This</em> is what makes the algorithm &#8220;online&#8221; and computationally feasible for real-world, high-frequency data streams. The <code>prune_threshold</code> is a trade-off between accuracy and speed, but at <code>-10.0</code>, the amount of discarded probability mass is negligible.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lF1e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" width="1456" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1158490,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3&quot;,&quot;text&quot;:&quot;Send me a copy&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3"><span>Send me a copy</span></a></p><p><strong>Point 7: The dual-trigger system </strong></p>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-switch-off-bayesian-online">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[​[WITH CODE​]​ Optimization: Scale-free portfolio selection ]]></title><description><![CDATA[A practical guide to implementing AdaHedge with L1 turnover caps and trust bands]]></description><link>https://www.quantbeckman.com/p/with-code-scale-free-portfolio-selection</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-scale-free-portfolio-selection</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Mon, 03 Nov 2025 08:11:13 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>Risks and model limitations.</p></li><li><p>Online Portfolio Selection without knobs.</p></li><li><p>Mathematical framework.</p><ol><li><p>AdaHedge and AdaNormalHedge.</p></li><li><p>The L1 turnover cap and trust band.</p></li></ol></li><li><p>Why this breaks common misconceptions.</p></li><li><p>Algorithm design.</p></li><li><p>Strongly-adaptive aggregation and diagnostics.</p></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read full story</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Introduction</h4><p>For decades, the world of quantitative portfolio management has been sitting on a fragile foundation. This isn&#8217;t a problem of bad math or weak computing power. It&#8217;s a problem of <em>philosophy</em>. Whether you&#8217;re running a small, nimble fund or a massive institutional book, the core problem is the same: your model is riddled with <em>knobs.</em></p><p>These knobs are the parameters, the magic numbers that you, or some sophisticated optimization routine, painstakingly tuned to make your strategy look perfect on a historical backtest. They are the ghosts in the machine. </p><p>Let&#8217;s be specific, as this isn&#8217;t an abstract complaint. These are real, structural points of failure present in 99% of models. Perhaps the most critical is the <strong>learning rate</strong>, or &#951;. In any gradient-based allocator, like Exponentiated Gradient or Online Newton Step, this knob dictates <em>how fast</em> your model learns from new information. If you tune &#951; to be high, you get a fast, twitchy model that reacts instantly and gets whipsawed by every piece of market noise, churning your portfolio to dust. If you tune &#951; to be low, you get a stable model that smoothly integrates new information but fails to react when a real crisis hits, leaving you fully exposed as the market crashes for three weeks straight. The &#951; you tuned for the low-vol calm of 2017 is <em>guaranteed</em> to be catastrophically wrong in the high-vol panic of March 2020.</p><p>Equally pervasive is the hand-picked <strong>lookback window</strong>, <em>W</em>. This is the 60-day in your 60-day momentum signal, or the 252-day in your annual volatility calculation. What&#8217;s magic about 60 days? Nothing. It&#8217;s a compromise. A short window is highly adaptive but incredibly noisy; a long window is stable but dangerously slow. When a regime breaks, your 252-day risk model is still averaging in data from a world that <em>no longer exists</em>. For the next six months, your optimal portfolio will be optimized for a dead regime.</p><p>Then there is the <strong>penalty weight</strong>, &#955;, the knob that governs cost. In a classic mean-variance optimization, this &#955; is the trade-off between expected return and expected risk. In a cost-aware allocator, it&#8217;s the &#955; in your &#955;||w<sub>t</sub> - w<sub>t-1</sub>||<sub>1 </sub>turnover penalty. You run a backtest and find that &#955;=0.0005 is optimal. But this &#955; is static. It doesn&#8217;t know that market liquidity just dried up, making your <em>actual</em> costs 10x higher. It doesn&#8217;t know that your AUM just doubled, meaning your market impact is now a real factor. Your optimal penalty is just an artifact of your backtest&#8217;s specific liquidity and volatility profile.</p><p>Finally, you have the heuristic <strong>rebalancing thresholds</strong>, &#964;. These are the simple <code>if...then</code> rules, like <em>If my target weights change by less than 1%, do nothing.</em> This is an attempt to reduce churn, but it&#8217;s just another arbitrary knob. A 1% threshold might be fine for a large-cap equity, but it&#8217;s a <em>massive</em> move for a bond. This single, fixed number is applied across all assets, ignoring their individual volatilities, and it&#8217;s almost certainly sub-optimal.</p><p>This entire process of <em>knob-tuning</em> creates a dangerous <strong>illusion of precision</strong>. Quants spend 80% of their time just turning these knobs, running thousands of backtests to find the combination that squeezes out another 0.1 of Sharpe ratio. But this isn&#8217;t finding signal. It&#8217;s <em>overfitting</em>. It&#8217;s a brute-force memorization of the <em>past&#8217;s</em> specific noise and volatility patterns.</p><blockquote><p>Here&#8217;s the dilemma: in a stable, stationary, predictable market&#8212;the Gaussian-fiction market that lives in textbooks&#8212;these optimized knobs would be your friends. They would represent the <em>true</em>, unchanging parameters of the market, and your model would be perfectly adapted.</p><p><em>But we don&#8217;t trade in that market.</em></p></blockquote><p>We trade in a market defined by non-stationarity. We live in a world of sudden, violent <strong>regime breaks</strong>. These are the events that change the underlying rules of the game: volatility spikes, correlation flips, global pandemics, sudden wars, or surprise central bank announcements.</p><p>In <em>this</em> context, your knobs are your enemy. Sounds familiar?</p><p>The moment the regime breaks, every single one of your tuned parameters is not just sub-optimal; it&#8217;s <em>actively dangerous. </em>That low &#951; you tuned for calm markets now forces your model to <em>under-react</em>, making you ride the crash all the way down. That long 252-day lookback window is now feeding your optimizer toxic, irrelevant data, telling it that &#8220;everything is fine.&#8221; That &#955; cost-penalty you set now <em>prevents</em> the model from making the single, large, life-saving trade&#8212;getting to cash&#8212;because it looks <em>too expensive</em> relative to its historical tuning.</p><p>Your finely-tuned, optimized model is now precisely and perfectly calibrated for a market that no longer exists. This is the fragile foundation. The model&#8217;s precision was a mirage, and the knobs are the levers that ensure its failure.</p><p>But don&#8217;t worry. There are alternatives! So to learn a little more about the topic we&#8217;re going to discuss, take a look at this PDF:</p><div class="file-embed-wrapper" data-component-name="FileToDOM"><div class="file-embed-container-reader"><div class="file-embed-container-top"><image class="file-embed-thumbnail" src="https://substackcdn.com/image/fetch/$s_!eQT-!,w_400,h_600,c_fill,f_auto,q_auto:best,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca10898-c366-4a25-8d3c-b06b7f37feb8_608x928.png"></image><div class="file-embed-details"><div class="file-embed-details-h1">Adahedge</div><div class="file-embed-details-h2">505KB &#8729; PDF file</div></div><a class="file-embed-button wide" href="https://www.quantbeckman.com/api/v1/file/fae747d3-63d2-4bb1-9898-6f23b0d030d5.pdf"><span class="file-embed-button-text">Download</span></a></div><a class="file-embed-button narrow" href="https://www.quantbeckman.com/api/v1/file/fae747d3-63d2-4bb1-9898-6f23b0d030d5.pdf"><span class="file-embed-button-text">Download</span></a></div></div><p>But before starting, let&#8217;s clarify that scale-free is not the same in this context as in the operational-trading context where scale-free means the rules and decisions <em>do not depend on arbitrary units</em> (euros, ticks, fixed timeframes, account size) but on <em>dimensionless or risk&#8209;normalized</em> magnitudes. In practice, it&#8217;s <em>invariance</em>: if prices are rebased, time is re&#8209;sampled, or capital is resized, the logic and the risk stay (approximately) the same&#8212;up to frictional limits (costs, impact, discrete tick sizes).</p><p>Formally, let r&#8712;R<sup>N</sup> denote the vector of (log) returns and w(r)&#8712;&#916;<sub>N</sub> the allocation chosen as a function of returns/features. After per&#8209;strategy volatility targeting (or any equivalent scale normalization), <em>homogeneity of degree zero</em> is the desideratum:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\boxed{\\quad w(\\alpha r) = w(r)\\quad \\text{for all } \\alpha>0.\\quad}&quot;,&quot;id&quot;:&quot;EUHAFGPYQJ&quot;}" data-component-name="LatexBlockToDOM"></div><p>This expresses independence from the overall return scale.</p><p>Now, even though we&#8217;re going to explore and critique the scale-free framework, remember that there are two critical aspects that make the difference between a hobby and a business:</p><ul><li><p><em>Scale-free risk</em> ensures <strong>each individual strategy</strong> behaves consistently across volatility and capital regimes.</p></li><li><p><em>Scalable strategy pool</em> ensures <strong>your collection of strategies</strong> can grow without turning into an unmanageable mess of correlated exposures.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Brx4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Brx4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png 424w, https://substackcdn.com/image/fetch/$s_!Brx4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png 848w, https://substackcdn.com/image/fetch/$s_!Brx4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png 1272w, https://substackcdn.com/image/fetch/$s_!Brx4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Brx4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png" width="1456" height="827" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:827,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1629692,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/177352465?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Brx4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png 424w, https://substackcdn.com/image/fetch/$s_!Brx4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png 848w, https://substackcdn.com/image/fetch/$s_!Brx4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png 1272w, https://substackcdn.com/image/fetch/$s_!Brx4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F415f1a1b-612e-4c3c-b9e4-26db19dd1a87_1460x829.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Risks and model limitations</h4><p>The moment the market regime changes, your knobs are wrong.</p><p>You can get the math right, you can have the perfect theoretical model, and <em>still</em> fail. Why?</p><ol><li><p>Your <strong>learning rate</strong> was too low, and the allocator failed to adapt to the new volatility regime. It just sat there, bleeding.</p></li><li><p>Your <strong>lookback window</strong> was too long, and your risk model was still averaging in data from a &#8220;dead&#8221; regime, making you under-allocate to the new winners.</p></li><li><p>Your <strong>turn penalty</strong> was just high enough to stop you from making the <em>one</em> simple rebalance that actually mattered, all because it was drowned out by noise.</p></li></ol><p>This leads to a horrible checkpointing culture. Quants are forced to constantly re-tune, re-backtest, and redeploy their models, effectively manually overriding the system. This isn&#8217;t alpha, it&#8217;s just brittle engineering.</p><p>The second issue, which is more insidious, is the claim of <em>parameter-free</em> models. Many algorithms claim to be parameter-free, but they merely hide their defaults. The success or failure of the entire strategy often hinges on some obscure &#951; decay schedule or a default window size that the authors conveniently found. This isn&#8217;t just bad science; it&#8217;s dangerous in production.</p><p>This fragility exposes any portfolio allocator to three fundamental risks:</p><ol><li><p><strong>Scale risk:</strong> Any update that isn&#8217;t <em>scale-free</em> is inherently dangerous. If your algorithm is tuned on 60 basis point (bp) daily volatility, it <em>will</em> misfire when volatility drops to 10 bps or spikes to 200 bps. A 1% loss and a 10% loss are not just different in magnitude; they are different in <em>kind</em>. Your allocator must react with a step size proportional to the signal, not with a fixed, arbitrary &#951;.</p></li><li><p><strong>Cost risk:</strong> Most online learning literature ignores transaction costs, or models them in a way that doesn&#8217;t enforce them. An allocator that flips its entire portfolio every day might look great on a gross basis, but it will donate its edge (and then some) to the market makers. You need a model that is <em>cost-aware</em> from the ground up, not one where cost is just another penalty term to be tuned.</p></li><li><p><strong>Adaptation risk:</strong> The market is non-stationary. A static optimizer that minimizes regret against a <em>single fixed comparator</em> (e.g., the best-performing asset over the <em>entire</em> 10-year backtest) is useless. Who cares? The game is to adapt. When a multi-month regime transition happens, your allocator can&#8217;t bleed for 3 months while it waits for its long-window average to catch up. The correct target is strong <em>local</em> performance, not global averages.</p></li></ol><p>And here is where r<em>egime breaks</em> come into action. This is the sudden change in loss geometry, volatility, and asset correlation. This is the event that makes all your tuned parameters obsolete overnight. So how do we solve this?  The answer is to build an allocator that can allocate on the <em>simplex</em> &#916; without <em>any</em> hand-tuning. It must be <em>scale-free</em> (to handle vol changes),<em> cost-aware</em> (to handle turnover), and <em>strongly-adaptive </em>(to handle regime breaks).</p><p>The solution that the machine learning and quant world offers, as we&#8217;ll see, is to move away from classic Exponentiated Gradient or Online Newton Step and embrace <em>scale-free Hedge updates</em> (like AdaHedge) at the asset level. This allocator is then coupled with a lazy rebalancing mechanism and strongly-adaptive wrappers to create a system that is robust by construction, not by tuning. No learning rate search, no window selection, no brittle temperature. Just math that works.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><h4>Online Portfolio Selection without knobs</h4><p>Let&#8217;s formalize this. We&#8217;re operating in the classic Online Portfolio Selection or OPS setting. This is a sequential decision problem.</p><p>At the start of each day <em>t</em>=1, &#8230;, <em>T</em>, we must choose a long-only weight vector w<sub>t</sub>. This vector must live on the <em>N</em>-dimensional simplex, &#916;<em><sup>N</sup></em>, meaning all weights are non-negative and sum to one:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\mathbf{w}_t \\in \\Delta^N := \\{ \\mathbf{w} \\ge 0, \\sum_{i=1}^N w_i = 1 \\}&quot;,&quot;id&quot;:&quot;ADDKNJIVAX&quot;}" data-component-name="LatexBlockToDOM"></div><p>At the end of the day, the market reveals a vector of log-returns, r<sub>t </sub>&#8712;R<em><sup>N</sup></em></p><p>Our gross wealth, in log space, evolves as:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\text{Eq}_{t+1}^{\\text{gross}} = \\text{Eq}_t^{\\text{gross}} + \\langle \\mathbf{w}_t, \\mathbf{r}_t \\rangle&quot;,&quot;id&quot;:&quot;EAQNDOCKDG&quot;}" data-component-name="LatexBlockToDOM"></div><p>This is the standard, frictionless world. But trading is not free. We must model transaction costs. The most realistic model for institutional trading is a <em>proportional L1 turnover cost</em>. The cost to trade is proportional to the <em>absolute value</em> of the change in your portfolio weights.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\text{cost}_t = \\lambda \\| \\mathbf{w}_{t+1} - \\mathbf{w}_t \\|_1&quot;,&quot;id&quot;:&quot;SYFYBBKKXX&quot;}" data-component-name="LatexBlockToDOM"></div><p>Here, &#955; is the cost parameter (e.g., &#955;=0.0002 for 2 bps). The L1 norm is key. If you sell 10% of asset A to buy 10% of asset B, your L1 turnover is |-0.10| + |+0.10| = 0.20.</p><p>Our <em>net</em> log-wealth, the only thing that actually matters, is therefore:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\text{Eq}_{t+1}^{\\text{net}} = \\text{Eq}_t^{\\text{net}} + \\langle \\mathbf{w}_t, \\mathbf{r}_t \\rangle - \\text{cost}_t&quot;,&quot;id&quot;:&quot;CWZNHFAYYM&quot;}" data-component-name="LatexBlockToDOM"></div><p>Most OPS methods, like <em>Exponentiated Gradient </em>(EG) or <em>Online Newton Step</em> (ONS), propose an update based on the day&#8217;s losses. Let L<sub>t,i</sub> = -r<sub>t,i </sub>be the loss for asset <em>i</em>. The classic EG update is:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;w_{t+1, i} \\propto w_{t, i} \\exp(-\\eta \\ell_{t, i})&quot;,&quot;id&quot;:&quot;LPWCNJSTBS&quot;}" data-component-name="LatexBlockToDOM"></div><p>And here it is: &#951;. The learning rate. The <em>knob</em>.</p><p>This &#951; is poison. It is scale-sensitive. If your typical losses L<sub>t,i</sub>  are around 0.01 (1%), an &#951;=0.5 might be fine. If the market panics and losses jump to 0.10 (10%), your update term exp(-0.5 x 0.10) is tiny, but exp(-0.5 x -0.10) is huge. The allocator&#8217;s weights will fly around, becoming brittle and unpredictable. You&#8217;ll either over-trade or under-react.</p><p>The core idea is to <em>remove </em>&#951;. We will replace it with a <em>data-driven, per-asset normalizer</em> that automatically adapts the step size based on the <em>history</em> of losses for that <em>specific asset</em>. This is the scale-free idea.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LMQ8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LMQ8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png 424w, https://substackcdn.com/image/fetch/$s_!LMQ8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png 848w, https://substackcdn.com/image/fetch/$s_!LMQ8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png 1272w, https://substackcdn.com/image/fetch/$s_!LMQ8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!LMQ8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png" width="1456" height="959" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:959,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1333690,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/177352465?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!LMQ8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png 424w, https://substackcdn.com/image/fetch/$s_!LMQ8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png 848w, https://substackcdn.com/image/fetch/$s_!LMQ8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png 1272w, https://substackcdn.com/image/fetch/$s_!LMQ8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F464c3494-ea29-46d0-a4ba-1cd8b08bc719_1486x979.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Building this <em>knob-less</em> allocator forces us to confront five major obstacles that are typically swept under the rug:</p><ol><li><p><strong>Costs and overtrading:</strong> We must not just <em>model</em> costs, we must <em>constrain</em> them. Even with a good update rule, a single large loss can cause a massive, costly portfolio jump. The algorithm <em>must</em> incorporate a trust band (to skip tiny, noisy rebalances) and a turnover cap (to prevent catastrophic, large rebalances).</p></li><li><p><strong>Non-stationarity:</strong> As discussed, the world changes. Static regret (beating the best fixed portfolio) is insufficient. We need a system that performs well on interval regret (beating the best fixed portfolio on <em>any</em> sub-interval) and dynamic regret (adapting to shifting regimes). This requires a strongly-adaptive wrapper.</p></li><li><p><strong>Numerical stability:</strong> Exponentials are dangerous. exp(1000) is <code>inf</code>. exp(-1000) is <code>0.0</code>. An update like exp(-&#951;L<sub>t</sub>) can easily overflow or underflow. The algorithm must be numerically stabilized, likely using the <code>log-sum-exp</code> trick, and must handle near-zero weights gracefully.</p></li><li><p><strong>Asset-level vs. expert-level:</strong> Many online aggregation methods assume you have a set of &#8220;experts&#8221; (e.g., 10 different pre-built strategies) and you&#8217;re just weighting them. This is a cop-out. We are addressing the harder problem: allocating directly to the <em>N</em> assets themselves. Our experts <em>are</em> the assets.</p></li><li><p><strong>Live mapping to orders:</strong> A vector w<sub>t</sub> is not an order. A practical OPS algorithm must produce feasible, tradable orders. This means the weight vector must be translatable into orders that respect market freeze levels, safety ticks, partial fills, and other production realities.</p></li></ol><p>Our goal is to build a single, coherent algorithm that solves all five of these problems simultaneously.</p><h4>Mathematical framework</h4><p>Here is the resolution. We will build our solution from the ground up, starting with a new mathematical framework.</p><p>Let&#8217;s be precise.</p><ul><li><p>Per-asset losses are:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\ell_t = - \\mathbf{r}_t \\in \\mathbb{R}^N&quot;,&quot;id&quot;:&quot;YYJILHUGJA&quot;}" data-component-name="LatexBlockToDOM"></div></li><li><p>The portfolio&#8217;s loss (ignoring costs for a moment) is:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\ell_t(\\mathbf{w}_t) = \\langle \\mathbf{w}_t, \\ell_t \\rangle&quot;,&quot;id&quot;:&quot;POIPRHBUVS&quot;}" data-component-name="LatexBlockToDOM"></div></li></ul><p>The entire field of Online Convex Optimization, from which OPS is derived, is built around the concept of <em>regret</em>. Regret is the difference between your algorithm&#8217;s cumulative loss and the loss of some optimal <em>comparator</em> in hindsight. The game is to minimize this regret. Check the previous article to know more about that:</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;1056f43b-2233-492f-a500-cea61db2fb79&quot;,&quot;caption&quot;:&quot;&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;[WITH CODE] Optimization: Adaptive regret for regime-shifting markets&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:311582722,&quot;name&quot;:&quot;&#120448;&#120478;&#120458;&#120471;&#120477; &#120433;&#120462;&#120460;&#120468;&#120470;&#120458;&#120471;&quot;,&quot;bio&quot;:&quot;Inside the intersection of data, mathematics, and systematic architecture &#8212; where research meets execution to build sustainable alpha.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/29f90c76-3fd2-4648-8b68-02caa8de3e77_800x800.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:100}],&quot;post_date&quot;:&quot;2025-10-20T11:58:39.377Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png&quot;,&quot;cover_image_alt&quot;:null,&quot;cover_image_crop&quot;:null,&quot;canonical_url&quot;:&quot;https://www.quantbeckman.com/p/with-code-optimization-adaptive-regret&quot;,&quot;section_name&quot;:&quot;Alpha Lab&quot;,&quot;video_upload_id&quot;:null,&quot;id&quot;:175403841,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:12,&quot;comment_count&quot;:0,&quot;publication_id&quot;:3833331,&quot;publication_name&quot;:&quot;Trading the Breaking&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>The key perspective shift is in <em>what</em> you choose as your comparator.</p><ol><li><p><strong>Static regret</strong> &#8594; This is the classic definition. The comparator is the single best fixed-weight portfolio over the entire history <em>T</em>.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;R_T^{\\text{stat}} = \\sum_{t=1}^T \\ell_t(\\mathbf{w}_t) - \\min_{\\mathbf{w}^* \\in \\Delta^N} \\sum_{t=1}^T \\ell_t(\\mathbf{w}^*)&quot;,&quot;id&quot;:&quot;BPJDYBBTXT&quot;}" data-component-name="LatexBlockToDOM"></div><p>Algorithms like EG can guarantee a regret of </p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;R_T^{\\text{stat}} = \\mathcal{O}(\\sqrt{T \\log N})&quot;,&quot;id&quot;:&quot;VZXMYGTCHD&quot;}" data-component-name="LatexBlockToDOM"></div><p>assuming bounded losses. This is a nice theoretical result, but practically useless. It tells you nothing about performance during a regime change.</p></li><li><p><strong>Interval regret</strong> &#8594; This is much stronger. We want to be competitive over all possible contiguous windows [<em>s, e</em>] from 1 &#8804;<em> s </em>&#8804;<em> e</em> &#8804; <em>T</em>.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;R_{s,e} = \\sum_{t=s}^e \\ell_t(\\mathbf{w}_t) - \\min_{\\mathbf{w}^* \\in \\Delta^N} \\sum_{t=s}^e \\ell_t(\\mathbf{w}^*)&quot;,&quot;id&quot;:&quot;MVQYLKKMSD&quot;}" data-component-name="LatexBlockToDOM"></div><p>We want to ensure that max<sub>1 &#8804; s &#8804; e &#8804; T</sub> <em>R<sub>s,e</sub></em> is small. This is a much harder benchmark and is what diagnostics should focus on.</p></li><li><p><strong>Dynamic (shifting) regret</strong> &#8594; This is the most realistic comparator. We compare our algorithm to the best sequence of portfolios <em>w<sup>*</sup></em><sub>1</sub><em>, &#8230;, w<sup>*</sup><sub>T</sub></em>, but we penalize that sequence for its own turnover.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;R_T^{\\text{dyn}} = \\sum_{t=1}^T \\ell_t(\\mathbf{w}_t) - \\min_{\\mathbf{w}_1^*, \\dots, \\mathbf{w}_T^* \\in \\Delta^N} \\left( \\sum_{t=1}^T \\ell_t(\\mathbf{w}_t^*) + C \\sum_{t=1}^{T-1} \\|\\mathbf{w}_{t+1}^* - \\mathbf{w}_t^*\\|_1 \\right)&quot;,&quot;id&quot;:&quot;EFTBVRPNRO&quot;}" data-component-name="LatexBlockToDOM"></div><p>This captures the essence of adapting to <em>S</em> different regimes.</p></li></ol><p>Our algorithm won&#8217;t <em>explicitly</em> optimize for all of these, but its scale-free nature will give it strong performance on all three, especially interval and dynamic regret.</p><p><strong>AdaHedge and AdaNormalHedge</strong></p><p>Let&#8217;s kill the &#951; knob. The key idea is to <em>normalize the observed loss by its own history</em>. We replace the single, global &#951; with a <em>per-asset, time-varying</em> step size, &#951;<sub>t-1</sub>.</p><p>We define two cumulative, per-asset trackers:</p><ul><li><p><strong>L1 tracker </strong>(AdaHedge-like): </p></li></ul><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\mathcal{A}_{t,i} = \\sum_{s=1}^t |\\ell_{s,i}|&quot;,&quot;id&quot;:&quot;HZOCJKPRUA&quot;}" data-component-name="LatexBlockToDOM"></div><ul><li><p><strong>L2 tracker </strong>(AdaNormalHedge-like): </p></li></ul><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\mathcal{V}_{t,i} = \\sum_{s=1}^t \\ell_{s,i}^2&quot;,&quot;id&quot;:&quot;DPUIRCKOUY&quot;}" data-component-name="LatexBlockToDOM"></div><p>Now, we define our step sizes:</p><ul><li><p>AdaHedge: The step size is inversely proportional to the cumulative L1 norm of losses.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\eta_{t,i} = 1 / \\mathcal{A}_{t,i}&quot;,&quot;id&quot;:&quot;ODCWZLUGYC&quot;}" data-component-name="LatexBlockToDOM"></div></li><li><p>AdaNormalHedge: The step size is inversely proportional to the L2 norm (the square root of cumulative squared losses). This is very similar to AdaGrad.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\eta_{t,i} = 1 / \\sqrt{\\mathcal{V}_{t,i}}&quot;,&quot;id&quot;:&quot;KWPCQFHMRM&quot;}" data-component-name="LatexBlockToDOM"></div></li></ul><p><em>Why is this scale-free? </em>Imagine a 10% loss (<em>L</em><sub>t,i </sub>= 0.10).</p><ul><li><p>If this is the first day, <em>V</em><sub>t,i</sub> = 0.01, so &#951;<sub>t,i</sub> = 1 / &#8730;0.01 = 10. The scaled loss is  &#951;<sub>t,i</sub><em>L</em><sub>t,i</sub> = 10 x 0.10 = 1.0.</p></li><li><p>Now imagine the same 10% loss happens after 100 days of 1% volatility (<em>V</em><sub>t-1 </sub>&#8776; 100 x 0.01<sup>2</sup> = 0.01). The new <em>V</em><sub>t-1</sub> = 0.01 + 0.10<sup>2</sup> = 0.02. The step size is &#951;<sub>t,i</sub> &#8776; 1 / &#8730;0.02 &#8776; 7.07. The scaled loss is 7.07 x 0.10 &#8776; 0.707.</p></li><li><p>Now imagine the 10% loss happens after 100 days of 10% volatility (<em>V</em><sub>t-1,i </sub> &#8776; 100 x 0.10<sup>2</sup> = 1.0). The new <em>V</em><sub>t,i</sub> = 1.0 + 0.10<sup>2</sup> = 1.01. The step size is &#951;<sub>t,i </sub>&#8776; 1 / &#8730;1.01 &#8776; 0.995. The scaled loss is 0.995 x 0.10 &#8776; 0.0995.</p></li></ul><p>The <em>effective</em> update (the scaled loss) is <em>automatically</em> tempered by the asset&#8217;s own history. High-vol assets get smaller step sizes. Low-vol assets get larger ones. If a low-vol asset suddenly wakes up, the allocator reacts <em>aggressively</em> (large scaled loss), and then <em>tapers off</em> as this becomes the new normal (as <em>V</em> grows). This is <em>exactly</em> the adaptive behavior we want, with zero tuning.</p><p>The multiplicative update rule is then:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;w_{t+1, i} \\propto w_{t,i} \\exp( - \\eta_{t,i} \\ell_{t,i} )&quot;,&quot;id&quot;:&quot;FAVEHNJTWQ&quot;}" data-component-name="LatexBlockToDOM"></div><p>(We&#8217;ll add stabilization terms later).</p><p>This gives us the <em>proposed</em> update, w&#771;<sub>t+1</sub>. But this update knows nothing about costs.</p><p><strong>The L1 turnover cap and trust band</strong></p><p>This is the second brilliant piece. We <em>do not</em> use a proximal step. We use a simple, two-part lazy rebalance scheme.</p><p>Let w&#771;<sub>t+1</sub> be the proposed scale-free update vector (after normalization).</p><p>Let w<sub>t</sub> be our current holding.</p><ol><li><p>Trust Band &#964;: First, we compute the L1 distance of the proposed trade:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\Delta = \\| \\tilde{\\mathbf{w}}_{t+1} - \\mathbf{w}_t \\|_1&quot;,&quot;id&quot;:&quot;OJETJDUJFQ&quot;}" data-component-name="LatexBlockToDOM"></div><p>If this trade is too small, it&#8217;s just noise. We skip it entirely.</p><p>If &#916; &#8804; &#964;, then w<sub>t+1</sub> = w<sub>t. </sub>A typical &#964; = 0.002 (20 bps) means we don&#8217;t bother trading if the total portfolio change is less than 20 bps. This kills churn.</p></li><li><p>Turnover Cap &#954;: If the trade is larger than &#964;, we do want to trade, but we don&#8217;t want to over-trade. We cap the size of our L1 move.</p><p>If &#916; &gt; &#964;, then compute &#945;<sub>t</sub> = min(1, &#954;/&#916;). Our actual new portfolio is a linear interpolation:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\mathbf{w}_{t+1} = \\mathbf{w}_t + \\alpha_t (\\tilde{\\mathbf{w}}_{t+1} - \\mathbf{w}_t) = (1 - \\alpha_t) \\mathbf{w}_t + \\alpha_t \\tilde{\\mathbf{w}}_{t+1}&quot;,&quot;id&quot;:&quot;SKQXHWVGDG&quot;}" data-component-name="LatexBlockToDOM"></div><p>A typical &#954; = 0.25 (25%) means that even if the scale-free update wants to trade 80% of the portfolio (&#916;=0.8), we only move &#945;<sub>t</sub> = 0.25 / 0.8 = 0.3125 of the way there. This is a &#8220;pro-rata&#8221; move that tames the allocator without distorting its direction.</p></li></ol><p>The cost at this step is </p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\text{cost}_t = \\lambda \\| \\mathbf{w}_{t+1} - \\mathbf{w}_t \\|_1 = \\lambda \\cdot (\\alpha_t \\Delta).&quot;,&quot;id&quot;:&quot;FPBGSOPJAT&quot;}" data-component-name="LatexBlockToDOM"></div><p>This is a complete, &#8220;pro-style&#8221; rebalancing framework that aligns perfectly with the theoretical L1 penalty, but is far more practical and implementable.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4veV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4veV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png 424w, https://substackcdn.com/image/fetch/$s_!4veV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png 848w, https://substackcdn.com/image/fetch/$s_!4veV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png 1272w, https://substackcdn.com/image/fetch/$s_!4veV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!4veV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png" width="1056" height="399" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:399,&quot;width&quot;:1056,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:38053,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/177352465?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4veV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png 424w, https://substackcdn.com/image/fetch/$s_!4veV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png 848w, https://substackcdn.com/image/fetch/$s_!4veV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png 1272w, https://substackcdn.com/image/fetch/$s_!4veV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b5b6577-c85c-4655-814a-07f3a2a2d528_1056x399.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Trust band and turnover cap</figcaption></figure></div><h4>Why this breaks common misconceptions</h4><p>This framework shatters several long-held, false beliefs in quantitative finance.</p><ol><li><p><em>&#8220;Parameter-free is marketing; you still need an &#951;&#8221;</em>:<em> </em>You don&#8217;t need a <em>fixed</em> <em>&#951;</em>. The <em>normalizer</em> <em>is</em> the data-dependent learning rate. It&#8217;s scale-free by construction. The &#949; we add for numerical stability is a floor, not a tuning knob.</p></li><li><p><em>&#8220;Hedge-style (multiplicative) updates are incompatible with cost penalties&#8221;</em>: This is a common theoretical confusion. You don&#8217;t need a complex proximal solver that mixes additive and multiplicative terms. The lazy rebalancing framework (trust band + turnover cap) <em>is</em> the cost-aware execution. It decouples the <em>proposal</em> (Hedge update) from the <em>execution</em> (cost control).</p></li><li><p><em>&#8220;A trust band and turnover cap are just more knobs to tune&#8221;</em><strong>: </strong>This is the most important one. <em>No</em><strong>.</strong> They are guards, not optimization parameters. You don&#8217;t tune <em>&#951;</em> to maximize Sharpe. You set <em>&#951;</em>=0.25 because your execution desk <em>cannot</em> trade more than 25% of the portfolio in a day without massive slippage. You set &#964;=0.002 because 20 bps is your <em>cost</em> of trading, so any trade smaller than that is guaranteed to be -EV. These are <em>physical constraints</em> of the market, not magic numbers for a backtest.</p></li><li><p><em>&#8220;This is just expert-weighting. I need to design the experts first.&#8221;</em>:<strong> </strong>We are doing this at the <em>asset level</em>. Each of the <em>N</em> assets <em>is</em> an expert. The comparator class is &#916;<em><sup>N</sup></em>, the simplex itself. This is far more direct. The algorithm directly weights the assets based on their <em>own</em> scale-free loss history. This also changes the comparator class for regret: we are competing against the best <em>fixed-weight portfolio of assets</em>, not the best expert.</p></li></ol><h4>Algorithm design</h4><p>This is the entire allocator. It is a learning-rate-free, cost-aware, and numerically stable algorithm.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BSEg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BSEg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png 424w, https://substackcdn.com/image/fetch/$s_!BSEg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png 848w, https://substackcdn.com/image/fetch/$s_!BSEg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png 1272w, https://substackcdn.com/image/fetch/$s_!BSEg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!BSEg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png" width="770" height="780" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/83215507-4087-4e9d-b152-05f8774ee668_770x780.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:780,&quot;width&quot;:770,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:123340,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/177352465?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BSEg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png 424w, https://substackcdn.com/image/fetch/$s_!BSEg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png 848w, https://substackcdn.com/image/fetch/$s_!BSEg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png 1272w, https://substackcdn.com/image/fetch/$s_!BSEg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83215507-4087-4e9d-b152-05f8774ee668_770x780.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This pseudocode maps directly to a clean Python class. The following is the exact <code>fit</code> method that implements the logic above, taken from the provided artifacts.</p><pre><code>import numpy as np

class ScaleFreeOPS:
    def __init__(self, mode: str = &#8220;adah&#8221;, lam: float = 0.0002, 
                 tau: float = 0.002, turnover_cap: float = 0.25, 
                 eps: float = 1e-12):
        &#8220;&#8221;&#8220;
        Initializes the Scale-Free OPS Allocator.
        
        :param mode: &#8220;adah&#8221; (AdaHedge-like) or &#8220;anorm&#8221; (AdaNormalHedge-like)
        :param lam: L1 cost per unit of turnover (e.g., 0.0002 for 2bps)
        :param tau: Trust band (e.g., 0.002 for 20bps L1)
        :param turnover_cap: Max L1 turnover per step (e.g., 0.25 for 25%)
        :param eps: Numerical stability floor
        &#8220;&#8221;&#8220;
        assert mode in (&#8221;adah&#8221;, &#8220;anorm&#8221;)
        self.mode = mode
        self.lam = float(lam)
        self.tau = float(tau)
        self.turnover_cap = float(turnover_cap)
        self.eps = float(eps)

    def fit(self, R: np.ndarray):
        &#8220;&#8221;&#8220;
        Runs the backtest.
        :param R: T x N array of daily log-returns
        &#8220;&#8221;&#8220;
        T, N = R.shape
        
        # 1. Initialization 
        w = np.ones(N) / N         # Current weights
        A = np.zeros(N)            # L1 tracker
        V = np.zeros(N)            # L2 tracker
        
        # History Trackers 
        # Note: T+1 length to store initial state
        eq_g = np.zeros(T+1)       # Gross log equity
        eq_n = np.zeros(T+1)       # Net log equity
        costs = np.zeros(T+1)      # Cost paid at each step
        turnover = np.zeros(T+1)   # L1 turnover at each step
        ret_net = np.zeros(T)      # Net returns (for metrics)
        
        # Main Loop
        for t in range(T):
            
            # EARN (Step #1 from pseudocode) 
            # We earn returns R[t] on weights w (chosen at t-1)
            ret = float(np.dot(w, R[t]))
            
            # Gross equity up to t+1
            eq_g[t+1] = eq_g[t] + ret 
            
            # Net equity is gross - realized cost from *this* step
            # Note: The provided script differs slightly, applying cost
            # from the *previous* step. We follow the user&#8217;s script logic.
            # In the user script, net equity is calculated *after* the trade.
            # Let&#8217;s stick to the logic in the *user&#8217;s provided script*
            # which is: earn, calc new w, calc cost, update net eq.
            # This is a common convention.
            
            # BUILD GRADIENT (Step #2)
            g = -R[t]                  # Loss vector
            A += np.abs(g)             # Update L1 tracker
            V += g*g                   # Update L2 tracker
            
            if self.mode == &#8220;adah&#8221;:
                # AdaHedge-like: 1 / sqrt(L1 norm)
                theta = 1.0 / np.sqrt(np.maximum(A, self.eps))
            else: # mode == &#8220;anorm&#8221;
                # AdaNormalHedge-like: 1 / sqrt(L2 norm)
                theta = 1.0 / np.sqrt(np.maximum(V, self.eps))
                
            scaled = -g * theta        # Normalized gradient
            
            # HEDGE UPDATE (Step #3) 
            m = np.max(scaled)
            exps = w * np.exp(scaled - m)
            s = exps.sum()
            
            if not np.isfinite(s) or s &lt;= 0:
                w_tilde = np.ones(N) / N
            else:
                w_tilde = exps / s     # Proposed new weights
                
            # LAZY REBALANCE (Step #4) 
            delta = np.abs(w_tilde - w).sum() # L1 distance
            
            if delta &lt;= self.tau:
                # --- Skip trade ---
                w_new = w
                cost = 0.0
                turn = 0.0
            else:
                # --- Cap trade ---
                alpha = min(1.0, self.turnover_cap / max(1e-12, delta))
                w_new = w + alpha * (w_tilde - w)
                turn = np.abs(w_new - w).sum()
                cost = self.lam * turn
                
            # TRACK &amp; UPDATE (Step #5) 
            # Now we update net equity with today&#8217;s return AND today&#8217;s cost
            net_return_t = ret - cost
            ret_net[t] = net_return_t
            eq_n[t+1] = eq_n[t] + net_return_t
            
            costs[t+1] = cost
            turnover[t+1] = turn
            w = w_new # Commit weights for next period
            
        return eq_g, eq_n, turnover, costs, ret_net</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4c7N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdec9455b-d008-423f-807e-b99c4c010782_1389x790.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4c7N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdec9455b-d008-423f-807e-b99c4c010782_1389x790.png 424w, https://substackcdn.com/image/fetch/$s_!4c7N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdec9455b-d008-423f-807e-b99c4c010782_1389x790.png 848w, https://substackcdn.com/image/fetch/$s_!4c7N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdec9455b-d008-423f-807e-b99c4c010782_1389x790.png 1272w, https://substackcdn.com/image/fetch/$s_!4c7N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdec9455b-d008-423f-807e-b99c4c010782_1389x790.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!4c7N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdec9455b-d008-423f-807e-b99c4c010782_1389x790.png" width="1389" height="790" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dec9455b-d008-423f-807e-b99c4c010782_1389x790.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:790,&quot;width&quot;:1389,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4c7N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdec9455b-d008-423f-807e-b99c4c010782_1389x790.png 424w, https://substackcdn.com/image/fetch/$s_!4c7N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdec9455b-d008-423f-807e-b99c4c010782_1389x790.png 848w, https://substackcdn.com/image/fetch/$s_!4c7N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdec9455b-d008-423f-807e-b99c4c010782_1389x790.png 1272w, https://substackcdn.com/image/fetch/$s_!4c7N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdec9455b-d008-423f-807e-b99c4c010782_1389x790.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>What we see here can be interpreted:</p><ul><li><p><code>AdaNormalHedge</code><em> is hyperactive</em><strong>:</strong> The <code>SF-OPS AdaNormalHedge-like</code> (green line) is by far the most aggressive and hyperactive strategy. Its cumulative turnover is <em>enormous</em> compared to the others. This confirms that the L2-norm is highly sensitive and reacts aggressively to market moves, causing it to constantly and expensively rebalance its portfolio.</p></li><li><p><code>EG</code><strong> </strong>and <code>AdaHedge</code> are <em>conservative</em>: The <code>EG</code> (blue line) and <code>SF-OPS AdaHedge-like</code> (orange line) are both low-turnover, conservative strategies.</p><ul><li><p><code>EG(eta=0.5)</code><strong>:</strong> This is the <em>least</em> active strategy. Its low, fixed learning rate, combined with the cost-control guards, results in very minimal, stable trading.</p></li><li><p><code>SF-OPS AdaHedge-like</code>: This scale-free model trades a bit more than the tuned <code>EG</code> model, but it is still extremely conservative. Its L1-norm tracker (<code>AdaHedge</code>) is inherently more stable and less reactive than the L2-norm (<code>AdaNormalHedge</code>).</p></li></ul></li></ul><p>About the results on synth-toy data: </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!RQT9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!RQT9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png 424w, https://substackcdn.com/image/fetch/$s_!RQT9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png 848w, https://substackcdn.com/image/fetch/$s_!RQT9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png 1272w, https://substackcdn.com/image/fetch/$s_!RQT9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!RQT9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png" width="1389" height="790" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:790,&quot;width&quot;:1389,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!RQT9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png 424w, https://substackcdn.com/image/fetch/$s_!RQT9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png 848w, https://substackcdn.com/image/fetch/$s_!RQT9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png 1272w, https://substackcdn.com/image/fetch/$s_!RQT9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5f37f10-67ad-4827-bcf2-82f86632fba8_1389x790.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>What does this mean? Why the performance is so similar and then, what are the advantages?</p><p>The advantage is not that SF-OPS ended with a slightly higher or lower number. <em>The advantage is how it got there</em>:</p><ol><li><p>The <code>EG</code> model (orange line) performed well <em>only</em> because we hand-picked a magic learning rate of <em>&#951;</em>=0.5. It&#8217;s fragile. If we ran this test on data from 2020-2023, the correct <em>&#951;</em> might have been 0.1 or 1.0. The <em>&#951;</em>=0.5$ model would have likely failed. You can&#8217;t know <em>&#951;</em> in advance. In live trading, you have to guess the <em>&#951;</em> for the future. You will almost certainly guess wrong.</p></li><li><p>The SF-OPS AdaHedge-like model (green line) achieved a similarly high Sharpe ratio (3.03 vs 3.30) with <em>zero tuning</em>. It is robust. The entire point is that this knob-free model was able to keep pace with a perfectly tuned (but fragile) model. This is the advantage. You can deploy the SF-OPS model on any dataset, in any regime, and it will adapt and perform robustly. You cannot do that with the EG model.</p></li></ol><p>When moving from research (a <code>fit</code> method) to production (a live trading system), a few more things are required:</p><ol><li><p><strong>Batching:</strong> Translate the final w<sub>new</sub> into target orders.</p></li></ol><ul><li><p><strong>Safety:</strong> Check against tickers, lot sizes, and other constraints.</p></li><li><p><strong>Residuals:</strong> The allocator produces a target w<sub>new</sub>. The trading desk tries to execute it. At the end of the day, due to partial fills, your <em>actual</em> portfolio w<sub>actual </sub>might be different. Your <em>next</em> update must start from w<sub>actual</sub>, not w<sub>new</sub>. This residual tracking is critical.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lF1e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png" width="1456" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1158490,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!lF1e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 424w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 848w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1272w, https://substackcdn.com/image/fetch/$s_!lF1e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb06dd-ccb1-4472-841c-512c42ff3531_1774x887.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3&quot;,&quot;text&quot;:&quot;Send me a copy&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.amazon.com/Quantitative-Research-Foundations-Quant-Beckman/dp/B0GWGJWDS3"><span>Send me a copy</span></a></p><h4>Strongly-adaptive aggregation and diagnostics</h4><p>We have the algorithm. How do we know it works? How do we <em>prove</em> it&#8217;s better than a tuned EG? We <em>don&#8217;t</em> just look at the final Sharpe ratio. That&#8217;s a single, global number. We need diagnostics that <em>measure adaptation</em>.</p><p>This is the key. Instead of one backtest, we run thousands. The real question is: <em>How often did my allocator underperform the best fixed portfolio over the last 3m/6m/9m windows?</em></p><p>This is a direct-fire test of interval regret. If your allocator is frequently lost (i.e., has high regret) over 60-day or 120-day periods, it means it&#8217;s failing to adapt, even if its total 10-year Sharpe is good.</p><p>A robust allocator should have bounded interval regret. It might be wrong for a week, but it should never be wrong for a whole quarter.</p><p>Theoretically, our scale-free hedge allocators <em>are</em> strongly-adaptive. This is a class of algorithm from OCO theory. A strongly-adaptive algorithm can compete against a shifting comparator. The regret bound for such an algorithm against a comparator with <em>S</em> switches (regimes) over <em>T</em> steps is:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;R_T^{\\text{dyn}} = \\mathcal{O}(\\sqrt{S(T+1)\\log N})&quot;,&quot;id&quot;:&quot;DGVPRTVOEF&quot;}" data-component-name="LatexBlockToDOM"></div><p>Let&#8217;s see the results:</p><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Suob!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png 424w, https://substackcdn.com/image/fetch/$s_!Suob!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png 848w, https://substackcdn.com/image/fetch/$s_!Suob!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png 1272w, https://substackcdn.com/image/fetch/$s_!Suob!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Suob!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png" width="774" height="159" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:159,&quot;width&quot;:774,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:31175,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/177352465?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Suob!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png 424w, https://substackcdn.com/image/fetch/$s_!Suob!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png 848w, https://substackcdn.com/image/fetch/$s_!Suob!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png 1272w, https://substackcdn.com/image/fetch/$s_!Suob!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F496c3bef-6029-4d8d-92bd-acd48c03cddd_774x159.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SabC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SabC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png 424w, https://substackcdn.com/image/fetch/$s_!SabC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png 848w, https://substackcdn.com/image/fetch/$s_!SabC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png 1272w, https://substackcdn.com/image/fetch/$s_!SabC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!SabC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png" width="1456" height="897" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:897,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SabC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png 424w, https://substackcdn.com/image/fetch/$s_!SabC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png 848w, https://substackcdn.com/image/fetch/$s_!SabC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png 1272w, https://substackcdn.com/image/fetch/$s_!SabC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e6d07e-59fb-4089-970d-daeaf7473045_1598x985.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This plot visualizes the portfolio allocation strategy of each algorithm, revealing why their performances differed so starkly. The Uniform model (top-left) is a flat, static 1/<em>N</em> benchmark, while the EG(eta=0.5) model (top-right) is a very slow, conservative learner that barely moves from its initial 1/<em>N</em> allocation. </p><p>In contrast, the scale-free models show clear adaptation: the SF-OPS AdaHedge-like model (bottom-left) correctly identifies and increases its weight in winning assets (like Asset 1, orange) but maintains robust diversification by never fully selling out of other assets. </p><p>Finally, the SF-OPS AdaNormalHedge-like model (bottom-right) exposes its critical flaw, behaving as an aggressive, high-risk allocator that rapidly concentrates over 90% of its portfolio into Asset 1, explaining both its initial massive gains and its subsequent catastrophic collapse when the market regime changed.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KKRX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 424w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 848w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1272w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png" width="1456" height="271" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:271,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:381253,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!KKRX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 424w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 848w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1272w, https://substackcdn.com/image/fetch/$s_!KKRX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d67508e-8262-4388-b414-4324f2b6925e_1569x292.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Okay! Well done today team! It&#8217;s time to say goodbye, but keep this idea in mind: T<strong>he fewer parameters and meta-parameters, the less tuning there is to do.</strong></p><p>We&#8217;ll continue with more interesting algorithms next week. Until then, take care! You&#8217;ve done a great job! &#129470;</p><div class="poll-embed" data-attrs="{&quot;id&quot;:398753}" data-component-name="PollToDOM"></div><div><hr></div><p>This is an<strong> invitation-only access</strong> to our <strong>QUANT COMMUNITY</strong>, so we verify numbers to avoid spammers and scammers. Feel free to join or decline at any time. <em>Tap the WhatsApp icon below to join</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://chat.whatsapp.com/LsXSHJxfp9l7tOd7OO6PQL" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nbxE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 424w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 848w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1272w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png" width="1280" height="352" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:352,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:87001,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://chat.whatsapp.com/LsXSHJxfp9l7tOd7OO6PQL&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/181419543?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!nbxE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 424w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 848w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1272w, https://substackcdn.com/image/fetch/$s_!nbxE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c743df-ac2e-471c-b4de-0d439ebf4bdb_1280x352.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Appendix</strong></p><p>Full script:</p>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-scale-free-portfolio-selection">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[[WITH CODE] Optimization: Adaptive regret for regime-shifting markets ]]></title><description><![CDATA[Why &#8220;looking good on average&#8221; can be useless after a regime change&#8212;and how shifting-experts fixes it.]]></description><link>https://www.quantbeckman.com/p/with-code-optimization-adaptive-regret</link><guid isPermaLink="false">https://www.quantbeckman.com/p/with-code-optimization-adaptive-regret</guid><dc:creator><![CDATA[Quant Beckman]]></dc:creator><pubDate>Mon, 20 Oct 2025 11:58:39 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AJt2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5cedd76e-1949-481c-a904-be1a249336c5_1280x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4zl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png" width="1142" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:496259,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/155848302?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4zl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 424w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 848w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1272w, https://substackcdn.com/image/fetch/$s_!4zl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62310809-5821-4b17-8996-60bfbd9cd904_1142x716.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p><strong>Table of contents:</strong></p><ol><li><p>Introduction.</p></li><li><p>Quantifying failure in non-stationary environments.</p></li><li><p>Model risks and limitations.</p></li><li><p>Formalism for failure.</p></li><li><p>A framework for principled adaptation.</p></li><li><p>Shifting-experts algorithm.</p></li><li><p>Meta-optimization dilemma.</p></li></ol><div><hr></div><p style="text-align: center;">Before you begin, remember that you have an index with the newsletter content <strong>organized </strong>by clicking on &#8220;<em>Read full story</em>&#8221; in this image.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.quantbeckman.com/p/welcome" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png" width="1040" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1040,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:639128,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.quantbeckman.com/p/welcome&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/198696914?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DI2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 424w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 848w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1272w, https://substackcdn.com/image/fetch/$s_!DI2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae83b52-d512-41ea-bfdc-3fd9171486b1_1040x496.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Introduction</h4><p>In our preceding discourse, we talked about the features of parameter-free optimization, a methodology designed to liberate quantitative strategists from the sinister task of parameter tuning. The allure was undeniable: escape the perilous cycle of tweaking lookback windows, volatility thresholds, and rebalancing frequencies&#8212;a process that often culminates in overfitted models, brittle constructs that perform pretty well in-sample only to disintegrate upon exposure to live market conditions. By excising these explicit parameters, the objective was to engineer systems with inherent robustness. Yet, an unsettling reality persists, one that we must now confront directly: </p><blockquote><p><em>Every model possesses an implicit parameter&#8212;the market regime in which it was conceived.</em></p></blockquote><p>A so-called &#8220;parameter-free&#8221; model is, in truth, inextricably linked to its developmental environment. It assimilates a collection of rules, statistical relationships, and dynamic behaviors from the dataset upon which it is trained. A market regime can be conceptualized as a distinct statistical properties:</p><ul><li><p>Its volatility structure, the covariance matrix governing inter-asset relationships, the degree of autocorrelation present in its price series.</p></li><li><p>And its characteristic signal-to-noise ratio. </p></li></ul><p>Consequently, a model trained during a placid era of low inflation and consistent economic expansion internalizes a set of assumptions&#8212;a market worldview&#8212;that may be invalidated when inflationary pressures mount and volatility becomes the dominant market force. A momentum strategy, for instance, honed in a trending, low-volatility bull market, is predisposed to systematic failure in a choppy, high-volatility, mean-reverting environment where every apparent breakout is a deceptive trap. The optimization problem has not been solved; it has merely been displaced to a higher, more abstract plane. The model is implicitly &#8220;optimized&#8221; for the statistical fingerprint of its training data.</p><p>Because of that, our focus shifts from the granular details of model parameters to the more encompassing and critical challenge of <em>model adaptation</em>. The primary risk we aim to dissect and mitigate is that of <em>model-regime mismatch</em>: the inevitable failure of a static model operating within a dynamic, non-stationary world. Conventional backtesting methodologies frequently compound this risk by fostering a hazardous form of analytical myopia. They assess performance using metrics that average across an entire historical timeline, such as the Sharpe ratio, Sortino ratio, or total compounded return. While these metrics may appear impressive, they can cover a dangerous truth: <strong>a strategy might have been exceptionally profitable for a five-year period before systematically bleeding capital for the subsequent two years</strong>. </p><p>This averaging effect is deceptive and represents the pivotal conflict we must resolve. A trader, after all, does not experience the &#8220;average&#8221; return; they live through the path-dependent, day-to-day fluctuations of their profit and loss. A strategy that incurs persistent losses for two years is not a viable strategy; it is a liability, irrespective of how stellar its long-term average performance appears on a backtest report.</p><p>The pivotal event that propels our entire research is the regime break&#8212;that critical juncture when the market fundamentally alters its behavior, when established rules cease to apply, often with startling abruptness and little to no forewarning. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zRPi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zRPi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png 424w, https://substackcdn.com/image/fetch/$s_!zRPi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png 848w, https://substackcdn.com/image/fetch/$s_!zRPi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png 1272w, https://substackcdn.com/image/fetch/$s_!zRPi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!zRPi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png" width="1272" height="848" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:848,&quot;width&quot;:1272,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:720333,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/175403841?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zRPi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png 424w, https://substackcdn.com/image/fetch/$s_!zRPi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png 848w, https://substackcdn.com/image/fetch/$s_!zRPi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png 1272w, https://substackcdn.com/image/fetch/$s_!zRPi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2bc4fd-826f-4d0f-a766-e969418ac551_1272x848.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Quantifying failure in non-stationary environments</h4><p>The principal obstacle confronting the quantitative strategist is not the market&#8217;s innate propensity for change, but rather the inadequacy of our conventional tools for performance measurement. Our analytical arsenal is largely predicated on an implicit, and often unstated, assumption of <em>stationarity</em>&#8212;the notion that the statistical properties of the financial universe are stable and time-invariant. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Dnr_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Dnr_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp 424w, https://substackcdn.com/image/fetch/$s_!Dnr_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp 848w, https://substackcdn.com/image/fetch/$s_!Dnr_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp 1272w, https://substackcdn.com/image/fetch/$s_!Dnr_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Dnr_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp" width="1024" height="489" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:489,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;\&quot;Two-panel graph showing a non-stationary inflation rate time series in blue and a differenced (stationary) inflation rate time series in green. The top plot displays a trend in inflation over time, indicating non-stationarity, while the bottom plot shows a stable mean after differencing, representing a stationary process.&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="&quot;Two-panel graph showing a non-stationary inflation rate time series in blue and a differenced (stationary) inflation rate time series in green. The top plot displays a trend in inflation over time, indicating non-stationarity, while the bottom plot shows a stable mean after differencing, representing a stationary process." title="&quot;Two-panel graph showing a non-stationary inflation rate time series in blue and a differenced (stationary) inflation rate time series in green. The top plot displays a trend in inflation over time, indicating non-stationarity, while the bottom plot shows a stable mean after differencing, representing a stationary process." srcset="https://substackcdn.com/image/fetch/$s_!Dnr_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp 424w, https://substackcdn.com/image/fetch/$s_!Dnr_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp 848w, https://substackcdn.com/image/fetch/$s_!Dnr_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp 1272w, https://substackcdn.com/image/fetch/$s_!Dnr_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc7b80777-835a-4eec-9bc1-91f5bfab252b_1024x489.webp 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Stationary vs Non-stationary</figcaption></figure></div><p>In the context of financial markets, this is arguably the single most perilous assumption one can make. Our task is to deconstruct this outdated paradigm and erect a new analytical framework from its foundations.</p><p>Let us ground this discussion in a concrete, quantitative examination. The standard operating procedure for evaluating a trading strategy involves computing a suite of performance metrics over a fixed historical period, for example, a decade. These metrics typically include the Sharpe Ratio, Calmar Ratio, Compound Annual Growth Rate (CAGR), and maximum drawdown. The fundamental, inescapable flaw in this approach is that these are <em>global metrics</em>. They perform an act of aggressive dimensionality reduction, condensing a rich, time-varying, path-dependent sequence of returns into a single, sterile numerical value.</p><p>To illustrate the severity of this issue, consider two hypothetical strategies, A and B, over a 10-year horizon:</p><ul><li><p><strong>Strategy A:</strong> Generates a consistent, steady 10% return each year for the entire 10-year period. Its CAGR is precisely 10%. The maximum drawdown is negligible. The performance is smooth, predictable, and confidence-inspiring.</p></li><li><p><strong>Strategy B:</strong> Delivers a spectacular 30% annual return for the first 5 years, followed by a painful -10% annual return for the next 5 years. The compounded total return over the decade results in a CAGR of approximately 8.2% (((1.305)&#215;(0.905))1/10&#8722;1&#8776;0.082). Its maximum drawdown, incurred entirely in the second half of the backtest, would be substantial, likely exceeding 40%.</p></li></ul><p>When judged solely by the global metric of CAGR, Strategy A appears only marginally superior to Strategy B. However, any rational investor or risk manager would&#8212;and should&#8212;abandon Strategy B after year 6 or 7, as it would be demonstrably failing in the <em>current</em> market environment. A quantitative analyst who places blind faith in the 10-year CAGR of Strategy B is courting disaster by deploying a model that is actively destroying capital. The global metric has utterly failed to signal the regime break in the strategy&#8217;s performance. It has averaged a period of exceptional success with a period of abject failure, yielding a final assessment of &#8220;mediocre,&#8221; a descriptor that accurately characterizes neither period and provides zero actionable intelligence for capital allocation decisions <em>today</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0659!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0659!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png 424w, https://substackcdn.com/image/fetch/$s_!0659!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png 848w, https://substackcdn.com/image/fetch/$s_!0659!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png 1272w, https://substackcdn.com/image/fetch/$s_!0659!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!0659!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png" width="1456" height="755" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:755,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:81453,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.quantbeckman.com/i/175403841?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0659!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png 424w, https://substackcdn.com/image/fetch/$s_!0659!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png 848w, https://substackcdn.com/image/fetch/$s_!0659!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png 1272w, https://substackcdn.com/image/fetch/$s_!0659!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59f71483-1d24-4aa8-b466-379bafe8c92a_1580x819.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Strategy comparison</figcaption></figure></div><p>The obstacle is one of analytical perspective. We are conditioned by the conventions of quantitative finance to place our trust in these summary statistics.</p><blockquote><p>They are convenient for reporting, for comparison, and for marketing. The first, most crucial step is to actively unlearn this reliance and begin to conceptualize performance as a <strong>local, not global, phenomenon</strong>. The key question is not How did this strategy perform <em>over the last decade</em>? but rather: How is this strategy performing <em>now</em>, and in the recent past? </p></blockquote><p>We need to care more about the derivative of the equity curve than its final value. To visualize this, let&#8217;s plot the rolling Sharpe ratio of our two strategies. We will simulate daily returns to make the analysis more realistic.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QUlv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QUlv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png 424w, https://substackcdn.com/image/fetch/$s_!QUlv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png 848w, https://substackcdn.com/image/fetch/$s_!QUlv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png 1272w, https://substackcdn.com/image/fetch/$s_!QUlv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!QUlv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png" width="1389" height="690" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:690,&quot;width&quot;:1389,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QUlv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png 424w, https://substackcdn.com/image/fetch/$s_!QUlv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png 848w, https://substackcdn.com/image/fetch/$s_!QUlv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png 1272w, https://substackcdn.com/image/fetch/$s_!QUlv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc59a32a9-1636-4c69-bb7d-65fe20b3176f_1389x690.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Judged by these numbers, Strategy B still looks somewhat viable, albeit inferior to A.</p><p>The plot, however, tells a different story. Strategy A&#8217;s rolling Sharpe ratio remains consistently positive and stable, oscillating around its long-term average. In stark contrast, Strategy B&#8217;s rolling Sharpe ratio is exceptionally high for the first five years and then plunges, remaining deeply and persistently negative for the second five years. The visual evidence is unequivocal: Strategy B is broken. The global metric, by averaging the good with the bad, commits a grievous error of misrepresentation. This visualization crystallizes the core problem: our tools are not designed to detect failure in real-time.</p><h4>Model risks and limitations</h4><p>Before continuing it&#8217;s important to be aware that adaptive regret framework has potential, but it is not infallible. A proficient quantitative strategist is defined not only by their models but by the awareness of their limitations. To deploy such a system without a rigorous understanding of its failure modes is to trade analytical hubris for financial ruin. </p><p>The single most critical limitation to internalize is that <strong>minimizing regret is not the same as maximizing risk-adjusted returns</strong>. The regret metric is a valuable <em>proxy</em> for adaptive capability, but it is not the ultimate objective function of a trading business, which is typically some function of PnL, volatility, and drawdown.</p><ul><li><p>Achieving a small or even negative adaptive/dynamic regret provides a guarantee that your algorithm performed nearly as well as the best available sequence of experts. However,<strong> if all the experts in your universe lose money during a particular period</strong> (e.g., a market crash where all long-only strategies suffer), <strong>your low-regret portfolio will still lose money</strong>. The framework guarantees you&#8217;ll lose money <em>efficiently</em>, but it doesn&#8217;t prevent the loss itself.</p></li><li><p>The solution is not to discard regret but to supplement it with a dashboard of traditional risk and performance metrics. <strong>Never rely on regret alone.</strong> Continuously monitor rolling PnL, Sharpe/Sortino ratios, maximum drawdown, and turnover-cost drag. Implement hard-coded risk management overlays, such as a kill-switch that flattens the portfolio if a predefined maximum drawdown threshold is breached or if realized volatility spikes beyond a certain limit. Regret tells you about your relative performance; PnL tells you if you&#8217;re still in business.</p></li></ul><p>Another important point is the choice of the mixing rate, &#945;, places the strategist on a knife&#8217;s edge between two opposing risks.</p><ul><li><p>A <strong>small &#945;</strong> makes the aggregator conservative and <strong>slow to adapt</strong>. After a sharp, sudden regime break, it will continue to place weight on the old, failing paradigm for too long, leading to protracted drawdowns. Conversely, a <strong>large &#945; </strong>makes the aggregator <strong>hyperactive and jittery</strong>. In a stable market regime, it will constantly overreact to noise, interpreting minor fluctuations as regime shifts, leading to excessive turnover and cost drag.</p></li><li><p>A way to mitigate this is to move beyond a static &#945;. More advanced implementations can feature a <strong>time-varying mixing rate</strong>. For example, &#945;<em><sub>t</sub>&#8203;</em> could be engineered to increase temporarily following a sharp spike in portfolio losses (a potential signal of a regime break) and then gradually decay back to a baseline level during periods of calm. An even more robust approach is a <em>strongly-adaptive meta-aggregator,</em> where multiple <code>FixedShareAggregator</code> instances are run in parallel, each with a different fixed &#945; (e.g., &#945;&#8712;{0,1/<em>T</em>,4/<em>T</em>,12/<em>T</em>}). A second-level aggregator then dynamically allocates weight to the aggregator that is performing best, effectively learning the optimal adaptation speed online.</p></li></ul><p>The adaptive aggregator is a manager, not a magician. It can only allocate capital among the strategies you provide it. Its performance is fundamentally bounded by the quality and diversity of this <em>expert universe.</em></p><ul><li><p>This is the &#8220;<strong>Garbage In, Garbage Out</strong>&#8221; principle. If the market enters a new regime for which you have no corresponding expert strategy, no amount of intelligent re-weighting can save you. If your universe consists of ten different trend-following strategies, and the market becomes violently mean-reverting, the aggregator will simply pick the least-bad trend-following strategy as it cycles through losses.</p></li><li><p>If the expert strategies are <em>highly correlated</em>, the benefits of adaptation are severely diminished. The weight updates will cause the portfolio to bang between experts that are essentially providing the same information. This can lead to risk concentration, where the <strong>aggregator collapses </strong>its weight into one corner of a highly correlated cluster of experts, creating a portfolio that is far less diversified than the number of experts would suggest.</p></li></ul><p>Finally, a host of risks arise from the gap between the clean mathematics of the algorithm and the finite, messy world of computer hardware and software protocols.</p><ul><li><p>The core update involves exponentiation, which can be <strong>numerically unstable</strong>. With many experts or very large losses, the values can underflow to zero or overflow to infinity, causing the algorithm to fail. The calculation of dynamic regret via dynamic programming is computationally intensive, making it infeasible for real-time monitoring with a large number of experts or a long history.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zr-N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png" width="1200" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zr-N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 424w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 848w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1272w, https://substackcdn.com/image/fetch/$s_!Zr-N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffda320ed-0fa5-4984-91de-c590b65319ef_1200x610.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.quantbeckman.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.quantbeckman.com/subscribe?"><span>Subscribe now</span></a></p><h4>Formalism for failure</h4><p>To advance beyond this qualitative critique, we must introduce a more rigorous, mathematical language to define and measure performance in this dynamic context. The field of online machine learning provides an exceptionally well-suited concept: <em>regret</em>. Regret quantifies the performance of our chosen algorithm relative to some benchmark, typically an oracle that possesses information unavailable to us at the time of decision-making (such as knowledge of the future).</p><p>Let us consider a universe of <em>N</em> distinct trading strategies, which we will refer to as experts. These experts represent our fundamental building blocks&#8212;they are our candidate sources of alpha. For example, Expert 1 could be a 50-day moving average crossover system, Expert 2 a statistical arbitrage strategy on a pair of equities, and Expert 3 a volatility-selling strategy. At each time step <em>t</em> (e.g., each trading day), each expert <em>i</em> incurs a loss &#8467;<sub>i,t</sub>. For our purposes, it is convenient to define loss as the negative of the logarithmic return: &#8467;<sub>i,t</sub>&#8203;=&#8722;<em>log</em>(1+<em>R</em><sub>i,t</sub>&#8203;). A lower loss signifies better performance. Our master algorithm, or &#8220;aggregator,&#8221; must dynamically combine the outputs of these experts to form a portfolio, which in turn incurs a loss &#8467;<sub>t</sub>.</p><p>The classical formulation of regret is <em>Static Regret.</em> It is defined as the difference between the cumulative loss of our algorithm and the cumulative loss of the <em>single best-performing expert in hindsight</em> over the entire period <em>T</em>.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;R_T^{\\text{stat}} = \\sum_{t=1}^{T} \\ell_t - \\min_{i \\in \\{1, \\ldots, N\\}} \\sum_{t=1}^{T} \\ell_{i,t}\n&quot;,&quot;id&quot;:&quot;IBAHHVGFWT&quot;}" data-component-name="LatexBlockToDOM"></div><p>Let&#8217;s implement this method because we weill use it in the main script:</p><pre><code>def static_regret(loss_agg: np.ndarray, loss_experts: np.ndarray) -&gt; float:
   &#8220;&#8221;&#8220;
   R_T^static = sum_t loss_agg[t] - min_i sum_t loss_experts[t, i]
   &#8220;&#8221;&#8220;
   L_agg = float(loss_agg.sum())
   L_i = loss_experts.sum(axis=0)  # (N,)
   return L_agg - float(np.min(L_i))</code></pre><p>The objective of many traditional online learning algorithms is to guarantee that this static regret grows sub-linearly with time. This is a theoretical guarantee, as it implies that on a per-period basis, the algorithm&#8217;s performance converges to that of the hindsight-optimal expert (<em>R</em><sub>T</sub><sup>stat</sup>&#8203;/<em>T</em>&#8594;0 as <em>T</em>&#8594;&#8734;). This sounds highly desirable: over a sufficiently long horizon, our algorithm will perform nearly as well as if we had possessed a crystal ball on day one, allowing us to select the single best strategy and commit to it for all time.</p><p>However, in the non-stationary data of finance, this is like science fiction. The single best expert is a static concept imposed upon the market. Let&#8217;s construct a simple simulation to demonstrate this catastrophic failure mode in action. We will model two experts: a momentum expert and a mean-reversion expert. The market will operate in a momentum-favoring regime for the first half of the simulation and then abruptly switch to a mean-reversion-favoring regime.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!b7n7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!b7n7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png 424w, https://substackcdn.com/image/fetch/$s_!b7n7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png 848w, https://substackcdn.com/image/fetch/$s_!b7n7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png 1272w, https://substackcdn.com/image/fetch/$s_!b7n7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!b7n7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png" width="1389" height="790" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:790,&quot;width&quot;:1389,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!b7n7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png 424w, https://substackcdn.com/image/fetch/$s_!b7n7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png 848w, https://substackcdn.com/image/fetch/$s_!b7n7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png 1272w, https://substackcdn.com/image/fetch/$s_!b7n7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F193f3ca7-4728-4c95-a867-64b87ce5d3e0_1389x790.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image buttonBase-GK1x3M"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image buttonBase-GK1x3M"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The resulting plot is a stark illustration of the problem. An algorithm optimized to minimize static regret would be judged against the performance of the orange line. Let&#8217;s say it successfully tracks this benchmark. What does that mean for the investor? It means the algorithm would have performed brilliantly for 500 days and then proceeded to systematically destroy capital for the next 500 days. The fact that its total static regret is low is cold comfort. The very benchmark we are using&#8212;the single best expert in hindsight&#8212;is an inappropriate and misleading target in a world that changes. Minimizing static regret forces our algorithm to find a compromise strategy, one that is mediocre on average but potentially catastrophic locally. This is the core analytical obstacle that must be surmounted. <em>We need a better definition of success</em>.</p><h4>A framework for principled adaptation</h4><p>The resolution to this dilemma lies not in building a better model, but in fundamentally changing the question we ask of it. Instead of asking, <em>How can my algorithm perform as well as the single best strategy over the entire historical period?</em> we must pivot to a more relevant and actionable question: <em>How can my algorithm perform, at all times, nearly as well as whatever strategy is currently the best?</em> This paradigm shift necessitates a radical overhaul of our mathematical framework and our algorithmic toolkit, moving from a static, global perspective to a dynamic, local one.</p><p>To construct a solution, we must first define the problem with precision. This requires the introduction of new performance metrics that explicitly capture the essence of adaptation and penalize failure within any time frame.</p><ol><li><p><strong>Adaptive (or interval) regret:</strong> This metric radically reframes the evaluation process. Instead of assessing performance over the entire history from <em>t</em>=1 to <em>T</em>, it examines every possible contiguous sub-interval [<em>r,s</em>] within that history. It measures the maximum regret our algorithm incurs over <em>any</em> such interval. Mathematically, it is defined as:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;R_{\\text{interval}}^{\\text{adapt}} \n= \\max_{0 \\le r \\le s \\le T} \n\\left\\{ \n\\sum_{t=r}^{s} \\ell_t \n- \n\\min_{i=1,\\ldots,N} \\sum_{t=r}^{s} \\ell_{i,t} \n\\right\\}&quot;,&quot;id&quot;:&quot;QXGQUNULSO&quot;}" data-component-name="LatexBlockToDOM"></div><p>An algorithm that achieves low adaptive regret provides an exceptionally strong performance guarantee. It promises that no matter which window of time an analyst chooses to examine&#8212;be it the last week, the last month, the last quarter, or the tumultuous period surrounding a specific economic event&#8212;the algorithm&#8217;s performance was nearly as good as the best possible strategy for that <em>specific period</em>. This is a profound improvement. It means the algorithm cannot conceal a period of disastrous performance behind a long history of prior success. It is held accountable for its performance locally in time, which is precisely what any rational risk manager or investor truly cares about. An algorithm with low adaptive regret would not have been permitted to bleed capital for 500 days, as we saw in our previous example, because its regret over that specific 500-day interval would have been enormous.</p><p>To implement this part we will use:</p><pre><code>def interval_regret_windows(
        loss_agg: np.ndarray,
        loss_experts: np.ndarray,
        windows: List[int] = [20, 60, 120]
    ) -&gt; Dict[int, float]:
        &#8220;&#8221;&#8220;
        Adaptive (interval) regret restricted to fixed window lengths.
        Returns max over all starting t for each window W:
          max_t sum_{s=t}^{t+W-1} loss_agg[s] - min_i sum_{s=t}^{t+W-1} loss_experts[s, i]
        &#8220;&#8221;&#8220;
        T, N = loss_experts.shape
        P_agg = np.concatenate(([0.0], np.cumsum(loss_agg)))
        P_exp = np.vstack((np.zeros((1, N)), np.cumsum(loss_experts, axis=0)))

        out = {}
        for W in windows:
            if W &gt; T:
                out[W] = np.nan
                continue
            worst = -np.inf
            for t in range(0, T - W + 1):
                L_a = P_agg[t + W] - P_agg[t]
                seg = P_exp[t + W] - P_exp[t]  # (N,)
                best_expert_seg = float(np.min(seg))
                regret = L_a - best_expert_seg
                if regret &gt; worst:
                    worst = regret
            out[W] = worst
        return out</code></pre></li><li><p><strong>Dynamic (or shifting) regret:</strong> This metric elevates the concept one step further. It compares the algorithm&#8217;s performance not to a single static expert, nor even to a sequence of interval-best experts, but to a pretty interesting benchmark that is permitted to <em>switch</em> between the available experts a predetermined number of times. Our benchmark is no longer a single entity but an optimal <em>sequence</em> of experts. If we allow for <em>S</em> switches over the total period <em>T</em>, the dynamic regret is defined as:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;R_{S,T}^{\\text{dyn}} \n= \\sum_{t=1}^{T} \\ell_t \n- \n\\min_{u_1, \\ldots, u_T \\in \\{1, \\ldots, N\\}^T \\text{ with } \\le S \\text{ switches}} \n\\sum_{t=1}^{T} \\ell_{u_t, t}&quot;,&quot;id&quot;:&quot;PNBISRJWXF&quot;}" data-component-name="LatexBlockToDOM"></div><p>Here, the sequence <em>u<sub>1</sub></em>&#8203;,&#8230;,<em>u<sub>T</sub></em>&#8203; represents the choice of expert at each time step, and the minimization is over all possible sequences that contain at most <em>S</em> changes (i.e., where u<sub>t</sub>&#8203;&#8800;u<sub>t-1</sub>&#8203;). Dynamic regret is perhaps the most intellectually honest metric for measuring adaptive performance. It explicitly acknowledges that the optimal strategy is not fixed. It asks, <em>How did my algorithm perform compared to a clairvoyant competitor who knew the perfect sequence of momentum &#8594; mean-reversion and was allowed to make that one switch at the perfect time?</em></p><p>Crucially, as we will see in our implementation, this hypothetical switching benchmark can be (and should be) charged a transaction cost for each switch it makes, creating a truly robust and fair comparison. If our algorithm can achieve low dynamic regret against this formidable, cost-adjusted benchmark, we can possess a high degree of confidence in its genuine adaptive capabilities.</p><pre><code>def dynamic_regret(
        self,
        loss_agg: np.ndarray,
        loss_experts: np.ndarray,
        S: int,
        include_costs: bool = True
    ) -&gt; float:
        &#8220;&#8221;&#8220;
        Dynamic (shifting) regret vs best comparator sequence with up to S switches.
        Returns:
          R_T^dyn(S) = sum_t loss_agg[t] - min_{u sequence, &#8804;S switches} sum_t loss_{t, u_t} [+ switching_costs]
        If include_costs=True, comparator pays 2*lam per switch (L1 distance between expert one-hots).
        &#8220;&#8221;&#8220;
        T, N = loss_experts.shape
        lam = self.lam if include_costs else 0.0

        INF = 1e100
        DP = np.full((T, N, S + 1), INF, dtype=float)

        # init at t=0
        for i in range(N):
            DP[0, i, 0] = loss_experts[0, i]

        # transitions
        for t in range(1, T):
            for i in range(N):
                li = loss_experts[t, i]
                # stay with i (no new switch)
                for s in range(S + 1):
                    DP[t, i, s] = min(DP[t, i, s], DP[t - 1, i, s] + li)
                # switch from j != i (pay 2*lam)
                for j in range(N):
                    if j == i:
                        continue
                    for s in range(1, S + 1):
                        DP[t, i, s] = min(DP[t, i, s], DP[t - 1, j, s - 1] + li + 2.0 * lam)

        best_comp = float(np.min(DP[T - 1]))
        return float(loss_agg.sum()) - best_comp</code></pre></li></ol><h4>Shifting-experts algorithm</h4>
      <p>
          <a href="https://www.quantbeckman.com/p/with-code-optimization-adaptive-regret">
              Read more
          </a>
      </p>
   ]]></content:encoded></item></channel></rss>