Pine Script v5 · Strategy
Crunchster's Normalised Trend Strategy
A Pine Script v5 strategy built on Stdev (src - src[1], 14), CUM (nRet) and WMA (nPrice[offset], hlength). It has 6 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade is 0.1 contracts and pays 0.06 commission. 17 settings; defaults include Source close, Lookback period for price normalisation filter 14 and Lookback period for Hull Moving Average 100.
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 Crunchster's Normalised Trend Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 14 of 14 trades.
- How did Crunchster's Normalised Trend Strategy do on BTCUSDT 1D with default settings?
- +22.88%
- From 26 Jan 2024 to 21 Oct 2026, over 73 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Crunchster's Normalised Trend Strategy run in Pine Script v6?
- No
- The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 73 before, 73 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does Crunchster's Normalised Trend Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 59, 68.
- Does Crunchster's Normalised Trend Strategy run in Zeal Terminal?
- Yes
- It compiles in 28 ms and runs 1,000 bars in 76 ms, in the browser.
BTCUSDT · 1D · default settings
- Real Price
- Hull MA
- Hull MA Offset
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +22.88%
- $228.79
- Buy and hold
- +102.5%
- same period
- Closed trades
- 73
- 20 won · 53 lost
- Profitable
- 27.4%
- of closed trades
- Profit factor
- 1.56
- gross profit ÷ gross loss
- Max drawdown
- 10.86%
- −$118.31
One run with the author's default settings, $1,000 starting capital, commission as the script sets it. A short 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.stdev(close-close[1], 14) ignores the settings line 35
Line 35 hard-codes 14 where line 28 uses “Lookback period for price normalisation filter”. Changing those settings moves one band but not this one.
ta.stdev(close-close[1], 14)ta.stdev(close-close[1], length)
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 |
|---|---|---|---|
| Strategy Settings | |||
| Source | close | source | nRet |
| Lookback period for price normalisation filterThis sets the lookback period for the volatility adjustment of returns, which is used to transform the price series into the "real price" | 14 | int · min 2 | nRet |
| Lookback period for Hull Moving Average | 100 | int · min 2 | fHMA |
| HMA Offset | 0 | int · min 0 | fHMA |
| Long | true | bool | line 53 |
| ShortToggle long/short strategy on/off | true | bool | line 62 |
| Risk Management Settings | |||
| Stop multipleMultiple for ATR, setting hard stop loss from entry price | 1 | float · step 0.25 | exit Stop Long exit Stop Short |
| Max LeverageMax leverage sets maximum allowable leverage of total capital (initial capital + any net profit), capping maximum volatility adjusted position size | 1 | float · step 0.5 | shares line 44 |
| Annualised Volatility Target %Specify annual risk target, used to determine volatility adjusted position size. Annualised daily volatility is referenced to this value and position size adjusted accordingly | 10 | float · max 75 | shares |
| Compounding | false | bool | line 39 |
| %Toggle compounding of profit, and set % of profit to compound | 50 | float · step 5 | risk |
| Backtest range | |||
| From Day | 1 | int · min 1 · max 31 | start |
| From Mon | 1 | int · min 1 · max 12 | start |
| From YrSet start of backtesting period | 2018 | int · min 1900 | start |
| To Day | 1 | int · min 1 · max 31 | finish |
| To Mon | 1 | int · min 1 · max 12 | finish |
| To YrSet end of backtesting period | 9999 | int · min 1900 | finish |
What it calculates
- Stdev(src - src[1]), Lookback period for price normalisation filter (14)
nRet · line 28 - CUMnRet
nPrice · line 29 - WMA2 * ta.wma(nPrice[offset], hlength / 2) - ta.wma(nPrice[offset], hlength), math.round(math.sqrt(hlength))
fHMA · line 31 - SMAtr, 14
stopL · line 34 - Stdevclose-close[1], 14
stdev · line 35 - CrossundernPrice, fHMA
exitlong · line 55 - CrossovernPrice, fHMA
exitshort · line 64
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 Go Long! long | (longCondition) | On | 57 |
| Exit Stop Long stop | strategy.position_size > 0 | On | 59 |
| Close Go Long! | (exitlong) | On | 61 |
| Open Go Short! short | (shortCondition) | On | 66 |
| Exit Stop Short stop | strategy.position_size < 0 | On | 68 |
| Close Go Short! | (exitshort) | On | 70 |
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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.
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 | +22.9% | +102.5% | 73 | 27% | 1.56 | 10.9% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +14.0% | +5.5% | 78 | 17% | 1.29 | 18.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +39.2% | +9.8% | 79 | 30% | 1.97 | 5.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −20.8% | +8.4% | 105 | 12% | 0.70 | 35.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +9.9% | +16.7% | 71 | 27% | 1.27 | 9.3% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −13.6% | +21.5% | 83 | 14% | 0.76 | 23.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −10.7% | +148.3% | 99 | 19% | 0.82 | 24.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −13.3% | +17.1% | 87 | 20% | 0.72 | 29.3% | 8 May 2026 – 21 Oct 2026 |
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 | 32 | 16% | −$53.32 |
| Sideways market | 25 | 40% | $137.70 |
| Falling market | 16 | 31% | $144.73 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
15.6%
The 95th percentile of the worst drawdown when the same 73 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 6.8% to 15.6%; the tick is what actually happened, 10.4%. Order luck alone moves it this much.
All 73 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 15 May 2024 66,206.60 | 11 Jun 2024 67,314.10 | 27 | $2.88 |
| short | 11 Jun 2024 67,314.10 | 12 Jun 2024 68,264.10 | 1 | −$5.53 |
| long | 12 Jun 2024 68,264.10 | 13 Jun 2024 66,772.90 | 1 | −$8.31 |
| short | 13 Jun 2024 66,772.90 | 15 Jul 2024 64,724.20 | 32 | $9.86 |
| long | 15 Jul 2024 64,724.20 | 2 Aug 2024 62,168.80 | 18 | −$7.67 |
| short | 2 Aug 2024 61,498.20 | 21 Aug 2024 61,156.10 | 19 | $1.00 |
| long | 21 Aug 2024 61,156.10 | 22 Aug 2024 60,375.70 | 1 | −$2.12 |
| short | 22 Aug 2024 60,375.70 | 23 Aug 2024 64,037.30 | 1 | −$15.58 |
| long | 23 Aug 2024 64,037.30 | 27 Aug 2024 61,484.10 | 4 | −$8.81 |
| short | 27 Aug 2024 59,414.90 | 10 Sep 2024 57,636.10 | 14 | $5.19 |
| long | 10 Sep 2024 57,636.10 | 9 Oct 2024 60,635.90 | 29 | $11.98 |
| short | 9 Oct 2024 60,635.90 | 11 Oct 2024 63,115.50 | 2 | −$11.00 |
| long | 11 Oct 2024 62,540.10 | 4 Nov 2024 67,849.90 | 24 | $22.39 |
| short | 4 Nov 2024 67,849.90 | 5 Nov 2024 69,372.10 | 1 | −$6.43 |
| long | 5 Nov 2024 69,372.10 | 2 Dec 2024 95,840.50 | 27 | $101.44 |
| short | 2 Dec 2024 95,840.50 | 4 Dec 2024 97,835.30 | 2 | −$4.93 |
| long | 4 Dec 2024 98,587.40 | 5 Dec 2024 96,945.50 | 1 | −$4.08 |
| short | 5 Dec 2024 96,945.50 | 6 Dec 2024 99,740.90 | 1 | −$7.43 |
| long | 6 Dec 2024 99,740.90 | 7 Dec 2024 99,831.90 | 1 | −$0.07 |
| short | 7 Dec 2024 99,831.90 | 8 Dec 2024 101,110.00 | 1 | −$3.40 |
| long | 8 Dec 2024 101,110.00 | 9 Dec 2024 97,276.40 | 1 | −$9.53 |
| short | 9 Dec 2024 97,276.40 | 11 Dec 2024 101,283.00 | 2 | −$10.78 |
| long | 16 Dec 2024 106,059.00 | 18 Dec 2024 101,417.00 | 2 | −$11.76 |
| short | 18 Dec 2024 100,204.00 | 6 Jan 2025 102,236.00 | 19 | −$4.26 |
| long | 6 Jan 2025 102,236.00 | 7 Jan 2025 96,954.50 | 1 | −$14.31 |
| short | 7 Jan 2025 96,954.50 | 17 Jan 2025 101,283.00 | 10 | −$10.28 |
| long | 17 Jan 2025 104,078.00 | 19 Jan 2025 100,203.00 | 2 | −$8.18 |
| short | 28 Jan 2025 101,335.00 | 29 Jan 2025 103,733.00 | 1 | −$5.91 |
| long | 29 Jan 2025 103,733.00 | 31 Jan 2025 102,429.00 | 2 | −$3.58 |
| short | 31 Jan 2025 102,429.00 | 20 Feb 2025 98,305.10 | 20 | $11.02 |
| long | 20 Feb 2025 98,305.10 | 21 Feb 2025 96,181.90 | 1 | −$10.91 |
| short | 21 Feb 2025 96,181.90 | 19 Mar 2025 86,846.00 | 26 | $39.54 |
| long | 19 Mar 2025 86,846.00 | 29 Mar 2025 82,151.30 | 10 | −$9.28 |
| short | 6 Apr 2025 78,429.90 | 9 Apr 2025 81,901.80 | 3 | −$9.86 |
| long | 9 Apr 2025 82,615.30 | 10 Apr 2025 79,607.20 | 1 | −$6.11 |
| short | 10 Apr 2025 79,607.20 | 11 Apr 2025 83,423.90 | 1 | −$7.48 |
| long | 11 Apr 2025 83,423.90 | 30 May 2025 103,985.00 | 49 | $37.19 |
| short | 30 May 2025 103,985.00 | 9 Jun 2025 108,263.00 | 10 | −$11.57 |
| long | 9 Jun 2025 110,263.00 | 11 Jun 2025 108,645.00 | 2 | −$4.79 |
| short | 11 Jun 2025 108,645.00 | 3 Jul 2025 109,585.00 | 22 | −$2.90 |
| long | 3 Jul 2025 109,585.00 | 4 Jul 2025 107,984.00 | 1 | −$5.39 |
| short | 4 Jul 2025 107,984.00 | 9 Jul 2025 110,795.00 | 5 | −$9.05 |
| long | 9 Jul 2025 111,234.00 | 30 Jul 2025 117,840.00 | 21 | $25.47 |
| short | 30 Jul 2025 117,840.00 | 11 Aug 2025 120,239.00 | 12 | −$12.80 |
| long | 12 Aug 2025 120,134.00 | 14 Aug 2025 117,355.00 | 2 | −$9.96 |
| short | 14 Aug 2025 118,295.00 | 10 Sep 2025 113,960.00 | 27 | $10.29 |
| long | 10 Sep 2025 113,960.00 | 22 Sep 2025 112,651.00 | 12 | −$4.89 |
| short | 22 Sep 2025 112,651.00 | 29 Sep 2025 114,312.00 | 7 | −$7.74 |
| long | 29 Sep 2025 114,312.00 | 10 Oct 2025 111,322.00 | 11 | −$9.27 |
| short | 10 Oct 2025 112,774.00 | 26 Oct 2025 114,560.00 | 16 | −$3.22 |
| long | 26 Oct 2025 114,560.00 | 29 Oct 2025 109,967.00 | 3 | −$13.46 |
| short | 29 Oct 2025 110,021.00 | 3 Dec 2025 93,430.10 | 35 | $46.82 |
| long | 3 Dec 2025 93,430.10 | 5 Dec 2025 89,757.60 | 2 | −$7.78 |
| short | 5 Dec 2025 89,329.90 | 7 Dec 2025 90,395.40 | 2 | −$2.70 |
| long | 7 Dec 2025 90,395.40 | 15 Dec 2025 87,059.50 | 8 | −$8.07 |
| short | 15 Dec 2025 86,432.00 | 16 Dec 2025 87,863.50 | 1 | −$3.97 |
| long | 16 Dec 2025 87,863.50 | 18 Dec 2025 84,561.00 | 2 | −$11.47 |
| short | 18 Dec 2025 85,516.30 | 19 Dec 2025 88,137.00 | 1 | −$10.03 |
| long | 19 Dec 2025 88,137.00 | 24 Jan 2026 89,225.20 | 36 | $3.48 |
| short | 24 Jan 2026 89,225.20 | 2 Mar 2026 68,830.20 | 37 | $60.17 |
| long | 2 Mar 2026 68,830.20 | 28 Mar 2026 66,376.90 | 26 | −$7.48 |
| short | 28 Mar 2026 66,376.90 | 30 Mar 2026 66,797.50 | 2 | −$1.53 |
| long | 30 Mar 2026 66,797.50 | 13 May 2026 79,313.50 | 44 | $43.01 |
| short | 13 May 2026 79,313.50 | 14 May 2026 81,090.10 | 1 | −$9.76 |
| long | 14 May 2026 81,090.10 | 15 May 2026 79,113.10 | 1 | −$10.07 |
| short | 15 May 2026 79,113.10 | 2 Jul 2026 61,560.10 | 48 | $80.30 |
| long | 2 Jul 2026 61,560.10 | 12 Aug 2026 63,479.90 | 41 | $9.02 |
| short | 12 Aug 2026 63,479.90 | 17 Aug 2026 64,532.20 | 5 | −$8.41 |
| long | 17 Aug 2026 64,532.20 | 10 Sep 2026 76,568.60 | 24 | $104.85 |
| short | 10 Sep 2026 76,568.60 | 18 Sep 2026 80,112.80 | 8 | −$12.86 |
| long | 18 Sep 2026 80,884.00 | 28 Sep 2026 83,499.90 | 10 | $8.41 |
| short | 28 Sep 2026 83,499.90 | 30 Sep 2026 85,619.00 | 2 | −$5.66 |
| long | 4 Oct 2026 86,530.10 | 5 Oct 2026 85,766.80 | 1 | −$2.79 |
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 |
|---|---|---|---|---|---|
| Crunchster's Normalised Trend Strategy this page | — | v5 strategy | +22.9% | 73 | 10.9% |
| BB+RSI+OBV by atakhadivi | cum sma stdev | v4 strategy | +52.5% | 71 | 29.3% |
| ADX_TSI_Bol Band Trend Chaser by gary_trades | sma stdev wma | v4 strategy | No trades on this market | ||
| meta-capitulation by serafimovkliment | sma cum | v5 strategy | No trades on this market | ||
| Double Bollinger Binary Options by Trade_by_DB | sma stdev | v5 strategy | No trades on this market | ||
| Improved Bollinger Swing Strategy Stock Nasdaq by exlux99 | sma stdev | v4 strategy | −20.4% | 7 | 22.3% |
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.
- 4 settings use the untyped input() (line 6, 10, 11, 15). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 73 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.source()
- Line 10: input() → input.bool()
- Line 11: input() → input.bool()
- Line 15: input() → input.bool()
⋯// Inputs and Parameters-src = input(close, 'Source', group='Strategy Settings')+src = input.source(close, 'Source', group='Strategy Settings')length = input.int(title="Lookback period for price normalisation filter", defval=14, minval=2, group='Strategy Settings', tooltip='This sets the lookback period for the volatility adjustment of returns, which is used to transform the price series into the "real price"')⋯offset = input.int(title="HMA Offset", defval=0, minval=0, group='Strategy Settings')-long = input(true, 'Long', inline='08', group='Strategy Settings')+long = input.bool(true, 'Long', inline='08', group='Strategy Settings')-short = input(true, 'Short', inline='08', group='Strategy Settings', tooltip='Toggle long/short strategy on/off')+short = input.bool(true, 'Short', inline='08', group='Strategy Settings', tooltip='Toggle long/short strategy on/off')stopMultiple = input.float(1, 'Stop multiple', step=0.25, group='Risk Management Settings', tooltip='Multiple for ATR, setting hard stop loss from entry price')⋯riskT = input.float(10, maxval=75, title='Annualised Volatility Target %', group='Risk Management Settings', tooltip='Specify annual risk target, used to determine volatility adjusted position size. Annualised daily volatility is referenced to this value and position size adjusted accordingly')-comp = input(false, 'Compounding', inline='09', group='Risk Management Settings')+comp = input.bool(false, 'Compounding', inline='09', group='Risk Management Settings')Comppct = input.float(50, '%', step=5, inline='09', group='Risk Management Settings', tooltip='Toggle compounding of profit, and set % of profit to compound')⋯Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 73 before, 73 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © Crunchster1-//@version=5+//@version=6strategy(title="Crunchster's Normalised Trend Strategy", shorttitle="Normalised Trend Strategy", overlay=false, slippage=10, pyramiding=1, precision = 4, calc_on_order_fills = false, calc_on_every_tick = false, default_qty_value = 0.1, initial_capital = 1000, commission_value = 0.06, process_orders_on_close = true)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 28 ms | With a workaround 1,000 bars in 76 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/// © Crunchster1//@version=5strategy(title="Crunchster's Normalised Trend Strategy", shorttitle="Normalised Trend Strategy", overlay=false, slippage=10, pyramiding=1, precision = 4, calc_on_order_fills = false, calc_on_every_tick = false, default_qty_value = 0.1, initial_capital = 1000, commission_value = 0.06, process_orders_on_close = true)// Inputs and Parameterssrc = input(close, 'Source', group='Strategy Settings')length = input.int(title="Lookback period for price normalisation filter", defval=14, minval=2, group='Strategy Settings', tooltip='This sets the lookback period for the volatility adjustment of returns, which is used to transform the price series into the "real price"')hlength = input.int(title="Lookback period for Hull Moving Average", defval=100, minval=2, group='Strategy Settings')offset = input.int(title="HMA Offset", defval=0, minval=0, group='Strategy Settings')long = input(true, 'Long', inline='08', group='Strategy Settings')short = input(true, 'Short', inline='08', group='Strategy Settings', tooltip='Toggle long/short strategy on/off')stopMultiple = input.float(1, 'Stop multiple', step=0.25, group='Risk Management Settings', tooltip='Multiple for ATR, setting hard stop loss from entry price')lev = input.float(1, 'Max Leverage', step=0.5, group='Risk Management Settings', tooltip='Max leverage sets maximum allowable leverage of total capital (initial capital + any net profit), capping maximum volatility adjusted position size')riskT = input.float(10, maxval=75, title='Annualised Volatility Target %', group='Risk Management Settings', tooltip='Specify annual risk target, used to determine volatility adjusted position size. Annualised daily volatility is referenced to this value and position size adjusted accordingly')comp = input(false, 'Compounding', inline='09', group='Risk Management Settings')Comppct = input.float(50, '%', step=5, inline='09', group='Risk Management Settings', tooltip='Toggle compounding of profit, and set % of profit to compound')// Backtesting periodFromDay = input.int(defval=1, title='From Day', minval=1, maxval=31, inline='04', group='Backtest range')FromMonth = input.int(defval=1, title='From Mon', minval=1, maxval=12, inline='04', group='Backtest range')FromYear = input.int(defval=2018, title='From Yr', minval=1900, inline='04', group='Backtest range', tooltip='Set start of backtesting period')ToDay = input.int(defval=1, title='To Day', minval=1, maxval=31, inline='05', group='Backtest range')ToMonth = input.int(defval=1, title='To Mon', minval=1, maxval=12, inline='05', group='Backtest range')ToYear = input.int(defval=9999, title='To Yr', minval=1900, inline='05', group='Backtest range', tooltip='Set end of backtesting period')start = timestamp(FromYear, FromMonth, FromDay, 00, 00)finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)window = time >= start and time <= finish// Normalised returns calculationnRet = (src - src[1]) / ta.stdev((src - src[1]), length)nPrice = ta.cum(nRet)//Hull Moving Average - using normalised price seriesfHMA = ta.wma(2 * ta.wma(nPrice[offset], hlength / 2) - ta.wma(nPrice[offset], hlength), math.round(math.sqrt(hlength)))//Risk Management formulaetr = math.max(high - low, math.abs(high - close), math.abs(low - close)) //True rangestopL = ta.sma(tr, 14) //Average true rangestdev = ta.stdev(close-close[1], 14) //volatility of recent returnsmaxcapital = strategy.initial_capital+strategy.netprofit //Maximum capital available to invest - initial capital net of profitannvol = 100*math.sqrt(365)*stdev/close //converts recent volatility of returns into annualised volatility of returns - assumes daily timeframerisk = 1.1if comp risk := (strategy.initial_capital+(Comppct*strategy.netprofit/100))//adjust investment capital to include compoundingelse risk := strategy.initial_capitalshares = (risk * (riskT/annvol)) / close //calculates volatility adjusted position size, dependent on user specified annualised risk targetif ((shares*close) > lev*maxcapital) //ensures position size does not exceed available capital multiplied by user specified maximum leverage shares := lev*maxcapital/close//To set the price at the entry point of tradePosopen() => math.abs(strategy.position_size[1]) <= 0 and math.abs(strategy.position_size) > 0var float openN = naif Posopen() openN := stopL// Strategy Rulesif long longCondition = ta.crossover(nPrice, fHMA) and window exitlong = ta.crossunder(nPrice, fHMA) if (longCondition) strategy.entry('Go Long!', strategy.long, qty=shares) if strategy.position_size > 0 strategy.exit('Stop Long', from_entry = 'Go Long!', stop=(strategy.opentrades.entry_price(0) - (openN * stopMultiple))) if (exitlong) strategy.close('Go Long!', immediately = true)if short shortCondition = ta.crossunder(nPrice, fHMA) and window exitshort = ta.crossover(nPrice, fHMA) if (shortCondition) strategy.entry('Go Short!', strategy.short, qty=shares) if strategy.position_size < 0 strategy.exit('Stop Short', from_entry = 'Go Short!', stop=(strategy.opentrades.entry_price(0) + (openN * stopMultiple))) if (exitshort) strategy.close('Go Short!', immediately = true)// Visuals of trend and directionplot(nPrice, title='Real Price', color=color.black)MAColor = fHMA > fHMA[3] ? #00ff00 : #ff0000MA1 = plot(fHMA, title='Hull MA', color=MAColor)MA2 = plot(fHMA[3], title='Hull MA Offset', color=MAColor)fill(MA1, MA2, title='Band Filler', color=MAColor)