Pine Script v5 · Strategy
Trading the Equity Curve Position Sizing Example
A Pine Script v5 strategy built on SMA (slowequityseed, 25), CMO (close, 9) and MOM (price, 12). It has 4 order rules, 4 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 10% of equity and pays no commission. 12 settings; defaults include Initial % Equity 10.0, Slow SMA Period 25 and Fast SMA Period 9.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Trading the Equity Curve Position Sizing Example repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 6 of 6 trades.
- How did Trading the Equity Curve Position Sizing Example do on BTCUSDT 1D with default settings?
- +3.34%
- From 26 Jan 2024 to 21 Oct 2026, over 14 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Trading the Equity Curve Position Sizing Example run in Pine Script v6?
- Yes
- Same 14 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does Trading the Equity Curve Position Sizing Example use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Trading the Equity Curve Position Sizing Example run in Zeal Terminal?
- Yes
- It compiles in 23 ms and runs 1,000 bars in 63 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +3.34%
- $3,339.72
- Buy and hold
- +102.5%
- same period
- Closed trades
- 14
- 6 won · 8 lost
- Profitable
- 42.9%
- of closed trades
- Profit factor
- 1.66
- gross profit ÷ gross loss
- Max drawdown
- 4.07%
- −$4,150.89
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A long position was still open at the last bar. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Likely bug
ta.mom(mom0, 1) ignores the settings line 61
Line 61 hard-codes 1 where line 60 uses “Momentum Length”. Changing those settings moves one band but not this one.
ta.mom( mom0, 1)ta.mom(mom0, Momentum_Length) -
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Use Trading the Equity Curve Position Sizing | true | bool | defulttraderule longCondition shortCondition line 45 |
| Initial % Equity | 10.0 | float | sizeadjdown sizeadjup qty |
| Slow SMA Period | 25 | int | smaslowequity |
| Fast SMA Period | 9 | int | smafastequity |
| Use Fast/Slow AvgFast Equity Avg is below Slow---otherwise if unchecked uses Slow Equity Avg below Equity | true | bool | equitymethod |
| Position Size Adjustment | Reduce size by (%) | string · options ["Reduce size by (%)","Increase size by (%)"] | line 39 line 46 |
| Increase/Decrease % Equity by: | 50 | int | sizeadjdown sizeadjup qty |
| Chande Momentum Length | 9 | int · min 1 | chandeMO |
| Chande Momentum Signal | 10 | int · min 1 | cmosignal |
| SuperTrend ATR Length | 10 | int | supertrend direction |
| SuperTrend Factor | 3.0 | float · step 0.01 | supertrend direction |
| Momentum Length | 12 | int | mom0 |
What it calculates
- SMAslowequityseed, Slow SMA Period (25)
smaslowequity · line 31 - SMAfastequityseed, Fast SMA Period (9)
smafastequity · line 32 - CMOclose, Chande Momentum Length (9)
chandeMO · line 54 - SMAchandeMO, Chande Momentum Signal (10)
cmosignal · line 55 - MOMprice, Momentum Length (12)
mom0 · line 60 - MOMmom0, 1
mom1 · line 61 - SupertrendSuperTrend Factor (3.0), SuperTrend ATR Length (10)
supertrend · line 62 - SupertrendSuperTrend Factor (3.0), SuperTrend ATR Length (10)
direction · line 62
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open DefLong long | (longConditiondefault) | On | 68 |
| Open DefShort short | (shortConditiondefault) | On | 71 |
| Open AdjLong long | (longCondition) | On | 75 |
| Open AdjShort short | (shortCondition) | On | 78 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +3.3% | +102.5% | 14 | 43% | 1.66 | 4.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.3% | +5.5% | 10 | 50% | 2.30 | 0.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.8% | +9.8% | 8 | 50% | 1.19 | 3.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +2.0% | +8.4% | 12 | 33% | 2.29 | 1.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +2.8% | +16.7% | 6 | 67% | 1.97 | 5.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −1.0% | +21.5% | 10 | 40% | 0.75 | 5.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +6.8% | +148.3% | 8 | 38% | 2.46 | 3.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −2.0% | +17.1% | 13 | 31% | 0.27 | 2.1% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Settings heatmap
Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (Chande Momentum Length 9, Chande Momentum Signal 12) made +5.8% before 26 Dec 2025 and −1.1% after it, worse than the defaults' −1.0% after. Tuning on the past did not carry forward. 88% of the 56 combinations made money in-sample and 30% out-of-sample.
| Chande Momentum Signal ↓ · Chande Momentum Length → | 5 | 6 | 8 | 9 | 11 | 13 | 15 |
|---|---|---|---|---|---|---|---|
| 5 | +3% | +4% | +2% | +2% | +1% | +3% | +3% |
| 6 | +5% | +4% | +1% | +2% | −0% | +3% | +2% |
| 7 | +2% | +0% | +1% | +2% | +2% | +4% | +2% |
| 9 | +3% | +3% | +3% | +2% | +3% | +1% | −0% |
| 10 | +3% | +4% | +4% | +4% | +3% | −0% | +2% |
| 12 | +3% | +3% | +3% | +6% | +3% | +2% | +1% |
| 14 | +5% | +3% | +1% | +2% | −2% | +2% | +1% |
| 17 | +5% | +1% | −3% | −1% | −0% | +2% | +4% |
| Chande Momentum Signal ↓ · Chande Momentum Length → | 5 | 6 | 8 | 9 | 11 | 13 | 15 |
|---|---|---|---|---|---|---|---|
| 5 | −1% | −0% | +0% | −2% | −1% | +2% | +0% |
| 6 | −0% | +0% | +0% | −3% | −1% | +1% | +1% |
| 7 | +1% | +0% | −1% | −2% | −1% | −0% | −2% |
| 9 | +0% | +0% | −1% | −1% | −1% | −0% | −4% |
| 10 | −0% | 0% | −1% | −1% | −1% | −0% | −4% |
| 12 | −0% | +0% | −0% | −1% | −2% | −2% | −5% |
| 14 | +0% | +1% | +1% | −3% | −2% | −2% | −5% |
| 17 | +1% | +1% | −0% | −2% | −4% | −3% | −1% |
Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 3 | 33% | $639.53 |
| Sideways market | 6 | 67% | $3,935.59 |
| Falling market | 5 | 20% | −$1,235.41 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.9%
The 95th percentile of the worst drawdown when the same 14 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.7% to 3.9%; the tick is what actually happened, 2.3%. Order luck alone moves it this much.
All 14 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 27 Feb 2024 54,476.50 | 3 Apr 2024 65,464.00 | 36 | $2,016.93 |
| short | 3 Apr 2024 65,464.00 | 5 Jun 2024 70,537.80 | 63 | −$775.06 |
| long | 5 Jun 2024 70,537.80 | 5 Jul 2024 57,050.00 | 30 | −$967.94 |
| short | 5 Jul 2024 57,050.00 | 24 Aug 2024 64,037.20 | 50 | −$614.05 |
| long | 24 Aug 2024 64,037.20 | 10 Jan 2025 92,552.50 | 139 | $4,452.92 |
| short | 10 Jan 2025 92,552.50 | 9 May 2025 103,262.00 | 119 | −$602.34 |
| long | 9 May 2025 103,262.00 | 30 Aug 2025 108,377.00 | 113 | $256.41 |
| short | 30 Aug 2025 108,377.00 | 14 Jan 2026 95,414.00 | 137 | $620.60 |
| long | 14 Jan 2026 95,414.00 | 1 Feb 2026 78,741.10 | 18 | −$912.05 |
| short | 1 Feb 2026 78,741.10 | 11 Mar 2026 69,948.60 | 38 | $577.72 |
| long | 11 Mar 2026 69,948.60 | 2 Apr 2026 68,113.90 | 22 | −$136.46 |
| short | 2 Apr 2026 68,113.90 | 18 Apr 2026 77,072.00 | 16 | −$683.34 |
| long | 18 Apr 2026 77,072.00 | 2 Jun 2026 71,408.90 | 45 | −$379.27 |
| short | 2 Jun 2026 71,408.90 | 6 Aug 2026 64,665.20 | 65 | $485.66 |
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Trading the Equity Curve Position Sizing Example this page | — | v5 strategy | +3.3% | 14 | 4.1% |
| Strategy Template by Trendoscope | sma supertrend | v4 strategy | −21.8% | 11 | 40.5% |
| Dual-Supertrend with MACD - Strategy [presentTrading] by PresentTrading | sma supertrend | v5 strategy | −0.9% | 36 | 5.3% |
| [VJ] Hulk Smash Intra by vikris | supertrend sma | v4 strategy | No trades on this market | ||
| Higher TF - Repainting + Limiting backtest results by Trendoscope | sma supertrend | v4 strategy | No trades on this market | ||
| Momentum by Zeal Terminal | mom sma | v5 indicator | indicator | ||
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
Does it run in Pine Script v6?
Yes
Same 14 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © shardison-//@version=5+//@version=6//EXPLANATION⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 23 ms | With a workaround 1,000 bars in 63 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © shardison//@version=5//EXPLANATION//"Trading the equity curve" as a risk management method is the //process of acting on trade signals depending on whether a system’s performance//is indicating the strategy is in a profitable or losing phase.//The point of managing equity curve is to minimize risk in trading when the equity curve is in a downtrend. //This strategy has two modes to determine the equity curve downtrend://By creating two simple moving averages of a portfolio's equity curve - a short-term//and a longer-term one - and acting on their crossings. If the fast SMA is below//the slow SMA, equity downtrend is detected (smafastequity < smaslowequity).//The second method is by using the crossings of equity itself with the longer-period SMA (equity < smasloweequity).//When "Reduce size by %" is active, the position size will be reduced by a specified percentage//if the equity is "under water" according to a selected rule. If you're a risk seeker, select "Increase size by %"//- for some robust systems, it could help overcome their small drawdowns quicker.strategy("Use Trading the Equity Curve Postion Sizing", shorttitle="TEC", default_qty_type = strategy.percent_of_equity, default_qty_value = 10, initial_capital = 100000)//TRADING THE EQUITY CURVE INPUTSuseTEC = input.bool(true, title="Use Trading the Equity Curve Position Sizing")defulttraderule = useTEC ? false: trueinitialsize = input.float(defval=10.0, title="Initial % Equity")slowequitylength = input.int(25, title="Slow SMA Period")fastequitylength = input.int(9, title="Fast SMA Period")seedequity = 100000 * .10if strategy.equity == 0 seedequityelse strategy.equityslowequityseed = strategy.equity > seedequity ? strategy.equity : seedequityfastequityseed = strategy.equity > seedequity ? strategy.equity : seedequitysmaslowequity = ta.sma(slowequityseed, slowequitylength)smafastequity = ta.sma(fastequityseed, fastequitylength)equitycalc = input.bool(true, title="Use Fast/Slow Avg", tooltip="Fast Equity Avg is below Slow---otherwise if unchecked uses Slow Equity Avg below Equity")sizeadjstring = input.string("Reduce size by (%)", title="Position Size Adjustment", options=["Reduce size by (%)","Increase size by (%)"])sizeadjint = input.int(50, title="Increase/Decrease % Equity by:")equitydowntrendavgs = smafastequity < smaslowequityslowequitylessequity = strategy.equity < smaslowequityequitymethod = equitycalc ? equitydowntrendavgs : slowequitylessequityif sizeadjstring == ("Reduce size by (%)") sizeadjdown = initialsize * (1 - (sizeadjint/100))else sizeadjup = initialsize * (1 + (sizeadjint/100))c = closeqty = 100000 * (initialsize / 100) / cif useTEC and equitymethod if sizeadjstring == "Reduce size by (%)" qty := (strategy.equity * (initialsize / 100) * (1 - (sizeadjint/100))) / c else qty := (strategy.equity * (initialsize / 100) * (1 + (sizeadjint/100))) / c //EXAMPLE TRADING STRATEGY INPUTSCMO_Length = input.int(defval=9, minval=1, title='Chande Momentum Length')CMO_Signal = input.int(defval=10, minval=1, title='Chande Momentum Signal')chandeMO = ta.cmo(close, CMO_Length)cmosignal = ta.sma(chandeMO, CMO_Signal)SuperTrend_atrPeriod = input.int(10, "SuperTrend ATR Length")SuperTrend_Factor = input.float(3.0, "SuperTrend Factor", step = 0.01)Momentum_Length = input.int(12, "Momentum Length")price = closemom0 = ta.mom(price, Momentum_Length)mom1 = ta.mom( mom0, 1)[supertrend, direction] = ta.supertrend(SuperTrend_Factor, SuperTrend_atrPeriod)stupind = (direction < 0 ? supertrend : na)stdownind = (direction < 0? na : supertrend)//TRADING CONDITIONSlongConditiondefault = ta.crossover(chandeMO, cmosignal) and (mom0 > 0 and mom1 > 0 and close > stupind) and defulttraderuleif (longConditiondefault) strategy.entry("DefLong", strategy.long, qty=qty)shortConditiondefault = ta.crossunder(chandeMO, cmosignal) and (mom0 < 0 and mom1 < 0 and close < stdownind) and defulttraderuleif (shortConditiondefault) strategy.entry("DefShort", strategy.short, qty=qty) longCondition = ta.crossover(chandeMO, cmosignal) and (mom0 > 0 and mom1 > 0 and close > stupind) and useTECif (longCondition) strategy.entry("AdjLong", strategy.long, qty = qty)shortCondition = ta.crossunder(chandeMO, cmosignal) and (mom0 < 0 and mom1 < 0 and close < stdownind) and useTECif (shortCondition) strategy.entry("AdjShort", strategy.short, qty = qty)plot(strategy.equity)plot(smaslowequity, color=color.new(color.red, 0))plot(smafastequity, color=color.new(color.green, 0))