Spaces:
Running
Running
| <html class="default" lang="en" data-base=".."><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>article/04_option_hedging | backtest-kit</title><meta name="description" content="Documentation for backtest-kit"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script><script async src="../assets/hierarchy.js" id="tsd-hierarchy-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><div class="table-cell" id="tsd-search"><div class="field"><label for="tsd-search-field" class="tsd-widget tsd-toolbar-icon search no-caption"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></label><input type="text" id="tsd-search-field" aria-label="Search"/></div><div class="field"><div id="tsd-toolbar-links"></div></div><ul class="results"><li class="state loading">Preparing search index...</li><li class="state failure">The search index is not available</li></ul><a href="../index.html" class="title">backtest-kit</a></div><div class="table-cell" id="tsd-widgets"><a href="#" class="tsd-widget tsd-toolbar-icon menu no-caption" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb"><li><a href="../modules.html">backtest-kit</a></li><li><a href="article_04_option_hedging.html">article/04_option_hedging</a></li></ul></div><div class="tsd-panel tsd-typography"><a id="🩸-why-the-price-drops-in-a-single-candle" class="tsd-anchor"></a><h1 class="tsd-anchor-link">🩸 Why the Price Drops in a Single Candle<a href="#🩸-why-the-price-drops-in-a-single-candle" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h1><p><img src="../media/candle.png" alt="Price drops"></p> | |
| <p>In 2026, "buy and hold" is a bet that you can endure a -40…-70% drawdown without an emotional or margin-triggered exit. The market has become fragile: liquidation cascades happen several times a year. Static holding no longer works.</p> | |
| <a id="what-are-options" class="tsd-anchor"></a><h2 class="tsd-anchor-link">What Are Options<a href="#what-are-options" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h2><p><img src="../media/gamma_convexity.svg" alt="Gamma - where profit comes from in a calm market"></p> | |
| <p>If you bought a stock for $100 and it went up 10%, you have +$10 — simple and linear. Options work differently. An option is an obligation to sell a stock later at a fixed price. Their price changes <strong>unevenly</strong>: sometimes slowly, sometimes very sharply. This "curvature" is the key feature. The further away from expiration, the more curvature is "accumulated." Closer to expiration it vanishes — time works against the holder.</p> | |
| <a id="smooth-movement-vs-sudden-jumps" class="tsd-anchor"></a><h2 class="tsd-anchor-link">Smooth Movement vs. Sudden Jumps<a href="#smooth-movement-vs-sudden-jumps" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h2><p><img src="../media/jump_diffusion.svg" alt="Smooth movement and jumps - why the hedge can't keep up"></p> | |
| <p>Most textbook models assume that a stock's price changes in very small steps — continuously, with no teleportation. Reality is different: the price suddenly <strong>drops</strong>. During such a jump, adjusting the position is impossible — there were no trades at intermediate prices. Therefore, the outcome during a gap depends more on luck than on the quality of your strategy.</p> | |
| <a id="how-positions-are-adjusted" class="tsd-anchor"></a><h2 class="tsd-anchor-link">How Positions Are Adjusted<a href="#how-positions-are-adjusted" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h2><p><img src="../media/interest_hedge.svg" alt="The corridor where hedging works properly"></p> | |
| <p>There are two major client groups:</p> | |
| <p><strong>First group</strong> — pension funds and banks. | |
| They buy put options to report nicely to management/depositors: "yes, the market fell, but we have protection, losses are unrealized."</p> | |
| <p><strong>Second group</strong> — company investors. | |
| They buy products like "protected growth": if the market rises significantly — you get a share of the profit; if it falls — you lose nothing (at least on paper).</p> | |
| <p><strong>This creates a fork</strong> — if the price rises, we buy at a better price. If the price falls, we have a SHORT open from another investor.</p> | |
| <a id="conflict-of-interest" class="tsd-anchor"></a><h2 class="tsd-anchor-link">Conflict of Interest<a href="#conflict-of-interest" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h2><p><img src="../media/fee_structure.svg" alt="2/20 Structure: conflict of interest"></p> | |
| <p>Hedge fund economics create a specific incentive structure.</p> | |
| <p><strong>A hedge fund does not guarantee loss coverage.</strong></p> | |
| <p><strong>2% management fee</strong> — collected regardless of results, even if the client is at a loss | |
| <strong>20% performance fee</strong> — only on profits (usually with a high-water mark) | |
| <strong>Losses</strong> — borne entirely by the client</p> | |
| <p>The first part (2% of AUM) is a "salary for time." The longer the money stays in the fund and the larger the AUM, the greater the guaranteed income.</p> | |
| <p><strong>The fund benefits from:</strong></p> | |
| <ol> | |
| <li>Attracting more money (more AUM → more 2%)</li> | |
| <li>Keeping money longer (2% accrues every year)</li> | |
| <li>Showing profit (earning 20% of it)</li> | |
| </ol> | |
| <p>Goals 2 (keep longer) and 3 (generate profit) conflict. A fund can hold low-risk positions for years, collect 2%, and not chase performance fees. Many do exactly that.</p> | |
| <p>This creates a mismatch: the fund benefits from long holding periods, while the client's capital remains exposed to tail risk.</p> | |
| <a id="how-cascades-and-sudden-crashes-occur" class="tsd-anchor"></a><h2 class="tsd-anchor-link">How Cascades and "Sudden" Crashes Occur<a href="#how-cascades-and-sudden-crashes-occur" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h2><p><img src="../media/cascade_feedback.svg" alt="Feedback loop - cascade"></p> | |
| <p>When many participants are forced to close positions simultaneously, the price starts moving on its own, amplifying the pressure. One large gap → hedge can't keep up → big loss → even more forced selling → even larger gap. This is how famous crashes happened: 2008, March 2020, and dozens of smaller episodes.</p> | |
| <a id="how-to-protect-against-a-drop" class="tsd-anchor"></a><h2 class="tsd-anchor-link">How to Protect Against a Drop<a href="#how-to-protect-against-a-drop" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h2><p>To reduce the probability of being caught in a GAP, you need to minimize the time capital stays in a position.</p> | |
| <p><img src="../media/static_vs_adaptive.svg" alt="Static vs Adaptive: opposing goals"></p> | |
| <a id="the-compromise" class="tsd-anchor"></a><h2 class="tsd-anchor-link">The Compromise<a href="#the-compromise" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h2><p>To balance between holding time and stock returns, one could monitor market sentiment.</p> | |
| <ol> | |
| <li>No authorities: Market sentiment is shaped not by one blogger's posts, but by a sample of posts shown to all visitors by the recommendation algorithm.</li> | |
| <li>No de-anonymization: There is no way to distinguish 10 rephrased posts with one opinion from one person from 10 posts with different opinions from different people.</li> | |
| </ol> | |
| <p><img src="../media/emh_timing.svg" alt="Price moves before the news"></p> | |
| <p>According to the efficient market hypothesis from Eugene Fama's work, Japanese candlesticks already incorporate the market's reaction to news. The thing is, traders buy on rumors and sell on news. You can verify this in practice by opening TradingView after a news event: the lightning bolt icon (Latest News) always shows a preceding sustained price movement of many points.</p> | |
| <p><img src="../media/news.png" alt="The market drops long before the news is published"></p> | |
| <a id="what-should-an-individual-do" class="tsd-anchor"></a><h2 class="tsd-anchor-link">What Should an Individual Do<a href="#what-should-an-individual-do" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h2><p>Instead of long-term holding — an adaptive system:</p> | |
| <ol> | |
| <li>Take a strategy skeleton with specific entry logic</li> | |
| <li>Run a backtest on fresh data (rolling window)</li> | |
| <li>AI analyzes execution logs: where signals worked, where they didn't</li> | |
| <li>Modify parameters based on regime detection</li> | |
| <li>Repeat</li> | |
| </ol> | |
| <a id="example-skeleton" class="tsd-anchor"></a><h2 class="tsd-anchor-link">Example Skeleton:<a href="#example-skeleton" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h2><ul> | |
| <li>Determine direction on the 4H timeframe</li> | |
| <li>Wait for a common retail signal on 15M (where the algo flow enters)</li> | |
| <li>If the 15M signal aligns with the 4H direction — check the volatility forecast</li> | |
| <li>Use realized volatility models (GARCH variants, HAR-RV) with the QLIKE criterion for selection</li> | |
| <li>If σ > threshold covering commissions and slippage — enter</li> | |
| <li>R:R 1:1, scale exit after breakeven</li> | |
| </ul> | |
| <a id="code-examples" class="tsd-anchor"></a><h2 class="tsd-anchor-link">Code Examples:<a href="#code-examples" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h2><p>Over a long time horizon, this won't converge to 50/50 with a zero-sum and no profit under the efficient market hypothesis: any public information (including patterns, indicators, signals) is already priced in. If RSI oversold "works," then arbitrageurs will buy before RSI reaches 30, diluting the signal.</p> | |
| <p>The key point: you need to cut backtest strategy time to zero through self-reinforcement runtime, since the bet is on temporary inefficiencies and regime adaptation.</p> | |
| <p>The adaptive cycle has no "done" state — parameters are recalculated on each iteration for the current market regime.</p> | |
| <p><strong>Volatility filter (GARCH):</strong></p> | |
| <pre><code class="javascript"><span class="hl-6">import</span><span class="hl-2"> { </span><span class="hl-5">predict</span><span class="hl-2"> } </span><span class="hl-6">from</span><span class="hl-2"> </span><span class="hl-3">'garch'</span><span class="hl-2">; </span><span class="hl-0">// https://www.npmjs.com/package/garch</span><br/><span class="hl-6">import</span><span class="hl-2"> { </span><span class="hl-5">getCandles</span><span class="hl-2"> } </span><span class="hl-6">from</span><span class="hl-2"> </span><span class="hl-3">'backtest-kit'</span><span class="hl-2">; </span><span class="hl-0">// https://www.npmjs.com/package/backtest-kit</span><br/><br/><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">candles</span><span class="hl-2"> = </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">getCandles</span><span class="hl-2">(</span><span class="hl-5">symbol</span><span class="hl-2">, </span><span class="hl-3">'15m'</span><span class="hl-2">, </span><span class="hl-8">300</span><span class="hl-2">);</span><br/><span class="hl-4">const</span><span class="hl-2"> { </span><span class="hl-7">sigma</span><span class="hl-2">, </span><span class="hl-7">reliable</span><span class="hl-2">, </span><span class="hl-7">upperPrice</span><span class="hl-2">, </span><span class="hl-7">lowerPrice</span><span class="hl-2"> } = </span><span class="hl-1">predict</span><span class="hl-2">(</span><span class="hl-5">candles</span><span class="hl-2">, </span><span class="hl-3">'15m'</span><span class="hl-2">);</span><br/><br/><span class="hl-0">// sigma - expected price movement (0.012 = 1.2%)</span><br/><span class="hl-0">// reliable - whether the model converged</span><br/><span class="hl-0">// upperPrice / lowerPrice - +-1 sigma corridor for the next candle</span><br/><br/><span class="hl-6">if</span><span class="hl-2"> (!</span><span class="hl-5">reliable</span><span class="hl-2">) </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-4">null</span><span class="hl-2">; </span><span class="hl-0">// model doesn't fit the data</span><br/><span class="hl-6">if</span><span class="hl-2"> (</span><span class="hl-5">sigma</span><span class="hl-2"> < </span><span class="hl-8">0.01</span><span class="hl-2">) </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-4">null</span><span class="hl-2">; </span><span class="hl-0">// movement < 1%, won't cover fees and slippage</span><br/><br/><span class="hl-0">// use upperPrice and lowerPrice as TP/SL</span><br/><span class="hl-6">return</span><span class="hl-2"> { </span><span class="hl-5">upperPrice</span><span class="hl-2">, </span><span class="hl-5">lowerPrice</span><span class="hl-2"> }</span> | |
| </code><button type="button">Copy</button></pre> | |
| <p><strong>JavaScript integration:</strong></p> | |
| <pre><code class="javascript"><span class="hl-6">import</span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-5">addExchangeSchema</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">addStrategySchema</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">addActionSchema</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">Cache</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">Constant</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">commitPartialProfit</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">getCandles</span><span class="hl-2">,</span><br/><span class="hl-2">} </span><span class="hl-6">from</span><span class="hl-2"> </span><span class="hl-3">"backtest-kit"</span><span class="hl-2">;</span><br/><br/><span class="hl-6">import</span><span class="hl-2"> { </span><span class="hl-5">extract</span><span class="hl-2">, </span><span class="hl-5">run</span><span class="hl-2">, </span><span class="hl-5">toSignalDto</span><span class="hl-2">, </span><span class="hl-5">File</span><span class="hl-2"> } </span><span class="hl-6">from</span><span class="hl-2"> </span><span class="hl-3">"@backtest-kit/pinets"</span><span class="hl-2">;</span><br/><span class="hl-6">import</span><span class="hl-2"> { </span><span class="hl-5">predict</span><span class="hl-2"> } </span><span class="hl-6">from</span><span class="hl-2"> </span><span class="hl-3">'garch'</span><span class="hl-2">;</span><br/><br/><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">getPlotHigher</span><span class="hl-2"> = </span><span class="hl-5">Cache</span><span class="hl-2">.</span><span class="hl-1">fn</span><span class="hl-2">(</span><br/><span class="hl-2"> </span><span class="hl-4">async</span><span class="hl-2"> (</span><span class="hl-5">symbol</span><span class="hl-2">) </span><span class="hl-4">=></span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">run</span><span class="hl-2">(</span><span class="hl-5">File</span><span class="hl-2">.</span><span class="hl-1">fromPath</span><span class="hl-2">(</span><span class="hl-3">"timeframe_4h.pine"</span><span class="hl-2">), {</span><br/><span class="hl-2"> </span><span class="hl-5">symbol</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">timeframe:</span><span class="hl-2"> </span><span class="hl-3">"4h"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">limit:</span><span class="hl-2"> </span><span class="hl-8">100</span><span class="hl-2">,</span><br/><span class="hl-2"> });</span><br/><span class="hl-2"> },</span><br/><span class="hl-2"> { </span><span class="hl-5">interval:</span><span class="hl-2"> </span><span class="hl-3">"4h"</span><span class="hl-2">, </span><span class="hl-1">key</span><span class="hl-5">:</span><span class="hl-2"> ([</span><span class="hl-5">symbol</span><span class="hl-2">]) </span><span class="hl-4">=></span><span class="hl-2"> </span><span class="hl-5">symbol</span><span class="hl-2"> },</span><br/><span class="hl-2">);</span><br/><br/><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-1">getDataHigher</span><span class="hl-2"> = </span><span class="hl-4">async</span><span class="hl-2"> (</span><span class="hl-5">symbol</span><span class="hl-2">) </span><span class="hl-4">=></span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">plots</span><span class="hl-2"> = </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">getPlotHigher</span><span class="hl-2">(</span><span class="hl-5">symbol</span><span class="hl-2">);</span><br/><span class="hl-2"> </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-1">extract</span><span class="hl-2">(</span><span class="hl-5">plots</span><span class="hl-2">, {</span><br/><span class="hl-2"> </span><span class="hl-5">allowLong:</span><span class="hl-2"> </span><span class="hl-3">"AllowLong"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">allowShort:</span><span class="hl-2"> </span><span class="hl-3">"AllowShort"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">allowBoth:</span><span class="hl-2"> </span><span class="hl-3">"AllowBoth"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">noTrades:</span><span class="hl-2"> </span><span class="hl-3">"NoTrades"</span><span class="hl-2">,</span><br/><span class="hl-2"> });</span><br/><span class="hl-2">};</span><br/><br/><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">getPlotLower</span><span class="hl-2"> = </span><span class="hl-5">Cache</span><span class="hl-2">.</span><span class="hl-1">fn</span><span class="hl-2">(</span><br/><span class="hl-2"> </span><span class="hl-4">async</span><span class="hl-2"> (</span><span class="hl-5">symbol</span><span class="hl-2">) </span><span class="hl-4">=></span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">run</span><span class="hl-2">(</span><span class="hl-5">File</span><span class="hl-2">.</span><span class="hl-1">fromPath</span><span class="hl-2">(</span><span class="hl-3">"timeframe_15m.pine"</span><span class="hl-2">), {</span><br/><span class="hl-2"> </span><span class="hl-5">symbol</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">timeframe:</span><span class="hl-2"> </span><span class="hl-3">"15m"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">limit:</span><span class="hl-2"> </span><span class="hl-8">100</span><span class="hl-2">,</span><br/><span class="hl-2"> });</span><br/><span class="hl-2"> },</span><br/><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-5">interval:</span><span class="hl-2"> </span><span class="hl-3">"15m"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-1">key</span><span class="hl-5">:</span><span class="hl-2"> ([</span><span class="hl-5">symbol</span><span class="hl-2">]) </span><span class="hl-4">=></span><span class="hl-2"> </span><span class="hl-3">`</span><span class="hl-4">${</span><span class="hl-5">symbol</span><span class="hl-4">}</span><span class="hl-3">`</span><span class="hl-2">,</span><br/><span class="hl-2"> },</span><br/><span class="hl-2">);</span><br/><br/><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-1">getDataLower</span><span class="hl-2"> = </span><span class="hl-4">async</span><span class="hl-2"> (</span><span class="hl-5">symbol</span><span class="hl-2">) </span><span class="hl-4">=></span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">plots</span><span class="hl-2"> = </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">getPlotLower</span><span class="hl-2">(</span><span class="hl-5">symbol</span><span class="hl-2">);</span><br/><span class="hl-2"> </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-1">extract</span><span class="hl-2">(</span><span class="hl-5">plots</span><span class="hl-2">, {</span><br/><span class="hl-2"> </span><span class="hl-5">position:</span><span class="hl-2"> </span><span class="hl-3">"Signal"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">priceOpen:</span><span class="hl-2"> </span><span class="hl-3">"Close"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">priceTakeProfit:</span><span class="hl-2"> </span><span class="hl-3">"TakeProfit"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">priceStopLoss:</span><span class="hl-2"> </span><span class="hl-3">"StopLoss"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">minuteEstimatedTime:</span><span class="hl-2"> </span><span class="hl-3">"EstimatedTime"</span><span class="hl-2">,</span><br/><span class="hl-2"> });</span><br/><span class="hl-2">};</span><br/><br/><span class="hl-1">addStrategySchema</span><span class="hl-2">({</span><br/><span class="hl-2"> </span><span class="hl-5">strategyName:</span><span class="hl-2"> </span><span class="hl-3">"main_strategy"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">interval:</span><span class="hl-2"> </span><span class="hl-3">"5m"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-1">getSignal</span><span class="hl-5">:</span><span class="hl-2"> </span><span class="hl-4">async</span><span class="hl-2"> (</span><span class="hl-5">symbol</span><span class="hl-2">) </span><span class="hl-4">=></span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">data_higher</span><span class="hl-2"> = </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">getDataHigher</span><span class="hl-2">(</span><span class="hl-5">symbol</span><span class="hl-2">);</span><br/><br/><span class="hl-2"> </span><span class="hl-6">if</span><span class="hl-2"> (</span><span class="hl-5">data_higher</span><span class="hl-2">.</span><span class="hl-5">noTrades</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-4">null</span><span class="hl-2">; </span><span class="hl-0">// ADX < 25, sideways market</span><br/><span class="hl-2"> }</span><br/><br/><span class="hl-2"> </span><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">data_lower</span><span class="hl-2"> = </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">getDataLower</span><span class="hl-2">(</span><span class="hl-5">symbol</span><span class="hl-2">);</span><br/><br/><span class="hl-2"> </span><span class="hl-6">if</span><span class="hl-2"> (</span><span class="hl-5">data_lower</span><span class="hl-2">.</span><span class="hl-5">position</span><span class="hl-2"> === </span><span class="hl-8">0</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-4">null</span><span class="hl-2">; </span><span class="hl-0">// no signal</span><br/><span class="hl-2"> }</span><br/><br/><span class="hl-2"> </span><span class="hl-0">// Filter: don't enter against the higher timeframe</span><br/><span class="hl-2"> </span><span class="hl-6">if</span><span class="hl-2"> (</span><span class="hl-5">data_higher</span><span class="hl-2">.</span><span class="hl-5">allowShort</span><span class="hl-2"> && </span><span class="hl-5">data_lower</span><span class="hl-2">.</span><span class="hl-5">position</span><span class="hl-2"> === </span><span class="hl-8">1</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-4">null</span><span class="hl-2">;</span><br/><span class="hl-2"> }</span><br/><span class="hl-2"> </span><span class="hl-6">if</span><span class="hl-2"> (</span><span class="hl-5">data_higher</span><span class="hl-2">.</span><span class="hl-5">allowLong</span><span class="hl-2"> && </span><span class="hl-5">data_lower</span><span class="hl-2">.</span><span class="hl-5">position</span><span class="hl-2"> === -</span><span class="hl-8">1</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-4">null</span><span class="hl-2">;</span><br/><span class="hl-2"> }</span><br/><br/><span class="hl-2"> </span><span class="hl-4">const</span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-7">upperPrice</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-7">lowerPrice</span><br/><span class="hl-2"> } = </span><span class="hl-1">predict</span><span class="hl-2">(</span><br/><span class="hl-2"> </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">getCandles</span><span class="hl-2">(</span><span class="hl-5">symbol</span><span class="hl-2">, </span><span class="hl-3">'15m'</span><span class="hl-2">, </span><span class="hl-8">300</span><span class="hl-2">),</span><br/><span class="hl-2"> </span><span class="hl-3">'15m'</span><br/><span class="hl-2"> );</span><br/><br/><span class="hl-2"> </span><span class="hl-0">// Long: TP = upperPrice, SL = lowerPrice</span><br/><span class="hl-2"> </span><span class="hl-0">// Short: TP = lowerPrice, SL = upperPrice</span><br/><span class="hl-2"> </span><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">isLong</span><span class="hl-2"> = </span><span class="hl-5">data_lower</span><span class="hl-2">.</span><span class="hl-5">position</span><span class="hl-2"> === </span><span class="hl-8">1</span><span class="hl-2">;</span><br/><span class="hl-2"> </span><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">tp</span><span class="hl-2"> = </span><span class="hl-5">isLong</span><span class="hl-2"> ? </span><span class="hl-5">upperPrice</span><span class="hl-2"> : </span><span class="hl-5">lowerPrice</span><span class="hl-2">;</span><br/><span class="hl-2"> </span><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">sl</span><span class="hl-2"> = </span><span class="hl-5">isLong</span><span class="hl-2"> ? </span><span class="hl-5">lowerPrice</span><span class="hl-2"> : </span><span class="hl-5">upperPrice</span><span class="hl-2">;</span><br/><br/><span class="hl-2"> </span><span class="hl-4">const</span><span class="hl-2"> </span><span class="hl-7">signal</span><span class="hl-2"> = </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">toSignalDto</span><span class="hl-2">(</span><span class="hl-5">signalId</span><span class="hl-2">, </span><span class="hl-5">data_lower</span><span class="hl-2">, </span><span class="hl-4">null</span><span class="hl-2">);</span><br/><br/><span class="hl-2"> </span><span class="hl-5">Object</span><span class="hl-2">.</span><span class="hl-1">assign</span><span class="hl-2">(</span><span class="hl-5">signal</span><span class="hl-2">, {</span><br/><span class="hl-2"> </span><span class="hl-5">priceTakeProfit:</span><span class="hl-2"> </span><span class="hl-5">tp</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">priceStopLoss:</span><span class="hl-2"> </span><span class="hl-5">sl</span><span class="hl-2">,</span><br/><span class="hl-2"> });</span><br/><br/><span class="hl-2"> </span><span class="hl-6">return</span><span class="hl-2"> </span><span class="hl-5">signal</span><span class="hl-2">;</span><br/><span class="hl-2"> },</span><br/><span class="hl-2">});</span> | |
| </code><button type="button">Copy</button></pre> | |
| <p><strong>Position management:</strong></p> | |
| <pre><code class="javascript"><span class="hl-1">addActionSchema</span><span class="hl-2">({</span><br/><span class="hl-2"> </span><span class="hl-5">actionName:</span><span class="hl-2"> </span><span class="hl-3">"partial_profit_action"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">handler:</span><span class="hl-2"> </span><span class="hl-4">class</span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-4">async</span><span class="hl-2"> </span><span class="hl-1">partialProfitAvailable</span><span class="hl-2">({ </span><span class="hl-5">symbol</span><span class="hl-2">, </span><span class="hl-5">level</span><span class="hl-2"> }) {</span><br/><span class="hl-2"> </span><span class="hl-6">if</span><span class="hl-2"> (</span><span class="hl-5">level</span><span class="hl-2"> === </span><span class="hl-5">Constant</span><span class="hl-2">.</span><span class="hl-7">TP_LEVEL3</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">commitPartialProfit</span><span class="hl-2">(</span><span class="hl-5">symbol</span><span class="hl-2">, </span><span class="hl-8">33</span><span class="hl-2">);</span><br/><span class="hl-2"> }</span><br/><span class="hl-2"> </span><span class="hl-6">if</span><span class="hl-2"> (</span><span class="hl-5">level</span><span class="hl-2"> === </span><span class="hl-5">Constant</span><span class="hl-2">.</span><span class="hl-7">TP_LEVEL2</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">commitPartialProfit</span><span class="hl-2">(</span><span class="hl-5">symbol</span><span class="hl-2">, </span><span class="hl-8">33</span><span class="hl-2">);</span><br/><span class="hl-2"> }</span><br/><span class="hl-2"> </span><span class="hl-6">if</span><span class="hl-2"> (</span><span class="hl-5">level</span><span class="hl-2"> === </span><span class="hl-5">Constant</span><span class="hl-2">.</span><span class="hl-7">TP_LEVEL1</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">commitPartialProfit</span><span class="hl-2">(</span><span class="hl-5">symbol</span><span class="hl-2">, </span><span class="hl-8">34</span><span class="hl-2">);</span><br/><span class="hl-2"> }</span><br/><span class="hl-2"> }</span><br/><span class="hl-2"> },</span><br/><span class="hl-2">});</span><br/><br/><span class="hl-1">addActionSchema</span><span class="hl-2">({</span><br/><span class="hl-2"> </span><span class="hl-5">actionName:</span><span class="hl-2"> </span><span class="hl-3">"breakeven_action"</span><span class="hl-2">,</span><br/><span class="hl-2"> </span><span class="hl-5">handler:</span><span class="hl-2"> </span><span class="hl-4">class</span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-4">async</span><span class="hl-2"> </span><span class="hl-1">breakevenAvailable</span><span class="hl-2">({ </span><span class="hl-5">symbol</span><span class="hl-2">, </span><span class="hl-5">currentPrice</span><span class="hl-2"> }) {</span><br/><span class="hl-2"> </span><span class="hl-0">// Tighten trailing stop after breakeven</span><br/><span class="hl-2"> </span><span class="hl-6">await</span><span class="hl-2"> </span><span class="hl-1">commitTrailingStop</span><span class="hl-2">(</span><span class="hl-5">symbol</span><span class="hl-2">, -</span><span class="hl-8">3</span><span class="hl-2">, </span><span class="hl-5">currentPrice</span><span class="hl-2">);</span><br/><span class="hl-2"> }</span><br/><span class="hl-2"> }</span><br/><span class="hl-2">});</span> | |
| </code><button type="button">Copy</button></pre> | |
| <p>Staged exit: 33% at each TP level. This locks in profit before a reversal. After breakeven — tighten the SL to protect accumulated gains.</p> | |
| </div></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>On This Page</h3></summary><div class="tsd-accordion-details"><a href="#🩸-why-the-price-drops-in-a-single-candle"><span>🩸 <wbr/>Why the <wbr/>Price <wbr/>Drops in a <wbr/>Single <wbr/>Candle</span></a><ul><li><a href="#what-are-options"><span>What <wbr/>Are <wbr/>Options</span></a></li><li><a href="#smooth-movement-vs-sudden-jumps"><span>Smooth <wbr/>Movement vs. <wbr/>Sudden <wbr/>Jumps</span></a></li><li><a href="#how-positions-are-adjusted"><span>How <wbr/>Positions <wbr/>Are <wbr/>Adjusted</span></a></li><li><a href="#conflict-of-interest"><span>Conflict of <wbr/>Interest</span></a></li><li><a href="#how-cascades-and-sudden-crashes-occur"><span>How <wbr/>Cascades and "<wbr/>Sudden" <wbr/>Crashes <wbr/>Occur</span></a></li><li><a href="#how-to-protect-against-a-drop"><span>How to <wbr/>Protect <wbr/>Against a <wbr/>Drop</span></a></li><li><a href="#the-compromise"><span>The <wbr/>Compromise</span></a></li><li><a href="#what-should-an-individual-do"><span>What <wbr/>Should an <wbr/>Individual <wbr/>Do</span></a></li><li><a href="#example-skeleton"><span>Example <wbr/>Skeleton:</span></a></li><li><a href="#code-examples"><span>Code <wbr/>Examples:</span></a></li></ul></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">backtest-kit</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html> | |
| <!-- Yandex.Metrika counter --> | |
| <script type="text/javascript"> | |
| (function(m,e,t,r,i,k,a){ | |
| m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)}; | |
| m[i].l=1*new Date(); | |
| for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }} | |
| k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a) | |
| })(window, document,'script','https://mc.yandex.ru/metrika/tag.js?id=105455585', 'ym'); | |
| ym(105455585, 'init', {ssr:true, webvisor:true, clickmap:true, ecommerce:"dataLayer", accurateTrackBounce:true, trackLinks:true}); | |
| </script> | |
| <noscript><div><img src="https://mc.yandex.ru/watch/105455585" style="position:absolute; left:-9999px;" alt="" /></div></noscript> | |
| <!-- /Yandex.Metrika counter --> | |
| <!-- Google tag (gtag.js) --> | |
| <script async src="https://www.googletagmanager.com/gtag/js?id=G-3MQZEBBDDR"></script> | |
| <script> | |
| window.dataLayer = window.dataLayer || []; | |
| function gtag(){dataLayer.push(arguments);} | |
| gtag('js', new Date()); | |
| gtag('config', 'G-3MQZEBBDDR'); | |
| </script> | |