<?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 Oleg Polakow on Medium]]></title>
        <description><![CDATA[Stories by Oleg Polakow on Medium]]></description>
        <link>https://medium.com/@polakowo?source=rss-a810fd509ab3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*pSOV_ymOtubSDnqL</url>
            <title>Stories by Oleg Polakow on Medium</title>
            <link>https://medium.com/@polakowo?source=rss-a810fd509ab3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 08:15:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@polakowo/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[SuperFast SuperTrend]]></title>
            <link>https://medium.datadriveninvestor.com/superfast-supertrend-6269a3af0c2a?source=rss-a810fd509ab3------2</link>
            <guid isPermaLink="false">https://medium.com/p/6269a3af0c2a</guid>
            <category><![CDATA[backtesting]]></category>
            <category><![CDATA[vectorbt]]></category>
            <category><![CDATA[numba]]></category>
            <category><![CDATA[supertrend-indicator]]></category>
            <category><![CDATA[trading]]></category>
            <dc:creator><![CDATA[Oleg Polakow]]></dc:creator>
            <pubDate>Sun, 13 Feb 2022 20:48:21 GMT</pubDate>
            <atom:updated>2022-04-05T16:31:48.613Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YJeRu9zVDgK8wGbJtVMBvQ.png" /></figure><p>While Python is slower than many compiled languages, it’s easy to use and extremely diverse. For many, especially in the data science domain, the practicality of the language beats the speed considerations — it’s like a Swiss army knife for programmers and researchers alike.</p><p>Unfortunately for quants, Python becomes a real bottleneck when iterating over (a large amount of) data. For this reason, there is an entire ecosystem of scientific packages such as NumPy and Pandas, which are highly optimized for performance, with critical code paths often written in Cython or C. Those packages mostly work on arrays, giving us a common interface for processing data in an efficient manner.</p><p>This ability is highly appreciated when constructing indicators that can be translated into a set of vectorized operations, such as <a href="https://www.investopedia.com/terms/o/onbalancevolume.asp">OBV</a>. But even non-vectorized operations, such as the exponential weighted moving average (EMA) powering numerous indicators such as <a href="https://www.investopedia.com/terms/m/macd.asp">MACD</a>, were implemented in a compiled language and are offered as a ready-to-use Python function. But sometimes, an indicator is difficult or even impossible to develop solely using standard array operations because the indicator introduces a path dependency, where a decision today depends upon a decision made yesterday. One member of such a family of indicators is SuperTrend.</p><p>In this article, you will learn how to design and implement a SuperTrend indicator, and gradually optimize it towards a never-seen performance using <a href="https://github.com/mrjbq7/ta-lib">TA-Lib</a> and <a href="http://numba.pydata.org/">Numba</a>. We will also backtest the newly created indicator on a range of parameters using <a href="https://vectorbt.pro/">vectorbt PRO</a> (<em>+ see hints on how to run various parts using the </em><a href="https://vectorbt.dev/"><em>community version</em></a><em> as well</em>).</p><h3>Data</h3><p>The first step is always getting the (right) data. In particular, we need a sufficient amount of data to benchmark different SuperTrend implementations. Let’s pull 2 years of hourly Bitcoin and Ethereum data from Binance using the vectorbt’s <a href="https://vectorbt.pro/api/data/custom/#vectorbtpro.data.custom.BinanceData">BinanceData</a> class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb1cf8dc239b4a84b6bdc36a81895641/href">https://medium.com/media/bb1cf8dc239b4a84b6bdc36a81895641/href</a></iframe><p><em>Hint: use </em><em>download in the community version.</em></p><p>The fetching operation for both symbols took us around 80 seconds to complete. Since Binance, as any other exchange, will never return the whole data at once, vectorbt first requested the maximum amount of data starting on January 1st, 2020 and then gradually collected the remaining data by also respecting the Binance’s API rate limits. In total, this resulted in 36 requests per symbol. Finally, vectorbt aligned both symbols in case their indexes or columns were different and made the final index timezone-aware (in UTC).</p><p>To avoid repeatedly hitting the Binance servers each time we start a new Python session, we should save the downloaded data locally using either the vectorbt’s <a href="https://vectorbt.pro/api/data/base/#vectorbtpro.data.base.Data.to_csv">Data.to_csv</a> or <a href="https://vectorbt.pro/api/data/base/#vectorbtpro.data.base.Data.to_hdf">Data.to_hdf</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3193d3eabc3f01aa56cb7771b37a975/href">https://medium.com/media/c3193d3eabc3f01aa56cb7771b37a975/href</a></iframe><p><em>Hint: local data is not supported by the community version but you can still save and retrieve data manually using Pandas.</em></p><p>We can then access the saved data easily using <a href="https://vectorbt.pro/api/data/custom/#vectorbtpro.data.custom.HDFData">HDFData</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5035911731cb72986f2cc8d7b3e0c5e/href">https://medium.com/media/b5035911731cb72986f2cc8d7b3e0c5e/href</a></iframe><p>Once we have the data, let’s take a quick look at what’s inside. To get any of the stored DataFrames, use the <a href="https://vectorbt.pro/api/data/custom/#vectorbtpro.data.base.Data.data">Data.data</a> dictionary with each DataFrame keyed by symbol:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9da2add3cdebca32faaa5654bdea1f5/href">https://medium.com/media/d9da2add3cdebca32faaa5654bdea1f5/href</a></iframe><pre>&lt;class &#39;pandas.core.frame.DataFrame&#39;&gt; <br>DatetimeIndex: 17514 entries, 2019-12-31 23:00:00+00:00 to 2021-12-31 22:00:00+00:00 <br>Data columns (total 10 columns): <br> #   Column              Non-Null Count  Dtype               <br>---  ------              --------------  -----               <br> 0   Open                17514 non-null  float64             <br> 1   High                17514 non-null  float64             <br> 2   Low                 17514 non-null  float64             <br> 3   Close               17514 non-null  float64             <br> 4   Volume              17514 non-null  float64             <br> 5   Close time          17514 non-null  datetime64[ns, UTC] <br> 6   Quote volume        17514 non-null  float64             <br> 7   Number of trades    17514 non-null  int64               <br> 8   Taker base volume   17514 non-null  float64             <br> 9   Taker quote volume  17514 non-null  float64             <br>dtypes: datetime64[ns, UTC](1), float64(8), int64(1) <br>memory usage: 2.0 MB</pre><p>We can also get an overview of all the symbols captured:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/673aebfefa6196e558568df9dffe1f03/href">https://medium.com/media/673aebfefa6196e558568df9dffe1f03/href</a></iframe><pre>Start                   2019-12-31 23:00:00+00:00<br>End                     2021-12-31 22:00:00+00:00<br>Period                                      17514<br>Total Symbols                                   2<br>Null Counts: BTCUSDT                          0.0<br>Null Counts: ETHUSDT                          0.0<br>Name: agg_func_mean, dtype: object</pre><p>Each symbol has 17514 data points with no NaNs — good!</p><p>If you ever worked with vectorbt, you would know that vectorbt loves the data to be supplied with symbols as columns — one per backtest — rather than features as columns. Since SuperTrend depends upon the high, low, and close price, let’s get those three features as separate DataFrames using <a href="https://vectorbt.pro/api/data/custom/#vectorbtpro.data.base.Data.get">Data.get</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ad21bf530a5a5f51bae281217b04830/href">https://medium.com/media/6ad21bf530a5a5f51bae281217b04830/href</a></iframe><pre>symbol                      BTCUSDT  ETHUSDT<br>Open time                                   <br>2019-12-31 23:00:00+00:00   7195.23   129.16<br>2020-01-01 00:00:00+00:00   7177.02   128.87<br>2020-01-01 01:00:00+00:00   7216.27   130.64<br>2020-01-01 02:00:00+00:00   7242.85   130.85<br>2020-01-01 03:00:00+00:00   7225.01   130.20<br>...                             ...      ...<br>2021-12-31 18:00:00+00:00  46686.41  3704.43<br>2021-12-31 19:00:00+00:00  45728.28  3626.27<br>2021-12-31 20:00:00+00:00  45879.24  3645.04<br>2021-12-31 21:00:00+00:00  46333.86  3688.41<br>2021-12-31 22:00:00+00:00  46303.99  3681.80</pre><pre>[17514 rows x 2 columns]</pre><p>We’re all set to design our first SuperTrend indicator!</p><h3>Design</h3><p>SuperTrend is a trend-following indicator that uses Average True Range (<a href="https://en.wikipedia.org/wiki/Average_true_range">ATR</a>) and <a href="https://www.incrediblecharts.com/indicators/median_price.php">median price</a> to define a set of upper and lower bands. The idea is rather simple: when the close price crosses above the upper band, the asset is considered to be entering an uptrend, hence a buy signal. When the close price crosses below the lower band, the asset is considered to have exited the uptrend, hence a sell signal.</p><p>Unlike the idea, the calculation procedure is anything but simple:</p><pre>BASIC UPPERBAND = (HIGH + LOW) / 2 + Multiplier * ATR<br>BASIC LOWERBAND = (HIGH + LOW) / 2 - Multiplier * ATR</pre><pre>FINAL UPPERBAND = IF (Current BASICUPPERBAND &lt; Previous FINAL UPPERBAND) or (Previous Close &gt; Previous FINAL UPPERBAND)<br>                  THEN Current BASIC UPPERBAND<br>                  ELSE Previous FINAL UPPERBAND<br>FINAL LOWERBAND = IF (Current BASIC LOWERBAND &gt; Previous FINAL LOWERBAND) or (Previous Close &lt; Previous FINAL LOWERBAND)<br>                  THEN Current BASIC LOWERBAND <br>                  ELSE Previous FINAL LOWERBAND</pre><pre>SUPERTREND      = IF (Previous SUPERTREND == Previous FINAL UPPERBAND) and (Current Close &lt;= Current FINAL UPPERBAND)) <br>                  THEN Current FINAL UPPERBAND<br>                  ELIF (Previous SUPERTREND == Previous FINAL UPPERBAND) and (Current Close &gt; Current FINAL UPPERBAND) <br>                  THEN Current FINAL LOWERBAND<br>                  ELIF (Previous SUPERTREND == Previous FINAL LOWERBAND) and (Current Close &gt;= Current FINAL LOWERBAND) <br>                  THEN Current FINAL LOWERBAND<br>                  ELIF (Previous SUPERTREND == Previous FINAL LOWERBAND) and (Current Close &lt; Current FINAL LOWERBAND) <br>                  THEN Current FINAL UPPERBAND</pre><p>Even though the basic bands can be well computed using the standard tools, you’ll certainly get a headache when attempting to do this for the final bands. The consensus among most open-source solutions is to use a basic Python for-loop and write the array elements one at a time. But is this scalable? We’re here to find out!</p><h4>Pandas</h4><p><a href="https://github.com/pandas-dev/pandas">Pandas</a> is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool. Since it’s a go-to library for processing data in Python, let’s write our first implementation using Pandas alone. It will take one column and one combination of parameters, and return four arrays: one for the SuperTrend (trend), one for the direction (dir_), one for the uptrend (long), and one for the downtrend (short). We&#39;ll also split the implementation into 5 parts for readability and to be able to optimize any component at any time:</p><ol><li>Calculation of the median price — get_med_price</li><li>Calculation of the ATR — get_atr</li><li>Calculation of the basic bands — get_basic_bands</li><li>Calculation of the final bands — get_final_bands</li><li>Putting all puzzles together — supertrend</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d64defd63e32401cf0d27e69470d656f/href">https://medium.com/media/d64defd63e32401cf0d27e69470d656f/href</a></iframe><p>Let’s run the supertrend function on the BTCUSDT symbol:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97db16b9e7ba762d1de71ef78ff49835/href">https://medium.com/media/97db16b9e7ba762d1de71ef78ff49835/href</a></iframe><pre>Open time<br>2019-12-31 23:00:00+00:00             NaN<br>2020-01-01 00:00:00+00:00             NaN<br>2020-01-01 01:00:00+00:00             NaN<br>...                                   ...<br>2021-12-31 20:00:00+00:00    47608.346563<br>2021-12-31 21:00:00+00:00    47608.346563<br>2021-12-31 22:00:00+00:00    47608.346563<br>Length: 17514, dtype: float64</pre><p>If you print out the head of the supert Series using supert.head(10), you&#39;ll notice that the first 6 data points are all NaN. This is because the ATR&#39;s rolling period is 7, so the first 6 computed windows contained incomplete data.</p><p>A graph is worth 1,000 words. Let’s plot the first month of data (January 2020):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7cbbaf34b90b892fa5b6a1014928548/href">https://medium.com/media/f7cbbaf34b90b892fa5b6a1014928548/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0uHmGH6pNXvLuhXqJXoM-A.png" /><figcaption>SuperTrend over the first month of BTCUSDT</figcaption></figure><p>We’ve generated and visualized the SuperTrend values, but what about performance? Can we already make our overfitting machine with thousands of parameter combinations rolling? Not so fast. As you might have guessed, the supertrend function takes some time to compute:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/972afea72c9d36008a803cef50c05300/href">https://medium.com/media/972afea72c9d36008a803cef50c05300/href</a></iframe><pre>2.15 s ± 19.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)</pre><p>Ouch! Doing 1000 backtests would take us roughly 33 minutes.</p><p>Let’s hear what <a href="https://github.com/twopirllc/pandas-ta">Pandas TA</a> has to say about this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5e1abea6d1bdf5c8104a5508dfba4d3/href">https://medium.com/media/e5e1abea6d1bdf5c8104a5508dfba4d3/href</a></iframe><p>That’s a 3x speedup, mostly due to the fact that Pandas TA uses ATR from TA-Lib.</p><p>Is it now acceptable? Of course not. Can we get better than this? Hell yeah!</p><h4>NumPy + Numba = ❤️</h4><p>Pandas shines whenever it comes to manipulating heterogeneous tabular data, but is this really applicable to indicators? You might have noticed that even though we used Pandas, none of the operations in any of our newly defined functions makes use of index or column labels. Moreover, most indicators take, manipulate, and return arrays of the same dimensions and shape, which makes indicator development a purely algebraic challenge that can be well decomposed into multiple vectorized steps or solved on the per-element basis (or both!). Given that Pandas just extends NumPy and the latter is considered as a faster (although lower level) package, let’s adapt our logic to NumPy arrays instead.</p><p>Both functions get_med_price and get_basic_bands are based on basic arithmetic computations such as addition and multiplication, which are applicable to both Pandas and NumPy arrays and require no further changes. But what about get_atr and get_final_bands? The former can be re-implemented using NumPy and vectorbt&#39;s own arsenal of Numba-compiled functions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61be6232015b71875033c2133dc01ac5/href">https://medium.com/media/61be6232015b71875033c2133dc01ac5/href</a></iframe><p>The latter, on the other hand, is an iterative algorithm — it’s rather a poor fit for NumPy and an ideal fit for Numba, which can easily run for-loops at a machine code speed:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0103c66d7ae22b69be4921a83ca2bd69/href">https://medium.com/media/0103c66d7ae22b69be4921a83ca2bd69/href</a></iframe><p>If you look at the function above, you’ll notice that 1) it’s a regular Python code that can run even without being decorated with @njit, and 2) it&#39;s almost identical to the implementation with Pandas - the main difference is in each iloc[...] being replaced by [...]. We can write a simple Python function that operates on constants and NumPy arrays, and Numba will try to make it <strong>much</strong> faster, fully automatically. Isn&#39;t that impressive?</p><p>Let’s look at the result of this refactoring:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f7d59dff351e60e3700f581f93846e1/href">https://medium.com/media/8f7d59dff351e60e3700f581f93846e1/href</a></iframe><pre>array([nan, nan, nan, ..., 47608.3465635, 47608.3465635, 47608.3465635])</pre><p>As expected, those are arrays similar to the ones returned by the supertrend function, just without any labels. To attach labels, we can simply do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/710d0f6bdc550072bcc34f3fa0079509/href">https://medium.com/media/710d0f6bdc550072bcc34f3fa0079509/href</a></iframe><pre>Open time<br>2019-12-31 23:00:00+00:00             NaN<br>2020-01-01 00:00:00+00:00             NaN<br>2020-01-01 01:00:00+00:00             NaN<br>2020-01-01 02:00:00+00:00             NaN<br>2020-01-01 03:00:00+00:00             NaN<br>                                 ...     <br>2021-12-31 18:00:00+00:00    48219.074906<br>2021-12-31 19:00:00+00:00    47858.398491<br>2021-12-31 20:00:00+00:00    47608.346563<br>2021-12-31 21:00:00+00:00    47608.346563<br>2021-12-31 22:00:00+00:00    47608.346563<br>Length: 17514, dtype: float64</pre><p>Wondering how much our code has gained in performance? Wonder no more:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddd9efae64701b268ae18c33b6846c33/href">https://medium.com/media/ddd9efae64701b268ae18c33b6846c33/href</a></iframe><pre>1.11 ms ± 7.05 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)</pre><p>That’s a 780x speedup over an average Pandas TA run 😈</p><h4>NumPy + Numba + TA-Lib = ⚡️</h4><p>If you think that this result cannot be topped, then apparently you haven’t worked with TA-Lib. Even though there is no SuperTrend indicator available in TA-Lib, we can still use its highly-optimized indicator functions for intermediate calculations. In particular, instead of reinventing the wheel and implementing the median price and ATR functionality from scratch, we can use the MEDPRICE and ATR TA-Lib functions respectively. They have two major advantages over our custom implementation:</p><ol><li>Single pass through data</li><li>No compilation overhead from Numba</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82e86a501e0376d5cfe7cf50d1891864/href">https://medium.com/media/82e86a501e0376d5cfe7cf50d1891864/href</a></iframe><p>Another 4x improvement — by the time another trader processed a single column of data, we would have processed around 3 thousand columns. Agreed, the speed of our indicator is slowly getting ridiculously high 😄</p><h3>Indicator factory</h3><p>Let’s stop here and ask ourselves: why do we even need such a crazy performance?</p><p>That’s when parameter optimization comes into play. The two parameters that we have — period and multiplier - are the default values commonly used in technical analysis. But what makes those values universal and how do we know whether there aren&#39;t any better values for the markets we&#39;re participating in? Imagine having a pipeline that can backtest hundreds or even thousands of parameters and reveal configurations and market regimes that correlate better on average?</p><p><a href="https://vectorbt.pro/api/indicators/factory/#vectorbtpro.indicators.factory.IndicatorFactory">IndicatorFactory</a> is a vectorbt’s own powerhouse that can make any indicator function parametrizable. To get a better idea of what this means, let’s supercharge the faster_supertrend_talib function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e5d1203b24f77a3456938b51880fd5b/href">https://medium.com/media/0e5d1203b24f77a3456938b51880fd5b/href</a></iframe><p><em>Hint: </em><em>takes_1d is not supported by the community version — you need to make </em><em>faster_supertrend_talib accept two-dimensional arrays.</em></p><p>The indicator factory is a class that can generate so-called indicator classes. You can imagine it being a conveyor belt that can take a specification of your indicator function and produce a stand-alone Python class for running that function in a very flexible way. In our example, when we called vbt.IF(...), it has internally created an indicator class SuperTrend, and once we supplied faster_supertrend_talib to <a href="https://vectorbt.pro/api/indicators/factory/#vectorbtpro.indicators.factory.IndicatorFactory.with_apply_func">IndicatorFactory.with_apply_func</a>, it attached a method SuperTrend.run for running the indicator. Let&#39;s try it out!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fec416f92491e2c915afd49bec31944b/href">https://medium.com/media/fec416f92491e2c915afd49bec31944b/href</a></iframe><pre>symbol                          BTCUSDT      ETHUSDT<br>Open time                                           <br>2019-12-31 23:00:00+00:00           NaN          NaN<br>2020-01-01 00:00:00+00:00           NaN          NaN<br>2020-01-01 01:00:00+00:00           NaN          NaN<br>2020-01-01 02:00:00+00:00           NaN          NaN<br>2020-01-01 03:00:00+00:00           NaN          NaN<br>...                                 ...          ...<br>2021-12-31 18:00:00+00:00  48219.074906  3701.151241<br>2021-12-31 19:00:00+00:00  47858.398491  3792.049621<br>2021-12-31 20:00:00+00:00  47608.346563  3770.258246<br>2021-12-31 21:00:00+00:00  47608.346563  3770.258246<br>2021-12-31 22:00:00+00:00  47608.346563  3770.258246</pre><pre>[17514 rows x 2 columns]</pre><p>Notice how our SuperTrend indicator magically accepted two-dimensional Pandas arrays, even though the function itself can only work on one-dimensional NumPy arrays. Not only it computed the SuperTrend on each column, but it also converted the resulting arrays back into the Pandas format for pure convenience. So, how does all of this impact the performance?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a24e632a27878007eab0bd1d86b48eb/href">https://medium.com/media/8a24e632a27878007eab0bd1d86b48eb/href</a></iframe><pre>1.83 ms ± 140 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)</pre><p>Not that much! With all the pre- and postprocessing taking place, the indicator needs roughly one millisecond to process one column (that is, 17k data points).</p><h4>Expressions</h4><p>If you think that calling vbt.IF(...) and providing input_names, param_names, and other information manually is too much work, well, vectorbt has something for you. Our faster_supertrend_talib is effectively a black box to the indicator factory - that&#39;s why the factory cannot introspect it and derive the required information programmatically. But it easily could if we converted faster_supertrend_talib into an <a href="https://realpython.com/python-eval-function/">expression</a>!</p><p>Expressions are regular strings that can be evaluated into Python code. By giving such a string to <a href="https://vectorbt.pro/api/indicators/factory/#vectorbtpro.indicators.factory.IndicatorFactory.from_expr">IndicatorFactory.from_expr</a>, the factory will be able to see what’s inside, parse the specification, and generate a full-blown indicator class.</p><p>Here’s an expression for faster_supertrend_talib:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59d1eaa218eb387b5f774459605c57a6/href">https://medium.com/media/59d1eaa218eb387b5f774459605c57a6/href</a></iframe><p>Using annotations with @ we tell the factory how to treat specific variables. For instance, any variable with the prefix @talib gets replaced by the respective TA-Lib function that has been upgraded with broadcasting and multidimensionality. You can also see that parameters were annotated with @p, while inputs and outputs weren&#39;t annotated at all - the factory knows exactly that high is the high price, while the latest line apparently returns 4 output objects.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/112dd9a04350b26f96dfe8aea85b1b59/href">https://medium.com/media/112dd9a04350b26f96dfe8aea85b1b59/href</a></iframe><pre>symbol                          BTCUSDT      ETHUSDT<br>Open time                                           <br>2019-12-31 23:00:00+00:00           NaN          NaN<br>2020-01-01 00:00:00+00:00           NaN          NaN<br>2020-01-01 01:00:00+00:00           NaN          NaN<br>2020-01-01 02:00:00+00:00           NaN          NaN<br>2020-01-01 03:00:00+00:00           NaN          NaN<br>...                                 ...          ...<br>2021-12-31 18:00:00+00:00  48219.074906  3701.151241<br>2021-12-31 19:00:00+00:00  47858.398491  3792.049621<br>2021-12-31 20:00:00+00:00  47608.346563  3770.258246<br>2021-12-31 21:00:00+00:00  47608.346563  3770.258246<br>2021-12-31 22:00:00+00:00  47608.346563  3770.258246</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c11cdc1f5b379f1b6d597b2d985ca00/href">https://medium.com/media/6c11cdc1f5b379f1b6d597b2d985ca00/href</a></iframe><pre>2.36 ms ± 81.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)</pre><p>By the way, this is exactly how WorldQuant’s Alphas are implemented in vectorbt. Never stop loving Python for the magic it enables ✨</p><p><em>Note: expressions are not supported by the community version.</em></p><h4>Plotting</h4><p>Remember how we previously plotted SuperTrend? We had to manually select the date range from each output array and add it to the plot by passing the figure around. Let’s subclass SuperTrend and define a method plot that does all of this for us:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc8648c3f2eea39e6a6935db6e937c42/href">https://medium.com/media/bc8648c3f2eea39e6a6935db6e937c42/href</a></iframe><p>But how are we supposed to select the date range to plot? Pretty easy: the indicator factory made SuperTrend indexable just like any regular Pandas object! Let&#39;s plot the same date range and symbol but slightly change the color palette:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9988797e90dbc8be5fc20da4c39af8f/href">https://medium.com/media/f9988797e90dbc8be5fc20da4c39af8f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NEH5JMwgzrSFX_Z1-6UgTg.png" /><figcaption>SuperTrend over the first month of BTCUSDT using IndicatorFactory</figcaption></figure><p>Beautiful!</p><h3>Backtesting</h3><p>Backtesting is usually the simplest step in vectorbt: convert the indicator values into two signal arrays — entries and exits - and supply them to <a href="https://vectorbt.pro/api/portfolio/base/#vectorbtpro.portfolio.base.Portfolio.from_signals">Portfolio.from_signals</a>. To make the test better reflect the reality, let&#39;s do several adjustments. Since we&#39;re calculating the SuperTrend values based on the current close price and vectorbt executes orders right away, we&#39;ll shift the execution of the signals by one tick forward:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b79544e5247a06e8f1a8e4a0a64cf6e/href">https://medium.com/media/7b79544e5247a06e8f1a8e4a0a64cf6e/href</a></iframe><p>We’ll also apply the commission of 0.1%:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc48d480ac6ce340f4fe8e4de21b4138/href">https://medium.com/media/bc48d480ac6ce340f4fe8e4de21b4138/href</a></iframe><p>We’ve got a portfolio with two columns that can be analyzed with numerous built-in tools. For example, let’s calculate and display the statistics for the ETHUSDT symbol:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94be2f121bdc8a0842a2c3a08faff62b/href">https://medium.com/media/94be2f121bdc8a0842a2c3a08faff62b/href</a></iframe><pre>Start                         2019-12-31 23:00:00+00:00<br>End                           2021-12-31 22:00:00+00:00<br>Period                                729 days 18:00:00<br>Start Value                                       100.0<br>End Value                                   1136.318395<br>Total Return [%]                            1036.318395<br>Benchmark Return [%]                        2750.572933<br>Max Gross Exposure [%]                            100.0<br>Total Fees Paid                              273.006977<br>Max Drawdown [%]                               37.39953<br>Max Drawdown Duration                  85 days 09:00:00<br>Total Trades                                        174<br>Total Closed Trades                                 174<br>Total Open Trades                                     0<br>Open Trade PnL                                      0.0<br>Win Rate [%]                                  43.103448<br>Best Trade [%]                                33.286985<br>Worst Trade [%]                              -13.783496<br>Avg Winning Trade [%]                          7.815551<br>Avg Losing Trade [%]                           -3.02012<br>Avg Winning Trade Duration              3 days 06:43:12<br>Avg Losing Trade Duration     1 days 07:55:09.090909090<br>Profit Factor                                  1.390995<br>Expectancy                                     5.955853<br>Sharpe Ratio                                   2.259173<br>Calmar Ratio                                     6.3245<br>Omega Ratio                                    1.103559<br>Sortino Ratio                                  3.279668<br>Name: ETHUSDT, dtype: object</pre><h4>Optimization</h4><p>Optimization in vectorbt can be performed in two ways: iteratively and column-wise.</p><p>The first approach involves a simple loop that goes through every combination of the strategy’s parameters and runs the whole logic. This would require you to manually generate a proper parameter grid and concatenate the results for analysis. On the upside, you would be able to use <a href="http://hyperopt.github.io/hyperopt/">Hyperopt</a> and other tools that work on the per-iteration basis.</p><p>The second approach is natively supported by vectorbt and involves stacking columns. If you have 2 symbols and 5 parameters, vectorbt will generate 10 columns in total — one for each symbol and parameter, and backtest each column separately without leaving Numba (that’s why most functions in vectorbt are specialized in processing two-dimensional data, by the way). Not only this has a huge performance benefit for small to medium-sized data, but this also enables parallelization with Numba and presentation of the results in a Pandas-friendly format.</p><p>Let’s test the period values 4, 5, ..., 20, and the multiplier values 2, 2.1, 2.2, ..., 4, which would yield 336 parameter combinations in total. Since our indicator is now parametrized, we can pass those two parameter arrays directly to the SuperTrend.run method by also instructing it to do the <a href="https://en.wikipedia.org/wiki/Cartesian_product">Cartesian product</a> using the param_product=True flag:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4f9b8eeb01361ebacd2b8f393885593/href">https://medium.com/media/d4f9b8eeb01361ebacd2b8f393885593/href</a></iframe><p>The indicator did 672 iterations — 336 per symbol. Let’s see the columns that have been stacked:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42af70353dcbba8a07fe5f4a41df3840/href">https://medium.com/media/42af70353dcbba8a07fe5f4a41df3840/href</a></iframe><pre>MultiIndex([( 4, 2.0, &#39;BTCUSDT&#39;),<br>            ( 4, 2.0, &#39;ETHUSDT&#39;),<br>            ( 4, 2.1, &#39;BTCUSDT&#39;),<br>            ( 4, 2.1, &#39;ETHUSDT&#39;),<br>            ( 4, 2.2, &#39;BTCUSDT&#39;),<br>            ...<br>            (19, 3.8, &#39;ETHUSDT&#39;),<br>            (19, 3.9, &#39;BTCUSDT&#39;),<br>            (19, 3.9, &#39;ETHUSDT&#39;),<br>            (19, 4.0, &#39;BTCUSDT&#39;),<br>            (19, 4.0, &#39;ETHUSDT&#39;)],<br>           names=[&#39;st_period&#39;, &#39;st_multiplier&#39;, &#39;symbol&#39;], length=672)</pre><p>Each of the DataFrames has now 672 columns. Let’s plot the latest combination by specifying the column as a regular tuple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/068db5852f2edf0d923cb108f582f2ce/href">https://medium.com/media/068db5852f2edf0d923cb108f582f2ce/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MHdsHg7buXu_e4Nf5W2iAw.png" /><figcaption>SuperTrend on period of 19, multiplier of 4, and symbol ETHUSDT</figcaption></figure><p>When stacking a huge number of columns, make sure that you are not running out of RAM. You can print the size of any pickleable object in vectorbt using the <a href="https://vectorbt.pro/api/utils/pickling/#vectorbtpro.utils.pickling.Pickleable.getsize">Pickleable.getsize</a> method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06ba79611284423c61ff095751ba0296/href">https://medium.com/media/06ba79611284423c61ff095751ba0296/href</a></iframe><pre>377.6 MB</pre><p>The backtesting part remains the same, irrespective of the number of columns:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/32a41452922615cef774adc27df7c9f2/href">https://medium.com/media/32a41452922615cef774adc27df7c9f2/href</a></iframe><p>Instead of computing all the statistics for each single combination, let’s plot a heatmap of their Sharpe values with the periods laid out horizontally and the multipliers laid out vertically. Since we have an additional column level that contains symbols, we’ll make it a slider:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c571755dbd7af323baf47a6735b55f1/href">https://medium.com/media/6c571755dbd7af323baf47a6735b55f1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UaWApqniZ4p1v7OTee5KwA.png" /><figcaption>Heatmap with Sharpe values for all parameter combinations by symbol</figcaption></figure><p>We now have a nice overview of any parameter regions that performed well during the backtesting period, yay!</p><h3>Conclusion</h3><p>This example proved that technical analysis with Python doesn’t have to be slow: there is a range of accelerator packages such as Cython, PyPy, and Numba available to make the performance of our code to be on par with other compiled languages, while we can still enjoy the perks of the rich package ecosystem and great flexibility of this programming language. The vectorbt package takes a solid place in that ecosystem by allowing us to take advantage of the introduced acceleration, for example, to do parameter optimization on arbitrary trading strategies and analyze the dynamics of entire markets in the blink of an eye.</p><p>View the original tutorial <a href="https://vectorbt.pro/tutorials/superfast-supertrend/">here</a>.</p><h3>Support</h3><p>Please support me on <a href="https://github.com/sponsors/polakowo">Github Sponsors</a> and contribute to the new growing ecosystem for offline trading analytics. In return, you will get an early access to <a href="https://vectorbt.pro/">vectorbt PRO</a> — the actively developed, commercial successor of vectorbt aimed at providing professional analytics tools for quants — and new exclusive learning content. In particular, you will learn how to</p><ol><li>design and unit test a streaming SuperTrend indicator that passes across the data only once and gets updated with every new data point</li><li>make the SuperTrend indicator utilize all cores by using multithreading</li><li>properly design a backtesting pipeline and perform 67,200 backtests on SuperTrend in just one second!</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JgOU1HuajAV0ndUcdx9nYw.gif" /></figure><p>As always, happy coding ❤️</p><p>Schedule a DDIChat Session in <a href="https://app.ddichat.com/category/coding-software-mobile-development"><strong>Coding, Software, and Mobile Development</strong></a>:</p><p><a href="https://app.ddichat.com/category/coding-software-mobile-development">Experts - Coding, Software, and Mobile Development - DDIChat</a></p><p>Apply to be a DDIChat Expert <a href="https://app.ddichat.com/expertsignup">here</a>.<br>Ways to work with DDI: <a href="https://datadriveninvestor.com/collaborate">https://datadriveninvestor.com/collaborate</a><br>Visit us at <a href="https://www.datadriveninvestor.com/">https://www.datadriveninvestor.com/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6269a3af0c2a" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/superfast-supertrend-6269a3af0c2a">SuperFast SuperTrend</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stop Loss, Trailing Stop, or Take Profit? 2 Million Backtests Shed Light]]></title>
            <link>https://medium.datadriveninvestor.com/stop-loss-trailing-stop-or-take-profit-2-million-backtests-shed-light-dde23bda40be?source=rss-a810fd509ab3------2</link>
            <guid isPermaLink="false">https://medium.com/p/dde23bda40be</guid>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[backtesting]]></category>
            <category><![CDATA[trading]]></category>
            <category><![CDATA[stop-loss]]></category>
            <dc:creator><![CDATA[Oleg Polakow]]></dc:creator>
            <pubDate>Sat, 19 Dec 2020 15:00:54 GMT</pubDate>
            <atom:updated>2022-02-21T06:32:38.558Z</atom:updated>
            <content:encoded><![CDATA[<h4>LARGE-SCALE BACKTESTING IN 5 MINUTES</h4><p>In this article, we will utilize large-scale backtesting with <a href="https://github.com/polakowo/vectorbt">vectorbt</a> to explore the performance of the most common stop signals for different cryptocurrencies, time periods, and stop values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tq9vJO_G1UcEwqN4NAU89w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@flyd?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">FLY:D</a> on <a href="https://unsplash.com/@flyd?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><blockquote>A trading strategy is just a grain of sand when compared to the whole universe of possible strategies; only the big picture can reveal its quality.</blockquote><h3>Methodology</h3><p>Our goal is to utilize large-scale backtesting to compare the performance of trading with and without stop loss (SL), trailing stop (TS), and take profit (TP) signals. To make this attempt representative, we will run a huge number of experiments across three different dimensions: instruments, time, and hyperparameters:</p><ul><li>First, we will pick 10 cryptocurrencies by market capitalization (except stablecoins such as USDT) and fetch 3 years of their daily pricing data. In particular, we aim at backtesting the time period from 2018 to 2021 as it contains periods of sharp price drops (e.g., corrections due to ATH in December 2017 and coronavirus in March 2020) as well as surges (ATH in December 2020) — this keeps things balanced.</li><li>For each instrument, we will split this time period into 400 smaller (and overlapping) time windows, each 6 months long. We will run our tests on each of these windows to account for different market regimes.</li><li>For each instrument and time window, we will then generate an entry signal at the very first bar and find an exit signal according to the stop configuration. We will test 100 stop values with a 1% increment and compare the performance of each one to that of trading randomly and holding within this particular time window.</li></ul><p>In total, we will conduct 2,000,000 backtests.</p><h3>Setup</h3><p>All we need is <a href="https://jupyter.org/">Jupyter Notebook/Lab</a> with Python ≥ 3.6, <a href="https://github.com/ranaroussi/yfinance">yfinance</a>, <a href="https://github.com/polakowo/vectorbt">vectorbt</a>, and packages required by them. We will use yfinance to download pricing data, and vectorbt to both run 2 million backtests in under 5 minutes and analyze the results visually.</p><blockquote>vectorbt is a next-generation backtesting library for Python that applies various backtesting and data science techniques to technical analysis. The way it works is by representing trading data — from time series to order records — as nd-arrays, and processing them using NumPy and <a href="https://github.com/numba/numba">Numba</a>. This in turn enables use cases such as blazingly fast hyperparameter optimization, which is otherwise mainly done using distributed and cloud computing. Another advantage is integration of <a href="https://github.com/plotly/plotly.py">Plotly</a> and <a href="https://github.com/jupyter-widgets/ipywidgets">ipywidgets</a> to display interactive charts and dashboards right in the Jupyter notebook.</blockquote><h3>Define parameters</h3><p>The first step is to define the parameters of the analysis pipeline. As discussed above, we will backtest 3 years of pricing data, 400 time windows, 10 cryptocurrencies, and 100 stop values. We will also set fees and slippage both to 0.25% and initial capital to $100 (the amount per se doesn’t matter, but it must be the same for all assets to be comparable). Feel free to change any parameter of interest.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6ffad9f9ca248bf97f3fe8c4752ff9f/href">https://medium.com/media/c6ffad9f9ca248bf97f3fe8c4752ff9f/href</a></iframe><pre>Start date                       2018-01-01 00:00:00<br>End date                         2021-01-01 00:00:00<br>Time period (days)                              1096<br>Assets                                            10<br>Window length                      180 days, 0:00:00<br>Windows                                          400<br>Exit types                                         5<br>Stop values                                      100<br>Tests per asset                               200000<br>Tests per window                                5000<br>Tests per exit type                           400000<br>Tests per stop type and value                   4000<br>Tests total                                  2000000<br>dtype: object</pre><p>Our configuration yields sample sizes with enough statistical power to analyze four variables: assets (200k tests per asset), time (5k tests per time window), exit types (400k tests per exit type), and stop values (4k tests per stop type and value). Similar to how Tableau handles dimensions and measures, we will be able to group our performance by each of these variables, but we will mainly focus on 5 exit types: SL exits, TS exits, TP exits, random exits, and holding exits (placed at the last bar).</p><h3>Download data</h3><p>Getting daily pricing data of each cryptocurrency is straightforward using yfinance:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42b31efa4606661c294925224ba8332e/href">https://medium.com/media/42b31efa4606661c294925224ba8332e/href</a></iframe><pre>dict_keys([&#39;BTC-USD&#39;, &#39;ETH-USD&#39;, &#39;XRP-USD&#39;, &#39;BCH-USD&#39;, &#39;LTC-USD&#39;, &#39;BNB-USD&#39;, &#39;EOS-USD&#39;, &#39;XLM-USD&#39;, &#39;XMR-USD&#39;, &#39;ADA-USD&#39;])<br>(1083, 5)</pre><p>The dictionary ohlcv_by_symbol now contains OHLCV data by cryptocurrency name. Each DataFrame has 1083 rows (days) and 5 columns (O, H, L, C, and V). You can plot a DataFrame as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a28e04a349b105148092dec12d73619e/href">https://medium.com/media/a28e04a349b105148092dec12d73619e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*PkGmalNUp2zl9Se2HE6fLw.png" /><figcaption>OHLCV of BTC-USD pair</figcaption></figure><p>Since assets are one of the dimensions we want to analyze, vectorbt expects us to pack them as columns into a single DataFrame and label them accordingly. To do so, we simply swap assets and features to get a dictionary of DataFrames (with assets now as columns) keyed by feature name, such as “Open”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/589311c3b0ee036f8a743d5a791be233/href">https://medium.com/media/589311c3b0ee036f8a743d5a791be233/href</a></iframe><pre>dict_keys([&#39;Open&#39;, &#39;Low&#39;, &#39;High&#39;, &#39;Close&#39;, &#39;Volume&#39;])<br>(1083, 10)</pre><h3>Generate time windows</h3><p>Next, we will move a 6-month sliding window over the whole time period and take 400 “snapshots” of each price DataFrame within this window. Each snapshot will correspond to a subset of data that should be independently backtested. As with assets and other variables, snapshots also need to be stacked horizontally as columns. As a result, we will get 180 rows (window length in days) and 4000 columns (10 assets x 400 windows); that is, one column will correspond to the price of one asset within one particular time window.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bacce50093cc07c096ecddca63d4d5a/href">https://medium.com/media/2bacce50093cc07c096ecddca63d4d5a/href</a></iframe><pre>(180, 4000)</pre><p>A nice feature of vectorbt is that it makes use of <a href="https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html">hierarchical indexing</a> to store valuable information on each backtest. It also ensures that this column hierarchy is preserved across the whole backtesting pipeline — from signal generation to performance modeling — and can be extended easily. Currently, our columns have the following hierarchy:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b11bc4a287b538d318a5e5bddd3ae9a/href">https://medium.com/media/2b11bc4a287b538d318a5e5bddd3ae9a/href</a></iframe><pre>MultiIndex([<br>    (&#39;BTC-USD&#39;, &#39;2017-12-31&#39;, &#39;2018-06-28&#39;),<br>    (&#39;BTC-USD&#39;, &#39;2018-01-02&#39;, &#39;2018-06-30&#39;),<br>    (&#39;BTC-USD&#39;, &#39;2018-01-05&#39;, &#39;2018-07-03&#39;),<br>    ...<br>    (&#39;ADA-USD&#39;, &#39;2020-06-16&#39;, &#39;2020-12-12&#39;),<br>    (&#39;ADA-USD&#39;, &#39;2020-06-19&#39;, &#39;2020-12-15&#39;),<br>    (&#39;ADA-USD&#39;, &#39;2020-06-21&#39;, &#39;2020-12-17&#39;)<br>], names=[<br>    &#39;symbol&#39;, <br>    &#39;range_start&#39;, <br>    &#39;range_end&#39;<br>], length=4000)</pre><p>This multi-index captures three parameters: the symbol, the start date of the time window, and its end date. Later, we will extend this multi-index with exit types and stop values such that each of the 2 million backtests has its own price series.</p><h3>Generate entry signals</h3><p>In contrast to most other backtesting libraries, signals are not stored as a signed integer array, but they are split into two boolean arrays: entries and exits, which makes manipulation a lot easier.</p><p>At the beginning of each time window, let’s generate an entry signal indicating a buy order. The data frame will have the same shape, index, and columns as that of price so that vectorbt can link their elements together.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a71c9a1163f77d2b2cb1328fd0e85112/href">https://medium.com/media/a71c9a1163f77d2b2cb1328fd0e85112/href</a></iframe><pre>(180, 4000)</pre><h3>Generate exit signals</h3><p>For each of the entry signals we generated, we will find an exit signal according to our 5 exit types: SL, TS, TP, random, and holding. We will also concatenate their DataFrames into a single (huge) DataFrame with 180 rows and 2,000,000 columns, each representing a separate backtest. Since exit signals are boolean, their memory footprint is tolerable.</p><p>Let’s generate exit signals according to stop conditions first. We want to test 100 different stop values with a 1% increment, starting from 1% and ending with 100% (i.e., find a timestamp where the price exceeds the entry price by 100%). Usually, when OHLC data is checked against such conditions, the position is closed at (or shortly after) the time of hitting the particular stop, but we will simplify things and use the “Close” price to exit any position.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/069c3ebbb2538fae548d99671780a985/href">https://medium.com/media/069c3ebbb2538fae548d99671780a985/href</a></iframe><pre>(180, 400000) (180, 400000) (180, 400000)</pre><p>This also extended our column hierarchy with a new column level indicating the stop value, we only have to make it consistent across all DataFrames:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/001894b12227872aeeeeaa98b426f347/href">https://medium.com/media/001894b12227872aeeeeaa98b426f347/href</a></iframe><pre>MultiIndex([<br>    (0.01, &#39;BTC-USD&#39;, &#39;2017-12-31&#39;, &#39;2018-06-28&#39;),<br>    (0.01, &#39;BTC-USD&#39;, &#39;2018-01-02&#39;, &#39;2018-06-30&#39;),<br>    (0.01, &#39;BTC-USD&#39;, &#39;2018-01-05&#39;, &#39;2018-07-03&#39;),<br>    ...<br>    ( 1.0, &#39;ADA-USD&#39;, &#39;2020-06-16&#39;, &#39;2020-12-12&#39;),<br>    ( 1.0, &#39;ADA-USD&#39;, &#39;2020-06-19&#39;, &#39;2020-12-15&#39;),<br>    ( 1.0, &#39;ADA-USD&#39;, &#39;2020-06-21&#39;, &#39;2020-12-17&#39;)<br>], names=[<br>    &#39;stop_value&#39;, <br>    &#39;symbol&#39;, <br>    &#39;range_start&#39;, <br>    &#39;range_end&#39;<br>], length=400000)</pre><p>One major feature of vectorbt is that it places a strong focus on data science, and so it allows us to apply popular analysis tools to almost any part of the backtesting pipeline. For example, let’s explore how the number of exit signals depends upon the stop type and value:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f21c4b84d1893c3149ea97047bdc8033/href">https://medium.com/media/f21c4b84d1893c3149ea97047bdc8033/href</a></iframe><pre>SL    0.434195<br>TS    0.590803<br>TP    0.514545<br>Name: avg_num_signals, dtype: float64</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*KREKKqYKbQMNlTwG7-1Crw.png" /><figcaption>Average number of signals by exit type and stop value</figcaption></figure><p>We see that TS is by far the most occurring exit signal. The SL and TP curves come hand in hand up to the stop value of 50% and then diverge in favor of TP. While it might seem that bulls are mostly in charge, especially for bigger price movements, remember that it is much easier to post a 50% profit than a 50% loss because the latter requires a 100% profit to recover; thus, negative downward spikes seem to dominate small to medium price movements (and shake out weak hands potentially). These are well-known cryptocurrency dynamics.</p><p>To simplify the analysis that follows, we should ensure that each column has at least one exit signal to close the position, which means that if a column has no exit signal now, it should get one at the last timestamp. This is done by combining the stop exits with the last-bar exit using the OR rule and selecting the one that comes first:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce0959edf169fdb8d150f5a9e92cd8de/href">https://medium.com/media/ce0959edf169fdb8d150f5a9e92cd8de/href</a></iframe><pre>SL    1.0<br>TS    1.0<br>TP    1.0<br>Name: avg_num_signals, dtype: float64</pre><p>Next, we will generate signals of the two remaining exit types: random and holding — they will act as benchmarks to compare SL, TS, and TP against.</p><p>“Holding” exit signals are signals placed at the very last bar of each time series. On most occasions, we shouldn’t bother ourselves with placing them, since we can simply assess open positions. The reason we do it anyway is consistency — we want to ensure that each column has (exactly) one signal. The other consideration is shape and columns: they should match that of stop signals so we can concatenate all DataFrames later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0a51f0cc6d5f299dbf85f443fafc2cc/href">https://medium.com/media/a0a51f0cc6d5f299dbf85f443fafc2cc/href</a></iframe><pre>(180, 400000)</pre><p>To generate random exit signals, just shuffle any signal array. The only requirement is that each column should contain exactly one signal.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2117c276b5361f22cb8f0c2e63011c4e/href">https://medium.com/media/2117c276b5361f22cb8f0c2e63011c4e/href</a></iframe><pre>(180, 400000)</pre><p>The last step is the concatenation of all DataFrames along the column axis:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a29c58cb076b5930a60a47437c44ee9d/href">https://medium.com/media/a29c58cb076b5930a60a47437c44ee9d/href</a></iframe><pre>(180, 2000000)</pre><p>The exits array now contains 2,000,000 columns — one per backtest. The column hierarchy is also complete — one tuple of hyperparameters per backtest.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d08d0fde29fb774af5d4a9e6fbb1514/href">https://medium.com/media/7d08d0fde29fb774af5d4a9e6fbb1514/href</a></iframe><pre>MultiIndex([<br>    (     &#39;SL&#39;, 0.01, &#39;BTC-USD&#39;, &#39;2017-12-31&#39;, &#39;2018-06-28&#39;),<br>    (     &#39;SL&#39;, 0.01, &#39;BTC-USD&#39;, &#39;2018-01-02&#39;, &#39;2018-06-30&#39;),<br>    (     &#39;SL&#39;, 0.01, &#39;BTC-USD&#39;, &#39;2018-01-05&#39;, &#39;2018-07-03&#39;),<br>    ...<br>    (&#39;Holding&#39;,  1.0, &#39;ADA-USD&#39;, &#39;2020-06-16&#39;, &#39;2020-12-12&#39;),<br>    (&#39;Holding&#39;,  1.0, &#39;ADA-USD&#39;, &#39;2020-06-19&#39;, &#39;2020-12-15&#39;),<br>    (&#39;Holding&#39;,  1.0, &#39;ADA-USD&#39;, &#39;2020-06-21&#39;, &#39;2020-12-17&#39;)<br>], names=[<br>    &#39;exit_type&#39;, <br>    &#39;stop_value&#39;, <br>    &#39;symbol&#39;, <br>    &#39;range_start&#39;, <br>    &#39;range_end&#39;<br>], length=2000000)</pre><p>This allows us to group signals by one or multiple levels and conveniently analyze them in one go. For example, let’s compare different exit types and stop values by an average distance of exit signal to entry signal (in days):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7df819029306af0f1d2039db6c5ea9ee/href">https://medium.com/media/7df819029306af0f1d2039db6c5ea9ee/href</a></iframe><pre>exit_type<br>Holding    179.000000<br>Random      89.929929<br>SL         123.815830<br>TP         114.185100<br>TS         103.323425<br>dtype: float64</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*oJQUjsZXRtwDrR5zdGUsHw.png" /><figcaption>Average distance of exit signal to entry signal by exit type and stop value</figcaption></figure><p>This scatterplot gives us a more detailed view of the distribution of exit signals. As expected, exit signals of plain holding have an exact distance of 179 days after entry (maximum possible), while random exit signals are evenly distributed over the time window and are not dependent upon any stop value. But we are more interested in stop curves, which are flat and thus hint at high volatility of price movements within our timeframe — the lower the curve, the higher is the chance of hitting a stop. To give an example, a TS of 20% is hit after just 30 days on average, while it would take 72 days for SL and 81 days for TP. But does an early exit any good?</p><h3>Run simulation</h3><p>Here comes the actual backtesting part:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/abce91c1c619acdf588ac22d9899be95/href">https://medium.com/media/abce91c1c619acdf588ac22d9899be95/href</a></iframe><pre>3995570</pre><p>Fairly easy, right?</p><p>The simulation took 4 minutes on my MacBook Air to finish and generated in total 3,995,570 orders that are ready to be analyzed (should be 4 million, but some price data points seem to be missing). Notice, however, that any array produced by the portfolio object of the same shape as our exit signals, such as portfolio value or returns, requires 8 * 180 * 2000000 bytes or almost 3GB of RAM, and it is automatically cached to be re-used by other portfolio components. Thus, we will disable caching to release memory as soon as the calculation of portfolio performance is over:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c92f829d708287e787af374516aac10/href">https://medium.com/media/7c92f829d708287e787af374516aac10/href</a></iframe><p>We can analyze anything from trades to Sharpe ratio, but given the amount of data, we will stick to a fast-to-calculate metric — total return.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba0becf57bd761d766081f6d5edf55ed/href">https://medium.com/media/ba0becf57bd761d766081f6d5edf55ed/href</a></iframe><pre>(2000000,)</pre><p>If your computer takes a substantial amount of time to simulate, you have several options:</p><ul><li>Use <a href="https://colab.research.google.com/">Google Colab</a></li><li>Reduce the number of stop values (e.g., from 1% to 2%)</li><li>Cast to np.float32 or even below (if supported)</li><li>Split the exit signal array into chunks and simulate per chunk. Just make sure each chunk has a shape compatible with that of the price and entries (remember to delete the previous portfolio if simulated):</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd55f07df46756990833edaf75590d13/href">https://medium.com/media/cd55f07df46756990833edaf75590d13/href</a></iframe><pre>100%|██████████| 5/5 [01:19&lt;00:00, 16.98s/it]<br>(2000000,)</pre><p>That’s much better.</p><h3>Analyze performance</h3><p>The first step is always taking a look at the distribution of the baseline:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a54dbeec8f3d2a8f3e7dfb004bb511ae/href">https://medium.com/media/a54dbeec8f3d2a8f3e7dfb004bb511ae/href</a></iframe><pre>count    400000.000000<br>mean          0.069392<br>std           0.814803<br>min          -0.915331<br>50%          -0.148971<br>max           6.526005<br>Name: Holding, dtype: float64</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*nUrVXYoRWrvQ-mta18hT0g.png" /><figcaption>Distribution of holding returns</figcaption></figure><p>The distribution of holding performance across time windows is highly left-skewed. On the one hand, this indicates prolonged sideways and bearish regimes within our timeframe. On the other hand, the price of any asset can climb to infinity but is limited by 0 — making the distribution denser on the left and more sparse on the right by nature. Every second return is a loss of more than 7%, but thanks to bull runs the strategy still manages to post an average profit of 7%.</p><p>Let’s include other strategies into the analysis:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aae6103b5ca1ac6ab0ef1bf38ba4c141/href">https://medium.com/media/aae6103b5ca1ac6ab0ef1bf38ba4c141/href</a></iframe><pre>               Mean    Median       Std<br>exit_type                              <br>SL         0.035266 -0.168662  0.750711<br>TS         0.038908 -0.112648  0.685156<br>TP         0.038539  0.070077  0.477094<br>Random     0.016484 -0.083610  0.570806<br>Holding    0.069122 -0.148971  0.814248</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*YZQKOccF1KRfSQlrH8rncQ.png" /><figcaption>Distribution of returns by exit type</figcaption></figure><p>None of the strategies beat the average return of the baseline. The TP strategy is the most consistent one though — although it introduces an upper bound that limits huge profits (see missing outliers), its trade returns are less volatile and mostly positive. The reason why SL and TS are unbounded at the top is that some of the stops haven’t been hit, and so their columns fall back to plain holding. The random strategy is also interesting: while it’s inferior in terms of average return, it finishes second after TP in terms of median return and returns volatility.</p><p>To confirm the picture above, let’s calculate the win rate of each strategy:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c26381788e559a5a7b0d5be00f92ce7f/href">https://medium.com/media/c26381788e559a5a7b0d5be00f92ce7f/href</a></iframe><pre>exit_type<br>SL         0.309275<br>TS         0.354683<br>TP         0.569420<br>Random     0.395713<br>Holding    0.394750<br>Name: win_rate, dtype: float64</pre><p>Almost 57% of trades with TP are profitable — a high contrast to other strategies. But having a high win ratio doesn’t necessarily guarantee longer-term trading success if your winning trades are often much smaller than your losing trades. Thus, let’s aggregate by stop type and value and compute the <a href="https://www.icmarkets.com/blog/reward-to-risk-win-ratio-and-expectancy/">expectancy</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8aa25279f605c169f37b47744e7cd674/href">https://medium.com/media/8aa25279f605c169f37b47744e7cd674/href</a></iframe><pre>exit_type<br>SL         3.657913<br>TS         4.073352<br>TP         4.156836<br>Random     1.715895<br>Holding    7.100061<br>dtype: float64</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*IkWYRUY1uB5O3qPk4jm0ig.png" /><figcaption>Expectancy by exit type and stop value</figcaption></figure><p>Each strategy is able to add gradually to our account in the long run, with the holding strategy being the clear winner here — we can expect to add to our account an average of $7 out of $100 invested after every 6 months of holding. The only configuration that beats the baseline is TS with stop values ranging from 20% to 40%. The worst-performing configuration is SL and TS with stop values around 40% and 55% respectively; both seem to get triggered once most corrections find the bottom, which is even worse than exiting randomly. The TP strategy, on the other hand, beats the random exit strategy after the stop value of 30%. Generally, waiting seems to pay off for cryptocurrencies.</p><p>Finally, let’s take a look at how our strategies perform under different market conditions. We will consider a simplified form of regime classification that divides holding returns into 20 bins and calculates the expectancy of each strategy within the boundaries of each bin (we leave out the latest bin for the sake of chart readability). Note that due to the highly skewed distribution of holding returns, we need to take into account the density of observations and make bins equally-sized.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d90975717098c4233aec77e8ea4a92c9/href">https://medium.com/media/d90975717098c4233aec77e8ea4a92c9/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*oaZrubhuR7e9lN5gE_RTaw.png" /><figcaption>Expectancy against binned holding returns by exit type</figcaption></figure><p>The chart above confirms the general intuition behind the behavior of stop orders: SL and TS limit the trader’s loss during downtrends, TP is beneficial for short-term traders interested in profiting from a quick bump in price, and holding performs best in top-growth markets. Surprisingly, while random exits perform poorly in sideways and bull markets, they match and often outperform stop exits in bear markets.</p><h3>Bonus: Jupyter dashboard</h3><p>Dashboards can be a really powerful way of interacting with the data.</p><p>First, let’s define the components of our dashboard. We have two types of components: controls, such as asset dropdown, and graphs. Controls define parameters and trigger updates for graphs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2523c011f65bc93e0c124149050bfc2b/href">https://medium.com/media/2523c011f65bc93e0c124149050bfc2b/href</a></iframe><p>The second step is the definition of the update function, which is triggered once any control has been changed. We also manually call this function to initialize the graphs with default parameters.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f790239d80cb5110d25af3bb48eaceb/href">https://medium.com/media/9f790239d80cb5110d25af3bb48eaceb/href</a></iframe><p>In the last step, we will define the layout of the dashboard and finally run it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63250354ea0b69f216988ff6b27d1331/href">https://medium.com/media/63250354ea0b69f216988ff6b27d1331/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*NnUPXcQq5ukidd8ojNTfqw.gif" /><figcaption>Illustration of the dashboard with every asset selected</figcaption></figure><h3>Conclusion</h3><p>The use of large-scale backtesting is not limited to hyperparameter optimization, but when properly utilized, it gives us a vehicle to explore complex phenomena related to trading. Especially utilization of multi-dimensional arrays, dynamic compilation, and integration with pandas, as done by vectorbt, allows us to quickly get new insights by applying popular data science tools to each component of a backtesting pipeline.</p><p>In this particular example, we conducted 2 million backtests to observe how different stop values impact the performance of stop signals and how different stop signals compare to holding and trading randomly. On the one hand, the findings confirm what we already know about the behavior of stop signals under various market conditions. On the other hand, they reveal optimal configurations that might have worked well for the last couple of years of trading cryptocurrencies.</p><p>But there is always more to it than a couple of charts we drew above. If you’re curious to apply the analysis on your own data or to try different hyperparameters, feel free to run the <a href="https://nbviewer.jupyter.org/github/polakowo/vectorbt/blob/master/examples/StopSignals.ipynb">notebook</a>.</p><h3>More articles</h3><p>Check out <a href="https://medium.datadriveninvestor.com/superfast-supertrend-6269a3af0c2a">SuperFast SuperTrend</a> where you will learn how to design and implement the fastest SuperTrend indicator in Python using TA-Lib, Numba, and vectorbt (PRO).</p><h3>Support</h3><p>Please support me on <a href="https://github.com/sponsors/polakowo">Github Sponsors</a> and contribute to the new growing ecosystem for offline trading analytics. In return, you will get an early access to <a href="https://vectorbt.pro/">vectorbt PRO</a> — the actively developed, commercial successor of vectorbt aimed at providing professional analytics tools for quants — and new exclusive learning content ❤️</p><p>Schedule a DDIChat Session in <a href="https://app.ddichat.com/category/data-science-ai-ml-dl"><strong>Data Science / AI / ML / DL</strong></a>:</p><p><a href="https://app.ddichat.com/category/data-science-ai-ml-dl">Experts - Data Science / AI / ML / DL - DDIChat</a></p><p>Apply to be a DDIChat Expert <a href="https://app.ddichat.com/expertsignup">here</a>.<br>Work with DDI: <a href="https://datadriveninvestor.com/collaborate">https://datadriveninvestor.com/collaborate</a><br>Subscribe to DDIntel <a href="https://ddintel.datadriveninvestor.com/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dde23bda40be" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/stop-loss-trailing-stop-or-take-profit-2-million-backtests-shed-light-dde23bda40be">Stop Loss, Trailing Stop, or Take Profit? 2 Million Backtests Shed Light</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>