<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Rodriguez on Medium]]></title>
        <description><![CDATA[Stories by Daniel Rodriguez on Medium]]></description>
        <link>https://medium.com/@danjrod?source=rss-76212358fd10------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*P817frWsEBLG1Tam.</url>
            <title>Stories by Daniel Rodriguez on Medium</title>
            <link>https://medium.com/@danjrod?source=rss-76212358fd10------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 18:41:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danjrod/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Is Germany on the path to becoming a “Tax Hell” for investors?]]></title>
            <link>https://medium.com/@danjrod/is-germany-on-the-path-to-becoming-a-tax-hell-for-investors-24d65d8a203c?source=rss-76212358fd10------2</link>
            <guid isPermaLink="false">https://medium.com/p/24d65d8a203c</guid>
            <category><![CDATA[trading]]></category>
            <category><![CDATA[germany]]></category>
            <category><![CDATA[taxes]]></category>
            <category><![CDATA[investing]]></category>
            <category><![CDATA[futures-trading]]></category>
            <dc:creator><![CDATA[Daniel Rodriguez]]></dc:creator>
            <pubDate>Mon, 26 Aug 2024 11:52:43 GMT</pubDate>
            <atom:updated>2024-08-26T11:52:43.381Z</atom:updated>
            <content:encoded><![CDATA[<p>December 2019 will be remembered as the start of the COVID-19 pandemic. But it may not be long until it is also known as the month in which something with infinite destructive potential was born in Germany. And it is, unfortunately, still alive and kicking. It has even received a popular name: “Bindingsteuer” <a href="https://www.bindingsteuer.de/en/">[→ bindingsteuer.de]</a><a href="https://www.buzer.de/gesetz/4499/al82883-0.htm">[→ buzer-dec-2019]</a>.</p><p>Let’s make a long story short: infinite taxation on capital gains on derivatives, taxes on losses, and irrecoverable growing tax credits.</p><p>As my sister-in-law said last week: “It cannot be that someone makes a Net Profit of 23,000 EUR and gets a tax bill of 60,000 EUR”. <a href="https://www.landesrecht.rlp.de/bsrp/document/NJRE001561738">[→ rlp-case]</a></p><p>But it is. The Federal Fiscal Court (Bundesfinanzhof in German) has already partially addressed this in a ruling: “This is not compliant with Article 3 Paragraph 1 of the German Constitution”, which literally says: “All people are equal before the law”. <a href="https://www.bundesfinanzhof.de/de/entscheidung/entscheidungen-online/detail/STRE202410113/">[→ bfh-ruling]</a> <a href="https://www.gesetze-im-internet.de/gg/art_3.html">[→ art3-gg]</a></p><p>However, reverting to the previous status will take years because the socialists stated in the press: “Fine and good, we will just wait until the Federal Constitutional Court rules”. And this means waiting another 10–15 years. The fact that some traders have publicly announced going down the path of “private bankruptcy” to save something, is irrelevant to socialists. <a href="https://www.faz.net/aktuell/finanzen/bundesfinanzhof-verwirft-steuergesetz-zu-termingeschaeften-19857848.html">[</a>→ <a href="https://www.faz.net/aktuell/finanzen/bundesfinanzhof-verwirft-steuergesetz-zu-termingeschaeften-19857848.html">faz-bfh-spd-comments]</a></p><p><strong>How is this possible, and why is this a danger and a potential “Tax Hell”?</strong></p><p>Let us first answer the “Tax Hell” question by imagining that the Federal Constitutional Court finds a technicality or interpretation that makes the pack of “Infinite Taxation” / “Taxes on Losses” / “Irrecoverable Tax Credits” concepts compatible with the German Constitution. The door will then be open for implementing the same ideas for anything and anyone. Corporations would be no exception and would be subject to these concepts too.</p><p>As for how this is possible, the law formulation, introduced in 2019 and in effect since January 2021, hides a design that changes the taxation model from a flat tax rate of 25% into one based on hyperbolic curves, based on the “Profit Factor” of the trader. I.e., the “Gross Profit” to “Gross Loss” ratio, which is of course 100% unrelated to the actual “Net Profit”. <a href="https://www.investopedia.com/articles/fundamental-analysis/10/strategy-performance-reports.asp">[→ investo-pf]</a></p><p>This chart summarizes the new taxation model for capital gains. It is part of my book on this topic: “Bindingsteuer - The Infinite Tax Rate Doctrine”. <a href="https://www.amazon.com/dp/B0DBHH72GT">[→ Amazon]</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iiWqtJt9FY5CZMcfa2zX0g.png" /><figcaption>Bindingsteuer — Taxation Model</figcaption></figure><p>Notice that as soon as the “Gross Loss” (net winners) or “Gross Profit” (net losers) exceeds 20,000 EUR the trader is fully taxed with a hyperbolic curve, with which the ratio of “Gross Profit” to “Gross Loss” determines the tax rate applied to the trader. And yes, the curves reach “infinity” in both directions. It is not just an artifact of the presentation.</p><p>It seems pointless to say that “Net Winners” have to pay taxes with private assets as soon as the tax rate exceeds 100% and that “Net Losers” must always pay with private assets. Take your pick on what that means: confiscation, expropriation, …</p><p>Before moving on, there is an incredible trait of this taxation model because the “Effective Tax Rate” (net winners) is always higher than the “Tax Rate” of 25%. Only a trader with an infinite “Profit Factor” can achieve the 25% official tax rate.</p><p>The irrecoverable eternally growing tax credits are implemented by prohibiting the compensation of capital gains of different types and restricting how much of a tax credit can be applied each year.</p><p>The arbitrary 20,000 EUR figure is also applied in this case. Showing that to readers in the book required a 3-D chart.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*44_XSLFIFovYq-qIj_AmLA.png" /><figcaption>Irrecoverable Tax Credits expressed as the growing number of years needed to recover them</figcaption></figure><p>The years to recover the tax credits depend only on the years traded, and the yearly Gross Loss grows because the accumulated irrecoverable tax credits only grow. A trader went public with his tax bill, and it will take <strong>749 years</strong> to recover, assuming he could, because the tax shows it is impossible. <a href="https://www.youtube.com/watch?v=styzoO6afzk">[→ youtube-trader-max]</a></p><p>How much of a danger is this? Just look at how the law was introduced: as an amendment to an unrelated law, and with the justification that this was meant to “protect investors”. Supporting politicians also argued in public, that derivatives trading is simply gambling, and those engaging in such activities are therefore risk-seeking gamblers, whose trading (gross loss) is a financial and tax burden for society. <a href="https://dserver.bundestag.de/btd/19/158/1915876.pdf">[→law-intro]</a> <a href="https://bindingsteuer.de/en/perpetrators/#public-defense-and-mocking">[→public-statements]</a></p><p>This means that any other meaningless and fake arguments could be used to support the enactment of infinite taxation in several areas by simply including amendments to the proceedings of unrelated laws.</p><p>The introduction of this beast targeting only a small group of taxpayers and financial products allowed it to go through because who cares what happens with speculators even if they have to undergo tax-induced bankruptcy. It is just collateral damage.</p><p>Corporations and investment companies should be aware of the danger that lies ahead if the Bindingsteuer survives and “The Infinite Tax Doctrine” becomes the norm.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24d65d8a203c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On Backtesting Performance and Out of Core Memory Execution]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-snippet">Originally at&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@danjrod/on-backtesting-performance-and-out-of-core-memory-execution-f3fdb0c9fdf8?source=rss-76212358fd10------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@danjrod/on-backtesting-performance-and-out-of-core-memory-execution-f3fdb0c9fdf8?source=rss-76212358fd10------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3fdb0c9fdf8</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[backtrader]]></category>
            <category><![CDATA[algotrading]]></category>
            <category><![CDATA[backtesting]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Daniel Rodriguez]]></dc:creator>
            <pubDate>Sat, 26 Oct 2019 09:01:00 GMT</pubDate>
            <atom:updated>2019-10-26T09:01:00.800Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Cross-Backtesting Pitfalls]]></title>
            <link>https://medium.com/@danjrod/cross-backtesting-pitfalls-cc0989e8a0ee?source=rss-76212358fd10------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc0989e8a0ee</guid>
            <category><![CDATA[backtesting]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[backtrader]]></category>
            <category><![CDATA[algotrading]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Daniel Rodriguez]]></dc:creator>
            <pubDate>Wed, 04 Sep 2019 12:21:01 GMT</pubDate>
            <atom:updated>2019-09-04T12:54:22.780Z</atom:updated>
            <content:encoded><![CDATA[<p>Originally at: <a href="https://www.backtrader.com/blog/posts/2019-09-04-donchian-across-platforms/donchian-across-platforms/">https://www.backtrader.com/blog/posts/2019-09-04-donchian-across-platforms/donchian-across-platforms/</a></p><p>Somethings that tends to repeat itself in the <a href="https://community.backtrader.com/">backtrader community </a>is that a user explains the will to replicate the backtesting results attained in, for example, <a href="https://www.tradingview.com/">TradingView </a>, quite popular these days, or some other backtesting platform.</p><p>Without really knowing the language used in <em>TradingView</em>, named Pinescript, and having null exposure to the internal of the backtesting engine, there is still a way to let users know, that coding across platform has to be taken with pinch of salt.</p><h3>Indicators: Not always faithful to the sources</h3><p>When a new indicator is being implemented for <em>backtrader</em>, either directly for the distribution or as a snippet for the website, a lot of emphasis is taken in respecting the original definition. The RSI is a good example.</p><ul><li>Welles Wilder designed the RSI using the Modified Moving Average (aka Smoothed Moving Average, see <a href="https://en.wikipedia.org/wiki/Moving_average#Modified_moving_average">Wikipedia - Modified Moving Average</a></li><li>In spite of which, a number of platforms give the users something called RSI but using a classic Exponential Moving Average and not what the book says.</li><li>Given that both averages are exponential, the differences are not huge, but it is <strong>NOT</strong> what Welles Wilder defined. It may still be useful, it may even be better, but it is <strong>NOT</strong> the RSI. And the documentation (when available) fails to mentions that.</li></ul><p>The default configuration for the RSI in <em>backtrader</em> is to use the MMA to be faithful to the sources, but which moving average to use is a parameter which can be changed via sub-classing or during run-time instantiation to use the EMA or even a <em>Simple Moving Average</em>.</p><h3>An example: The Donchian Channels</h3><p>The Wikipedia definition: <a href="https://en.wikipedia.org/wiki/Donchian_channel">Wikipedia — Donchian Channel</a>. It is just text and it makes no mention of using channel breakouts as a trading signal.</p><p>Another two definitions:</p><ul><li><a href="https://school.stockcharts.com/doku.php?id=technical_indicators:price_channels">StockCharts — School — Price Channels</a>.</li><li><a href="https://www.incrediblecharts.com/indicators/donchian_channels.php">IncredibleCharts — Donchian Channels</a></li></ul><p>These two references explicitly state, that the data for the calculation of the channel does not include the current bar, because if it did … breakouts would not be reflected. Here is a sample chart from <em>StockCharts (which shows breakouts)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/0*vYAntXUcnYDS731S.png" /></figure><p>Going now to <em>TradingView</em>. First the link</p><ul><li><a href="https://www.tradingview.com/wiki/Donchian_Channels_(DC)">TradingView — Wiki — Donchian Channels</a></li></ul><p>And a chart from that page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/0*3ds8lUORYE_rR2DV.png" /></figure><p>Even <em>Investopedia</em> uses a chart from <em>TradingView</em> that shows <strong>no breakout</strong>. Here: <a href="https://www.investopedia.com/terms/d/donchianchannels.asp">Investopedia — Donchian Channels — https://www.investopedia.com/terms/d/donchianchannels.asp</a></p><p>As some people would put it … <strong>Blistering Barnacles!!!!</strong>. Because there are <strong>no</strong> breakouts to be seen in the <em>TradingView</em> charts. This means that the implementation of the indicator is using the <strong>current</strong> price bar for the calculation of the channels.</p><h3>The Donchian Channels in backtrader</h3><p>There is no DonchianChannels implementation in the standard <em>backtrader</em> distribution, but it can be quickly crafted. A parameter will be the deciding factor as to whether the current bar will be used for the channel calculation or not.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa82c748a0e02907a5a252c88b33d967/href">https://medium.com/media/fa82c748a0e02907a5a252c88b33d967/href</a></iframe><p>Using it with lookback=-1 a sample chart looks like this (zoomed in)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LhmI3YqN37wHcAUP.png" /></figure><p>One can clearly see the breakouts, whereas there are no breakouts in the lookback=0 version.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*99aMPUQmc9x7GJ5p.png" /></figure><h3>Coding Implications</h3><p>The programmer first goes to the commercial platform and implements a strategy using <em>The Donchian Channels</em>. Because the chart does not show breakouts, a comparison of the current price value has to be done against the previous channel value. Something as</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/074cdff08f7962e4d59abe6fa546df8b/href">https://medium.com/media/074cdff08f7962e4d59abe6fa546df8b/href</a></iframe><p>The current price, i.e.: price0 is being compared to the high/low channel values of 1 period ago (hence the _1 suffix)</p><p>Being a cautious programmer and unaware that the implementation of the <em>Donchian Channels</em> in <em>backtrader</em> defaults to having breakout, the code is ported and looks like this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ce0a1855331d9fd3c811d90beb6a2c2/href">https://medium.com/media/0ce0a1855331d9fd3c811d90beb6a2c2/href</a></iframe><p>Which is wrong!!! Because the breakout happens at the same moment of the comparison. The correct code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27a813dbabe6674ad350909d20060415/href">https://medium.com/media/27a813dbabe6674ad350909d20060415/href</a></iframe><p>Although this is just a small example, it shows how backtesting results may differ because an indicator has been coded with a 1 bar difference. It may not seem much, but it will for sure make a difference when the wrong trade is started.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc0989e8a0ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fractional Sizes in backtrader]]></title>
            <link>https://medium.com/@danjrod/fractional-sizes-in-backtrader-2987c38491bc?source=rss-76212358fd10------2</link>
            <guid isPermaLink="false">https://medium.com/p/2987c38491bc</guid>
            <category><![CDATA[backtesting]]></category>
            <category><![CDATA[algotrading]]></category>
            <category><![CDATA[finance]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[backtrader]]></category>
            <dc:creator><![CDATA[Daniel Rodriguez]]></dc:creator>
            <pubDate>Thu, 29 Aug 2019 15:05:40 GMT</pubDate>
            <atom:updated>2019-08-29T17:58:24.477Z</atom:updated>
            <content:encoded><![CDATA[<h3>Trading Cryptocurrency Fractional Sizes in backtrader</h3><p>Originally @: <a href="https://www.backtrader.com/blog/posts/2019-08-29-fractional-sizes/fractional-sizes/">https://www.backtrader.com/blog/posts/2019-08-29-fractional-sizes/fractional-sizes/</a></p><p>To start with, let’s summarize in two lines how the approach to <em>backtrader</em> works:</p><ul><li>It is like a construction kit with a basic building block (Cerebro) into which many different pieces can be plugged in</li><li>The basic distribution contains many pieces like <em>Indicators</em>, <em>Analyzers</em>, <em>Observers</em>, <em>Sizers</em>, <em>Filters</em>, <em>Data Feeds</em>, <em>Brokers</em>, <em>Commission/Asset Info Schemes</em>, …</li><li>New building blocks can be easily constructed from scratch or based on existing building block</li><li>The basic building block (Cerebro) does already some automatic <em>&quot;plugging in&quot;</em> to make it easier to work with the framework without having to worry about all the details.</li></ul><p>As such the framework is pre-configured to offer a behavior with defaults such as:</p><ul><li>Work with a single/main data feed</li><li>1-day timeframe/compression combination</li><li>10,000 units of monetary currency</li><li>Stock trading</li></ul><p>This may of may not fit everyone, but the important thing is: <strong>it can be customized to the individual needs of each trader/programmer</strong></p><h3>Trading Stocks: Integers</h3><p>As stated above, the default configuration is for <em>stock</em> trading and when one is trading stocks one buys/sells complete shares, (i.e.: 1, 2 … 50 … 1000, and not amounts like 1.5 or 1001.7589shares.</p><p>This means that when a user does the following in the default configuration:</p><pre>def next(self):<br>        # Apply 50% of the portfolio to buy the main asset<br>        self.order_target_percent(target=0.5)</pre><p>The following happens:</p><ul><li>The system calculates how many shares of the asset are needed, so that the value in the portfolio of the given asset is as close as possible to 50%</li><li>But because the default configuration is to work with <strong>shares</strong> the resulting number of shares will be an whole number, i.e.: <strong>an integer</strong></li></ul><p>Note</p><p>Notice that the default configuration is to work with a single/main data feed, and that’s why the actual data is not specified in the call to order_percent_target. When operating with multiple data feeds, one has to specify which data to acquire/sell (unless the main data is meant)</p><h3>Trading Cryptocurrencies: Fractions</h3><p>It is obvious that when trading cryptocurrencies, with even 20 decimals, one can buy <em>“half of a bitcoin”</em>.</p><p>The good thing is that one can actually change the information pertaining to the asset. This is achieved through the CommissionInfo family of pluggable pieces.</p><p>Some documentation: <a href="https://www.backtrader.com/docu/commission-schemes/commission-schemes/">Docs — Commission Schemes — https://www.backtrader.com/docu/commission-schemes/commission-schemes/</a></p><p>Note</p><p>It has to be admitted that the name is unfortunate, because the schemes do not only contain information about <em>commission</em>, but also about other things.</p><p>In the fractional scenario, the interest is this method of the scheme: getsize(price, cash), which has the following docstring</p><pre>Returns the needed size to meet a cash operation at a given price</pre><p>The schemes are intimately related to the broker and through the broker api, the schemes can be added in the system.</p><p>The broker docs are at: <a href="https://www.backtrader.com/docu/broker/">Docs — Broker — https://www.backtrader.com/docu/broker/</a></p><p>And the relevant method is: addcommissioninfo(comminfo, name=None). Where in addition to adding a scheme which applies to all assets (when name is None), one can set schemes which apply only to assets with specific names.</p><h3>Implementing the fractional scheme</h3><p>This can be easily achieved by extending the existing basis scheme, named CommissionInfo.</p><pre>class CommInfoFractional(bt.CommissionInfo):<br>    def getsize(self, price, cash):<br>        &#39;&#39;&#39;Returns fractional size for cash operation @price&#39;&#39;&#39;<br>        return self.p.leverage * (cash / price)</pre><p>Ditto and done. Subclassing CommissionInfo and writing a one line method, the objective is achieved. Because the original scheme definition supports leverage, this is taken into account into the calculation, just in case cryptocurrencies can be bought with leverage (for which the default value is 1.0, i.e.: no leverage)</p><p>Later in the code, the scheme will be added (controlled via a command line parameter) like this</p><pre>if args.fractional:  # use the fractional scheme if requested<br>        cerebro.broker.addcommissioninfo(CommInfoFractional())</pre><p>I.e.: an instance (notice the () to instantiate) of the subclassed scheme is added. As explained above, the name parameter is not set and this means it will apply to all assets in the system.</p><h3>Testing the Beast</h3><p>A full script implementing a trivial moving average crossover for long/short positions is provided below which can be directly used in the shell. The default data feed for the test is one of the data feeds from the <em>backtrader</em> repository.</p><p><strong>Integer Run: No Fractions — No Fun</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/355649deebce4db3170b0a2d37d0ce74/href">https://medium.com/media/355649deebce4db3170b0a2d37d0ce74/href</a></iframe><p>A short trade with a size of 16 units has been opened. The entire log, not shown for obvious reasons, contains many other operations all with trades with whole sizes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BpmTVBZaOfP7oSyA.png" /></figure><p><strong>Fractional Run</strong></p><p>After the hard subclassing and one-lining work for the fractions …</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c985f435cb666ca231653726c27dd7b/href">https://medium.com/media/7c985f435cb666ca231653726c27dd7b/href</a></iframe><p>V for Victory. The short trade has been opened with the same crossover, but this time with a fractional size of -16.457437774427774</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yL62qXmWpTDXTc_H.png" /></figure><p>Notice that the final portfolio value in the charts is different and that is because the actual trades sizes are different.</p><h3>Conclusion</h3><p>Yes, <em>backtrader</em> can. With the pluggable/extensible construction kit approach, it is easy to customize the behavior to the particular needs of the trader programmer.</p><h3>The script</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/459f55b16ca5cbee8fef8a2cbf606030/href">https://medium.com/media/459f55b16ca5cbee8fef8a2cbf606030/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2987c38491bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beating The Random Entry]]></title>
            <link>https://medium.com/@danjrod/beating-the-random-entry-62bbdb9deac8?source=rss-76212358fd10------2</link>
            <guid isPermaLink="false">https://medium.com/p/62bbdb9deac8</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[backtrader]]></category>
            <category><![CDATA[backtesting]]></category>
            <category><![CDATA[trading]]></category>
            <category><![CDATA[algotrading]]></category>
            <dc:creator><![CDATA[Daniel Rodriguez]]></dc:creator>
            <pubDate>Mon, 26 Aug 2019 21:50:24 GMT</pubDate>
            <atom:updated>2019-08-29T17:59:27.636Z</atom:updated>
            <content:encoded><![CDATA[<p>Originally @: <a href="https://www.backtrader.com/blog/2019-08-22-practical-backtesting-replication/practical-replication/">https://www.backtrader.com/blog/2019-08-22-practical-backtesting-replication/practical-replication/</a></p><p>There have been recently some threads on <a href="https://www.reddit.com/r/algotrading/">reddit/r/algotrading </a>about the success in replicating published algotrading strategies. First</p><ul><li><a href="https://www.reddit.com/r/algotrading/comments/cr7jey/ive_reproduced_130_research_papers_about/">I’ve reproduced 130+ research papers about “predicting the stock market”, coded them from scratch and recorded the results. Here’s what I’ve learnt</a></li></ul><p>Because it has been deleted here a quick summary:</p><ul><li>The strategies do not work</li><li>If the authors claim a given strategy stopped working due to alpha decay, the tests were run against past data and it still did not work</li><li>Bottomline: It’s all overfitting, p-hacking or a tiny alpha which doesn’t need to decay because commissions do already destroy the alpha.</li></ul><p>A replica to the replication problem was written by <em>Artem Kaznatcheev</em> in:</p><ul><li><a href="https://egtheory.wordpress.com/2019/08/17/algotrading-replication/">Allegory of the replication crisis in algorithmic trading</a></li></ul><p>Followed by:</p><ul><li><a href="https://www.reddit.com/r/algotrading/comments/cv83yh/overfitting/">Overfitting</a></li></ul><p>The first two were theoretical (even if the first mentioned having implemented the 130 strategies), with <em>“Overfitting”</em> offering actual code.</p><p>With so much going on, what about trying to replicate something which was published but not as a paper, and like in the <em>“overfitting”</em> case, with a practical approach. Something published in a well known book.</p><p>The target: <em>“Trying to Beat Random Entry”</em>. This is a section in Part 3, Chapter 8 of this book:</p><ul><li><a href="https://www.amazon.com/Trade-Your-Way-Financial-Freedom/dp/007147871X">Amazon — Trade Your Way to Financial Freedom</a></li><li><a href="https://www.vantharp.com/trade-your-way-to-financial-freedom">Van Tharp Institute — Trade Your Way to Financial Freedom</a></li></ul><p>A structured approach to engage into algotrading is presented in the book with a special emphasis on: <strong>position sizing</strong> and management of the position (i.e.: when to actually exit a trade). This being a lot more important than, for example, the entry setup, which apparently most people consider the main driver.</p><p>In Chapter 8, Van K. Tharp talks to <em>Tom Basso</em> and says: <em>“From what you are saying it sounds like you could make money consistently with a random entry as long as you have good exits and size your positions intelligently.”</em> to which the answer was that <em>he probably could</em>.</p><p><strong>Rules</strong>:</p><ul><li>Coin-Flip based entry</li><li>Always in the market — Long or Short</li><li>Reentry as soon as the exit signal was given</li><li>Volatility of the market is determined by a 10-day Exponential Moving Average of the Average True Range</li><li>Trailing stop at distance from the close 3-times the volatility</li><li>The stop could only move in the direction of the trade</li><li>Fixed Stake (1 contract) or 1% risk model (Chapter 12 in the book)</li></ul><p><strong>Results</strong></p><ul><li>Tested against 10 markets</li><li>Fixed Stake: made money 80% of the time</li><li>1% Risk Model: made money 100% of the time</li><li>Reliability level: 38% (percentage of winning trades)</li></ul><p><strong>The missing bits</strong>:</p><ul><li>Tested markets</li><li>Tested periods</li><li>If always in the market means closing the trade “today” and re-entering “tomorrow” or if it means issuing both a the closing/re-opening orders at the same time.</li></ul><p>This is actually the easiest to overcome.</p><p>For the last two items, the book says the conversation took place in 1991 and futures were used. To be fair to the book, futures data from before 1991 will be used. 1-day price bars are also assumed, given the mention of the <em>10-day Exponential Moving Average</em></p><p>The most obvious problem seems to get the algorithm right, but in this case the book does a good job at describing the simple algorithm and the results. To complete it, let’s summarize the <em>“Percentage Risk Model”</em> (aka “Model 3” in the book) from <em>Chapter 12</em>.</p><ul><li>Maximal Loss: limited to x% of the account value (i.e.: the percentage risk)</li><li>Risk per Contract : with the given algorithm, it will be the initial stop distance (3 times the volatility) multiplied by the multiplier of the future</li><li>Amount of contracts: Maximal Loss / Risk Per Contract</li></ul><h3>The replication details</h3><p><strong>The data</strong></p><p>The CL (Crude Oil) futures data from 1985 to 1990 (i.e.: 6 full years) will be used. The contract specification has a:</p><ul><li>Tick size: 0.01 (i.e.: 100 ticks per point)</li><li>Cost per tick: $10</li></ul><p>With this in mind, we will use a multiplier of 1000 per 1 point (100 ticks/point x 10 dollars/tick = 1000 dollars)</p><p><strong>Commission</strong></p><p>A 2.00 monetary units per contract per trip will be used (IB-like)</p><p><strong>Some implementation Details</strong></p><p>The coin flip is modeled as an indicator, to make it easy to visualize where the flips have taken place (and if several entries, for example, go in the same direction, which is to be expected from randomness)</p><p>To also have a good visualization of the stop and how it moves, the stop price calculation and logic are also embedded in a indicator. Notice that the stop calculation logic has two different stages</p><ul><li>When a trade starts the stop price has to be set at the given distance from the close with no relation to previous stop prices</li><li>When a trade is running, the stop price is adjusted if possible to follow the trend.</li></ul><p><strong>Charting</strong></p><p>The code produces two types of charts</p><ol><li>A chart containing the details a single test run (--plot) option. It makes most sense to use it when running a single iteration (--iterations 1)</li><li>A scatter chart showing the Profit and Loss of the runs.</li></ol><p>Sample of #1</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kzRe7C5EqLVWchW8.png" /></figure><p>Sample of #2 for 10 runs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*H6xjbxg2sj8bCHD5.png" /></figure><h3>Sample invocations of the script</h3><p><strong>A single run with fixed size stake of 1 and plotting</strong></p><pre>./vanktharp-coinflip.py --years 1985-1990 --fixedsize --sizer stake=1 --iterations 1 --plot</pre><pre>**** Iteration:    1<br>-- PNL: 10482.00<br>--   Trades 49 - Won 22 - %_Won: 0.45<br>**** Summary of Runs<br>-- Total       :        1<br>-- Won         :        1<br>-- % Won       : 1.00<br>**** Summary of Trades<br>-- Total       :       49<br>-- Total Won   :       22<br>-- % Total Won : 0.45</pre><p><strong>100 runs with a 1% risk model, 10 iterations and a scatter chart</strong></p><p>(The output is shortened for practical purposes)</p><pre>$ ./vanktharp-coinflip.py --years 1985-1990 --percrisk --sizer percrisk=0.01 --iterations 100 --scatter<br>**** Iteration:    1<br>-- PNL: -18218.00<br>--   Trades 60 - Won 24 - %_Won: 0.40<br>**** Iteration:    2<br>...<br>...<br>**** Iteration:  100<br>-- PNL: 111366.00<br>--   Trades 50 - Won 26 - %_Won: 0.52<br>**** Summary of Runs<br>-- Total       :      100<br>-- Won         :       50<br>-- % Won       : 0.50<br>**** Summary of Trades<br>-- Total       :     5504<br>-- Total Won   :     2284<br>-- % Total Won : 0.41</pre><h3>Test Runs Mix</h3><p>10 test runs of 100 iterations were done, mixing the following variables:</p><ul><li>Fixed size stake of 1 or percentage risk model with 1%</li><li>Executing entry/exit in the same bar or in consecutive bars</li></ul><h3>Summary of results</h3><ul><li>In average, 49% of the runs made a profit. Fixed size remained close to 50% in the test runs and the percentage risk model varied more with a test run scoring a minimum of 39% profitable runs and another scoring a maximum of 65% profitable runs (out of 10)</li><li>In average, 39% of the trades were profitable (small deviation)</li></ul><p>Recalling what the book said:</p><ul><li>80% of profitable runs when using a fixed size stake of 1</li><li>100% of profitable runs with the 1% percentage risk model</li><li>38% of profitable trades</li></ul><p>It would therefore seem that:</p><ul><li>Only the last of the items was replicated.</li></ul><h3>Conclusion</h3><p>As pointed out by <em>Artem Kaznatcheev</em>, the replication crisis can be due to:</p><ul><li>Using the wrong data set</li><li>Failing to correctly implement the algorithm</li></ul><p>Or it could well be that the original implementation did not follow its own rules or that not all details were published.</p><p>Note</p><p>In any case I do still personally recommend reading the book. Failing to replicate that specific case does not mean the book is not a good read and shows a practical approach at algotrading.</p><h3>The complete script</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34598c2c0e12014035aea18b1c05fa52/href">https://medium.com/media/34598c2c0e12014035aea18b1c05fa52/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62bbdb9deac8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rebalancing with the Conservative Formula]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-snippet">The Conservative Formula approach is presented in this paper: The Conservative Formula in Python: Quantitative Investing made Easy</p><p class="medium-feed-link"><a href="https://medium.com/@danjrod/rebalancing-with-the-conservative-formula-44a4d5f15e4d?source=rss-76212358fd10------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@danjrod/rebalancing-with-the-conservative-formula-44a4d5f15e4d?source=rss-76212358fd10------2</link>
            <guid isPermaLink="false">https://medium.com/p/44a4d5f15e4d</guid>
            <category><![CDATA[investing]]></category>
            <category><![CDATA[algotrading]]></category>
            <category><![CDATA[backtrader]]></category>
            <category><![CDATA[backtesting]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Daniel Rodriguez]]></dc:creator>
            <pubDate>Mon, 22 Jul 2019 22:12:06 GMT</pubDate>
            <atom:updated>2019-08-29T18:02:18.684Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[MFI Generic]]></title>
            <link>https://medium.com/@danjrod/mfi-generic-7c805c001a6b?source=rss-76212358fd10------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c805c001a6b</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[backtrader]]></category>
            <category><![CDATA[backtesting]]></category>
            <category><![CDATA[algotrading]]></category>
            <category><![CDATA[indicators]]></category>
            <dc:creator><![CDATA[Daniel Rodriguez]]></dc:creator>
            <pubDate>Wed, 17 Jul 2019 11:13:59 GMT</pubDate>
            <atom:updated>2019-08-29T18:03:05.344Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Originally published at </em><a href="https://www.backtrader.com/blog/2019-07-17-mfi-generic/mfi-generic/">https://www.backtrader.com/blog/2019-07-17-mfi-generic/mfi-generic/</a></p><p>In the recent <a href="https://www.backtrader.com/blog/2019-07-08-canonical-or-not/canonical-or-not/">Canonical vs Non-Canonical</a> post, the MFI (aka MoneyFlowIndicator) was developed.</p><p>Although it is developed in the <em>canonical</em> way, it does still offer some room for improvement and becoming generic.</p><p>Let’s focus of the 1st lines of the implementation, the ones which create the typical price</p><p><strong>Canonical MFI — Typical Price and Raw Money Flow</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd248e4f05b9f76dde06b3cf0065bc5d/href">https://medium.com/media/fd248e4f05b9f76dde06b3cf0065bc5d/href</a></iframe><p>A typical instantiation would look like this</p><p><strong>MFI Typical Instantiation</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c8acf8f3f5049d534d22392a1d5d564/href">https://medium.com/media/6c8acf8f3f5049d534d22392a1d5d564/href</a></iframe><p>The problem here should be obvious: <em>“One needs an input for the indicator which features </em><em>close, </em><em>low, </em><em>high and </em><em>volume components (aka lines</em> in the <em>backtrader</em> <em>ecosystem</em>)”</p><p>It may, of course, be the case that one wishes to create a MoneyFlowIndicator using components from different data sources (lines from data feeds or lines from other indicators) As simple as wanting to give the close a lot more weight, without having to develop a specific indicator. Considering the industry-standard OHLCV field ordering, a multiple inputs, extra weight for close, instantiation could look like this</p><p><strong>MFI Multiple Inputs Instantiation</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f65d48c1385783fd35831953f0cb1f29/href">https://medium.com/media/f65d48c1385783fd35831953f0cb1f29/href</a></iframe><p>Or because the user previously worked with ta-lib and fancies the multiple inputs style.</p><h3>Supporting multiple inputs</h3><p><em>backtrader</em> tries to be as <em>pythonic</em> as possible and the self.datas array containing the list of data feeds in the system (and which is auto-magically provided to your strategy) can be queried for its length. Let&#39;s use this to discriminate what the caller wants and properly calculate tpriceand mfraw</p><p><strong>MFI — Multiple Inputs using len</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3d0f3b2e276eeac56d3a86fd3cb250e/href">https://medium.com/media/b3d0f3b2e276eeac56d3a86fd3cb250e/href</a></iframe><blockquote><strong>NOTE</strong></blockquote><blockquote>Notice how the individual components are referenced as self.dataX (such as self.data0, self.data1)</blockquote><blockquote>This is the same as using self.datas[x], as in self.datas[0] ...</blockquote><p>Let’s see graphically that this indicator produces the same results as the <em>canonical</em> one, and the same results when the multiple inputs correspond to the original components of the data feed. To do so, it will be run in a strategy as this</p><p><strong>MFI — Results Check</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5be2e573943f3dcf9b83353b03da8ce4/href">https://medium.com/media/5be2e573943f3dcf9b83353b03da8ce4/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ygigllf6qk6WtRoD.png" /></figure><p>Without having to resort to check each value, it should be obvious from the picture that the results are the same for the three.</p><p>Let’s finally see what happens if put a lot more weight on to the close. Let&#39;s run like this.</p><p><strong>MFI — 5 x Close</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb2b70e4bee3e333bc569c9125f17450/href">https://medium.com/media/cb2b70e4bee3e333bc569c9125f17450/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qq89lrAPijQRpcwI.png" /></figure><p>Whether this makes sense or not is left to the reader, but one can clearly see that adding weight to the close has altered the pattern.</p><h3>Conclusion</h3><p>By simple using the pythonic len, one can transform an indicator which uses a data feed with multiple components (and fixed names) into an indicator which accepts multiple generic inputs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c805c001a6b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Canonical vs Non Canonical — Backtrader]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@danjrod/canonical-vs-non-canonical-backtrader-8e17c23c69de?source=rss-76212358fd10------2"><img src="https://cdn-images-1.medium.com/max/1920/0*q8l-fmqYpvgAQeqf.png" width="1920"></a></p><p class="medium-feed-snippet">The question has shown up several times more or less like this:</p><p class="medium-feed-link"><a href="https://medium.com/@danjrod/canonical-vs-non-canonical-backtrader-8e17c23c69de?source=rss-76212358fd10------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@danjrod/canonical-vs-non-canonical-backtrader-8e17c23c69de?source=rss-76212358fd10------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e17c23c69de</guid>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Daniel Rodriguez]]></dc:creator>
            <pubDate>Tue, 09 Jul 2019 12:14:02 GMT</pubDate>
            <atom:updated>2019-08-29T18:03:35.114Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Custom “recursive” indicator in Python with backtrader]]></title>
            <link>https://medium.com/@danjrod/custom-recursive-indicator-in-python-with-backtrader-1878959ec011?source=rss-76212358fd10------2</link>
            <guid isPermaLink="false">https://medium.com/p/1878959ec011</guid>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Daniel Rodriguez]]></dc:creator>
            <pubDate>Mon, 03 Sep 2018 14:18:57 GMT</pubDate>
            <atom:updated>2018-09-03T14:18:57.316Z</atom:updated>
            <content:encoded><![CDATA[<p>In the previous article (<a href="https://medium.com/@danjrod/custom-indicator-development-in-python-with-backtrader-bc775552dc3e"><em>Custom Indicator Development in Python with backtrader</em></a>) the development of a custom indicator was explore (the <em>Stochastic</em> indicator)</p><p>But some indicators have a particularity: <strong><em>they are recursive</em></strong>. This requires some extra management, because recursion needs to be solved for the calculation of the first value to be delivered (or else one remains stuck in the classic chicken-egg problem)</p><p>Let’s use the well known <em>Exponential Moving Average</em> (aka <em>EMA</em>) as an example of how a recursive indicator is developed. To simplify things, this is how the <em>EMA</em> is calculated</p><ul><li>ema(0) = (1 — alpha) * ema(-1) + alpha * data</li></ul><p>Where ema(0) is the current value and ema(-1) is the previous value (hence the <em>recursion</em>)</p><p>Although an exponential smoothing takes into account all the past values of a series, the trading industry (and traders) tends to think in terms of the period (or numbers of bars) to which the exponential smoothing is applied. The smoothing factor ( alpha ) can be calculated as follows</p><ul><li>alpha = 2 / (period + 1</li></ul><p>For those interested in further reading the article in the <em>Wikipedia</em> explains the details</p><ul><li><a href="https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average">https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average</a></li></ul><p>The obvious impulse when creating the EMA in <em>backtrader</em> would be to do the following</p><pre>import backtrader as bt</pre><pre>class MyEMA(bt.ind.PeriodN):<br>    lines = (&#39;ema&#39;,)<br>    params = ((&#39;period&#39;, 30),)<br>    plotinfo = dict(subplot=False)  # plot in same axis as the data</pre><pre>    def __init__(self):<br>        alph = 2.0 / (self.p.period + 1.0)<br>        self.l.ema = (1 - alph) * self.l.ema(-1) + alph * self.data</pre><p>Which creates the <em>recursion conundrum</em> because one needs the previous value to calculate the current one, and there is obviously no previous value at the beginning of the chain of calculations.</p><p><strong>Note</strong>: the indicator subclasses PeriodN which already knows what to do with a parameter period to inform the platform about the minimum warm up period requirements</p><p>If one decided to run this code against the sample data in the <em>backtrader</em> distribution the graphical output would show the problem</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bXSCe2sOu2Br1_BpKiGkFw.png" /></figure><p><strong>The solution: add a seed value</strong></p><p>next is the standard method for the step by step calculations in all objects with <em>lines</em> in <em>backtrader</em>, but there is one additional method which comes to the rescue for our problem of setting a seed value</p><ul><li>nextstart which is called exactly once: when the minimum warmup period for the indicator is met. It defaults to delegating the work to next but in this case it will seed the calculation</li></ul><pre>import backtrader as bt</pre><pre>class MyEMA(bt.ind.PeriodN):<br>    lines = (&#39;ema&#39;,)<br>    params = ((&#39;period&#39;, 30),)<br>    plotinfo = dict(subplot=False)  # plot in same axis as the data</pre><pre>    def __init__(self):<br>        alph = 2.0 / (self.p.period + 1.0)<br>        self.l.ema = (1 - alph) * self.l.ema(-1) + alph * self.data</pre><pre><br>    def nextstart(self):<br>        period = self.p.period  # for readability<br>        self.l.ema[0] = self.data.get(size=period) / period</pre><p>But if we were to run this in the default execution mode <em>backtrader</em> uses (<strong>runonce<em>=</em>True</strong>) this would simple deliver no value and the chart, just like above, would also be empty.</p><p>Running it with cerebro.run(runonce=False) does the magic as seen in this chart.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LlJbXLUwrtOWcTPVnnDbkg.png" /></figure><p><strong>Why this?</strong></p><ul><li>When running in the default mode, <em>backtrader</em> calculates things in vectorized mode and this has a consequence: self.l.ema(-1) is a made to be a vector of values (i.e.: an array) which is precalculated before being using for the calculation of self.l.ema</li></ul><p>Which simply means that the declarative approach (we declared the calculation method during __init__ , hence the <em>declarative</em> nature) combined with <em>recursion</em> faces restrictions when working in <em>vectorized</em> mode.</p><p><strong>Can it be done?</strong></p><p>Yes it can. But in this case one can forego the declarative approach and perform all calculations manually using the dirtiest inner tricks known only to advanced *bactraders*</p><pre>import backtrader as bt</pre><pre>class MyEMA(bt.ind.PeriodN):<br>    lines = (&#39;ema&#39;,)<br>    params = ((&#39;period&#39;, 30),)<br>    plotinfo = dict(subplot=False)  # plot in same axis as the data</pre><pre>    def oncestart(self, start, end):<br>        # Calculate a seed value for the EMA<br>        src, dst = self.data.array, self.line.array<br>        period = self.p.period  # for readability<br>        dst[start] = sum(src[start - period + 1:end]) / period</pre><pre>    def once(self, start, end):<br>        src, dst = self.data.array, self.line.array<br>        alpha = 2.0 / (self.p.period + 1.0)<br>        alpha1 = 1.0 - alpha</pre><pre>        ema1 = dst[start - 1]<br>        for i in range(start, end):<br>            dst[i] = ema1 = alpha1 * ema1 + src[i] * alpha</pre><p>In this case the methods oncestart and once have been used (recall the parameter for <em>cerebro</em> is calledrunonce and this allows the calculation of the indicator in the faster runonce=True mode in <em>backtrader</em> without being worried about recursion problems.</p><p>Of course, one doesn’t usually write two different versions of an <em>Indicator</em>, but rather one. See the source code for the <em>ExponentialSmoothing</em> indicator which is used in <em>backtrader</em> (the <em>ExponentialMovingAverage</em> is a wrapper to make it part of the larger family of moving averages) See it here</p><ul><li><a href="https://github.com/backtrader/backtrader/blob/0fa6ac8bd4bc2f89d0ae45a9447b411d0db88fa9/backtrader/indicators/basicops.py#L367">https://github.com/backtrader/backtrader/blob/0fa6ac8bd4bc2f89d0ae45a9447b411d0db88fa9/backtrader/indicators/basicops.py#L367</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1878959ec011" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Custom Indicator Development in Python with backtrader]]></title>
            <link>https://medium.com/@danjrod/custom-indicator-development-in-python-with-backtrader-bc775552dc3e?source=rss-76212358fd10------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc775552dc3e</guid>
            <category><![CDATA[backtesting]]></category>
            <category><![CDATA[backtrader]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Daniel Rodriguez]]></dc:creator>
            <pubDate>Sat, 18 Aug 2018 20:38:32 GMT</pubDate>
            <atom:updated>2018-08-18T20:38:32.969Z</atom:updated>
            <content:encoded><![CDATA[<p>The origins of <em>backtrader</em> are rooted in a simple idea:</p><ul><li>Being able to quickly test and prototype new indicators and strategies</li></ul><p>Being one of the reasons why <em>Python</em> was chosen as the language and after some iterations the “<em>canonical</em>” way to develop <em>Indicators</em> was to use a declarative approach, i.e.: declare during __init__ the entire set of operations/formulas that make up the indicator (and where needed be, some extra calculations during next although this is avoided whenever possible.</p><p>Coupled with the already declarative approach the lines of an indicator (output arrays) and the params which can be passed to it, one can have a complete declarative approach. There is actually no need to declare any <em>input</em> because this is handled automatically with the <em>automagically</em> provided self.datas array (and the aliases self.data0, self.data1, self.dataX) which is already available for the indicator.</p><p>Let’s see a very dummy <em>Indicator</em> which will simply divide the the difference of the current data point minus a previous data point</p><pre>import backtrader as bt</pre><pre>class DummyDifferenceDivider(bt.Indicator):<br>    lines = (&#39;diffdiv&#39;,)  # output line (array)<br>    params = (<br>        (&#39;period&#39;, 1),  # distance to previous data point<br>        (&#39;divfactor&#39;, 2.0),  # factor to use for the division<br>    )</pre><pre>    def __init__(self):<br>        diff = self.data(0) - self.data(-self.p.period)<br>        diffdiv = diff / self.p.divfactor</pre><pre>        self.lines.diffdiv = diffdiv</pre><p>It should self-explanatory. The actual operation in __init__ could have been written in a single line, but it has been divided into the 3 basic steps (difference, division and assignment to array) for clarity.</p><p>One thing to note here if you haven’t done <em>Indicator</em> development in <em>backtrader</em> ist that the <strong>minimum period</strong> constraints needed are automatically calculated from the constraints. What are these constraints in this case?</p><ul><li>self.data(-self.p.period) is pulling the data from period bars ago. As such, the platform knows it has to buffer those many period bars.</li></ul><p>This is a declarative <em>Indicator</em> as explained above. There are no step by step calculations. The question here:</p><ul><li>Can it be done in a different way? Yes</li></ul><p>Rather than seeing it with that dummy <em>Indicator</em>, a real life example is going to be used from a discussion in the <em>backtrader</em> Community. The link</p><ul><li><a href="https://community.backtrader.com/topic/1245/custom-indicator-understanding-lines-list-index-out-of-range/18">https://community.backtrader.com/topic/1245/custom-indicator-understanding-lines-list-index-out-of-range/</a></li></ul><p>In that thread the user is trying to develop (doing it himself rather than asking for someone to write the code for him, which is quite common in our modern days) a custom <em>Stochastic</em> which first calculates the actual stochastic value and then a value derived from it.</p><p>Two approaches are going to be examined (and charted to visually see the results are the same)</p><ol><li>Declarative as above</li><li>Non-declarative or step by step</li></ol><p>For anyone interested the definitions of the <em>Stochastic Indicator:</em></p><ul><li>Stockcharts: <a href="https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:stochastic_oscillator_fast_slow_and_full">https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:stochastic_oscillator_fast_slow_and_full</a></li></ul><p>Summarizing:</p><ul><li>%K = (Current Close — Lowest Low (x periods ago))/(Highest High (x periods ago) — Lowest Low (x periods ago)) * 100</li><li>%D = y-day SMA of %K</li></ul><p>There are <em>Fast</em>, <em>Slow</em> and <em>Full</em> versions of the <em>Stochastic</em>. For simplicity this will focus on the <em>Fast</em> version, which simply calculates %<em>K</em> and <em>%D</em> and doesn’t perform any additional smoothing (“<em>slowing</em>”)</p><p>In this case the <em>lowest low</em> and <em>highest high</em> will be taken from the data (ideally the high and low components should be considered)</p><p><strong>Declarative Approach</strong></p><p>Simple version of a <em>Fast Stochastic</em> which uses a single value data feed and doesn’t handle <em>division by zero</em> errors.</p><p>The customized value <em>“</em><strong><em>mystoc</em></strong><em>”</em> will be a very simple operation:</p><pre>abs(k - k(-1)) / 2</pre><p>I.e.: the absolute half value of the difference between the current k and the previous k (which is depicted as k(-1) )</p><pre>import backtrader as bt</pre><pre>class MyStochastic1(bt.Indicator):<br>    lines = (&#39;k&#39;, &#39;d&#39;, &#39;mystoc&#39;,)  # declare the output lines</pre><pre>    params = (<br>        (&#39;k_period&#39;, 14),  # lookback period for highest/lowest<br>        (&#39;d_period&#39;, 3),  # smoothing period for d with the SMA<br>    )</pre><pre><br>    def __init__(self):<br>        # declare the highest/lowest<br>        highest = bt.ind.Highest(self.data, period=self.p.k_period)<br>        lowest = bt.ind.Lowest(self.data, period=self.p.k_period)</pre><pre>        # calculate and assign lines<br>        self.lines.k = k = (self.data - lowest) / (highest - lowest)<br>        self.lines.d = d = bt.ind.SMA(k, period=self.p.d_period)<br>        self.lines.mystoc = abs(k - k(-1)) / 2.0</pre><p>Et voilá! It does actually seem very similar to the definition.</p><p><strong>Non-declarative Approach</strong></p><p>Let’s get dirty</p><pre>import backtrader as bt</pre><pre>class MyStochastic2(bt.Indicator):<br>    lines = (&#39;k&#39;, &#39;d&#39;, &#39;mystoc&#39;,)<br>    # manually counted period<br>    # 14 for the fast moving k<br>    # 3 for the slow moving d<br>    # No extra for the previous k (-1) is needed because<br>    # already buffers more than the 1 period lookback<br>    # If we were doing d - d(-1), there is nothing making<br>    # sure k(-1) is being buffered and an extra 1 would be needed</pre><pre>    params = (<br>        (&#39;k_period&#39;, 14),  # lookback period for highest/lowest<br>        (&#39;d_period&#39;, 3),  # smoothing period for d with the SMA<br>    )<br></pre><pre>    def __init__(self):<br>        self.addminperiod(self.p.k_period + self.p.d_period)</pre><pre><br>    def next(self):<br>        # Get enough data points to calculate k and do it<br>        d = self.data.get(size=self.p.k_period)<br>        hi = max(d)<br>        lo = min(d)<br>        self.lines.k[0] = k0 = (self.data[0] - lo) / (hi - lo)</pre><pre>        # Get enough ks to calculate the SMA of k. Assign to d<br>        last_ks = self.l.k.get(size=self.p.d_period)<br>        self.lines.d[0] = sum(last_ks) / self.p.d_period</pre><pre>        # Now calculate mystoc<br>        self.lines.mystoc[0] = abs(k0 - self.l.k[-1]) / 2.0</pre><p>In comparison with the <em>declarative</em> approach the following can be seen</p><ul><li>Manual calculation of the lookback period and having to understand what contributes to the actual lookback period</li><li>Manual set-up of the lookback period during __init__ with addminperiod</li><li>Using get with the different data and lines objects to get the buffers for calculations</li><li>The simple moving average is calculated manually</li></ul><p>In any case it is for sure not clearer and not cleaner and with many things to consider.</p><p><strong>The graphical output</strong></p><p>Now and using the sample data that is bundled with <em>backtrader</em>, and a script using the standard skeleton most samples use, the two indicators will be put in play to show that they are actually equivalent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vG4aZ0PUyZxH5uTKsUtXtw.png" /><figcaption>The two custom Stochastic Indicators shown</figcaption></figure><p><strong>Conclusion</strong></p><p>Both approaches deliver the same results. The declarative approach was the one conceived for the platform, but this doesn’t have to be what everybody likes and a step by step approach is also possible (and mixing both of course)</p><p>In any case, the goal was to be able to quickly and easily conceive and develop new indicators … and at least in the opinion of the author, the goal was reached.</p><p>Enjoy!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc775552dc3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>