<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lei Chen</title>
    <description>The latest articles on DEV Community by Lei Chen (@leichenbuilds).</description>
    <link>https://dev.to/leichenbuilds</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4218649%2Ff7c8da51-6e8e-4089-8923-2e4269d4ec7a.png</url>
      <title>DEV Community: Lei Chen</title>
      <link>https://dev.to/leichenbuilds</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leichenbuilds"/>
    <language>en</language>
    <item>
      <title>I Removed the Profit Target. The Strategy Turned Profitable — Because of One Trade.</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Sat, 21 Nov 2026 11:16:22 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/i-removed-the-profit-target-the-strategy-turned-profitable-because-of-one-trade-3dn0</link>
      <guid>https://dev.to/leichenbuilds/i-removed-the-profit-target-the-strategy-turned-profitable-because-of-one-trade-3dn0</guid>
      <description>&lt;h1&gt;
  
  
  I Removed the Profit Target. The Strategy Turned Profitable — Because of One Trade.
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Post #14 ended with a question: the 10-bar time exit kept absorbing the trades the other exits gave up — so what happens when the limit is removed entirely and the time exit runs the bracket alone? The strategy turned profitable for the first time in this series: net &lt;strong&gt;+$2,420.59&lt;/strong&gt;, Profit Factor &lt;strong&gt;1.378&lt;/strong&gt;, in our testing. But one trade — entry 2024-09-20, exit 2024-10-11 — is &lt;strong&gt;+$2,331.99 of that $2,420.59&lt;/strong&gt;. Without it the net is +$88.60. And the entry stream is byte-identical to #14's, so this is the cleanest comparison in the series: every dollar of improvement comes from 16 trades that used to cap at the limit and instead ran to bar 10.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Limit 2×ATR + stop 2×ATR (#14)&lt;/th&gt;
&lt;th&gt;No limit, stop 2×ATR + 10-bar time exit (#15)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Closed trades&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Limit exits&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 — removed&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop exits&lt;/td&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time exits&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Net profit&lt;/td&gt;
&lt;td&gt;−$880.55&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$2,420.59&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Profit Factor&lt;/td&gt;
&lt;td&gt;0.856&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1.378&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Win rate&lt;/td&gt;
&lt;td&gt;24 / 48 (50.0%)&lt;/td&gt;
&lt;td&gt;23 / 48 (47.9%)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The contract: one question, chosen before I looked
&lt;/h2&gt;

&lt;p&gt;Post #14 pre-committed this test: same script, limit removed, stop 2×ATR, 10-bar time exit. The contract for this post is three questions, in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Was the limit doing anything — or was it cutting winners short?&lt;/li&gt;
&lt;li&gt;Is the comparison clean this time, or does the entry stream move again?&lt;/li&gt;
&lt;li&gt;If the net turns positive, what is actually carrying it — the bracket, or a few trades?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The script is post #5's fixed RSI mean-reversion strategy, byte-identical apart from the removed limit and the title string — the compiled &lt;code&gt;.py&lt;/code&gt; diffs clean against the #14 base except for one line and the title. Zero diagnostics from the local compiler. Same 4,089 daily bars of CSI 300 (2010-01-04 → 2026-11-06), same $10,000, same PyneCore patched runtime, no commission, no slippage.&lt;/p&gt;

&lt;p&gt;The one-line change, in Pine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if inTrade
    strategy.exit("Exit Long", "Long", stop=strategy.position_avg_price - atrVal * 2)
    if barsInTrade &amp;gt;= maxHold
        strategy.close("Long", comment="Time exit")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the compiled Python, verbatim from the local compiler (&lt;code&gt;node bin/pine2pyne.mjs&lt;/code&gt;, zero diagnostics) — the only line that differs from the #14 run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Exit Long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position_avg_price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;atrVal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What you're checking here:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does removing the limit change the net — or just move wins from one exit column to another?&lt;/li&gt;
&lt;li&gt;Does a bracket change touch the entry stream again, like it did in #14?&lt;/li&gt;
&lt;li&gt;If the strategy finally turns profitable, how much of the profit is one trade?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Probe 1: the exit split — the time exit is now the whole strategy
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Exit reason&lt;/th&gt;
&lt;th&gt;Trades&lt;/th&gt;
&lt;th&gt;Wins&lt;/th&gt;
&lt;th&gt;Net&lt;/th&gt;
&lt;th&gt;Avg per trade&lt;/th&gt;
&lt;th&gt;Avg hold&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Stop (2×ATR)&lt;/td&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$5,778.55&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;−$321.03&lt;/td&gt;
&lt;td&gt;4.1 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time (10 bars)&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$8,199.14&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;+$273.30&lt;/td&gt;
&lt;td&gt;10.0 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There is no limit column any more. The time exit went from 14 trades to 30 — 62.5% of all exits — and it is the only exit that ever wins. The stop column is unchanged in count (18) and still has never once produced a winner. Its average loss crept up again, −$311.79 → −$321.03, for a reason Probe 2 explains: it is not the prices, it is the position size.&lt;/p&gt;

&lt;p&gt;Two verdicts hiding in that table:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The limit was cutting winners short.&lt;/strong&gt; In #14 the 16 limit exits averaged +$240.01. The same 16 trades, allowed to run to bar 10, are worth +$7,196.69 between them — +$449.79 each. The limit did not protect anything. It capped the upside at exactly the level where this strategy's winners were still going.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The stop is still the strategy.&lt;/strong&gt; 18 of 48 exits are stops, 0 wins, −$5,778.55 — still the largest loss column, still larger than the time exit's average win in absolute terms ($321.03 vs $273.30). Removing the limit did not fix the stop. It just let the time exit out-earn it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 2: the matched 49 — the cleanest comparison in the series
&lt;/h2&gt;

&lt;p&gt;This is the part #14 could not deliver. In #14 the wider stop ate 10 entries, so half the headline improvement was entry-stream luck. This time the entry stream did not move at all: &lt;strong&gt;49 entries in #14, 49 entries in #15, identical bars, identical prices.&lt;/strong&gt; Removing the limit lengthened 16 holds from ~3.4 bars to 10 — and not one new signal fired inside those extra windows. So the comparison is fully clean: same entries, different exits, and the delta is purely an exit effect.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Transition (#14 → #15)&lt;/th&gt;
&lt;th&gt;Trades&lt;/th&gt;
&lt;th&gt;Dollar delta&lt;/th&gt;
&lt;th&gt;Point delta&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;LIMIT → TIME&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$3,356.61&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+982.30 pts&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;STOP → STOP&lt;/td&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;−$166.40&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+0.00 pts&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TIME → TIME&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;+$110.93&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+0.00 pts&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OPEN → OPEN&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;49&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$3,301.14&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+982.30 pts&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Read the point column before the dollar column. In points — the only unit that is immune to position sizing — the 18 stops are worth exactly what they were worth in #14 (+0.00 pts), and the 14 time exits are worth exactly what they were (+0.00 pts). &lt;strong&gt;All +982.30 points of improvement come from the 16 trades that used to hit the limit.&lt;/strong&gt; One mechanism, one number, no confound.&lt;/p&gt;

&lt;p&gt;Then why do the dollar deltas on the identical trades move at all? Because the script sizes at 100% of equity, so the same losing prices cost more dollars when the account is bigger. Two stops prove it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Trade #15 (entry 2013-06-17): same entry price, same exit price, same bars — but 4 contracts in #15 vs 3 in #14, so −$335.99 vs −$251.99. The extra −$84.00 is sizing, not price.&lt;/li&gt;
&lt;li&gt;Trade #41 (entry 2023-12-12): same prices, 3 contracts vs 2, so −$247.22 vs −$164.81. The extra −$82.41 is sizing, not price.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The TIME → TIME +$110.93 is the same effect in reverse: identical prices, bigger account, bigger dollar wins. &lt;strong&gt;Rule I now follow: when sizing is percent-of-equity, measure an exit change in points first and dollars second.&lt;/strong&gt; The dollar delta (+$3,301.14) is the number you report; the point delta (+982.30 pts, all from one transition) is the number you trust.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 3: a few trades carry the result — the concentration check
&lt;/h2&gt;

&lt;p&gt;This is the check the moonthetrain SuperTrend series runs on every strategy, and it is the one this post cannot skip. The top 5 winners are $4,922.84 of the $8,830.95 gross profit — 55.7%. And one trade does almost all the work:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Trade&lt;/th&gt;
&lt;th&gt;Entry&lt;/th&gt;
&lt;th&gt;Exit&lt;/th&gt;
&lt;th&gt;Hold&lt;/th&gt;
&lt;th&gt;Profit&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;#45&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;2024-09-20 @ 3,192.43&lt;/td&gt;
&lt;td&gt;2024-10-11 @ 3,969.76&lt;/td&gt;
&lt;td&gt;10 bars (time)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$2,331.99&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#14&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;10 bars (time)&lt;/td&gt;
&lt;td&gt;+$890.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#11&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;10 bars (time)&lt;/td&gt;
&lt;td&gt;+$653.10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#5&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;10 bars (time)&lt;/td&gt;
&lt;td&gt;+$550.77&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#29&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;10 bars (time)&lt;/td&gt;
&lt;td&gt;+$496.98&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Trade #45 is the 2024-09-30 rally trade — the same rally that carried post #12's weekly result (61.5% of that post's gain from a single trade). It rode the entry at 3,192.43 to 3,969.76 across the ten bars that include the sharpest rally in the sample, and it exited on the time rule, not on skill: it held 10 bars because the script holds 10 bars.&lt;/p&gt;

&lt;p&gt;The honest arithmetic: &lt;strong&gt;net +$2,420.59 minus trade #45 = +$88.60.&lt;/strong&gt; Without one trade, sixteen years of this strategy is breakeven. The top 5 winners are 55.7% of all gross profit; the other 18 winners between them net +$3,908.11.&lt;/p&gt;

&lt;p&gt;So the verdict on this post's headline is two sentences, not one: the limit was genuinely cutting winners short — that part is real, measured on 16 matched trades, +982.30 points. And the strategy is still not a profitable strategy: it is a breakeven strategy plus one rally trade. A first profitable variant is a suspect until the concentration check clears it, and this one does not clear it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 4: the gap-through didn't move — the one constant
&lt;/h2&gt;

&lt;p&gt;Of the 18 stops, the same 3 filled at the exit bar's open as in #14. The extra loss vs the level the script actually set:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Gap fills&lt;/th&gt;
&lt;th&gt;Extra vs level&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Stop exits&lt;/td&gt;
&lt;td&gt;3 of 18&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$227.75&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Net gap effect&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;3 of 18&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$227.75&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Identical to #14 to the cent ($227.75, 75.92 points) — because the 18 stops are the same trades at the same prices. The verification holds too: every non-gap stop fill lands exactly on the level computed from the previous bar's ATR (distance 0.0000 ATR), same as #13 and #14. Removing the limit changed nothing about how stops fill. It only changed what the trades that &lt;em&gt;don't&lt;/em&gt; stop are allowed to become.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Count exits by reason before tuning anything.&lt;/strong&gt; (From #13 — still the first rule. The limit column is gone and the story is still in the exit split, not the net.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A bracket change is an entry change — until proven otherwise.&lt;/strong&gt; (From #14. This time the entry stream provably didn't move: 49 = 49, identical bars. Report the entry count alongside the exit count, or the net comparison is fiction.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Match trades before claiming an effect.&lt;/strong&gt; (From #14. Same entry, different exit is the only clean comparison — and this post finally has one: all 49 entries matched.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Measure in points before dollars when sizing is percent-of-equity.&lt;/strong&gt; (New. Two stops cost $166.40 more on identical prices, because the account was bigger when they fired. Points don't have that problem.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A first profitable variant is a suspect until the concentration check clears it.&lt;/strong&gt; (New, borrowed from moonthetrain's SuperTrend series: top-5 winners' share of gross profit, and net-minus-the-biggest-trade, before any verdict. Here: 55.7%, and +$88.60 without trade #45.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Price the escape, don't just count it.&lt;/strong&gt; (From #14. Sixteen trades escaped the limit — and they were worth +982.30 points. The count alone would have hidden which transition did the work.)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What this post gives you, and what it doesn't
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This post gives you:&lt;/strong&gt; the full exit-by-reason count with the limit removed on 4,089 daily bars (0 limit / 18 stop / 30 time), the matched-trade decomposition on an identical 49-entry stream (+982.30 points, all from LIMIT → TIME), the sizing correction that separates price effects from dollar effects (+0.00 pts on the 18 stops, −$166.40 in dollars), and the concentration check (top-5 winners 55.7% of gross profit; net ex-trade-#45 +$88.60).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This post does &lt;em&gt;not&lt;/em&gt; give you:&lt;/strong&gt; a profitable strategy, or a verdict on RSI mean reversion. The Profit Factor is 1.378 and the net is +$2,420.59 — but it beats buy &amp;amp; hold (+$1,468.22 on the same span) by $952.37, and trade #45 alone is worth $2,331.99. It also doesn't test whether the 10-bar hold is the reason the time exit works, or just the window this rally happened to fit. That is the next post, and the setup is fixed before I look at the result: same script, no limit, stop 2×ATR, &lt;strong&gt;20-bar&lt;/strong&gt; time exit.&lt;/p&gt;

&lt;p&gt;Next in the series: the hold doubled — 10 bars vs 20, counting whether the "profitable" variant survives a longer window, or whether 10 was the lucky number.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I couldn't verify
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Costs.&lt;/strong&gt; No commission, no slippage, same as every post in this series. The time exit holds 10 bars and trades 49 times in 16 years, so costs would bite less here than on a fast strategy — but "less" is not a number, and I haven't run it with costs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The sizing.&lt;/strong&gt; All dollar figures are percent-of-equity at 100% with 3 contracts in this run's size range. The point totals (+982.30 pts) travel; the dollar totals don't, because the contract count follows the equity path.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Whether 10 bars is special.&lt;/strong&gt; The 10-bar hold is the script's original &lt;code&gt;maxHold&lt;/code&gt;, picked in post #2 before any of these numbers existed. It was not tuned to this result — but it also hasn't been tested against any other hold. That is exactly what #16 does.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Out of sample.&lt;/strong&gt; Same 4,089 bars as every post in the series. Nothing here has been tested on data the script hasn't already seen.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;Removing the limit turned the strategy profitable — net +$2,420.59, PF 1.378, in our testing — and the mechanism is real: 16 trades that used to cap at 2×ATR ran to bar 10 and gained 982.30 points. But the profit is one trade deep: without the 2024-09-30 rally trade the net is +$88.60. It's a better bracket on a breakeven strategy, not a profitable strategy yet.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Do you measure a bracket change in points or in dollars — and have you ever checked how much of your backtest's profit is a single trade?&lt;/strong&gt; I read every comment — the concentration stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>trading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
    <item>
      <title>The Stop Fired 38 Times. I Doubled It. It Fired 18 — and the Net Barely Moved.</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Fri, 20 Nov 2026 11:15:18 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/the-stop-fired-38-times-i-doubled-it-it-fired-18-and-the-net-barely-moved-1335</link>
      <guid>https://dev.to/leichenbuilds/the-stop-fired-38-times-i-doubled-it-it-fired-18-and-the-net-barely-moved-1335</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Post #13 ended with a question: the stop was the exit doing all the damage, so what happens when it widens to 2×ATR? The stop count fell 38 → 18, and the net improved from −$2,572.67 to −$880.55. But half of that "improvement" is not a better bracket — it is 10 trades the strategy never took, because the wider stop kept it in positions when the next entry signal fired. On the 49 entries both runs share, the gain is +$841.60, and the 18 stops that still fired cost nearly twice as much each (−$311.79 vs −$161.95). Widening the stop is a trade, not a fix: you pay double on the stops that still fire, to buy room for the ones that escape.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;1×ATR stop (#13)&lt;/th&gt;
&lt;th&gt;2×ATR stop (#14)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Closed trades&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Limit exits&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop exits&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;38&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;18&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time exits&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Net profit&lt;/td&gt;
&lt;td&gt;−$2,572.67&lt;/td&gt;
&lt;td&gt;−$880.55&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Profit Factor&lt;/td&gt;
&lt;td&gt;0.587&lt;/td&gt;
&lt;td&gt;0.856&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Win rate&lt;/td&gt;
&lt;td&gt;19 / 58 (32.8%)&lt;/td&gt;
&lt;td&gt;24 / 48 (50.0%)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The contract: one number, chosen before I looked
&lt;/h2&gt;

&lt;p&gt;Post #13 pre-committed this test: same script, stop widened to 2×ATR, multiplier chosen before looking at the result. The contract for this post is three questions, in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Does the stop count finally move?&lt;/li&gt;
&lt;li&gt;Does the net move with it, or does the average stop loss eat the gain?&lt;/li&gt;
&lt;li&gt;Does anything else change — entries, holds, gap fills — that the exit table hides?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The script is post #5's fixed RSI mean-reversion strategy, byte-identical apart from the stop multiplier — the compiled &lt;code&gt;.py&lt;/code&gt; diffs clean against the 2×-limit base except for one number and the title string. Zero diagnostics from the local compiler. Same 4,089 daily bars of CSI 300 (2010-01-04 → 2026-11-06), same $10,000, same PyneCore patched runtime, no commission, no slippage.&lt;/p&gt;

&lt;p&gt;The one-line change, in Pine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if inTrade
    strategy.exit("Exit Long", "Long", limit=strategy.position_avg_price + atrVal * 2, stop=strategy.position_avg_price - atrVal * 2)
    if barsInTrade &amp;gt;= maxHold
        strategy.close("Long", comment="Time exit")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the compiled Python, verbatim from the local compiler (&lt;code&gt;node bin/pine2pyne.mjs&lt;/code&gt;, zero diagnostics) — the only line that differs from the base run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Exit Long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position_avg_price&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;atrVal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position_avg_price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;atrVal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What you're checking here:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does doubling the stop halve the stop count, or does the market just find the wider level?&lt;/li&gt;
&lt;li&gt;When the stop count falls, does the net follow — or does each remaining stop cost more?&lt;/li&gt;
&lt;li&gt;Does a bracket change touch anything besides exits?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Probe 1: the exit split at 2×ATR — the stop count moved, the stop didn't go away
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Exit reason&lt;/th&gt;
&lt;th&gt;Trades&lt;/th&gt;
&lt;th&gt;Wins&lt;/th&gt;
&lt;th&gt;Net&lt;/th&gt;
&lt;th&gt;Avg per trade&lt;/th&gt;
&lt;th&gt;Avg hold&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Limit (2×ATR)&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$3,840.08&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;+$240.01&lt;/td&gt;
&lt;td&gt;3.4 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop (2×ATR)&lt;/td&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$5,612.15&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;−$311.79&lt;/td&gt;
&lt;td&gt;4.1 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time (10 bars)&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;+$891.52&lt;/td&gt;
&lt;td&gt;+$63.68&lt;/td&gt;
&lt;td&gt;10.0 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The stop count did move: 38 → 18. But read the average column before celebrating. The average stop loss nearly doubled, −$161.95 → −$311.79, because the level is twice as far from the entry. The stop column as a whole improved only $541.79 (−$6,153.94 → −$5,612.15) despite 20 fewer stops. The limit column improved $1,104.68 (+$2,735.40 → +$3,840.08) and the time column $45.65 — and the total net improved $1,692.12.&lt;/p&gt;

&lt;p&gt;Three verdicts hiding in that table:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The stop is still the strategy.&lt;/strong&gt; 18 of 48 exits (37.5%) are stops, down from 65.5% — but the stop has still never once produced a winner, and its average loss is now larger than the limit's average win ($311.79 vs $240.01). A strategy whose most common exit loses more per trade than its best exit wins is still a strategy built around its stop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The limit got better by accident, again.&lt;/strong&gt; The limit's average win rose from +$195.39 to +$240.01 — not because the limit changed (it didn't), but because the trades that reach it now are the ones that survived a wider stop. Same mechanism as #13's accidental time exit: change one exit and the other exits' numbers move, because the trade population reaching them changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The win rate doubled and means nothing yet.&lt;/strong&gt; 32.8% → 50.0% looks like a transformation. It is mostly arithmetic: the denominator shrank (58 → 48 trades) while the numerator grew by 5. A 50% win rate with a Profit Factor of 0.856 still loses money, and still loses to buy &amp;amp; hold (+$1,468.22 on the same span).&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 2: the bracket change ate 10 entries
&lt;/h2&gt;

&lt;p&gt;This is the part the exit table hides. The base run took 59 entries. This run took 49. Ten entry signals vanished — not because the signal changed (the RSI crossover is identical), but because the strategy was already in a trade when the signal fired, and the script only enters &lt;code&gt;if longSignal and not inTrade&lt;/code&gt;. No pyramiding, so a longer hold blocks the next entry.&lt;/p&gt;

&lt;p&gt;I checked all ten. Nine were blocked by a trade the wider stop kept open — a stop that now fires at 4.1 bars instead of 2.7, a limit that now fires at 3.4, a time exit at bar 10. The tenth is the final bar's signal, blocked by the still-open last trade.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Missing entry bar&lt;/th&gt;
&lt;th&gt;Blocked by&lt;/th&gt;
&lt;th&gt;In the base run it was&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;81, 481, 1463, 1467&lt;/td&gt;
&lt;td&gt;a trade the wider stop held longer&lt;/td&gt;
&lt;td&gt;STOP (−$537.09, −$149.04, −$327.68, −$202.79)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;93, 981, 1338&lt;/td&gt;
&lt;td&gt;a 10-bar time exit&lt;/td&gt;
&lt;td&gt;STOP / LIMIT / TIME&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;652, 850&lt;/td&gt;
&lt;td&gt;a limit exit&lt;/td&gt;
&lt;td&gt;LIMIT (+$259.95, +$193.48)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4087&lt;/td&gt;
&lt;td&gt;the still-open last trade&lt;/td&gt;
&lt;td&gt;OPEN&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Those 10 skipped trades lost −$850.53 in the base run. Half of this post's $1,692.12 "improvement" is trades the strategy simply didn't take. That is not a better bracket. It is a smaller sample, and the smaller sample happened to skip losers. On a different stretch of data the same mechanism skips winners — the two skipped limits were worth +$453.43 in the base run, and they are gone too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A bracket change is an entry change.&lt;/strong&gt; Any exit rule that changes how long you hold changes which signals you can take. If you compare two brackets by net alone, you are comparing two different trade lists and calling the difference a bracket effect. The clean comparison is the next probe.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 3: same entry, different exit — the matched 49
&lt;/h2&gt;

&lt;p&gt;Forty-nine entries appear in both runs. On those, and only those, the comparison is clean:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Transition (1× stop → 2× stop)&lt;/th&gt;
&lt;th&gt;Trades&lt;/th&gt;
&lt;th&gt;Base net&lt;/th&gt;
&lt;th&gt;New net&lt;/th&gt;
&lt;th&gt;Delta&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;STOP → LIMIT&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;−$408.42&lt;/td&gt;
&lt;td&gt;+$881.45&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$1,289.86&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;STOP → TIME&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;−$1,324.72&lt;/td&gt;
&lt;td&gt;+$306.24&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$1,630.96&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;STOP → STOP&lt;/td&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;−$2,687.68&lt;/td&gt;
&lt;td&gt;−$5,612.15&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$2,924.47&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LIMIT → LIMIT&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;+$2,281.97&lt;/td&gt;
&lt;td&gt;+$2,958.64&lt;/td&gt;
&lt;td&gt;+$676.67&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TIME → TIME&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;+$168.58&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;STOP → OPEN&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Common-entry total&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;49&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$1,722.15&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$880.55&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$841.60&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is the real price of widening the stop, with the entry-stream confound removed. Thirteen trades escaped the stop — 4 became limits, 9 became time exits — and those 13 are worth +$2,920.82. But the 18 stops that still fired got $2,924.47 &lt;em&gt;worse&lt;/em&gt;, almost exactly cancelling the escape. The net gain on identical entries is +$841.60, and $676.67 of that is the 12 limit trades that were limits in both runs but held longer and won more — the same accidental-improvement mechanism as Probe 1, now measured on matched trades.&lt;/p&gt;

&lt;p&gt;So the honest sentence is: &lt;strong&gt;widening the stop bought 13 escapes for $2,924.47, and the escapes were worth $2,920.82.&lt;/strong&gt; A wash, plus a little. The remaining +$841.60 is mostly limit trades drifting further before they hit the same target — a second-order effect, not a stop effect at all.&lt;/p&gt;

&lt;p&gt;One trade deserves a line of its own. Trade #2 is the worst trade in &lt;em&gt;both&lt;/em&gt; runs: −$508.83, identical to the cent. In the base run it gapped through the 1× stop; in this run the bar opened below the 2× stop as well, so it filled at the same open. A gap that jumps your stop jumps the wider stop too. Widening the stop protects you from the drift-through stops, not from the gap-through ones — and the gap-through ones are the expensive ones.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 4: the gap-through shrank — the one unambiguous win
&lt;/h2&gt;

&lt;p&gt;Of the 18 stops at 2×ATR, only 3 filled at the exit bar's open (vs 18 of 38 at 1×ATR). The extra loss vs the level the script actually set:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Gap fills&lt;/th&gt;
&lt;th&gt;Extra vs level&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Stop exits&lt;/td&gt;
&lt;td&gt;3 of 18&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$227.75&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Limit exits&lt;/td&gt;
&lt;td&gt;0 of 16&lt;/td&gt;
&lt;td&gt;$0.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Net gap effect&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;3 of 34&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$227.75&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Against #13's −$1,094.28, that is the one clean improvement in this post: $866.53 of gap-through cost gone, because a wider stop sits further from the open and fewer bars open past it. The verification holds too — every non-gap bracket fill in this run lands exactly on the level computed from the previous bar's ATR (distance 0.0000 ATR), same as #13.&lt;/p&gt;

&lt;p&gt;But note the scale: the gap saving ($866.53) is smaller than the extra loss on the 18 stops that still fired ($2,924.47). The gap win is real and it is not enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Count exits by reason before tuning anything.&lt;/strong&gt; (From #13 — still the first rule. The stop count halved and the story is still in the exit split, not the net.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A bracket change is an entry change.&lt;/strong&gt; If a wider exit keeps you in trades longer, it blocks entries. Report the entry count alongside the exit count, or the net comparison is fiction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Match trades before claiming an effect.&lt;/strong&gt; Same entry, different exit is the only clean comparison. The headline delta (+$1,692.12) was half entry-stream luck; the matched delta (+$841.60) is the bracket's actual effect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Price the escape, don't just count it.&lt;/strong&gt; Thirteen trades escaped the stop — and the eighteen that didn't cost $2,924.47 more. An escape count without the price of the stops that remain is marketing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The stop is the strategy until proven otherwise.&lt;/strong&gt; At 1×ATR the stop was 65.5% of exits; at 2×ATR it is 37.5% and still the largest loss column. Widen it again and the count falls further — but the average loss doubles again, and the entry stream thins further. There is a width where the stop stops being an exit and becomes a hope.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What this post gives you, and what it doesn't
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This post gives you:&lt;/strong&gt; the full exit-by-reason count at a 2×ATR stop on 4,089 daily bars (16 limit / 18 stop / 14 time), the matched-trade decomposition that separates the bracket effect (+$841.60) from the entry-stream effect ($850.53 of skipped trades), and the gap-through measured in dollars ($227.75, down from $1,094.28).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This post does &lt;em&gt;not&lt;/em&gt; give you:&lt;/strong&gt; a better bracket, or a verdict on RSI mean reversion. All variants lose money; the Profit Factor crawled from 0.587 to 0.856 and is still below 1.0, still below buy &amp;amp; hold. It also doesn't test the question #13's accidental finding raised — what happens when the limit is removed entirely and the 10-bar time exit runs the bracket alone, since the time exit keeps absorbing the trades the other exits give up. That is the next post, and the setup will be fixed before I look at the result: same script, no limit, stop 2×ATR, 10-bar time exit.&lt;/p&gt;

&lt;p&gt;Next in the series: the limit removed — stop 2×ATR and the time exit alone, counting whether the limit was doing anything at all.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Do you tune your stop by the exit count, or by the net — and have you ever checked whether the change also ate your entries?&lt;/strong&gt; I read every comment — the exit-split stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>trading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
    <item>
      <title>38 of 58 Exits Were the Stop. Widening the Target Fixed Nothing.</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Thu, 19 Nov 2026 11:17:26 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/38-of-58-exits-were-the-stop-widening-the-target-fixed-nothing-2nd5</link>
      <guid>https://dev.to/leichenbuilds/38-of-58-exits-were-the-stop-widening-the-target-fixed-nothing-2nd5</guid>
      <description>&lt;h1&gt;
  
  
  38 of 58 Exits Were the Stop. Widening the Target Fixed Nothing.
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Post #12 ended with a question: how often does the 2×ATR limit fire first versus the 1×ATR stop, and what happens when the limit widens? I counted every exit by reason on the daily feed. The stop fired 38 of 58 times and never won once — all 38 exits lost a combined −$6,153.94. Widening the limit from 2× to 4×ATR moved the net only from −$2,572.67 to −$1,998.77, because the limit wins didn't grow — they turned into time exits. And 18 of those 38 stops filled below the stop level itself, costing an extra $1,094.28. The bracket's problem was never the target. It was the stop.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Limit&lt;/th&gt;
&lt;th&gt;Closed trades&lt;/th&gt;
&lt;th&gt;Limit exits&lt;/th&gt;
&lt;th&gt;Stop exits&lt;/th&gt;
&lt;th&gt;Time exits&lt;/th&gt;
&lt;th&gt;Net profit&lt;/th&gt;
&lt;th&gt;Profit Factor&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2×ATR&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;38&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;−$2,572.67&lt;/td&gt;
&lt;td&gt;0.587&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3×ATR&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;39&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;−$2,290.95&lt;/td&gt;
&lt;td&gt;0.639&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4×ATR&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;39&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;−$1,998.77&lt;/td&gt;
&lt;td&gt;0.700&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The setup: one number changed, nothing else
&lt;/h2&gt;

&lt;p&gt;The script is post #5's fixed RSI mean-reversion strategy, byte-identical apart from the limit multiplier — the compiled &lt;code&gt;.py&lt;/code&gt; for each variant diffs clean against the 2× version except for one number (&lt;code&gt;atrVal * 2&lt;/code&gt; → &lt;code&gt;* 3&lt;/code&gt; → &lt;code&gt;* 4&lt;/code&gt;) and the title string. Zero diagnostics from the local compiler on all three. Same 4,089 daily bars of CSI 300 (2010-01-04 → 2026-11-06), same $10,000, same PyneCore patched runtime, no commission, no slippage. The entries are identical across all three runs — all 59 entries, same bars, same prices — so every difference below is purely an exit difference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if inTrade
    strategy.exit("Exit Long", "Long", limit=strategy.position_avg_price + atrVal * 2, stop=strategy.position_avg_price - atrVal)
    if barsInTrade &amp;gt;= maxHold
        strategy.close("Long", comment="Time exit")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing the script does that is easy to miss: the bracket levels are recomputed from the &lt;em&gt;current&lt;/em&gt; bar's ATR every bar. The level that actually fills you is set on the bar where the exit order is placed — the previous bar's ATR, not the entry bar's. I verified that directly: every non-gap bracket fill in all three runs lands exactly on the level computed from the previous bar's ATR (distance 0.0000 ATR), and the fills that don't match are the ones that opened beyond the level. That check is what makes the counting below exact instead of approximate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you're checking here:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How often does each exit reason actually fire — limit, stop, or time?&lt;/li&gt;
&lt;li&gt;When you widen the limit, do the stops go away, or does something else absorb the trades?&lt;/li&gt;
&lt;li&gt;How much of the loss is the stop level itself, and how much is filling &lt;em&gt;past&lt;/em&gt; it?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Probe 1: the exit split at 2×ATR — the stop is the strategy
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Exit reason&lt;/th&gt;
&lt;th&gt;Trades&lt;/th&gt;
&lt;th&gt;Wins&lt;/th&gt;
&lt;th&gt;Net&lt;/th&gt;
&lt;th&gt;Avg per trade&lt;/th&gt;
&lt;th&gt;Avg hold&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Limit (2×ATR)&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$2,735.40&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;+$195.39&lt;/td&gt;
&lt;td&gt;3.2 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop (1×ATR)&lt;/td&gt;
&lt;td&gt;38&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$6,153.94&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;−$161.95&lt;/td&gt;
&lt;td&gt;2.7 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time (10 bars)&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;+$845.87&lt;/td&gt;
&lt;td&gt;+$140.98&lt;/td&gt;
&lt;td&gt;10.0 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Read that table as a strategy, not as a bracket. Two out of three trades (38 of 58, 65.5%) end at the stop, and the stop has never once produced a winner — by construction it can't, since the fill is always below the entry. The limit is perfect in its own small way (14 for 14, +$195.39 average), and the time exit is quietly the second-best exit in the system (5 of 6 wins). But neither matters, because the stop fires more than twice as often as the other two combined, and its average loss (−$161.95) is nearly the size of the limit's average win (+$195.39).&lt;/p&gt;

&lt;p&gt;The shape of the loss is also in the table: the stop fires fast (2.7 bars on average) while the limit takes 3.2. The strategy doesn't bleed slowly. It gets stopped out quickly, over and over, and the occasional limit win can't cover 38 small cuts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 2: widen the limit 2× → 3× → 4× — the stops don't move
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;2×ATR&lt;/th&gt;
&lt;th&gt;3×ATR&lt;/th&gt;
&lt;th&gt;4×ATR&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Limit exits&lt;/td&gt;
&lt;td&gt;14 (+$2,735.40)&lt;/td&gt;
&lt;td&gt;8 (+$2,095.68)&lt;/td&gt;
&lt;td&gt;2 (+$551.56)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop exits&lt;/td&gt;
&lt;td&gt;38 (−$6,153.94)&lt;/td&gt;
&lt;td&gt;39 (−$6,258.25)&lt;/td&gt;
&lt;td&gt;39 (−$6,578.53)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time exits&lt;/td&gt;
&lt;td&gt;6 (+$845.87)&lt;/td&gt;
&lt;td&gt;11 (+$1,871.62)&lt;/td&gt;
&lt;td&gt;17 (+$4,028.20)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Net&lt;/td&gt;
&lt;td&gt;−$2,572.67&lt;/td&gt;
&lt;td&gt;−$2,290.95&lt;/td&gt;
&lt;td&gt;−$1,998.77&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Profit Factor&lt;/td&gt;
&lt;td&gt;0.587&lt;/td&gt;
&lt;td&gt;0.639&lt;/td&gt;
&lt;td&gt;0.700&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Win rate&lt;/td&gt;
&lt;td&gt;19 / 58 (32.8%)&lt;/td&gt;
&lt;td&gt;17 / 58 (29.3%)&lt;/td&gt;
&lt;td&gt;17 / 58 (29.3%)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Widening the limit does exactly one thing: it moves trades from the limit column to the time-exit column. The limit count falls 14 → 8 → 2, the time-exit count rises 6 → 11 → 17, and the stop count sits at 38 → 39 → 39, essentially frozen. The net improves by $574 across the whole doubling, and the Profit Factor crawls from 0.587 to 0.700 — still below 1.0, still losing, still below buy &amp;amp; hold (+$1,468.22 on the same span).&lt;/p&gt;

&lt;p&gt;The interesting cell is the one nobody tunes for: at 4×ATR, the time exit is the best exit in the system — 17 trades, 15 wins, +$4,028.20 net, +$236.95 average. It wins by accident. The trades that used to hit the 2× limit now survive to bar 10, and bar 10 turns out to be a better exit than the limit was. That is not a finding about the time exit. It is a finding about how little the limit was doing: remove it, and the strategy gets &lt;em&gt;better&lt;/em&gt;, because the 10-bar close catches what the limit caught, plus a little more.&lt;/p&gt;

&lt;p&gt;Two honesty notes before this table persuades anyone. First, the limit multiplier was chosen after seeing the 2× result, so this is exploration, not a confirmation — the same post-hoc label post #12 put on the weekly test. Second, the win rate &lt;em&gt;falls&lt;/em&gt; as the limit widens (32.8% → 29.3%): fewer wins, but bigger ones. A wider target that "improves" the net while cutting the win rate is not a better strategy. It is the same strategy with its wins re-labeled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 3: the stop level is not the fill price
&lt;/h2&gt;

&lt;p&gt;Of the 52 bracket exits at 2×ATR, 19 filled at the exit bar's &lt;em&gt;open&lt;/em&gt;, not at the limit or stop level — the bar opened beyond the level and PyneCore filled at the open. Eighteen of those were stops. The gap-through cost, measured against the level the script actually set:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Gap fills&lt;/th&gt;
&lt;th&gt;Extra vs level&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Stop exits&lt;/td&gt;
&lt;td&gt;18 of 38&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;−$1,094.28&lt;/strong&gt; (filled below the stop)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Limit exits&lt;/td&gt;
&lt;td&gt;1 of 14&lt;/td&gt;
&lt;td&gt;+$41.32 (filled above the limit)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Net gap effect&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;19 of 52&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−$1,052.96&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;$1,094.28 is 42.5% of the strategy's entire net loss. Nearly half of what this strategy loses is not the bracket design — it is the market opening past the stop. The worst single case is trade #3: the stop sat at 2945.17, the bar opened at 2835.88, and the fill was 109.29 points below the stop — an extra $327.87 on that one trade alone, at 3 contracts.&lt;/p&gt;

&lt;p&gt;The same mechanism runs in reverse on the weekly feed, and it rewrites post #12's headline trade. Trade #12 — the +$1,937.34 week in September 2024 that was 61.5% of the weekly net — filled $410.84 &lt;em&gt;above&lt;/em&gt; its limit level: the bar gapped up through the limit and filled at the open. $1,232.53 of that trade's profit is gap, not bracket. Without it, trade #12 is +$704.81 and the weekly net is +$1,919.99, not +$3,152.52. The weekly "win" is still a win without the gap — but it is a much smaller one, and the reader of post #12 deserved that number in the first paragraph, not in a follow-up post. Consider this the correction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Count exits by reason before tuning anything.&lt;/strong&gt; A bracket is three exits, not one. If you only look at the net, you will tune the exit that fires least.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The stop is the strategy.&lt;/strong&gt; When 65.5% of your exits are stops, the stop &lt;em&gt;is&lt;/em&gt; the strategy — the entry and the limit are decorations on it. Tune the stop (wider? different basis?) before touching the target.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A stop level is not a fill price.&lt;/strong&gt; On daily bars, 18 of 38 stops filled past the level. Report the gap-through as its own line item, in dollars, before quoting the bracket's "risk".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch for the exit that wins by accident.&lt;/strong&gt; At 4×ATR the time exit became the best exit in the system without anyone designing it that way. An accidental best exit is a question, not a feature: it means the intended exit wasn't doing the job.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The level is set by the previous bar's ATR, not the entry's.&lt;/strong&gt; If you backtest a bracket by hand from entry prices, use the ATR of the bar before the exit bar — that's the level that actually fills. Using the entry bar's ATR left one of my early classifications 1.57 ATR away from the fill; the previous bar's ATR lands at 0.0000.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What this post gives you, and what it doesn't
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This post gives you:&lt;/strong&gt; the full exit-by-reason count for one real strategy on 4,089 daily bars (14 limit / 38 stop / 6 time), the same count at 3× and 4× limits, and the gap-through measured in dollars — $1,094.28 of extra stop loss on daily bars, and a $1,232.53 gap windfall inside post #12's headline weekly trade.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This post does &lt;em&gt;not&lt;/em&gt; give you:&lt;/strong&gt; a better bracket, or a verdict on RSI mean reversion. All three variants lose money; widening the target is not a fix, it is a re-labeling. It also doesn't test the obvious next question — what happens when the &lt;em&gt;stop&lt;/em&gt; widens to 2×ATR, since the stop is the exit doing all the damage. That is the next post, and the multiplier will be chosen before I look at the result.&lt;/p&gt;

&lt;p&gt;Next in the series: the stop side under the microscope — same script, stop widened to 2×ATR, counting whether the stop count finally moves.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;How do you count your exits — by reason, or just by win and loss?&lt;/strong&gt; I read every comment — the exit-split stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>trading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
    <item>
      <title>The Same Losing Strategy Made $3,153 on Weekly Bars. Twelve Trades Say Don't Believe It.</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Wed, 18 Nov 2026 11:19:15 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/the-same-losing-strategy-made-3153-on-weekly-bars-twelve-trades-say-dont-believe-it-5h3i</link>
      <guid>https://dev.to/leichenbuilds/the-same-losing-strategy-made-3153-on-weekly-bars-twelve-trades-say-dont-believe-it-5h3i</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Post #11 ended with a question: does the RSI mean-reversion bracket survive a timeframe change? Same script, same symbol, zero code changes — only the feed switched from 4,089 daily bars to 862 weekly bars. The strategy flips from -$2,572.67 (PF 0.587) to +$3,152.52 (PF 2.235). But it only traded 12 times in 16 years, and one trade — a single week in late September 2024 — made $1,937.34, 61.5% of the entire net. The flip is real, and it is not a finding.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Daily (#5 / #11)&lt;/th&gt;
&lt;th&gt;Weekly (this post)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bars&lt;/td&gt;
&lt;td&gt;4,089 (2010-01-04 → 2026-11-06)&lt;/td&gt;
&lt;td&gt;862 (2010-01-08 → 2026-11-06)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Closed trades&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;12&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Net profit&lt;/td&gt;
&lt;td&gt;-$2,572.67&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$3,152.52 (+31.53%)&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Profit Factor&lt;/td&gt;
&lt;td&gt;0.587&lt;/td&gt;
&lt;td&gt;2.235&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Win rate&lt;/td&gt;
&lt;td&gt;19 / 58 (32.8%)&lt;/td&gt;
&lt;td&gt;6 / 12 (50%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Avg hold&lt;/td&gt;
&lt;td&gt;3.6 bars&lt;/td&gt;
&lt;td&gt;3.6 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Buy &amp;amp; hold, same span&lt;/td&gt;
&lt;td&gt;+$1,468.22 (+14.68%)&lt;/td&gt;
&lt;td&gt;+$1,649.79 (+16.50%)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The setup: one feed changed, nothing else
&lt;/h2&gt;

&lt;p&gt;The script is post #5's fixed version, byte-identical — the compiled &lt;code&gt;.py&lt;/code&gt; for this run diffs clean against the daily run's output, zero diagnostics from the local compiler. The only difference is the feed: the weekly bars of the same CSI 300 index, 862 bars, same $10,000, same PyneCore patched runtime, no commission, no slippage. The exit is still price levels, not tick distances:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if inTrade
    entryPrice = strategy.position_avg_price
    strategy.exit("Exit Long", "Long", limit=entryPrice + atrVal * 2, stop=entryPrice - atrVal)
    if barsInTrade &amp;gt;= maxHold
        strategy.close("Long", comment="Time exit")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The question is never "does it run on weekly bars" — of course it runs. The question is whether the exit logic &lt;em&gt;means the same thing&lt;/em&gt; when one bar is a week instead of a day. A 2×ATR limit and a 1×ATR stop are priced in points, and a weekly bar's ATR is a different animal from a daily bar's. Same code, different contract.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you're checking here:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does the strategy actually get better on weekly bars, or does the trade count just collapse?&lt;/li&gt;
&lt;li&gt;How much of the weekly profit is one trade?&lt;/li&gt;
&lt;li&gt;What does the bracket do differently when each bar carries five days of movement?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Probe 1: the flip is real — and the sample is twelve
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Daily&lt;/th&gt;
&lt;th&gt;Weekly&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Trades (closed)&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Net profit&lt;/td&gt;
&lt;td&gt;-$2,572.67&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$3,152.52&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gross profit / gross loss&lt;/td&gt;
&lt;td&gt;— / —&lt;/td&gt;
&lt;td&gt;$5,704.65 / $2,552.13&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Profit Factor&lt;/td&gt;
&lt;td&gt;0.587&lt;/td&gt;
&lt;td&gt;2.235&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Win rate&lt;/td&gt;
&lt;td&gt;32.8%&lt;/td&gt;
&lt;td&gt;50% (6 / 12)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Avg win / avg loss&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;+$950.78 / -$425.36&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Avg hold, winners / losers&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;5.7 bars / 1.5 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max equity drawdown&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;$1,148.21 (10.52%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Buy &amp;amp; hold&lt;/td&gt;
&lt;td&gt;+$1,468.22&lt;/td&gt;
&lt;td&gt;+$1,649.79&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The weekly version beats buy &amp;amp; hold by roughly 2× (+31.53% vs +16.50%) while the daily version lost money against the same benchmark. On the headline numbers, the timeframe change looks like it fixed the strategy.&lt;/p&gt;

&lt;p&gt;Then count the trades. Twelve, over sixteen years. Six winners, six losers. A 50% win rate on twelve trades has a confidence interval wide enough to drive a truck through — flip two trades and the story reverses. And the entry rate per bar is nearly identical on both feeds (12 signals in 862 weekly bars vs 58 in 4,089 daily bars, ~1.4% of bars in both cases): the signal didn't get rarer per bar, there are just five times fewer bars. The strategy didn't get smarter. It got fewer chances to be wrong — and fewer chances to be right.&lt;/p&gt;

&lt;p&gt;There is also a six-year silence in the middle: no entry at all between 2012-02-24 and 2018-07-20, and nothing after 2024-09-27. A strategy that trades twelve times in sixteen years and sleeps for six of them is not a strategy you can evaluate on its equity curve; it is a strategy you evaluate on its individual trades. So let's do that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 2: one trade is 61.5% of the net
&lt;/h2&gt;

&lt;p&gt;The twelve trades, in order:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Entry&lt;/th&gt;
&lt;th&gt;Exit&lt;/th&gt;
&lt;th&gt;Bars&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;th&gt;How it exited&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2010-07-16&lt;/td&gt;
&lt;td&gt;2010-08-20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;+$918.06&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2011-10-21&lt;/td&gt;
&lt;td&gt;2011-10-28&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;-$567.56&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;2011-11-04&lt;/td&gt;
&lt;td&gt;2011-11-25&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;-$389.85&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;2012-01-20&lt;/td&gt;
&lt;td&gt;2012-02-24&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;+$981.20&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;2018-07-20&lt;/td&gt;
&lt;td&gt;2018-08-03&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;-$418.26&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;2022-04-08&lt;/td&gt;
&lt;td&gt;2022-04-15&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;-$352.06&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;2022-05-27&lt;/td&gt;
&lt;td&gt;2022-07-01&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;+$702.06&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;2022-11-11&lt;/td&gt;
&lt;td&gt;2023-01-13&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;+$591.94&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;2024-01-05&lt;/td&gt;
&lt;td&gt;2024-01-12&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;-$310.98&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;2024-02-02&lt;/td&gt;
&lt;td&gt;2024-02-08&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;-$513.42&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;2024-02-23&lt;/td&gt;
&lt;td&gt;2024-04-30&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;+$574.05&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Time exit&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;2024-09-27&lt;/td&gt;
&lt;td&gt;2024-09-30&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$1,937.34 (+20.18%)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Bracket&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Trade #12 entered 2024-09-27 and exited one weekly bar later, +20.18% — the late-September 2024 stimulus rally week, when the whole index ripped. That single bar is $1,937.34 of the $3,152.52 net. Remove it and the weekly net falls to &lt;strong&gt;+$1,215.18&lt;/strong&gt;; remove it from the bracket exits and their net falls from +$2,578.47 to +$641.13. The "weekly version wins" headline is, to first order, a headline about one week in September 2024.&lt;/p&gt;

&lt;p&gt;The rest of the table is less flattering than the PF suggests. Four of the six losers exit after a single bar — the 1×ATR stop gets hit almost immediately, at -$311 to -$568 a time. The winners take their time (5.7 bars on average vs 1.5 for losers), which is the one genuinely good shape in the data: the bracket lets winners run and cuts losers fast. But with six winners, that shape is a description of six trades, not a property of the rule.&lt;/p&gt;

&lt;p&gt;And the time exit — the hero of post #5, the subject of post #11's whole sensitivity check — fired exactly once in twelve trades (+$574.05, trade #11, the only trade that survived the full 10-bar cap). Post #11 concluded the cap barely matters on daily bars; on weekly bars it matters even less, because almost nothing survives ten weeks. The maxHold debate from #11 is closed by this run, not by argument: the rule fired once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 3: what the bracket actually does on weekly bars
&lt;/h2&gt;

&lt;p&gt;The average hold is 3.6 bars on both feeds — the same number, a different duration. On daily bars that's 3.6 days; on weekly bars it's 3.6 weeks, roughly 18 trading days. The bracket's distances are priced in ATR points, and weekly ATR is far wider than daily ATR, so the 2×ATR limit sits much further from the entry in price terms. The strategy holds through moves that would have stopped it out on daily bars, and when the stop does hit, it hits after a full week of adverse movement — hence losers averaging -$425 while daily losers were smaller and more frequent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where weekly shines:&lt;/strong&gt; fewer, slower trades; the bracket's asymmetry (winners 5.7 bars, losers 1.5) finally shows up instead of being drowned in 58 noisy exits; the strategy beats buy &amp;amp; hold instead of losing to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where weekly hurts:&lt;/strong&gt; twelve trades is not a sample, it's a list. One week is 61.5% of the profit. A six-year gap with no signals means the strategy is untestable for most of the sample. And the same code that lost -$2,573 on daily bars "wins" here — which means the timeframe, not the logic, is doing the work, and the timeframe was picked after seeing the daily result. That is selection, and it should be labeled as such.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to check before you trust a timeframe change
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Count the trades first, read the equity curve second.&lt;/strong&gt; Twelve trades cannot support a PF of 2.235 as a claim. The headline number is the least informative cell in the table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find the biggest trade and remove it.&lt;/strong&gt; If the verdict changes when one trade leaves, the verdict belongs to that trade. Here it doesn't flip the sign (+$1,215.18 without #12) — but it cuts the net by 61.5%, and that deserves to be in the first paragraph, not a footnote.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the hold in calendar time, not bars.&lt;/strong&gt; "3.6 bars" sounds identical on both feeds. It is 3.6 days on one and 3.6 weeks on the other. Any exit priced in points (ATR multiples, fixed stops) changes meaning when the bar widens.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check whether the rule you argued about still exists.&lt;/strong&gt; The maxHold cap fired once in twelve trades. A sensitivity check on a rule that fires once is a check on one trade.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Say which decision was made after seeing the result.&lt;/strong&gt; The weekly test was chosen because the daily version lost. That doesn't invalidate it, but it means this is an exploration, not a confirmation — and it needs a second symbol or a second timeframe to become one.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A timeframe flip is a new strategy until proven otherwise.&lt;/strong&gt; Same code, different bar width, different exit distances in points, different hold in days. Test it as if you had never seen the daily result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One trade over 50% of the net gets named in the headline paragraph.&lt;/strong&gt; Not buried in a table. If a single week made the strategy, the reader should know before the equity curve.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Report the gap, not just the trades.&lt;/strong&gt; Six years with no signal is data: it tells you how often the strategy is even in the market.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Beats buy &amp;amp; hold" needs the trade count next to it.&lt;/strong&gt; +31.53% vs +16.50% sounds like a verdict. With 12 trades, it is a suggestion.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What this post gives you, and what it doesn't
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This post gives you:&lt;/strong&gt; the same script on weekly bars, trade by trade, with the one-trade dominance measured (+$1,937.34 of +$3,152.52) — plus a five-point checklist for any timeframe change you run yourself (count trades first, remove the biggest one, convert bars to days, check the rule still fires, label post-hoc choices).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This post does &lt;em&gt;not&lt;/em&gt; give you:&lt;/strong&gt; a verdict on weekly RSI mean reversion, or on this strategy. Twelve trades, one symbol, no costs — that is enough to retire the idea that "weekly fixes it", not enough to claim weekly works. It also does not test whether a different cap, a different RSI length, or a different symbol would change the answer; each of those is its own post, and each would need to be chosen before seeing the result.&lt;/p&gt;

&lt;p&gt;Next in the series: the bracket itself under the microscope — on the daily feed, how often does the 2×ATR limit fire first versus the 1×ATR stop, and what does the exit split look like when the limit widens? Same script, no new code, just counting what the exit actually does.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What timeframe change flipped one of your strategies from losing to winning — and how many trades was it really?&lt;/strong&gt; I read every comment — the measurement stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>trading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
    <item>
      <title>The Time Exit Fired 6 Times and Made $846. Then I Changed One Number.</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Tue, 17 Nov 2026 11:20:53 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/the-time-exit-fired-6-times-and-made-846-then-i-changed-one-number-ce9</link>
      <guid>https://dev.to/leichenbuilds/the-time-exit-fired-6-times-and-made-846-then-i-changed-one-number-ce9</guid>
      <description>&lt;h1&gt;
  
  
  The Time Exit Fired 6 Times and Made $846. Then I Changed One Number.
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Post #5 fixed the RSI strategy's exit bracket and found a small hero inside the wreckage: the 10-bar time exit fired 6 times, won 5 of them, and made +$845.87 while the strategy overall lost -$2,572.67. Post #5 ended with a warning of its own: "Six observations is not a finding." This post tests that warning. Same script, same feed, one input changed — &lt;code&gt;maxHold&lt;/code&gt; at 5, 10, and 20. At 5 the time exit fires 18 times and &lt;em&gt;loses&lt;/em&gt; -$110.68. At 20 it fires once and loses -$98.04. The total barely moves across all three caps ($122.60 from best to worst). The +$845.87 was a property of the number 10, not of the rule.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This post is not about finding the best &lt;code&gt;maxHold&lt;/code&gt;. It is about checking whether a rule that fired six times deserves the paragraph post #5 gave it. The test is cheap — one input, three runs — and it is the test post #5's own rule 4 demands: vary the parameter before you praise the rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup: #5's script, one input changed
&lt;/h2&gt;

&lt;p&gt;The script is post #5's fixed version, unchanged except the &lt;code&gt;maxHold&lt;/code&gt; default. The exit uses price levels, not tick distances:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if inTrade
    entryPrice = strategy.position_avg_price
    strategy.exit("Exit Long", "Long", limit=entryPrice + atrVal * 2, stop=entryPrice - atrVal)
    if barsInTrade &amp;gt;= maxHold
        strategy.close("Long", comment="Time exit")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three runs, tonight, 2026-11-17, same feed as every post in this series (CSI 300 daily, 4,089 bars, 2010-01-04 to 2026-11-06), same $10,000, same PyneCore patched runtime, no commission, no slippage. The only difference between the runs is the &lt;code&gt;maxHold&lt;/code&gt; default: 5, 10, 20. Each run was compiled fresh with the local compiler — zero diagnostics, all three — and the &lt;code&gt;maxHold = 10&lt;/code&gt; run reproduces post #5 byte-identically: same trade list, same -$2,572.67. That reproduction is the control; the other two caps are the experiment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you're checking here:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does the total move when the cap moves — or is the cap just along for the ride?&lt;/li&gt;
&lt;li&gt;Does the time exit's +$845.87 survive a different cap, or does it evaporate?&lt;/li&gt;
&lt;li&gt;Do the same trades behave the same way at every cap, or does the cap change which trades exist at all?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Probe 1: the total barely moves
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;maxHold 5&lt;/th&gt;
&lt;th&gt;maxHold 10 (#5)&lt;/th&gt;
&lt;th&gt;maxHold 20&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Trades (closed)&lt;/td&gt;
&lt;td&gt;57&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Net profit&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;-$2,450.07&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;-$2,572.67&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;-$2,496.75&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Profit Factor&lt;/td&gt;
&lt;td&gt;0.554&lt;/td&gt;
&lt;td&gt;0.587&lt;/td&gt;
&lt;td&gt;0.600&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Win rate&lt;/td&gt;
&lt;td&gt;22 / 57 (38.6%)&lt;/td&gt;
&lt;td&gt;19 / 58 (32.8%)&lt;/td&gt;
&lt;td&gt;18 / 58 (31.0%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average hold&lt;/td&gt;
&lt;td&gt;2.8 bars&lt;/td&gt;
&lt;td&gt;3.6 bars&lt;/td&gt;
&lt;td&gt;4.0 bars&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Buy &amp;amp; hold, same span&lt;/td&gt;
&lt;td&gt;+$1,468.22 (+14.68%)&lt;/td&gt;
&lt;td&gt;+$1,468.22 (+14.68%)&lt;/td&gt;
&lt;td&gt;+$1,468.22 (+14.68%)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The spread from best to worst net is &lt;strong&gt;$122.60&lt;/strong&gt; — about 5% of the loss, on a $10,000 account, over 16 years. The Profit Factor moves from 0.554 to 0.600, and every version loses. Doubling the cap from 10 to 20 adds 0.4 bars to the average hold; halving it to 5 subtracts 0.8. The bracket still does almost all the exiting at every cap: even at 20, the average trade lasts 4 bars, and only one trade in the whole run reaches the 20-bar cap at all.&lt;/p&gt;

&lt;p&gt;The honest reading is that the cap is not the strategy. A parameter that moves the total by $122 out of a -$2,500 loss is a parameter you can stop arguing about. That is itself a finding — just not the one a parameter sweep is usually run to produce.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 2: the hero exit evaporates
&lt;/h2&gt;

&lt;p&gt;The time exit, broken out by cap:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;maxHold 5&lt;/th&gt;
&lt;th&gt;maxHold 10 (#5)&lt;/th&gt;
&lt;th&gt;maxHold 20&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Time exits fired&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;18&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;6&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time-exit wins&lt;/td&gt;
&lt;td&gt;10 / 18&lt;/td&gt;
&lt;td&gt;5 / 6&lt;/td&gt;
&lt;td&gt;0 / 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time-exit net&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;-$110.68&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+$845.87&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;-$98.04&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bracket-exit net&lt;/td&gt;
&lt;td&gt;-$2,339.39 (39 trades)&lt;/td&gt;
&lt;td&gt;-$3,418.54 (52 trades)&lt;/td&gt;
&lt;td&gt;-$2,398.71 (57 trades)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;At cap 10 the time exit looks like the one good rule in a bad strategy: 5 of 6 winners, +$845.87. At cap 5 the same rule fires three times as often — 18 times, 10 winners — and loses -$110.68. At cap 20 it gets one chance in 58 trades and loses -$98.04. The rule did not change. The number did. The +$845.87 was a 6-trade artifact of the cap value 10, exactly as post #5's rule 4 warned it might be: "Six observations is not a finding."&lt;/p&gt;

&lt;p&gt;Note the shape of the failure, because it is the general lesson. A time exit only ever fires on trades the bracket &lt;em&gt;didn't&lt;/em&gt; kill first. At cap 5, the survivors are different trades than at cap 10 — the rule is sampling a different, larger set of stragglers, and that set loses money. At cap 20, almost nothing survives 20 bars, so the rule barely exists. The rule's record is not a property of the rule; it is a property of which trades happen to outlive the bracket at that particular cap. Change the cap and you change the sample, and the verdict flips with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 3: the same trade, three caps
&lt;/h2&gt;

&lt;p&gt;The trade-by-trade comparison makes the point concrete. Most trades are identical at every cap — the bracket fires first and the cap never matters. The ones that differ are the survivors, and they do not agree with each other:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Entry&lt;/th&gt;
&lt;th&gt;maxHold 5&lt;/th&gt;
&lt;th&gt;maxHold 10&lt;/th&gt;
&lt;th&gt;maxHold 20&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2010-02-04&lt;/td&gt;
&lt;td&gt;+$30.78 (time exit)&lt;/td&gt;
&lt;td&gt;+$137.16 (time exit)&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;+$367.19&lt;/strong&gt; (time exit)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2015-07-10&lt;/td&gt;
&lt;td&gt;+$107.49 (time exit)&lt;/td&gt;
&lt;td&gt;+$338.93 (time exit)&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;-$225.30&lt;/strong&gt; (bracket)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2016-02-01&lt;/td&gt;
&lt;td&gt;-$101.22 (time exit)&lt;/td&gt;
&lt;td&gt;+$289.78 (time exit)&lt;/td&gt;
&lt;td&gt;+$321.84 (time exit)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2025-04-10&lt;/td&gt;
&lt;td&gt;+$11.92 (time exit)&lt;/td&gt;
&lt;td&gt;+$76.66 (time exit)&lt;/td&gt;
&lt;td&gt;+$224.90 (time exit)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Longer holds help the 2010 trade (+$31 → +$367) and hurt the 2015 trade (+$339 → -$225). There is no monotone pattern to optimize — no direction in which "longer is better" or "shorter is better" holds across trades. That is what a sensitivity check is for: if the answer depended cleanly on the cap, one of the three columns would dominate, and it doesn't.&lt;/p&gt;

&lt;p&gt;One second-order effect worth stating plainly. The cap changes not just exits but &lt;em&gt;which later trades exist&lt;/em&gt;: the 2018-07-10 entry appears at caps 10 and 20 and not at cap 5, because at cap 5 the strategy was still holding the previous trade when that signal fired, and the entry rule (&lt;code&gt;longSignal and not inTrade&lt;/code&gt;) skipped it. A parameter that only touches exits still reshapes the entry list downstream. Any sweep that counts "the same N trades" at every parameter value is counting a different set of trades and should say so.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A rule that fired six times gets a sensitivity check before it gets a paragraph.&lt;/strong&gt; Vary the parameter first, praise it second. If the verdict flips when the parameter moves, the verdict belonged to the parameter, not the rule.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Report every value you tried, not the best one.&lt;/strong&gt; All three caps are in the table above, including the worst. A sweep that only reports the winning cap is a different kind of post, and not one this series writes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A sensitivity check is not an optimization.&lt;/strong&gt; No cap is crowned here, because the totals differ by $122 and all three lose. "The cap doesn't matter much" is a legitimate conclusion — it tells you where &lt;em&gt;not&lt;/em&gt; to spend your next test.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count what the parameter changes downstream.&lt;/strong&gt; The cap moved exits, and the exits moved which later entries were taken. If your sweep assumes the trade list is fixed, check that assumption against the trade list.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Six observations is not a finding — and now it is a measured one.&lt;/strong&gt; Post #5 said it as a warning. This post measured it: the same rule, +$846 at one cap, -$111 at another, -$98 at a third.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What this post gives you, and what it doesn't
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This post gives you:&lt;/strong&gt; a three-number sensitivity check on one real rule, with the trade-by-trade table that shows why the headline flips — plus a check you can run on your own strategy in an afternoon (pick the parameter your best rule depends on, run it at half and double, report all three).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This post does &lt;em&gt;not&lt;/em&gt; give you:&lt;/strong&gt; a verdict on time exits in general, or on RSI mean reversion. Three caps on one symbol, one timeframe, 57–58 trades, no costs — that is enough to retire one small hero story, not enough to conclude time exits never work. It also does not prove the cap is irrelevant everywhere: on a strategy whose bracket fires less often, the cap would matter more, and that is a different test on a different strategy.&lt;/p&gt;

&lt;p&gt;Next in the series: the same fixed strategy on the weekly feed — does the bracket survive a timeframe change, or does the exit logic fall apart when the bars get five times wider? Same script, same symbol, 862 weekly bars, no new code.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What rule in your strategy has only fired a handful of times — and have you varied its parameter to check it's real?&lt;/strong&gt; I read every comment — the measurement stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>algotrading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
    <item>
      <title>Same Pine Script, Two Backtests, $1,480 Apart: What request.security() Actually Returns</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Mon, 16 Nov 2026 11:20:52 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/same-pine-script-two-backtests-1480-apart-what-requestsecurity-actually-returns-3h7c</link>
      <guid>https://dev.to/leichenbuilds/same-pine-script-two-backtests-1480-apart-what-requestsecurity-actually-returns-3h7c</guid>
      <description>&lt;h1&gt;
  
  
  Same Pine Script, Two Backtests, $1,480 Apart: What request.security() Actually Returns
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; One line — &lt;code&gt;lookahead=barmerge.lookahead_on&lt;/code&gt; plus &lt;code&gt;[1]&lt;/code&gt; — moved a backtest from +$5,209.86 to +$6,690.14 (Profit Factor 2.51 → 3.02, in our testing) on the same strategy, same data, same 23 trades. The "confirmed" version looks better. It is also not reading what its Pine says it reads: the value it returns is, on 93% of bars, the &lt;em&gt;daily close of the first trading day of the current week&lt;/em&gt; — not the previous weekly close. The translator compiled all of it with zero diagnostics. That is the point: the dangerous case is not the script that fails, it is the script that compiles cleanly and returns a different number than the one you asked for.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the tenth post in a series where I convert Pine strategies to Python line by line. The last post was the version-to-version table — 84 errors became 5 across three recorded stages. This one opens the biggest family in that fix ledger: &lt;code&gt;request.security()&lt;/code&gt;, 25 files, one construct. Seventeen of those files were tuple literals, eight were scalar expressions, and the batch moved compile from 1,827 to 1,850 of 1,911 scripts. The construct compiles now. The question this post answers is what it &lt;em&gt;returns&lt;/em&gt; — because a translator that compiles your multi-timeframe filter and quietly changes what the filter reads has not finished the job.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup: one strategy, two filters, one line apart
&lt;/h2&gt;

&lt;p&gt;The strategy is deliberately boring: a daily EMA 10/30 crossover on the CSI 300, long only, 10% of equity per trade, &lt;code&gt;strategy.close&lt;/code&gt; on the crossunder. The only difference between the two variants is the weekly filter — the daily cross only fires when the weekly close is above the weekly SMA 20.&lt;/p&gt;

&lt;p&gt;Variant A — the developing value, the default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wClose = request.security(syminfo.tickerid, "W", close)
wSma = request.security(syminfo.tickerid, "W", ta.sma(close, 20))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Variant B — the "confirmed" value, the pattern every guide recommends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wClose = request.security(syminfo.tickerid, "W", close[1], lookahead=barmerge.lookahead_on)
wSma = request.security(syminfo.tickerid, "W", ta.sma(close, 20)[1], lookahead=barmerge.lookahead_on)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The data is the same feed every post in this series uses: CSI 300 daily, 4,089 bars, 2010-01-04 to 2026-11-06, with a weekly feed resampled from that same daily file (862 weekly bars — same price path, so every security value in this post is checkable against the daily file by hand). The runtime is the open-source PyneCore project, with the patched build we ship for the two constructs stock PyneCore doesn't handle. Everything below was run tonight, 2026-11-16, on the local machine — nothing in this post is quoted from documentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 1: the scalar request works, and I can prove it bar by bar
&lt;/h2&gt;

&lt;p&gt;The compiler output for the scalar probe, verbatim — this is what the translator emits, and what the runtime then runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;wClose&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;security&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;syminfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tickerid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"W"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;wSma&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;security&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;syminfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tickerid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"W"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zero diagnostics. And the values check out against the hand-computed weekly resample, bar for bar:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Check&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;First non-NaN weekly close&lt;/td&gt;
&lt;td&gt;Row 5, 2010-01-11 — the first bar of week 2. The first week has no &lt;em&gt;confirmed&lt;/em&gt; weekly bar yet, so there is nothing to return.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Weekly close, 2010-01-11&lt;/td&gt;
&lt;td&gt;3480.13 — exactly the close of the week ending 2010-01-08 in the resampled file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Weekly close / SMA 20, 2020-06-01&lt;/td&gt;
&lt;td&gt;3867.02 / 3914.76 — both match the hand-computed weekly series exactly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Last bar, 2026-11-06&lt;/td&gt;
&lt;td&gt;Weekly close reads 3985.50 — the &lt;em&gt;previous&lt;/em&gt; week's close, not 4054.28, the current week's final close&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That last row is the documented &lt;code&gt;lookahead_off&lt;/code&gt; behaviour, and it is worth stating plainly because it is the behaviour the whole "confirmed" pattern exists to work around: with the default, a daily bar never sees the weekly bar it belongs to. It sees the last one that finished. On the final bar of the sample the current week never "finishes" inside the run, so the value lags one week. That is not a bug. It is the price of not looking ahead — and the next section is about what happens when you try to stop paying it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 2: the tuple request returns the same numbers, in one call
&lt;/h2&gt;

&lt;p&gt;The fix ledger said 17 files were tuple literals. Here is what the translator does with one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[wSma, wRsi] = request.security(syminfo.tickerid, "W", [ta.sma(close, 20), ta.rsi(close, 14)])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;_pyne_br0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;security&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;syminfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tickerid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"W"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;wSma&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_pyne_br0&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;wRsi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_pyne_br0&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The single request returns both values, and the tuple's SMA is byte-for-byte the scalar probe's SMA: &lt;strong&gt;0 differing rows out of 4,089&lt;/strong&gt;. The RSI runs 22.66 to 92.1 across 4,015 non-NaN bars — a sane range for a weekly RSI 14 on this index. The tuple path, the part that used to fail analysis on 17 scripts, now returns exactly the numbers the two separate requests return. That is the check the fix ledger never had room for: not "it compiles", but "it returns the same values as the scalar form".&lt;/p&gt;

&lt;h2&gt;
  
  
  Probe 3: gaps_on is mostly NaN, and that is the documented behaviour
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gOff = request.security(syminfo.tickerid, "W", close, gaps=barmerge.gaps_off)
gOn = request.security(syminfo.tickerid, "W", close, gaps=barmerge.gaps_on)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The two columns differ on &lt;strong&gt;3,227 of 4,089 rows&lt;/strong&gt;. &lt;code&gt;gaps_on&lt;/code&gt; is NaN on 3,232 bars; &lt;code&gt;gaps_off&lt;/code&gt; is NaN on 5. The 857 non-NaN values in the &lt;code&gt;gaps_on&lt;/code&gt; column are exactly the 857 distinct weekly closes — one value per week, NaN on every other daily bar. That is what &lt;code&gt;gaps_on&lt;/code&gt; means: no new confirmed weekly value, no value at all. If you plot a &lt;code&gt;gaps_on&lt;/code&gt; series expecting a line, you get 857 dots. If you feed it into a condition expecting a filter, you get a filter that is false four days out of five. The translator emits both forms with zero diagnostics, and the difference between them is entirely in the runtime — which is why this probe exists: the compile step cannot tell you which one you wanted.&lt;/p&gt;

&lt;h2&gt;
  
  
  The two backtests: same 23 trades, $1,480 apart
&lt;/h2&gt;

&lt;p&gt;Same strategy, same data, same 4,089 bars. The only difference is the filter — developing (A) versus "confirmed" (B).&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;A: developing (&lt;code&gt;lookahead_off&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;B: "confirmed" (&lt;code&gt;[1]&lt;/code&gt; + &lt;code&gt;lookahead_on&lt;/code&gt;)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Trades&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wins&lt;/td&gt;
&lt;td&gt;10 (43.5%)&lt;/td&gt;
&lt;td&gt;11 (47.8%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Net profit&lt;/td&gt;
&lt;td&gt;+$5,209.86&lt;/td&gt;
&lt;td&gt;+$6,690.14&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Profit Factor&lt;/td&gt;
&lt;td&gt;2.515&lt;/td&gt;
&lt;td&gt;3.023&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max equity drawdown&lt;/td&gt;
&lt;td&gt;2.09%&lt;/td&gt;
&lt;td&gt;1.89%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sharpe&lt;/td&gt;
&lt;td&gt;0.308&lt;/td&gt;
&lt;td&gt;0.380&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Buy &amp;amp; hold, same window&lt;/td&gt;
&lt;td&gt;+$14,682.21&lt;/td&gt;
&lt;td&gt;+$14,682.21&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Three things to notice before the better number does any persuading.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First, both variants lose to doing nothing.&lt;/strong&gt; Buy and hold on the same index over the same window returns +$14,682.21 — roughly double the better variant. The filter is not a money machine; it is a filter. The honest number in this table is the buy-and-hold row, and it is there so nobody reads the PF 3.02 as a recommendation. A translator moves your logic. It does not make it profitable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second, the better number comes from two extra trades, not from better timing.&lt;/strong&gt; The first three trades are identical. The sequences diverge at trade 4: variant B takes a trade on &lt;strong&gt;2012-12-13 (+$1,164.52)&lt;/strong&gt; that variant A never takes, adds another on &lt;strong&gt;2019-08-26 (+$189.22)&lt;/strong&gt;, and drops two of A's trades (2021-12-08, +$6.10; 2026-02-10, −$132.64). The arithmetic reconciles exactly: 1,164.52 + 189.22 − 6.10 + 132.64 = 1,480.28, the full net difference. The two variants re-sync at the last two trades (2026-04-15 and 2026-06-17, identical in both). So the $1,480 is not a slow accumulation of small edges — it is two entries that exist in one backtest and not in the other, both gated by the weekly filter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Third, the filter that "wins" is the one whose values I cannot fully explain.&lt;/strong&gt; Which is the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the "confirmed" value actually is
&lt;/h2&gt;

&lt;p&gt;Here is the emitted Python for variant B, verbatim:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;wClose&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;security&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;syminfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tickerid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"W"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lookahead&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;barmerge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lookahead_on&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;_pyne_h0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;wSma&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;security&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;syminfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tickerid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"W"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_pyne_h0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lookahead&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;barmerge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lookahead_on&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read those three lines carefully. &lt;code&gt;close[1]&lt;/code&gt; survives inline — the identifier history offset makes it through. But &lt;code&gt;ta.sma(close, 20)[1]&lt;/code&gt; does not: the translator hoists the call into &lt;code&gt;_pyne_h0 = ta.sma(close, 20)&lt;/code&gt; and passes the plain series in, and &lt;strong&gt;the &lt;code&gt;[1]&lt;/code&gt; is gone&lt;/strong&gt;. The Pine asked for the previous weekly bar's SMA. The Python asks for the SMA, no offset. That is a silent change — zero diagnostics, clean compile, different value — and it is visible only if you read the emitted code instead of the Pine.&lt;/p&gt;

&lt;p&gt;Then I measured what the runtime actually returns for the &lt;code&gt;[1]&lt;/code&gt; that &lt;em&gt;did&lt;/em&gt; survive. Take one week, June 2020, and read the values off the run:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Daily bar&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;cOff&lt;/code&gt; (developing)&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;cOn&lt;/code&gt; ("confirmed")&lt;/th&gt;
&lt;th&gt;What &lt;code&gt;cOn&lt;/code&gt; actually is&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2020-05-29 (Fri)&lt;/td&gt;
&lt;td&gt;3824.06&lt;/td&gt;
&lt;td&gt;3829.32&lt;/td&gt;
&lt;td&gt;Daily close of 2020-05-25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2020-06-01 (Mon)&lt;/td&gt;
&lt;td&gt;3867.02&lt;/td&gt;
&lt;td&gt;3829.32&lt;/td&gt;
&lt;td&gt;Daily close of 2020-05-25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2020-06-02 (Tue)&lt;/td&gt;
&lt;td&gt;3867.02&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;3971.34&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Daily close of 2020-06-01&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2020-06-05 (Fri)&lt;/td&gt;
&lt;td&gt;3867.02&lt;/td&gt;
&lt;td&gt;3971.34&lt;/td&gt;
&lt;td&gt;Daily close of 2020-06-01&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2020-06-08 (Mon)&lt;/td&gt;
&lt;td&gt;4001.25&lt;/td&gt;
&lt;td&gt;3971.34&lt;/td&gt;
&lt;td&gt;Daily close of 2020-06-01&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The previous weekly close on 2020-06-02 is 3867.02 — the close of the week ending 2020-05-29. The "confirmed" value that day is 3971.34, which appears in no weekly bar at all: it is the &lt;em&gt;daily&lt;/em&gt; close of Monday 2020-06-01. Across the full sample, &lt;code&gt;cOn&lt;/code&gt; equals the daily close of the first trading day of the current week (the previous week's first day, if today is that first day) on &lt;strong&gt;3,802 of 4,088 bars (93.0%)&lt;/strong&gt;. The remaining 286 bars follow a pattern I have not identified — they are spread across all years and weekdays, and they are not float32 noise. I am not going to invent an explanation for them.&lt;/p&gt;

&lt;p&gt;And the SMA leg is worse: &lt;code&gt;sOn&lt;/code&gt; matches &lt;strong&gt;none&lt;/strong&gt; of the candidate series I checked — not the weekly SMA 20, not the daily SMA 20 at any shift, not any daily SMA or EMA window from 5 to 250, not the SMA of the &lt;code&gt;cOn&lt;/code&gt; series, not the weekly SMA with a developing bar. It changes on 4,003 of 4,088 day boundaries, so it is a daily-frequency series, but it is not any standard average of the closes I can construct. I report it as unidentified, not as a known quantity.&lt;/p&gt;

&lt;p&gt;So the honest summary of variant B is this: its close leg returns Monday's daily close on 93% of bars and something unidentified on 7%, and its SMA leg returns a daily-frequency series I cannot identify at all — while its Pine says "previous weekly close" and "previous weekly SMA". The variant that looks better by $1,480 is built on a filter that is not reading what its author wrote. That does not make the $1,480 fake — the trades are real rows in a real trade list — but it does make the &lt;em&gt;reason&lt;/em&gt; for the $1,480 unknowable from the Pine alone. A backtest that beats the alternative for a reason you cannot name is the exact thing the "confirmed" pattern was supposed to protect you from.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Read the emitted code for every &lt;code&gt;request.security()&lt;/code&gt; call.&lt;/strong&gt; The Pine is the intent; the Python is the behaviour. If a &lt;code&gt;[1]&lt;/code&gt; or a &lt;code&gt;lookahead&lt;/code&gt; does not survive the trip, the backtest is measuring something else — and the compiler will not warn you, because the call itself is valid.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never put a history offset on a function call inside &lt;code&gt;request.security()&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;close[1]&lt;/code&gt; survives; &lt;code&gt;ta.sma(close, 20)[1]&lt;/code&gt; loses the offset when the call is hoisted. Compute the indicator, take the offset, then request — or request first and offset the returned series on the chart side, where the &lt;code&gt;[1]&lt;/code&gt; is visible in the emitted code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify a security value against a hand-computed series before trusting a strategy that uses it.&lt;/strong&gt; One column, one resample, one equality check: the scalar probe in this post took four lines of Python and caught the one case (the last bar lagging a week) that the documentation describes but that is easy to forget.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;gaps_on&lt;/code&gt; is a filter that is false four days out of five.&lt;/strong&gt; Count the NaNs before you use it in a condition. If you wanted a step series, you wanted &lt;code&gt;gaps_off&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A better backtest number is not evidence until the values behind it are identified.&lt;/strong&gt; Variant B wins by $1,480 on values I cannot fully name. Until the SMA leg is identified, the honest statement is "B returned more on this sample", not "the confirmed filter is better".&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Next in the series: &lt;a href="https://dev.to/leichenbuilds/the-time-exit-fired-6-times-and-made-846-then-i-changed-one-number-ce9"&gt;The Time Exit Fired 6 Times and Made $846. Then I Changed One Number.&lt;/a&gt; — post #5 found a small hero in the fixed bracket (the 10-bar time exit, 6 fires, +$845.87); this post varies &lt;code&gt;maxHold&lt;/code&gt; to 5, 10, and 20 and checks whether that hero survives.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What does your &lt;code&gt;request.security()&lt;/code&gt; call actually return — and when did a "confirmed" value turn out to be something else?&lt;/strong&gt; I read every comment — the measurement stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>algotrading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
    <item>
      <title>84 Errors Became 5: The Version-to-Version Table for a Pine-to-Python Translator</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Sun, 15 Nov 2026 11:17:25 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/84-errors-became-5-the-version-to-version-table-for-a-pine-to-python-translator-261</link>
      <guid>https://dev.to/leichenbuilds/84-errors-became-5-the-version-to-version-table-for-a-pine-to-python-translator-261</guid>
      <description>&lt;h1&gt;
  
  
  84 Errors Became 5: The Version-to-Version Table for a Pine-to-Python Translator
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; A translator that went from 84 failed scripts to 5 did not get 16 times better in one fix. It got better in three recorded stages — 84 → 13 → 7 → 5 — across nine compile checkpoints and eight run checkpoints, with a per-script diff after every batch proving nothing that used to pass started failing. The headline number, 99.7% in our testing, is the least interesting part. The table is the evidence — and the table includes the one number that got worse, and the one sweep that lied.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the ninth post in a series where I convert Pine strategies to Python line by line. The last post was about a single fix — the &lt;code&gt;when=&lt;/code&gt; desugar — measured trade for trade. This one steps back to the sweep that produced all those fixes, because a single percentage hides the only question that matters: did it get better, version by version, without anything silently getting worse?&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compile:&lt;/strong&gt; 1,827 of 1,911 scripts compiled on 2026-10-21 (95.6%), with &lt;strong&gt;84&lt;/strong&gt; analyze failures. By the v45 baseline on 2026-10-24: &lt;strong&gt;1,904 of 1,909 (99.7%)&lt;/strong&gt;, with &lt;strong&gt;5&lt;/strong&gt; failures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run:&lt;/strong&gt; clean runs went from 1,859 to &lt;strong&gt;1,887 of 1,904 (99.1%)&lt;/strong&gt;, and run errors fell from 45 files to &lt;strong&gt;17&lt;/strong&gt; — 9 unique scripts in 7 families.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The three stages:&lt;/strong&gt; 84 → 13 (the big construct families) → 7 (three two-file fixes) → 5 (one overload fix). The last three fixes were two files each.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The number that got worse:&lt;/strong&gt; diagnostics rose from 41 to 55 while failures fell. That is not a regression — it is the price of compiling more scripts, and the post explains why.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zero regressions is a measured claim, not a boast:&lt;/strong&gt; after every batch, a per-script diff (&lt;code&gt;ok → error&lt;/code&gt; must be 0) ran before anything was committed. The totals never moved without the diff agreeing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The sweep that lied:&lt;/strong&gt; one full sweep reported &lt;code&gt;ran_ok&lt;/code&gt; 1,428 — invalid data, deleted, baseline reset. The table below is what survived the rule that caught it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The compile table: nine checkpoints, one direction
&lt;/h2&gt;

&lt;p&gt;Each row is a recorded checkpoint from the project log. The "what changed" column is the batch that moved the number; the delta is the sweep total, not a promise about the next batch.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Checkpoint&lt;/th&gt;
&lt;th&gt;Compiled&lt;/th&gt;
&lt;th&gt;Failures&lt;/th&gt;
&lt;th&gt;What changed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-10-21 evening baseline&lt;/td&gt;
&lt;td&gt;1,827 / 1,911&lt;/td&gt;
&lt;td&gt;84&lt;/td&gt;
&lt;td&gt;Starting point: 95.6%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;security&lt;/code&gt; batch&lt;/td&gt;
&lt;td&gt;1,850 / 1,911&lt;/td&gt;
&lt;td&gt;61&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;request.security()&lt;/code&gt; tuple literals (17 files) and scalar expressions (8)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;for-in&lt;/code&gt; batch&lt;/td&gt;
&lt;td&gt;1,858 / 1,911&lt;/td&gt;
&lt;td&gt;53&lt;/td&gt;
&lt;td&gt;Iterator typing for &lt;code&gt;for...in&lt;/code&gt; (8)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;namespace batch&lt;/td&gt;
&lt;td&gt;1,874 / 1,911&lt;/td&gt;
&lt;td&gt;37&lt;/td&gt;
&lt;td&gt;Namespace field access (16) + UDT method calls&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;continuation batch&lt;/td&gt;
&lt;td&gt;1,890 / 1,911&lt;/td&gt;
&lt;td&gt;21&lt;/td&gt;
&lt;td&gt;Line-continuation and indentation parsing (8)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;chart.point&lt;/code&gt; batch&lt;/td&gt;
&lt;td&gt;1,894 / 1,911&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;chart.point&lt;/code&gt; field reassignment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;table.cell&lt;/code&gt; batch&lt;/td&gt;
&lt;td&gt;1,896 / 1,911&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;&lt;code&gt;table.cell_set_text_formatting&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;batch 2 (§16.7)&lt;/td&gt;
&lt;td&gt;1,902 / 1,909&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Tuple-switch tail returns, drawing kwargs, chained receivers (2 files each)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;batch 2b (§16.8)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1,904 / 1,909&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;int[]&lt;/code&gt;/&lt;code&gt;float[]&lt;/code&gt; overload dispatch on &lt;code&gt;012_volume-profile-maps&lt;/code&gt; (2 files)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Three things to notice before the numbers do any persuading:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First, the denominator moved.&lt;/strong&gt; The corpus went from 1,911 files to 1,909 because two illegal inputs were quarantined out of the corpus entirely (M4). Those two were never "fixed" — they were removed from what counts. So the failure count fell by 79, and that 79 is exactly 77 scripts newly compiling plus 2 files removed from the corpus. If you only read the percentage, 95.6% → 99.7%, you miss the two files that left through the side door. They are in the log; they are not hidden.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second, the fix ledger and the checkpoints disagree by two files — and the post says so.&lt;/strong&gt; The per-construct fix log lists twelve groups totalling 81 files — 17 + 16 + 12 + 8 + 8 + 8 + 2 + 2 + 2 + 2 + 2 across eleven fix groups, plus the 2 quarantined inputs — against a net failure drop of 79. The two-file gap is the difference between counting fixes and counting files: the ledger counts per construct, so a file touched by two fixes counts twice, and the corpus holds duplicate copies of the same script under different folders. The ledger is a fix log, not a partition of the total. The sweep totals are the numbers that reconcile; the ledger is the map of what was touched. A translator that hides a two-file gap in its own accounting is exactly the kind of tool you should not trust — so here is the gap, in the open.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Third, the fixes got smaller.&lt;/strong&gt; The early batches moved 23 and 16 files at a time. The last three fixes moved two files each. That is what the end of a fix journey looks like: not a breakthrough, but a narrowing. When the remaining failures are three unique scripts — two waiting on external evidence before anyone touches the runtime, one waiting on a library import — the work has moved out of the translator and into the scripts, the data, and the runtime contract underneath.&lt;/p&gt;

&lt;h2&gt;
  
  
  The run table: compiling is not running
&lt;/h2&gt;

&lt;p&gt;A script can compile cleanly and still fail bar by bar. The sweep ran every compiled script, so there is a second table — and it is the one with the stricter claim, because a run either finishes or it does not.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Clean runs&lt;/th&gt;
&lt;th&gt;Run errors&lt;/th&gt;
&lt;th&gt;Unique scripts&lt;/th&gt;
&lt;th&gt;Families&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;v28&lt;/td&gt;
&lt;td&gt;1,859&lt;/td&gt;
&lt;td&gt;45&lt;/td&gt;
&lt;td&gt;25&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v33&lt;/td&gt;
&lt;td&gt;1,861&lt;/td&gt;
&lt;td&gt;43&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v34&lt;/td&gt;
&lt;td&gt;1,873&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v35&lt;/td&gt;
&lt;td&gt;1,875&lt;/td&gt;
&lt;td&gt;29&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v36&lt;/td&gt;
&lt;td&gt;1,880&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v37&lt;/td&gt;
&lt;td&gt;1,882&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v39&lt;/td&gt;
&lt;td&gt;1,884&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v44&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1,887&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;17&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;9&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;7&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;v45 is identical to v44 on every metric — its only change was a &lt;code&gt;matrix&lt;/code&gt; field annotation, and the generated code did not change by a single byte (&lt;code&gt;changed python 0&lt;/code&gt;). A version that changes nothing measurable is still a row in the table, because a table that only shows the versions that improved is a brochure, not a log. The same goes for v38, a measurement-only batch whose generated code was byte-identical to v37: the numbers did not move, and the log says so instead of skipping the version.&lt;/p&gt;

&lt;p&gt;The family count fell from 17 to 7, and the shape of that fall is the finding: families 2, 4, and 5 disappeared entirely (a feed session gap, three strategies fixed by a runtime patch, a missing 12M feed), while the seven that remain are, as post #3 tabulated, scripts that are broken on TradingView too, scripts that are not supposed to run on this feed, and one structural limit around history inside helper functions. The fixable bucket is empty — that was #3's conclusion, and this table is the version-by-version proof of how it got emptied.&lt;/p&gt;

&lt;h2&gt;
  
  
  The number that got worse: diagnostics 41 → 55
&lt;/h2&gt;

&lt;p&gt;Here is the row a brochure would delete. While failures fell from 84 to 5, the diagnostic count — warnings attached to scripts that compile but are not fully, faithfully translated — rose from 41 to 55.&lt;/p&gt;

&lt;p&gt;That is not a regression. It is arithmetic with a cause: 77 scripts that previously failed analysis now compile, and 14 of the diagnostics they carry came in with them. The single largest group, 47 of the 55, is history reads inside module-level helpers (&lt;code&gt;zigzag&lt;/code&gt;, &lt;code&gt;renderStructures&lt;/code&gt;, &lt;code&gt;backfill&lt;/code&gt; — a helper that reads &lt;code&gt;high[1]&lt;/code&gt; from inside a function, where the call site can only pass plain values). Those scripts used to fail outright; now they compile with a warning that says exactly where the translation stops being faithful. A warning is better than a failure, and a failure is better than silence — the ranking matters, and the diagnostic count going up while the failure count goes down is what that ranking looks like in numbers.&lt;/p&gt;

&lt;p&gt;The check that it is not a regression is the companion number: scripts translating with zero diagnostics rose from 1,806 to &lt;strong&gt;1,879&lt;/strong&gt; (98.4% in our testing). Both numbers moved in the honest direction at once — more scripts fully clean, more warnings on the ones that are not. If diagnostics had risen while the zero-diagnostic count fell, that would be a real regression, and this post would say so.&lt;/p&gt;

&lt;p&gt;There is a second number that "improved" without anything getting better, and it deserves the same treatment. When the two illegal inputs were quarantined, the compile rate went from 1,896/1,911 (99.2%) to 1,896/1,909 (99.3%) — the rate rose 0.1 points and not one script got better. Denominator changes are free percentage points. Any vendor quote that cannot tell you whether its denominator moved is decoration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Zero regressions — and what that claim actually rests on
&lt;/h2&gt;

&lt;p&gt;"Zero regressions" is easy to say and easy to fake: totals can hide a script that broke while another one got fixed. The claim here rests on a tool, not on the totals. After every batch, a per-script diff compared the old and new sweep file by file — compile flips, generated-code hashes, run flips, plot counts — and any &lt;code&gt;ok → error&lt;/code&gt; flip failed the batch with exit code 1. Totals were never accepted without the diff agreeing.&lt;/p&gt;

&lt;p&gt;The recorded diffs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Batch&lt;/th&gt;
&lt;th&gt;&lt;code&gt;ok → error&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;error → ok&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Generated code changed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;v19 → v20 (block scope)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;11 files, all one batch's 6 scripts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v20 → v21 (parameter routing)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;2 (one script, both copies)&lt;/td&gt;
&lt;td&gt;2 files, both the same script&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v24 → v26 (batch 2)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6 files — exactly the three fixed scripts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v26 → v27 (batch 2b)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;2 files — only the fixed script's copies&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;v42 → v44 (strategy namespace)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;3 (three strategies)&lt;/td&gt;
&lt;td&gt;4 files, each explained&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two details in that table are worth more than the zeros. First, v20 → v21 changed a script's error without changing its compile status — one script swapped one runtime error for a clean run, and the diff caught that the plot count moved 0 → 5 on the same script, which was itself investigated before the batch was accepted. Second, batch 2 fixed three scripts whose &lt;code&gt;ok&lt;/code&gt; status did not change at all — and the diff still flagged their six generated files as changed, which is how the next section was found.&lt;/p&gt;

&lt;p&gt;The sample gate ran alongside: 8 out of 8 declared samples, byte-identical output, after every batch. A sample that drifts is a regression the sweep totals would never see.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fixes the table cannot see
&lt;/h2&gt;

&lt;p&gt;Batch 2 (§16.7) is the best argument in this whole post for why totals are not enough. It fixed three scripts — tuple-switch tails, untyped drawing kwargs, chained receivers — and the compile count went up by exactly 6, three scripts times two copies. But the same batch also fixed something the totals never showed: a silent mis-translation in six files whose &lt;code&gt;ok&lt;/code&gt; status was unchanged before and after.&lt;/p&gt;

&lt;p&gt;The bug was in how free methods were registered: by method name only. Two methods with the same name on different owners — &lt;code&gt;line&lt;/code&gt; arrays and &lt;code&gt;label&lt;/code&gt; arrays both calling &lt;code&gt;clear&lt;/code&gt; — overwrote each other, so a &lt;code&gt;line&lt;/code&gt; array silently called the &lt;code&gt;label&lt;/code&gt; version of the delete. It ran fine. It did nothing. No error, no diagnostic, no failed run — a script that compiled, ran, and quietly skipped its own cleanup. The fix routed methods by owner plus name, and the generated code changed in six files that the sweep had been counting as healthy all along.&lt;/p&gt;

&lt;p&gt;That is the honest limit of this post's own table: every number in it counts failures, and a silent wrong value is not a failure. The version-to-version table proves the translator stopped crashing. It cannot prove it stopped being quietly wrong — only the per-file diffs, read one by one, catch that. The six files were caught because someone read the diff instead of the total.&lt;/p&gt;

&lt;h2&gt;
  
  
  The sweep that lied
&lt;/h2&gt;

&lt;p&gt;One row is missing from the run table, and it is missing on purpose: v43. Its full sweep reported &lt;code&gt;ran_ok&lt;/code&gt; 1,428 out of 1,904 — a collapse of nearly 460 scripts in a single version. It was invalid data. During the sweep, the runtime file the workers import was halfway through an edit, so chunk workers loaded a broken module and failed scripts that had nothing wrong with them. The report was deleted, the baseline was reset to v44, and the rule it produced is now written down: &lt;strong&gt;no edits to the runtime or the sweep runner during a full sweep&lt;/strong&gt; — the environment fingerprint is snapshotted at startup and cannot catch a file changed mid-run.&lt;/p&gt;

&lt;p&gt;There was an earlier, quieter version of the same lesson: the output folder &lt;code&gt;out/corpus_gen&lt;/code&gt; once held 1,819 stale generated files while the current version's tree held 1,896. Running a sweep against the stale folder measures a three-day-old translator and reports it as today's. The fix was procedural, not technical — every batch's tree gets its own versioned folder, and the sweep must point at the same tree the report names. Both incidents are in the project log, and both are why this post trusts the table: not because the numbers are good, but because the two times the numbers lied, the lie was caught, recorded, and turned into a rule before the next batch.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a healthy table looks like
&lt;/h2&gt;

&lt;p&gt;Borrowing the calibration idea from this week's reading: a version-to-version table is healthy when every row satisfies all four of these, and any row that fails one of them is investigated before the next batch starts.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Compiled never decreases.&lt;/strong&gt; A version that compiles fewer scripts than the last one is a regression until proven otherwise — the diff, not the author, decides which.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ok → error&lt;/code&gt; is always 0.&lt;/strong&gt; New fixes may turn errors into clean runs; they may never turn a clean run into an error. Exit code 1 enforces it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Every changed file is explainable in one sentence.&lt;/strong&gt; Batch 2 changed six files and all six were the three fixed scripts. A changed file with no explanation is an unexamined change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Diagnostics may rise, but only with a reason attached.&lt;/strong&gt; Newly compiling scripts bring their own warnings. If diagnostics rise on scripts that already compiled, something regressed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The translator's table passes all four across the recorded window — and the two times it did not (the stale tree, the mid-edit sweep), the failures were in the measurement, not the translator, and they are recorded here rather than smoothed over.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A percentage is a snapshot; a table is a claim.&lt;/strong&gt; 99.7% tells you where the translator stands. 84 → 13 → 7 → 5 tells you how it got there, and whether anything broke on the way.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Totals hide one-for-one swaps. Diff per script, every batch.&lt;/strong&gt; A version that fixes one script and breaks another shows up as "no change" in the totals. The diff is the only thing that sees it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count the denominator.&lt;/strong&gt; A rate that rises because files left the corpus is not an improvement. Say which files left and why.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A warning is better than a failure, and a failure is better than silence.&lt;/strong&gt; Diagnostics rising while failures fall is progress, not decay — as long as the zero-diagnostic count rises with it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Record the measurements that lied.&lt;/strong&gt; The v43 sweep and the stale tree are in the log because the next person to run a sweep needs to know how a sweep can lie, not just that this one once did.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next in the series: &lt;a href="https://dev.to/leichenbuilds/same-pine-script-two-backtests-1480-apart-what-requestsecurity-actually-returns-3h7c"&gt;the request.security() deep dive is now published&lt;/a&gt; — the same strategy, two backtests, $1,480 apart, and the 'confirmed' value that isn't.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What does your version-to-version table look like — and when did a total hide a regression from you?&lt;/strong&gt; I read every comment — the measurement stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>algotrading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
    <item>
      <title>The Translator Got Better: One Fix, One Script That Used to Fail, Before and After</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Sat, 14 Nov 2026 11:17:31 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/the-translator-got-better-one-fix-one-script-that-used-to-fail-before-and-after-5090</link>
      <guid>https://dev.to/leichenbuilds/the-translator-got-better-one-fix-one-script-that-used-to-fail-before-and-after-5090</guid>
      <description>&lt;h1&gt;
  
  
  The Translator Got Better: One Fix, One Script That Used to Fail, Before and After
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Your Pine strategy uses &lt;code&gt;strategy.entry(..., when=...)&lt;/code&gt;. The converter passes &lt;code&gt;when=&lt;/code&gt; through verbatim. The output compiles clean — and dies on the first bar with &lt;code&gt;TypeError: entry() got an unexpected keyword argument 'when'&lt;/code&gt;. The fix is four lines of &lt;code&gt;if&lt;/code&gt;. This post measures the before and the after, trade for trade.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the eighth post in a series where I convert Pine strategies to Python line by line. The last post was about 366 scripts that draw and never plot. This one is about a fix: one family of calls, one script that used to fail, and the check that proves the fixed version means the same thing as the Pine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The short version.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Before:&lt;/strong&gt; the converter passed &lt;code&gt;when=&lt;/code&gt; through to PyneCore, which has no &lt;code&gt;when&lt;/code&gt; parameter on any &lt;code&gt;strategy.*&lt;/code&gt; function. The output compiled clean and died on the first bar: &lt;code&gt;TypeError: entry() got an unexpected keyword argument 'when'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;After:&lt;/strong&gt; the same script converts with &lt;strong&gt;zero diagnostics&lt;/strong&gt;. The two &lt;code&gt;when=&lt;/code&gt; calls become &lt;code&gt;if&lt;/code&gt; wrappers, and the run produces &lt;strong&gt;66 trades&lt;/strong&gt; on 4,089 bars.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The check:&lt;/strong&gt; an independent pandas re-implementation of the same rules produces the &lt;strong&gt;identical 66 entry bars and 66 exit bars&lt;/strong&gt; — trade for trade, no exceptions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The gate actually mattered once:&lt;/strong&gt; of 67 EMA crossovers, one was skipped because RSI was 77.28 — 2024-09-26, the sharp rally week. The filter is not decoration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Honest scope:&lt;/strong&gt; this fix shipped in the October build, and it is in the deployed converter today. The numbers below were re-measured tonight on the current compiler. The strategy itself is a demo — 66 trades, profit factor 1.50 in our testing — not a system.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How we tested
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Feed: 4,089 bars of CSI 300 daily data, 2010-01-04 to 2026-11-06, same feed as posts #4–#7.&lt;/li&gt;
&lt;li&gt;Compiler: the local compiler (&lt;code&gt;node bin/pine2pyne.mjs&lt;/code&gt;), &lt;strong&gt;zero diagnostics&lt;/strong&gt; on the test script.&lt;/li&gt;
&lt;li&gt;Runner: the open-source PyneCore runtime (6.10.12). Market orders fill at the next bar's open, the TradingView default.&lt;/li&gt;
&lt;li&gt;Control: a from-scratch pandas implementation of the same rules — Pine's &lt;code&gt;ta.ema&lt;/code&gt; recurrence (seeded with the SMA), Wilder RSI(14), crossover/crossunder, same fill rule. It shares no code with the converter or the runtime.&lt;/li&gt;
&lt;li&gt;No tuning, no picking the good cases afterwards: the script below is the smallest strategy that uses &lt;code&gt;when=&lt;/code&gt; twice — once on entry, once on close.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The before: compiles clean, dies on bar one
&lt;/h2&gt;

&lt;p&gt;Pine v5 lets you gate a strategy call with a &lt;code&gt;when=&lt;/code&gt; argument: fire this call only on the bars where the condition is true. PyneCore has no &lt;code&gt;when&lt;/code&gt; parameter on any &lt;code&gt;strategy.*&lt;/code&gt; function, in any version — and Pine v6 removed the argument entirely. So the old converter did the literal thing: it passed the argument through.&lt;/p&gt;

&lt;p&gt;That output is reproduced below, rebuilt line for line from the current output by putting the &lt;code&gt;when=&lt;/code&gt; arguments back where the old emitter left them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;long&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;longCond&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rsi&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;shortCond&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It compiles clean. Then you run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Traceback (most recent call last):
  ...
  File "when_gate_before.py", line 15, in main
    strategy.entry("Long", strategy.long, when=(longCond and (rsi &amp;lt; 70)))
TypeError: entry() got an unexpected keyword argument 'when'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the whole failure, and it is the worst kind: nothing is wrong with the script, nothing is wrong with the strategy, and the error message names a keyword the user wrote correctly in Pine. A converter that passes through what it cannot translate is not being faithful — it is handing you a program that was never going to run.&lt;/p&gt;

&lt;h2&gt;
  
  
  The after: four lines of &lt;code&gt;if&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=5
strategy("When gate: EMA cross with RSI filter", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
fast = ta.ema(close, 10)
slow = ta.ema(close, 30)
rsi = ta.rsi(close, 14)
longCond = ta.crossover(fast, slow)
shortCond = ta.crossunder(fast, slow)
strategy.entry("Long", strategy.long, when=longCond and rsi &amp;lt; 70)
strategy.close("Long", when=shortCond)
plot(fast, "Fast")
plot(slow, "Slow")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Python is the actual output of the converter, verified tonight with the local compiler (&lt;code&gt;node bin/pine2pyne.mjs&lt;/code&gt;, zero diagnostics). Verbatim:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="s2"&gt;"""
@pyne
"""&lt;/span&gt;

&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pynecore&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pynecore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;

&lt;span class="nd"&gt;@script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"When gate: EMA cross with RSI filter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;initial_capital&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default_qty_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;percent_of_equity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default_qty_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;longCond&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;crossover&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;shortCond&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;crossunder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;longCond&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rsi&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;shortCond&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Long"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Fast"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Slow"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The desugar is exactly Pine's semantics: a &lt;code&gt;strategy.*&lt;/code&gt; call with &lt;code&gt;when=&lt;/code&gt; fires on the bars where the condition is true, and an &lt;code&gt;if&lt;/code&gt; around the call is the same thing. The fix covers seven functions — &lt;code&gt;entry&lt;/code&gt;, &lt;code&gt;exit&lt;/code&gt;, &lt;code&gt;order&lt;/code&gt;, &lt;code&gt;close&lt;/code&gt;, &lt;code&gt;close_all&lt;/code&gt;, &lt;code&gt;cancel&lt;/code&gt;, &lt;code&gt;cancel_all&lt;/code&gt; — and it applies wherever the call sits, including inside &lt;code&gt;if&lt;/code&gt; bodies, because the wrapper lands at the call's own indentation level.&lt;/p&gt;

&lt;h2&gt;
  
  
  The check: three versions, one trade list
&lt;/h2&gt;

&lt;p&gt;Same script, same feed, three ways of running it:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Before (verbatim passthrough)&lt;/th&gt;
&lt;th&gt;After (desugared)&lt;/th&gt;
&lt;th&gt;Independent pandas check&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Entries&lt;/td&gt;
&lt;td&gt;— (dies on bar 1)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;66&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;66, same bars&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exits&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;66&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;66, same bars&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;First entry&lt;/td&gt;
&lt;td&gt;&lt;code&gt;TypeError&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;bar 57, 2010-03-31 @ 3,369.19&lt;/td&gt;
&lt;td&gt;bar 57&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Last exit&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;bar 4007, 2026-07-08 @ 4,804.48&lt;/td&gt;
&lt;td&gt;bar 4007&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gated-out crossovers&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;1 (bar 3579, RSI 77.28)&lt;/td&gt;
&lt;td&gt;1, same bar&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The pattern is simple: the fix changes nothing about the strategy and everything about whether it runs. Every entry bar and every exit bar matches the independent implementation exactly — 66 out of 66 on both sides. That is the claim the fix makes, and the table is the check.&lt;/p&gt;

&lt;p&gt;The trade economics, for the record and not as a recommendation: 66 trades, 26 wins / 40 losses, net &lt;strong&gt;+$5,564.51 (+5.56%)&lt;/strong&gt;, profit factor &lt;strong&gt;1.502&lt;/strong&gt; in our testing. The strategy is a two-line EMA crossover with a filter; the numbers say it is a demo, not a system.&lt;/p&gt;

&lt;p&gt;And the claim-check table, because a fix deserves the same treatment as a strategy:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What the fix claims&lt;/th&gt;
&lt;th&gt;What I checked&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;when=&lt;/code&gt; means "fire only on bars where the condition is true"&lt;/td&gt;
&lt;td&gt;Entry bars equal the pandas signal bars + 1, 66 out of 66&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;strategy.close&lt;/code&gt; with &lt;code&gt;when=&lt;/code&gt; exits only on crossunder bars&lt;/td&gt;
&lt;td&gt;Exit bars equal the pandas crossunder bars + 1, 66 out of 66&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The gate actually filters something&lt;/td&gt;
&lt;td&gt;1 of 67 crossovers skipped — bar 3579, RSI 77.28&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nothing else changes&lt;/td&gt;
&lt;td&gt;Same fill rule, same position sizing, same 4,089-row plot output&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The one trade the gate changed
&lt;/h2&gt;

&lt;p&gt;67 crossovers fired on this feed. 66 became entries. The one that didn't:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bar 3579, 2024-09-26.&lt;/strong&gt; EMA(10) crossed above EMA(30) — fast 3,314.04 vs slow 3,296.37 — but RSI(14) read &lt;strong&gt;77.28&lt;/strong&gt;, above the 70 gate. No entry.&lt;/li&gt;
&lt;li&gt;That week was the sharp September 2024 rally. Whether skipping it helped or hurt is a separate question, and the run does not answer it — the gate is one skipped trade out of 67, not a verdict on RSI filters.&lt;/li&gt;
&lt;li&gt;The point is narrower and more useful: the gate is load-bearing. If the desugar had dropped the condition instead of wrapping it, that entry would have fired, and the trade list would differ by exactly one trade. The check above would have caught it — 66 vs 67 entries — which is why the check exists.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One more count for completeness: 67 crossunders fired, and 66 became exits. The one that didn't fired while flat — a &lt;code&gt;close&lt;/code&gt; with no open position is a no-op in both worlds.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the fix doesn't do
&lt;/h2&gt;

&lt;p&gt;Two boundaries, both measured tonight, both worth knowing before you trust the fix:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The condition has to be named &lt;code&gt;when=&lt;/code&gt;.&lt;/strong&gt; Pass it positionally and the compiler has nothing to recognize:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;strategy.entry("Long", strategy.long, c)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This compiles with zero diagnostics — the boolean lands in the &lt;code&gt;qty&lt;/code&gt; slot — and it runs. What it does is worse than crashing: it enters &lt;strong&gt;1 contract at the first crossover (bar 57) and holds it to the last bar (bar 4088)&lt;/strong&gt;, one trade, +$685.09. No error, no diagnostic, just a strategy that quietly means something else. The old project note said the positional form "errors at runtime"; on the current runtime it doesn't error — it silently mis-trades, which is worse, and the note is corrected here. If your script passes a condition positionally, the converter will not save you. Name the argument.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;when=&lt;/code&gt; in value position is rejected before the emitter ever sees it.&lt;/strong&gt; Try to assign the call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = strategy.entry("Long", strategy.long, when=c)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the analyzer stops the compile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;when_value.pine: analyze failed
  'strategy.entry' call is only supported in statement position (no return value — cannot be called in assignment/expression/argument position) (L6:19)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The emitter also carries a strip-and-diagnose fallback for any form that slips through analysis, but the measured behavior is the analyzer's: the call cannot be a value, so it cannot silently lose its gate inside an expression. Two layers, and the first one fires.&lt;/p&gt;

&lt;p&gt;And the scope note: Pine v6 removed &lt;code&gt;when&lt;/code&gt; entirely, so v6 scripts never hit this — the fix is a v5 bridge. If you are writing new Pine, write the &lt;code&gt;if&lt;/code&gt; yourself; the converter exists for the library you already have.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A clean compile is not a working program.&lt;/strong&gt; The old output compiled. It died on bar one. Compile success is a claim about syntax; run it before you believe anything.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A fix earns a control, not a testimonial.&lt;/strong&gt; "It works now" is one run. The trade list matching an independent implementation bar for bar is the evidence.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count the gate.&lt;/strong&gt; A filter that never fires is decoration. 67 crossovers, 1 gated out — the gate is real, and its one firing is named with a date and a number.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Name the argument.&lt;/strong&gt; The positional form compiles, runs, and silently means something else. Named arguments are not style; they are the difference between a gate and a quantity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Correct the old note in public.&lt;/strong&gt; The project note said the positional form errors at runtime. It doesn't anymore — it silently mis-trades. A stale warning is worse than no warning, because it tells you to watch for the wrong failure.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next in the series: the sweep numbers — 84 errors became 5, across three fixes, with zero regressions. What the version-to-version table actually says about a translator getting better.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What did your translator silently drop first — a gate, a condition, or a whole branch? And did it crash, or did it just run wrong?&lt;/strong&gt; I read every comment — the silent-failure stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>algotrading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
    <item>
      <title>It Drew 238 Lines and the CSV Is Empty: What 366 Drawing-Only Pine Scripts Taught Me</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Fri, 13 Nov 2026 11:20:52 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/it-drew-238-lines-and-the-csv-is-empty-what-366-drawing-only-pine-scripts-taught-me-5h4h</link>
      <guid>https://dev.to/leichenbuilds/it-drew-238-lines-and-the-csv-is-empty-what-366-drawing-only-pine-scripts-taught-me-5h4h</guid>
      <description>&lt;h1&gt;
  
  
  It Drew 238 Lines and the CSV Is Empty: What 366 Drawing-Only Pine Scripts Taught Me
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Your Pine script draws 238 lines, 486 labels, 360 boxes. The Python port runs without errors. The output file is 0 bytes. The drawings are not missing — they are in a different place than you looked.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the seventh post in a series where I convert Pine strategies to Python line by line. The last post was about 196 scripts that ran fine and produced nothing. This one is about the 366 scripts that produce something — just not the kind of something a CSV can hold.&lt;/p&gt;

&lt;p&gt;The headline numbers first, because this is the part you can check:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Script&lt;/th&gt;
&lt;th&gt;Objects created&lt;/th&gt;
&lt;th&gt;Live at end&lt;/th&gt;
&lt;th&gt;What the run CSV holds&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Pivot lines&lt;/td&gt;
&lt;td&gt;238 lines&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 bytes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pivot labels&lt;/td&gt;
&lt;td&gt;486 labels&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 bytes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gap boxes&lt;/td&gt;
&lt;td&gt;360 boxes&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 bytes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dashboard table&lt;/td&gt;
&lt;td&gt;1 table, 4,087 cell updates&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 bytes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One updating ray&lt;/td&gt;
&lt;td&gt;238 lines, 3,808 updates&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 bytes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Control: same feed, one &lt;code&gt;plot()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;4,089 rows (post #6)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The honest number is 0. Five scripts, five kinds of drawing, the same measured result: the standard output file is empty. The rest of this post is where the output actually went — and the checklist I now run before I ever say a drawing-only port "produced nothing."&lt;/p&gt;

&lt;h2&gt;
  
  
  How we tested
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Feed: 4,089 bars of CSI 300 daily data, 2010-01-04 to 2026-11-06, same feed as posts #4–#6.&lt;/li&gt;
&lt;li&gt;Compiler: the local compiler (&lt;code&gt;node bin/pine2pyne.mjs&lt;/code&gt;), &lt;strong&gt;zero diagnostics&lt;/strong&gt; on all five scripts — nothing unsupported, nothing dropped.&lt;/li&gt;
&lt;li&gt;Runner: the open-source PyneCore runtime (6.10.12), with its drawing journal switched on, so every create, update, and delete of every drawing object is recorded bar by bar.&lt;/li&gt;
&lt;li&gt;No tuning, no picking the good cases afterwards: the five scripts below are the five drawing types Pine has — line, label, box, table, and the one-line update idiom — each in its smallest realistic form.&lt;/li&gt;
&lt;li&gt;Every count in this post comes from the journal and the final drawing registries, and every count was cross-checked against an independent pandas calculation on the same feed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  First, what "drawing-only" means — and what it doesn't
&lt;/h2&gt;

&lt;p&gt;The corpus numbers come from the sweep I reported in &lt;a href="https://dev.to/leichenbuilds/i-ran-1909-real-pine-scripts-through-a-translator-heres-what-actually-breaks-24h7"&gt;post #3&lt;/a&gt;: 1,909 real Pine scripts run through the translator, baseline dated 2026-10-24, in our testing. "Drawing-only" has a precise definition there, and the definition matters more than the count:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Drawing-only&lt;/strong&gt; = the script compiled, ran bar-by-bar to completion, and has &lt;strong&gt;no &lt;code&gt;plot()&lt;/code&gt; at all&lt;/strong&gt; — only drawing objects: lines, labels, boxes, tables. 366 of 1,909 scripts, &lt;strong&gt;19.2%&lt;/strong&gt; of the corpus.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drawing-only is not silent.&lt;/strong&gt; The 196 silent scripts from &lt;a href="https://dev.to/leichenbuilds/it-ran-fine-it-produced-nothing-what-196-silent-pine-scripts-taught-me-42nc"&gt;post #6&lt;/a&gt; produced nothing on the test feed — no plot, no drawing, no trigger. The 366 produce drawings and nothing else. Conflating the two is the first mistake; the silent ones got their own post, these get this one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drawing-only is not an error.&lt;/strong&gt; None of the 366 threw anything. A translator that reports "success" on all of them is telling the truth — and still misleading you if you only read the CSV, because the CSV is the wrong place to look.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One scope note, stated plainly: the 366 are corpus files, and the corpus itself lives on my other machine — I can't re-open the individual drawing-only scripts from here tonight. So the five reproductions below are minimal scripts I wrote to isolate each drawing type, compiled with the local compiler and run on the series' usual feed. They are reproductions, not the corpus scripts themselves — but every number they produce was cross-checked against the raw feed, so the counts are measurements, not illustrations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repro 1: pivot lines — 238 drawn, 50 survive
&lt;/h2&gt;

&lt;p&gt;The most common drawing-only shape in the wild is a line at every swing point: support and resistance drawn as horizontal rays. The Pine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=5
indicator("Draw repro 1: pivot lines", overlay=true)
ph = ta.pivothigh(high, 5, 5)
if not na(ph)
    line.new(bar_index - 5, ph, bar_index, ph, extend=extend.right, color=color.new(color.blue, 0))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Python is the actual output of the converter, verified tonight with the local compiler (&lt;code&gt;node bin/pine2pyne.mjs&lt;/code&gt;, zero diagnostics). Verbatim:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pynecore&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pynecore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;bar_index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;na&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;

&lt;span class="nd"&gt;@script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indicator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Draw repro 1: pivot lines"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;ph&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pivothigh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;na&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ph&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bar_index&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ph&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar_index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ph&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Measured on 4,089 bars:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What we counted&lt;/th&gt;
&lt;th&gt;Result, in our testing&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Lines created (&lt;code&gt;line.new&lt;/code&gt; calls that fired)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;238&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lines deleted by the cap&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;188&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lines live at the end&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;50&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;First line ever drawn&lt;/td&gt;
&lt;td&gt;bar 38 @ 3,345.91 — &lt;strong&gt;evicted&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Oldest line still live&lt;/td&gt;
&lt;td&gt;bar 3,228 @ 4,169.67&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Newest line still live&lt;/td&gt;
&lt;td&gt;bar 4,061 @ 4,583.38&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;extend&lt;/code&gt; on every live line&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;right&lt;/code&gt; — preserved&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The control column is the proof, twice over. First, the independent check: pandas finds exactly 238 pivot highs on this feed, the first at bar 38 @ 3,345.91 and the last at bar 4,061 @ 4,583.38 — the same two numbers the journal reports. Second, the arithmetic closes: 238 created − 188 deleted = 50 live, which is exactly the default &lt;code&gt;max_lines_count&lt;/code&gt; of 50. TradingView's rule is that the 51st line deletes the oldest one, first-in-first-out; the Python port enforces the same cap, the same way. The first line this script ever drew is gone in both worlds — not because the port dropped it, but because the cap is the semantics. If your port "loses" old lines, check the cap before you blame the translator.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repro 2: pivot labels — the text is real data
&lt;/h2&gt;

&lt;p&gt;Labels are where a drawing-only script stops being decoration and starts being a data table stapled to the chart. The Pine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=5
indicator("Draw repro 2: pivot labels", overlay=true)
ph = ta.pivothigh(high, 5, 5)
pl = ta.pivotlow(low, 5, 5)
if not na(ph)
    label.new(bar_index - 5, ph, "PH " + str.tostring(ph, format.mintick), style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white)
if not na(pl)
    label.new(bar_index - 5, pl, "PL " + str.tostring(pl, format.mintick), style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Measured: &lt;strong&gt;486 labels created, 436 deleted, 50 live&lt;/strong&gt; — and the arithmetic closes again (486 − 436 = 50, the default &lt;code&gt;max_labels_count&lt;/code&gt;). The independent check: 238 pivot highs + 248 pivot lows = 486, exactly. And the labels carry real values, not placeholders — the first three live ones read &lt;code&gt;"PH 4098.380000"&lt;/code&gt;, &lt;code&gt;"PL 3899.750000"&lt;/code&gt;, &lt;code&gt;"PH 4007.759999"&lt;/code&gt;, and the last one reads &lt;code&gt;"PL 3958.140002"&lt;/code&gt;, which is the final pivot low on the feed (bar 4,078 @ 3,958.14). A label's text is a measurement with a bar attached to it. If you throw the labels away because "they're just drawings," you throw away 486 measurements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repro 3: gap boxes — 360 boxes, same cap
&lt;/h2&gt;

&lt;p&gt;Fair-value-gap and range boxes are the same shape with a different object. The Pine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=5
indicator("Draw repro 3: gap boxes", overlay=true)
gapUp = low &amp;gt; high[1]
gapDn = high &amp;lt; low[1]
if gapUp
    box.new(bar_index - 1, high[1], bar_index, low, bgcolor=color.new(color.green, 90))
if gapDn
    box.new(bar_index - 1, low[1], bar_index, high, bgcolor=color.new(color.red, 90))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Measured: &lt;strong&gt;360 boxes created (182 gap-up + 178 gap-down), 310 deleted, 50 live&lt;/strong&gt; — 360 − 310 = 50, the default &lt;code&gt;max_boxes_count&lt;/code&gt;, and the independent gap count on the feed is 182 + 178 = 360, exactly. Three drawing types, three caps, three times the same story: the port creates every object the script asks for and evicts exactly the ones TradingView would evict. The cap is not a port limitation. It is the chart's memory, implemented in Python.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repro 4: the dashboard table — one object, 4,087 updates
&lt;/h2&gt;

&lt;p&gt;Tables are the odd one out: a drawing-only script that behaves like a dashboard, not a chart. The Pine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=5
indicator("Draw repro 4: dashboard table", overlay=true)
var table dash = table.new(position.top_right, 2, 2)
rsi = ta.rsi(close, 14)
table.cell(dash, 0, 0, "RSI")
table.cell(dash, 1, 0, str.tostring(rsi, "#.##"))
table.cell(dash, 0, 1, "Close")
table.cell(dash, 1, 1, str.tostring(close, format.mintick))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Measured: &lt;strong&gt;1 table created, 4,087 cell updates, 1 table live.&lt;/strong&gt; The &lt;code&gt;var&lt;/code&gt; matters — one table for the whole run, not one per bar — and the port honors it: the emitted &lt;code&gt;Persistent&lt;/code&gt; variable is created once. The final cell values are the check: RSI reads &lt;code&gt;"37.64"&lt;/code&gt; and Close reads &lt;code&gt;"4054.280000"&lt;/code&gt;. Both were verified independently: a pandas Wilder RSI(14) on the same feed ends at 37.64, and the last close is 4,054.28. The table is the one drawing object whose content is a single verifiable number, and it verifies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repro 5: the one updating ray — the idiom that works
&lt;/h2&gt;

&lt;p&gt;Every serious drawing script has one version of this pattern: keep a single line, move its right end to the current bar, replace it when a new pivot arrives. If the port gets this wrong, nothing else matters. The Pine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=5
indicator("Draw repro 5: one updating ray", overlay=true)
var line ray = na
ph = ta.pivothigh(high, 5, 5)
if not na(ph)
    if not na(ray)
        line.delete(ray)
    ray := line.new(bar_index - 5, ph, bar_index, ph, extend=extend.right, color=color.new(color.orange, 0))
if not na(ray)
    line.set_x2(ray, bar_index)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Measured: &lt;strong&gt;238 lines created, 237 deleted, 3,808 updates, 1 line live.&lt;/strong&gt; The arithmetic closes a third way: the ray exists from bar 43 (the first pivot is confirmed) to bar 4,088, which is 4,046 bars; 238 of those bars create a new line, and the other 3,808 update the existing one — 238 + 3,808 = 4,046, exactly. The one surviving line ends at x1 = 4,061 @ 4,583.38 with x2 = 4,088, the last bar index: it points at the last pivot high and reaches the present. The delete–recreate–update idiom works, the &lt;code&gt;var&lt;/code&gt; holds, and &lt;code&gt;line.set_x2&lt;/code&gt; tracks the current bar. This is the positive control for the whole post: the port handles the standard drawing pattern correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  So where is the output? The journal, not the CSV
&lt;/h2&gt;

&lt;p&gt;Here is the part that would have saved me an hour if someone had told me on day one. The runner writes two things: a plot CSV and a drawing journal (NDJSON). A drawing-only script has no plots, so the CSV is 0 bytes — not corrupt, not truncated, just empty, because there is nothing to put in it. The drawings are in the journal: one &lt;code&gt;create&lt;/code&gt; event per object, one &lt;code&gt;update&lt;/code&gt; per &lt;code&gt;set_*&lt;/code&gt; call, one &lt;code&gt;delete&lt;/code&gt; per eviction, plus a final snapshot of everything still live. That journal is the deliverable. It is what a renderer would consume to repaint the chart — coordinates, colors, text, and the full lifecycle of every object.&lt;/p&gt;

&lt;p&gt;Which means the honest answer to "what does a translator owe a drawing-only script" is this: &lt;strong&gt;coordinates plus lifecycle, not pixels.&lt;/strong&gt; The port owes you every object the script created, with its real coordinates and its real text, and an exact record of which ones the cap evicted and when. It does not owe you a picture — there is no canvas in a Python backtest, and any tool that shows you one is rendering the journal for you, not recovering something the translator hid. If your workflow reads only the CSV, a drawing-only port will always look empty. Read the journal.&lt;/p&gt;

&lt;p&gt;Both things are true at once: the port is faithful — 238 lines created, 50 live, every count cross-checked — and the default output file is empty. The contradiction dissolves once you know there are two output channels and drawings live in the second one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Count the drawings before reading the code.&lt;/strong&gt; A run with 0 bytes of CSV and 238 journal events is a different problem from a run with 0 bytes and 0 events. Diagnose the channel first, correctness second.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the cap before blaming the translator.&lt;/strong&gt; &lt;code&gt;max_lines_count&lt;/code&gt;, &lt;code&gt;max_labels_count&lt;/code&gt;, &lt;code&gt;max_boxes_count&lt;/code&gt; all default to 50, in Pine and in the port. Old objects being gone is the semantics, not data loss.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Treat label text as data.&lt;/strong&gt; A label that says &lt;code&gt;"PH 4098.380000"&lt;/code&gt; is a measurement with a bar attached. Verify it against the feed the same way you would verify a plot value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep drawing-only and silent apart.&lt;/strong&gt; 366 scripts draw and never plot; 196 run and produce nothing. Different categories, different fixes — don't let one number stand for both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A reproduction beats an explanation.&lt;/strong&gt; Five small scripts, five runs, one table. Every claim in this post can be re-run from the code printed above — and the journal is the receipt.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next in the series: the translator got better — one fix family, one script that used to fail, before and after.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What's the drawing your port drops first — the line, the label, or the table? And did you find it in the CSV, or somewhere else?&lt;/strong&gt; I read every comment — the lost-drawing stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>pinescript</category>
      <category>python</category>
      <category>algotrading</category>
      <category>backtesting</category>
    </item>
    <item>
      <title>It Ran Fine. It Produced Nothing: What 196 Silent Pine Scripts Taught Me</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Thu, 12 Nov 2026 11:16:27 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/it-ran-fine-it-produced-nothing-what-196-silent-pine-scripts-taught-me-42nc</link>
      <guid>https://dev.to/leichenbuilds/it-ran-fine-it-produced-nothing-what-196-silent-pine-scripts-taught-me-42nc</guid>
      <description>&lt;h1&gt;
  
  
  It Ran Fine. It Produced Nothing: What 196 Silent Pine Scripts Taught Me
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Your Pine script compiles. Your Python port runs without errors. The output file is empty. "It ran" and "it works" are different claims — and in a corpus of 1,909 real scripts, 196 of them ran fine and produced nothing at all.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the sixth post in a series where I convert Pine strategies to Python line by line. The last two posts were about backtests that disagreed with TradingView. This one is about a quieter failure: the port that agrees with nothing, because it never produces anything to disagree with.&lt;/p&gt;

&lt;p&gt;The headline numbers first, because this is the part you can check:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What we measured&lt;/th&gt;
&lt;th&gt;Result, in our testing&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Scripts that compiled&lt;/td&gt;
&lt;td&gt;1,904 / 1,909 = &lt;strong&gt;99.7%&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;Analyzer + emitter both pass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Silent scripts&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;196&lt;/strong&gt; of the 1,904 that compiled (&lt;strong&gt;10.3%&lt;/strong&gt;)&lt;/td&gt;
&lt;td&gt;Ran fine, nothing triggered on the test feed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Drawing-only scripts&lt;/td&gt;
&lt;td&gt;366&lt;/td&gt;
&lt;td&gt;No &lt;code&gt;plot()&lt;/code&gt;, only drawing objects — a different category, next post&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repro 1: input gate, default &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;0 / 4,089&lt;/strong&gt; bars with a value&lt;/td&gt;
&lt;td&gt;All three plots &lt;code&gt;na&lt;/code&gt;, every bar&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repro 1, one flip: default &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;4,075 / 4,089&lt;/strong&gt; bars with a value&lt;/td&gt;
&lt;td&gt;Same script, same data — the gate was the whole story&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repro 2: intraday guard on a daily feed&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 / 4,089&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The condition is false on every bar&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repro 3: crossover vs 100,000&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 / 4,089&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Highest close on the feed is 5,807.72&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The honest number is 0. Three different scripts, three different causes, the same measured result: zero values in 4,089 bars. The rest of this post is the autopsy for each one — and the four-check list I now run before I ever blame the translator.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, what "silent" means — and what it doesn't
&lt;/h2&gt;

&lt;p&gt;The corpus numbers come from the sweep I reported in &lt;a href="https://dev.to/leichenbuilds/i-ran-1909-real-pine-scripts-through-a-translator-heres-what-actually-breaks-24h7"&gt;post #3&lt;/a&gt;: 1,909 real Pine scripts run through the translator, baseline dated 2026-10-24, in our testing. "Silent" has a precise definition there, and the definition matters more than the count:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Silent&lt;/strong&gt; = the script compiled, ran bar-by-bar to completion with no errors and no violations, and produced &lt;strong&gt;no plot values and no drawing objects&lt;/strong&gt; on the test feed.&lt;/li&gt;
&lt;li&gt;The test feed for the sweep is a synthetic 500-bar daily feed (2020-01-01 to 2021-05-14). "Silent" is always relative to a feed: silent on this feed is not silent everywhere.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Silent is not drawing-only.&lt;/strong&gt; 366 other scripts also have no &lt;code&gt;plot()&lt;/code&gt;, but they do produce lines, boxes, or labels — they are visible, just not plotted. Conflating the two is the first mistake; the drawing-only scripts get their own post next.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Silent is not an error.&lt;/strong&gt; None of the 196 threw anything. A translator that reports "success" on all of them is telling the truth and still misleading you, because "ran" is the only claim it can make. Whether the script &lt;em&gt;does anything&lt;/em&gt; is a claim about the script, the inputs, and the feed — and only you can check that.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One scope note, stated plainly: the 196 are corpus files, and the corpus itself lives on my other machine — I can't re-open the individual silent scripts from here tonight. So the three reproductions below are minimal scripts I wrote to isolate each cause, compiled with the local compiler and run on the series' usual feed (CSI 300 daily, 4,089 bars, 2010-01-04 to 2026-11-06). They are reproductions, not the corpus scripts themselves — except for the first one, which mirrors a real corpus case the sweep already named: script &lt;code&gt;006_support-resistance-vr&lt;/code&gt;, whose 55 plots were all &lt;code&gt;na&lt;/code&gt; because an &lt;code&gt;input.bool&lt;/code&gt; defaulted to &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repro 1: the input gate — 55 plots, all &lt;code&gt;na&lt;/code&gt;, one default
&lt;/h2&gt;

&lt;p&gt;This is the most common way a script goes silent, and the hardest to spot, because the code looks correct. The Pine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=5
indicator("Silent repro 1: input gate", overlay=false)
showPlots = input.bool(false, "Show plots")
rsi = ta.rsi(close, 14)
plot(showPlots ? rsi : na, "RSI")
plot(showPlots ? ta.sma(close, 20) : na, "SMA20")
plot(showPlots ? ta.ema(close, 50) : na, "EMA50")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Python is the actual output of the converter, verified tonight with the local compiler (&lt;code&gt;node bin/pine2pyne.mjs&lt;/code&gt;, zero diagnostics). Verbatim:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;_pyne_in_showPlots&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Show plots"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indicator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Silent repro 1: input gate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rsi&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_pyne_in_showPlots&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;na&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"RSI"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_pyne_in_showPlots&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;na&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"SMA20"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_pyne_in_showPlots&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;na&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"EMA50"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same harness note as posts #4 and #5: the run copy inlines the input default as a plain &lt;code&gt;False&lt;/code&gt;, because stock PyneCore (GitHub master) rejects the converter's hoisted-input style. Same values, same script.&lt;/p&gt;

&lt;p&gt;Measured on 4,089 bars of CSI 300 daily:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Plot&lt;/th&gt;
&lt;th&gt;Non-&lt;code&gt;na&lt;/code&gt; bars, default &lt;code&gt;false&lt;/code&gt;
&lt;/th&gt;
&lt;th&gt;Non-&lt;code&gt;na&lt;/code&gt; bars, default &lt;code&gt;true&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;RSI&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;4,075&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SMA20&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;4,070&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EMA50&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;4,040&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The control column is the proof. Nothing about the script is broken: flip one default and all three plots appear, with exactly the warm-up counts you'd expect (RSI 4,089 − 14 = 4,075; SMA20 4,089 − 19 = 4,070; EMA50 4,089 − 49 = 4,040). The silence was never in the logic. It was in a checkbox the author left off — and in the corpus, the real version of this was 55 plots, not three. An input default is a decision about what the script does when nobody touches it. Read the defaults before you read the logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repro 2: the timeframe guard — right script, wrong feed
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=5
indicator("Silent repro 2: timeframe guard", overlay=false)
rsi = ta.rsi(close, 14)
plot(timeframe.isintraday ? rsi : na, "RSI intraday only")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indicator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Silent repro 2: timeframe guard"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rsi&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;timeframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isintraday&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;na&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"RSI intraday only"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Measured: &lt;strong&gt;0 non-&lt;code&gt;na&lt;/code&gt; bars out of 4,089.&lt;/strong&gt; The script is correct — on an intraday chart. On the daily feed it runs, &lt;code&gt;timeframe.isintraday&lt;/code&gt; is false on every single bar, and the plot is &lt;code&gt;na&lt;/code&gt; 4,089 times. This is the feed-relative half of "silent": the same file, unchanged, would light up on a 15-minute feed. If your port produces nothing, the second question (after the inputs) is whether the feed you're testing on is the feed the script was written for. Symbol guards are the same shape — a script that only draws when &lt;code&gt;syminfo.ticker&lt;/code&gt; is BTCUSD is silent on every other symbol, correctly, by design.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repro 3: the threshold that never fires — a condition, not a bug
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=5
indicator("Silent repro 3: threshold never met", overlay=false)
crossUp = ta.crossover(close, 100000)
plot(crossUp ? close : na, "Cross 100k")
bgcolor(crossUp ? color.new(color.green, 90) : na)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indicator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Silent repro 3: threshold never met"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;crossUp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;crossover&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;crossUp&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;na&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Cross 100k"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;bgcolor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;green&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;crossUp&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;na&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Measured: &lt;strong&gt;0 non-&lt;code&gt;na&lt;/code&gt; bars out of 4,089&lt;/strong&gt;, and the background never colors once — both outputs share the same &lt;code&gt;crossUp&lt;/code&gt; condition, so one measurement covers both. The highest close on this feed is 5,807.72; the threshold is 100,000. No bar can cross it. This is the version of silence that looks most like a translator bug ("my port dropped the signal!") and is least likely to be one: the condition is doing exactly what it says, on data where it can never be true. Before you debug the port, compute the range of the data the condition is tested against. A threshold above your data's maximum is not a failed translation. It is an untested branch.&lt;/p&gt;

&lt;h2&gt;
  
  
  The false suspect: &lt;code&gt;barstate.isconfirmed&lt;/code&gt; is innocent
&lt;/h2&gt;

&lt;p&gt;One guard looks exactly like the culprit and isn't — I ran it so I wouldn't have to guess:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=5
indicator("Silent repro 4: isconfirmed suspect", overlay=false)
rsi = ta.rsi(close, 14)
plot(barstate.isconfirmed ? rsi : na, "RSI confirmed only")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Measured: &lt;strong&gt;4,075 non-&lt;code&gt;na&lt;/code&gt; bars out of 4,089&lt;/strong&gt; — identical to the RSI warm-up count. On historical data every bar is already closed, so &lt;code&gt;isconfirmed&lt;/code&gt; is true on every bar where RSI is defined, including the last one in this PyneCore run. Both things are true at once: the guard is real code with real meaning, and it cannot explain silence on a historical backtest. It only bites on live bars, where the current bar is still open. Suspects need the same treatment as causes: run them, count them, and let the innocent ones go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this checklist works
&lt;/h2&gt;

&lt;p&gt;Each check isolates one decision the script makes before it produces anything, and each one has a measurement that can only come out one of two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Read the input defaults first.&lt;/strong&gt; Count the &lt;code&gt;input.*&lt;/code&gt; calls, list their defaults, and ask what the script does when nobody touches them. The fix is one flip; the control run proves it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Match the feed to the script's assumptions.&lt;/strong&gt; Timeframe guards, symbol guards, session guards — check &lt;code&gt;timeframe.*&lt;/code&gt; and &lt;code&gt;syminfo.*&lt;/code&gt; against the feed you're actually running. Silent on this feed is not silent everywhere.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compute the condition's range.&lt;/strong&gt; For any threshold or crossover, get the max and min of the data it tests. If the threshold sits outside the data's range, the branch is untested, not broken.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run the suspects, don't argue about them.&lt;/strong&gt; A guard you suspect takes five minutes to reproduce and count. &lt;code&gt;barstate.isconfirmed&lt;/code&gt; survived; the other three didn't. The checklist is cheap because the alternative — re-reading the translator's output looking for a bug that isn't there — is not.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Count the outputs before reading the code.&lt;/strong&gt; A run with zero non-&lt;code&gt;na&lt;/code&gt; values is a different problem from a run with wrong values. Diagnose silence first, correctness second.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the defaults before the logic.&lt;/strong&gt; An input default is a decision. The corpus's loudest silent case was 55 plots behind one &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Name the feed in every claim.&lt;/strong&gt; "Silent" without a feed is meaningless. The sweep's feed is 500 daily bars; mine here is 4,089. Say which one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep silent and drawing-only apart.&lt;/strong&gt; 196 scripts produced nothing; 366 produced drawings but no plots. Different categories, different fixes — don't let one number stand for both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A reproduction beats an explanation.&lt;/strong&gt; Four small scripts, five runs, one table. Every cause in this post can be re-run from the code printed above.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next in the series: the 366 drawing-only scripts — what a translator owes a script with no &lt;code&gt;plot()&lt;/code&gt; at all, and where the Python port's output differs from TradingView's canvas.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What's the quietest failure you've hit porting a script — the one where everything ran and nothing happened?&lt;/strong&gt; I read every comment — the silence stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>algotrading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
    <item>
      <title>I Fixed the Exit Bracket My Last Post Got Wrong. The Strategy Got Worse.</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Wed, 11 Nov 2026 11:17:11 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/i-fixed-the-exit-bracket-my-last-post-got-wrong-the-strategy-got-worse-339d</link>
      <guid>https://dev.to/leichenbuilds/i-fixed-the-exit-bracket-my-last-post-got-wrong-the-strategy-got-worse-339d</guid>
      <description>&lt;h1&gt;
  
  
  I Fixed the Exit Bracket My Last Post Got Wrong. The Strategy Got Worse.
&lt;/h1&gt;

&lt;p&gt;In my last post I proved my own post #2 was wrong: the "ATR bracket" in that RSI strategy was never an ATR bracket. &lt;code&gt;strategy.exit&lt;/code&gt;'s &lt;code&gt;profit&lt;/code&gt; and &lt;code&gt;loss&lt;/code&gt; are distances in &lt;strong&gt;ticks&lt;/strong&gt;, not price points, so on the data I ran, the bracket was about 0.00016 points wide and every trade exited one bar after entry. The 10-bar time exit was dead code — it fired zero times in 60 trades.&lt;/p&gt;

&lt;p&gt;This post applies the fix that post promised: same strategy, same data, same parameters, but the exit rewritten with price levels — &lt;code&gt;limit&lt;/code&gt; and &lt;code&gt;stop&lt;/code&gt; computed from the entry price. The time exit finally fires. And the strategy loses &lt;em&gt;more&lt;/em&gt; money. Both of those are true, and the gap between them is the whole point.&lt;/p&gt;

&lt;p&gt;The headline numbers first, because this is the part you can check:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;#4: tick bracket (&lt;code&gt;profit&lt;/code&gt;/&lt;code&gt;loss&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;#5: price bracket (&lt;code&gt;limit&lt;/code&gt;/&lt;code&gt;stop&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;Buy &amp;amp; hold, same period&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Trades (closed)&lt;/td&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;td&gt;58 (+1 still open)&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Net result&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;-$1,353.29 (-13.53%)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;-$2,572.67 (-25.73%)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;+14.68%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Win rate&lt;/td&gt;
&lt;td&gt;27 / 60 (45%)&lt;/td&gt;
&lt;td&gt;19 / 58 (33%)&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Profit factor&lt;/td&gt;
&lt;td&gt;0.695&lt;/td&gt;
&lt;td&gt;0.587&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Avg bars held&lt;/td&gt;
&lt;td&gt;1.0&lt;/td&gt;
&lt;td&gt;3.6&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time exits (10-bar rule)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;6&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Same Pine, same 4,089 bars of CSI 300 daily data (2010-01-04 to 2026-11-06), same $10,000. The only change is one line. The rest of this post is the autopsy: what the fix actually changed, the exact bar where the time exit first fires, and why a correct fix made the numbers worse.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: one line
&lt;/h2&gt;

&lt;p&gt;The script is the RSI mean-reversion strategy from post #2, unchanged except the exit. The old line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;strategy.exit("Exit Long", "Long", profit=atrVal * 2, loss=atrVal)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if inTrade
    entryPrice = strategy.position_avg_price
    strategy.exit("Exit Long", "Long", limit=entryPrice + atrVal * 2, stop=entryPrice - atrVal)
    if barsInTrade &amp;gt;= maxHold
        strategy.close("Long", comment="Time exit")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;limit&lt;/code&gt; and &lt;code&gt;stop&lt;/code&gt; are price levels, so the bracket is now what post #2 always claimed it was: take profit 2×ATR above the entry price, stop loss 1×ATR below it. One detail worth stating plainly: the bracket is re-placed on every bar while the trade is open, using that bar's ATR, so the levels drift a few points as volatility changes — the stop is not frozen at the entry bar's ATR. That is the natural reading of the code as written, and it is what ran.&lt;/p&gt;

&lt;p&gt;The Python is the actual output of the converter, verified tonight with the local compiler (&lt;code&gt;node bin/pine2pyne.mjs&lt;/code&gt;, zero diagnostics). The changed lines, verbatim:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;inTrade&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;entryPrice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position_avg_price&lt;/span&gt;
        &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Exit Long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entryPrice&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;atrVal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entryPrice&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;atrVal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;barsInTrade&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;_pyne_in_maxHold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Long"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;comment&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Time exit"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same harness note as post #4, stated plainly: the run copy moves the four &lt;code&gt;input.int&lt;/code&gt; calls into &lt;code&gt;main()&lt;/code&gt; defaults, because stock PyneCore (GitHub master) rejects the converter's hoisted-input style. Same values, same script.&lt;/p&gt;

&lt;h2&gt;
  
  
  The first trade: dead code comes alive, 2.76 points from dying
&lt;/h2&gt;

&lt;p&gt;Trade #1 is the same signal in both runs — RSI crosses up through 30 on 2010-02-03 (bar 22), entry the next day at 3206.43. In post #4 it exited one bar later at -$176.13. Here is what happens with a real bracket. ATR on the entry bar is 79.27, so the bracket is limit 3364.96, stop 3127.16:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bar&lt;/th&gt;
&lt;th&gt;Date&lt;/th&gt;
&lt;th&gt;Low&lt;/th&gt;
&lt;th&gt;High&lt;/th&gt;
&lt;th&gt;Stop (placed prior bar)&lt;/th&gt;
&lt;th&gt;Closest call&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;td&gt;2010-02-05&lt;/td&gt;
&lt;td&gt;3129.92&lt;/td&gt;
&lt;td&gt;3177.42&lt;/td&gt;
&lt;td&gt;3127.16&lt;/td&gt;
&lt;td&gt;Low misses stop by &lt;strong&gt;2.76&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;25&lt;/td&gt;
&lt;td&gt;2010-02-08&lt;/td&gt;
&lt;td&gt;3133.81&lt;/td&gt;
&lt;td&gt;3172.31&lt;/td&gt;
&lt;td&gt;3126.48&lt;/td&gt;
&lt;td&gt;Misses by 7.33&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;td&gt;2010-02-09&lt;/td&gt;
&lt;td&gt;3144.48&lt;/td&gt;
&lt;td&gt;3178.95&lt;/td&gt;
&lt;td&gt;3129.44&lt;/td&gt;
&lt;td&gt;Misses by 15.04&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;td&gt;2010-02-23&lt;/td&gt;
&lt;td&gt;3152.93&lt;/td&gt;
&lt;td&gt;3225.39&lt;/td&gt;
&lt;td&gt;3142.68&lt;/td&gt;
&lt;td&gt;Misses by 10.25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;33&lt;/td&gt;
&lt;td&gt;2010-02-25&lt;/td&gt;
&lt;td&gt;3249.33&lt;/td&gt;
&lt;td&gt;3294.14&lt;/td&gt;
&lt;td&gt;3140.44&lt;/td&gt;
&lt;td&gt;Time exit fires&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The trade nearly dies on the first bar after entry — the low comes within 2.76 points of a stop that is 79 points wide. It survives, drifts upward for nine more bars, and on bar 33 the counter hits 10: the time exit fires at the bar's open, 3252.15, for &lt;strong&gt;+$137.16&lt;/strong&gt;. The same trade, the same signal, the same entry price: -$176.13 in post #4, +$137.16 here. A $313.29 swing on one trade, from one line. The time exit is no longer dead code — it fires on the very first trade of the run.&lt;/p&gt;

&lt;h2&gt;
  
  
  The aggregate: the fix is real, and the strategy is worse
&lt;/h2&gt;

&lt;p&gt;Six time exits fire across the run, five of them winners, net &lt;strong&gt;+$845.87&lt;/strong&gt;. That is the part the fix was supposed to deliver, and it delivers it. But the bracket exits tell the other half: 52 trades exit on the bracket, only 14 of them winners, net &lt;strong&gt;-$3,418.54&lt;/strong&gt;. Add them together and you get the headline: -$2,572.67, worse than the tick bracket's -$1,353.29.&lt;/p&gt;

&lt;p&gt;Why doesn't the fix help? Because the fix changes what the strategy &lt;em&gt;is&lt;/em&gt;, not whether it &lt;em&gt;works&lt;/em&gt;. Three things the aggregate shows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. A real bracket still exits fast.&lt;/strong&gt; 23 of the 52 bracket exits happen one bar after entry. The bracket is wide — 2×ATR of profit, 1×ATR of stop, against a median daily range of about 46 points — but a mean-reversion entry after an oversold bounce is, by construction, an entry into a falling market. The stop gets hit on big down days before the bounce has room to develop. The widest losing trade is the proof: trade #3 enters 2010-05-06 at 3014.91 with the stop at 2945.17, and the next bar gaps straight through it, opening at 2835.88 — 109 points below the stop — and fills at the open for -$537.09. A stop loss does not protect you from a gap. It never did.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The time exit is a small-sample hero.&lt;/strong&gt; Six fires in 58 trades, five winners. That looks like a good rule until you notice the denominator: the rule only gets a chance to fire when the bracket &lt;em&gt;doesn't&lt;/em&gt; fire first for ten bars, which happens 6 times out of 58. Its +$845.87 is real money in this run, but it is six observations. I am not going to call a 6-trade sample a finding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Longer holds desynchronize the trade list.&lt;/strong&gt; The first 39 entries are identical between the two runs. After that, the lists part ways: a trade that lasts 10 bars in this run is still holding when the next signal fires, so the signal is skipped, while the tick-bracket run — flat after every trade — takes it. 59 of the 60 entry dates from post #4 still appear here, but in a different order and combination. Once two backtests desynchronize by one trade, every later difference is partly a consequence of the first. You cannot fix that by patching the trade list. You fix it by fixing the units — which is what this post did — and then accepting the new list on its own terms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Both are true at once
&lt;/h2&gt;

&lt;p&gt;The fix is correct: &lt;code&gt;limit&lt;/code&gt;/&lt;code&gt;stop&lt;/code&gt; is what the strategy meant, the time exit now fires, and trade #1 proves it bar by bar. And the strategy is worse: -25.73% against -13.53%, profit factor 0.587 against 0.695, win rate 33% against 45%. If you only read one of those sentences you walk away with the wrong lesson. The wrong lesson from post #4 would have been "the strategy loses 13.53%". The wrong lesson from this post would be "the fix made it worse, so don't fix your brackets". The honest reading is narrower: &lt;strong&gt;the strategy, as intended, loses 25.73% on this data, and the strategy as written in post #2 was never run at all&lt;/strong&gt; — its bracket was a rounding error. Neither number is the strategy's "true" result in any larger sense. Both are the measured result of a specific reading of the code on a specific 4,089 bars. The only claim this series will defend is that the reading is now the intended one, and the numbers are what that reading produces.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules I now follow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Fix the units before judging the strategy.&lt;/strong&gt; A backtest of the wrong rule is not a pessimistic backtest. It is a backtest of a different strategy. Post #4's -13.53% was never this strategy's number.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quote the exit count, not just the net.&lt;/strong&gt; "Time exit fired 6 times" is the check that the fix landed. Net profit alone cannot tell you whether the rule you think you tested is the rule that ran.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A gap through the stop is a fill, not a bug.&lt;/strong&gt; Trade #3's -$537.09 is the largest loss in the run and it is exactly what a stop order does when the market opens below it. If your backtest never shows a gap-through fill, your fill model is flattering you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Six observations is not a finding.&lt;/strong&gt; The time exit's 5-of-6 winners is a reason to keep the rule in the next test, not a reason to claim it works.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Change one line, re-run everything.&lt;/strong&gt; The whole post is a one-line diff. The whole result changed. That is the only sense in which backtesting is cheap.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next in the series: the 196 silent scripts — the ones that ran fine and produced nothing, and the four-check list that finds why.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Which fix changed your numbers most — a unit, a fill, or a warm-up?&lt;/strong&gt; I read every comment — the fix stories are what the next posts are built from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>algotrading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
    <item>
      <title>Same Pine Script, Two Backtests, Opposite Results — Here's the Exact Bar Where They Diverge</title>
      <dc:creator>Lei Chen</dc:creator>
      <pubDate>Tue, 10 Nov 2026 11:26:37 +0000</pubDate>
      <link>https://dev.to/leichenbuilds/same-pine-script-two-backtests-opposite-results-heres-the-exact-bar-where-they-diverge-3c6i</link>
      <guid>https://dev.to/leichenbuilds/same-pine-script-two-backtests-opposite-results-heres-the-exact-bar-where-they-diverge-3c6i</guid>
      <description>&lt;p&gt;One script. One dataset. Two backtests. One says the strategy loses 13.5%. The other says it makes money. Neither backtest is lying — they disagree about three things the script never says out loud: what a "tick" is, how an indicator warms up, and when an order fills.&lt;/p&gt;

&lt;p&gt;This is the fourth post in a series where I convert classic Pine strategies to Python line by line. Post #2 converted an RSI mean-reversion strategy with an ATR exit bracket. This post runs that exact strategy twice — once in the PyneCore bar-by-bar emulator, once in a hand-written pandas port, the way most people would write it — on the same 4,089 bars of CSI 300 daily data (2010-01-04 to 2026-11-06), and finds the exact bar where the two results part ways.&lt;/p&gt;

&lt;p&gt;The headline numbers first, because this is the part you can check:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;PyneCore emulator&lt;/th&gt;
&lt;th&gt;Naive pandas hand-port&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Trades&lt;/td&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;td&gt;57&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Net result&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;-$1,353.29 (-13.53% on $10,000)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+206.49 index points&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Win rate&lt;/td&gt;
&lt;td&gt;27 / 60 (45%)&lt;/td&gt;
&lt;td&gt;22 / 57 (39%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Profit factor&lt;/td&gt;
&lt;td&gt;0.695&lt;/td&gt;
&lt;td&gt;— (no position sizing in the hand-port)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Avg bars held&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1.0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;4.1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time exits (10-bar rule)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Buy &amp;amp; hold, same period&lt;/td&gt;
&lt;td&gt;+14.68%&lt;/td&gt;
&lt;td&gt;+14.68%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Same Pine, same data, same parameters. Opposite signs. The rest of this post is the autopsy: three divergences, each with the exact bar and price where it happens.&lt;/p&gt;

&lt;h2&gt;
  
  
  The script under test
&lt;/h2&gt;

&lt;p&gt;The RSI mean-reversion strategy from post #2, unchanged. Buy the oversold bounce (RSI crossing up through 30), exit on an ATR bracket or a 10-bar time stop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//@version=6
strategy("RSI Mean Reversion", overlay=true, initial_capital=10000,
     default_qty_type=strategy.percent_of_equity, default_qty_value=100)

rsiLen     = input.int(14, "RSI length", minval=1)
overbought = input.int(70, "Overbought level", minval=51, maxval=100)
oversold   = input.int(30, "Oversold level", minval=0, maxval=49)
maxHold    = input.int(10, "Max hold (bars)", minval=1)

rsi = ta.rsi(close, rsiLen)
atrVal = ta.atr(14)

var int barsInTrade = 0
inTrade = strategy.position_size != 0
if inTrade
    barsInTrade += 1
else
    barsInTrade := 0

longSignal = ta.crossover(rsi, oversold)

if longSignal and not inTrade
    strategy.entry("Long", strategy.long)

if inTrade
    strategy.exit("Exit Long", "Long", profit=atrVal * 2, loss=atrVal)
    if barsInTrade &amp;gt;= maxHold
        strategy.close("Long", comment="Time exit")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Python is the actual output of the converter from post #2, verified byte-identical tonight with the local compiler (&lt;code&gt;node bin/pine2pyne.mjs&lt;/code&gt;, zero diagnostics). One harness note, stated plainly: stock PyneCore (GitHub master, installed 2026-11-10) rejects the converter's hoisted-input style, so the run copy moves the four &lt;code&gt;input.int&lt;/code&gt; calls into &lt;code&gt;main()&lt;/code&gt; defaults — same values, same script. The hand-port is a vectorized pandas version: Wilder RSI/ATR via &lt;code&gt;ewm(alpha=1/14)&lt;/code&gt;, signals from the RSI series, fill at the signal bar's close, bracket checked against each following bar's high/low in price points, time exit counted from the entry bar. It is deliberately the version a careful reader would write without knowing TradingView's execution model.&lt;/p&gt;

&lt;h2&gt;
  
  
  Divergence #1: the RSI warm-up — bar 22, 2010-02-03
&lt;/h2&gt;

&lt;p&gt;The two engines disagree about RSI before they disagree about anything else. PyneCore's &lt;code&gt;ta.rsi&lt;/code&gt; seeds Wilder's smoothing with the average of the first 14 bars; the pandas port starts the exponential average at bar 1. The values converge — after bar 1,000 the two RSI series are identical to three decimals — but the first hundred bars differ by up to 16.47 points (mean absolute difference 2.80 over bars 14–100).&lt;/p&gt;

&lt;p&gt;That is enough to create a trade in one engine that never exists in the other. On 2010-02-03 (bar 22):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;PyneCore RSI&lt;/th&gt;
&lt;th&gt;pandas RSI&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2010-02-02&lt;/td&gt;
&lt;td&gt;23.80&lt;/td&gt;
&lt;td&gt;33.52&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2010-02-03&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;35.36&lt;/strong&gt; (crosses above 30)&lt;/td&gt;
&lt;td&gt;44.09 (already above 30)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;PyneCore sees a crossover and enters the next day. The pandas port sees no crossover, because its RSI crossed 30 days earlier, during the warm-up, when the two series still disagreed. Trade #1 in the PyneCore run — entry 2010-02-04 at 3206.43, exit the next day at 3147.72, -$176.13 — exists only because of how RSI is seeded. One crossover out of sixty differs. It is the first divergence, and it is the only one that lives in the indicator math. Everything after it lives in the execution model.&lt;/p&gt;

&lt;h2&gt;
  
  
  Divergence #2: the fill — every trade, starting 2010-04-30
&lt;/h2&gt;

&lt;p&gt;PyneCore fills entries at the next bar's open. The hand-port fills at the signal bar's close. Take the signal on 2010-04-30 (bar 78), a signal both engines agree on:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Entry price&lt;/th&gt;
&lt;th&gt;Exit price&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Naive pandas&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;3067.36&lt;/strong&gt; (Apr 30 close)&lt;/td&gt;
&lt;td&gt;3006.20 (stop, May 4)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PyneCore&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;3005.49&lt;/strong&gt; (May 4 open)&lt;/td&gt;
&lt;td&gt;2981.78 (May 5)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The fill gap on that one trade is 61.87 points — 2.02% of the entry price — before the exit logic even runs. The signal is identical; the price paid is not. This gap is present on every shared trade, in both directions, and it is the largest single source of difference between the two equity curves. It is also the easiest to miss, because both versions "enter on the signal" and the code never says when the order fills. In Pine, a market entry signaled on bar N fills at bar N+1's open by default. The hand-port's &lt;code&gt;close&lt;/code&gt; fill is a choice the author made without noticing they made it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Divergence #3: the bracket is in ticks, not points — and that kills the time exit
&lt;/h2&gt;

&lt;p&gt;This is the one that changes the strategy's shape entirely, and it is a correction to my own post #2, which described this exit as "an ATR-based bracket." The bracket as written is not 2×ATR and 1×ATR in price. In Pine, &lt;code&gt;strategy.exit&lt;/code&gt;'s &lt;code&gt;profit&lt;/code&gt; and &lt;code&gt;loss&lt;/code&gt; are distances &lt;strong&gt;in ticks&lt;/strong&gt; from the entry price — TradingView's own strategy docs say so: &lt;code&gt;loss&lt;/code&gt; sets the stop "a certain number of ticks away from the entry order's price," and &lt;code&gt;profit&lt;/code&gt; defines "the profit size in ticks from the entry price." Price-level brackets use &lt;code&gt;limit&lt;/code&gt; and &lt;code&gt;stop&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;profit=atrVal * 2, loss=atrVal&lt;/code&gt; means roughly 158 ticks and 79 ticks, not 158 and 79 points. How wide is that in price? It depends on &lt;code&gt;syminfo.mintick&lt;/code&gt;, which the script never mentions. The PyneCore run fed it a CSV, and the converter's default symbol info for a custom CSV sets &lt;code&gt;mintick = 0.000001&lt;/code&gt;. The bracket is therefore about &lt;strong&gt;0.00016 points wide&lt;/strong&gt; — effectively zero. Any move at all, in either direction, triggers a leg at the next bar's open.&lt;/p&gt;

&lt;p&gt;I checked this was really the mechanism and not an emulator quirk: I re-ran the same script with the bracket widened 10× (&lt;code&gt;profit=atrVal*20, loss=atrVal*10&lt;/code&gt;) and again with constant &lt;code&gt;profit=200, loss=100&lt;/code&gt;. All three runs produce byte-identical trade lists: 60 trades, same exits, same -$1,353.29. The bracket width cannot matter, because at these tick sizes every version of it is still ~zero points wide.&lt;/p&gt;

&lt;p&gt;And even with a realistic tick size the conclusion barely changes: even if the tick were 0.01 — the usual tick size for an index like this — the bracket would be 1.58 points of profit and 0.79 points of stop against daily ranges around 60 points. The bracket would still fire on the first bar after entry, almost every time. The observed result is exactly that: &lt;strong&gt;all 60 PyneCore trades exit 1 bar after entry, all 60 at the exit bar's open, all 60 via the bracket — and the 10-bar time exit fires zero times.&lt;/strong&gt; In the hand-port, where the bracket is (wrongly, but understandably) read as price levels, trades last 1–10 bars and the time exit fires 8 times. The two engines are not running the same exit rule. One of them is running the rule as written; the other is running the rule as intended. The fix, if you want the intended bracket, is price levels: &lt;code&gt;limit = entry + 2*atrVal, stop = entry - atrVal&lt;/code&gt; — or keep &lt;code&gt;profit&lt;/code&gt;/&lt;code&gt;loss&lt;/code&gt; and divide by &lt;code&gt;syminfo.mintick&lt;/code&gt; yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  The knock-on: two signals that vanish because of state
&lt;/h2&gt;

&lt;p&gt;Two more PyneCore signals — bars 92 and 1464 — exist in both engines' RSI series, but the hand-port never takes them. At both bars the hand-port is still holding an earlier trade (bars 89–92 and 1462–1464), while PyneCore, whose trades all last exactly one bar, is flat and free to enter. This is the part people miss when they diff two backtests trade by trade: once the engines desynchronize by one trade, every later difference is partly a consequence of the first three. You cannot fix divergence #3 by patching the trade list. You fix it by fixing the units, the warm-up, and the fill — in that order.&lt;/p&gt;

&lt;h2&gt;
  
  
  The honest summary
&lt;/h2&gt;

&lt;p&gt;The two backtests do not disagree about the strategy. They disagree about three things the strategy never states: RSI's first 14 bars (seeding), the entry fill (next open vs signal close), and the bracket's units (ticks vs points). Measured on the same 4,089 bars: PyneCore, running the rule as written, loses 13.53% with every trade lasting one bar; the hand-port, running the rule as intended, gains 206.49 points over 57 trades lasting 4.1 bars on average. Neither number is the strategy's "true" result — the strategy has no true result until you pin down the units, the warm-up, and the fill. The correction to post #2 stands: its "ATR bracket" is a tick bracket, and on this data the time exit in that script is dead code. If you port a strategy and the Python version disagrees with TradingView, check the units first. On a corpus this size, "the exit was in ticks" is not a rare explanation — it was the whole trade list.&lt;/p&gt;

&lt;p&gt;This conversion work was done by &lt;a href="https://pine2py.com" rel="noopener noreferrer"&gt;Pine2Py&lt;/a&gt;, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.&lt;/p&gt;

&lt;p&gt;Next in the series: the diagnostics teardown — one real &lt;code&gt;strategy.exit&lt;/code&gt; rewritten with price-level brackets, run the same way, and the exact bar where the time exit finally fires.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Which unit has bitten you — ticks, points, or percent — when you ported a strategy by hand?&lt;/strong&gt; I read every comment — the unit stories are what the next posts are built from.&lt;/p&gt;

</description>
      <category>python</category>
      <category>algotrading</category>
      <category>backtesting</category>
      <category>pinescript</category>
    </item>
  </channel>
</rss>
