Pine Script v5 · Strategy
KAMA Strategy - Kaufman's Adaptive Moving Average
A Pine Script v5 strategy built on Change (close, 14), Stdev (kama1delta, 14) and EMA (close, 14). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100% of equity and pays no commission. 37 settings; defaults include Source close, KAMA 1: Length 14 and KAMA 1: Fast KAMA Length 2.
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 KAMA Strategy - Kaufman's Adaptive Moving Average 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 KAMA Strategy - Kaufman's Adaptive Moving Average do on BTCUSDT 1D with default settings?
- +51.13%
- From 26 Jan 2024 to 21 Oct 2026, over 20 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does KAMA Strategy - Kaufman's Adaptive Moving Average 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: 20 before, 18 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 KAMA Strategy - Kaufman's Adaptive Moving Average use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does KAMA Strategy - Kaufman's Adaptive Moving Average run in Zeal Terminal?
- Yes
- It compiles in 35 ms and runs 1,000 bars in 142 ms, in the browser.
BTCUSDT · 1D · default settings
- KAMA 1
- KAMA 2
- KAMA 3
- KAMA 4
- KAMA 5
- KAMA 6
- KAMA 7
- KAMA 8
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +51.13%
- $5,113.48
- Buy and hold
- +102.5%
- same period
- Closed trades
- 20
- 8 won · 12 lost
- Profitable
- 40.0%
- of closed trades
- Profit factor
- 1.53
- gross profit ÷ gross loss
- Max drawdown
- 34.62%
- −$6,302.44
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. 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.
-
Check
1 setting does nothing
“Trail Mode” is declared but never read.
-
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.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 124, 127. The upgraded version below moves each call inside an if block.
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 |
|---|---|---|---|
| Source | close | source | mom volatility kama kama1 kama2 kama3 kama4 kama5 kama6 kama7 kama8 srcEma |
| KAMA 1 Stop Loss | true | bool | longClose |
| ATR Trailing Stop Loss | false | bool | plotLow longClose |
| PSAR Stop Loss | false | bool | psarPlot longClose |
| KAMA 1: Length | 14 | int | mom volatility kama1 entryMAAF exitMAAF srcEma |
| KAMA 1: Fast KAMA Length | 2 | int | fastAlpha kama1 |
| KAMA 1: Slow KAMA Length | 20 | int | slowAlpha kama1 |
| KAMA 2: Length 2 | 15 | int | kama2 |
| KAMA 2: Fast KAMA Length | 3 | int | kama2 |
| KAMA 2: Slow KAMA Length | 22 | int | kama2 |
| KAMA 3: Length 3 | 16 | int | kama3 |
| KAMA 3: Fast KAMA Length | 4 | int | kama3 |
| KAMA 3: Slow KAMA Length | 24 | int | kama3 |
| KAMA 4: Length | 17 | int | kama4 |
| KAMA 4: Fast KAMA Length | 5 | int | kama4 |
| KAMA 4: Slow KAMA Length | 26 | int | kama4 |
| KAMA 5: Length | 18 | int | kama5 |
| KAMA 5: Fast KAMA Length | 6 | int | kama5 |
| KAMA 5: Slow KAMA Length | 28 | int | kama5 |
| KAMA 6: Length | 19 | int | kama6 |
| KAMA 6: Fast KAMA Length | 7 | int | kama6 |
| KAMA 6: Slow KAMA Length | 30 | int | kama6 |
| KAMA 7: Length | 20 | int | kama7 |
| KAMA 7: Fast KAMA Length | 8 | int | kama7 |
| KAMA 7: Slow KAMA Length | 32 | int | kama7 |
| KAMA 8: Length | 21 | int | kama8 |
| KAMA 8: Fast KAMA Length | 9 | int | kama8 |
| KAMA 8: Slow KAMA Length | 34 | int | kama8 |
| KAMA Entry Filter | 1 | float · min 0.01 | entryMAAF |
| KAMA Exit Filter | 0.5 | float · min 0.01 | exitMAAF |
| Trailing ATR Lookback Period | 14 | int | atrValue |
| Trailing ATR Multiplier | 3 | float · min 0.5 · max 4 · step 0.1 | atrMultiplied |
| Trail Mode | Trailing | string · options ['Running', 'Trailing'] | unused |
| Trigger Trailing Stop On | Wick | string · options ['Close', 'Wick'] | trigSupport |
| PSAR Start | 0.02 | float | psar |
| PSAR Increment | 0.02 | float | psar |
| PSAR Max Value | 0.2 | float | psar |
What it calculates
- ChangeSource (close), KAMA 1: Length (14)
mom · line 47 - ChangeSource (close)
volatility · line 48 - Stdevkama1delta, KAMA 1: Length (14)
entryMAAF · line 84 - Stdevkama1delta, KAMA 1: Length (14)
exitMAAF · line 85 - EMASource (close), KAMA 1: Length (14)
srcEma · line 86 - ATRTrailing ATR Lookback Period (14)
atrValue · line 96 - SARPSAR Start (0.02), PSAR Increment (0.02), PSAR Max Value (0.2)
psar · line 118 - Crossunderclose, psar
longClose · line 126
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.
The script has 79 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Buy long | longkama1delta > 0 and kama1delta > entryMAAF | On | 124 |
| Close Buy | longClose | On | 127 |
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 | +51.1% | +102.5% | 20 | 40% | 1.53 | 34.6% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −5.6% | +5.5% | 20 | 30% | 0.83 | 20.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +27.3% | +9.8% | 20 | 35% | 1.21 | 41.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −1.9% | +8.4% | 20 | 25% | 0.96 | 19.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +6.4% | +16.7% | 18 | 33% | 1.05 | 54.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +2.7% | +21.5% | 20 | 30% | 1.05 | 20.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +61.9% | +148.3% | 20 | 25% | 1.54 | 41.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −1.6% | +17.1% | 21 | 29% | 0.93 | 15.5% | 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 (KAMA 1: Length 10, KAMA Entry Filter 1.7) made +100.9% before 26 Dec 2025 and +16.4% after it, against +16.2% for the defaults. 100% of the 64 combinations made money in-sample and 56% out-of-sample.
| KAMA Entry Filter ↓ · KAMA 1: Length → | 7 | 8 | 10 | 12 | 14 | 17 | 20 | 24 |
|---|---|---|---|---|---|---|---|---|
| 0.5 | +42% | +70% | +98% | +41% | +49% | +36% | +14% | +35% |
| 0.6 | +41% | +60% | +99% | +43% | +46% | +36% | +10% | +35% |
| 0.7 | +37% | +60% | +97% | +47% | +45% | +30% | +10% | +35% |
| 0.85 | +47% | +64% | +99% | +61% | +50% | +22% | +8% | +32% |
| 1 | +60% | +51% | +98% | +58% | +35% | +17% | +4% | +32% |
| 1.2 | +47% | +51% | +93% | +56% | +35% | +16% | +2% | +25% |
| 1.4 | +49% | +56% | +91% | +49% | +23% | +22% | +8% | +21% |
| 1.7 | +49% | +61% | +101% | +54% | +20% | +19% | +12% | +22% |
| KAMA Entry Filter ↓ · KAMA 1: Length → | 7 | 8 | 10 | 12 | 14 | 17 | 20 | 24 |
|---|---|---|---|---|---|---|---|---|
| 0.5 | +23% | +29% | −3% | +11% | +23% | +11% | −1% | −17% |
| 0.6 | +22% | +15% | −4% | +11% | +21% | +11% | −2% | −20% |
| 0.7 | +21% | +13% | +3% | +9% | +20% | −1% | −2% | −20% |
| 0.85 | +9% | +12% | +3% | −2% | +21% | −1% | −9% | −20% |
| 1 | +10% | +8% | +1% | −2% | +16% | −1% | −10% | −23% |
| 1.2 | +9% | +8% | +17% | −0% | +4% | +1% | −10% | −23% |
| 1.4 | +10% | +8% | +16% | −11% | +3% | +1% | −10% | −24% |
| 1.7 | +10% | +8% | +16% | −16% | −7% | −0% | −11% | −13% |
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 | 5 | 60% | −$221.55 |
| Sideways market | 10 | 30% | $4,188.95 |
| Falling market | 5 | 40% | $1,146.08 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
39.0%
The 95th percentile of the worst drawdown when the same 20 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 18.5% to 39.0%; the tick is what actually happened, 33.4%. Order luck alone moves it this much.
All 20 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 10 Feb 2024 47,132.80 | 3 Apr 2024 65,464.00 | 53 | $3,889.27 |
| long | 9 Apr 2024 71,620.00 | 13 Apr 2024 67,116.50 | 4 | −$873.36 |
| long | 16 May 2024 66,206.50 | 14 Jun 2024 66,773.00 | 29 | $111.37 |
| long | 19 Jul 2024 63,987.90 | 3 Aug 2024 61,498.30 | 15 | −$510.74 |
| long | 22 Aug 2024 61,156.00 | 2 Sep 2024 57,301.80 | 11 | −$795.14 |
| long | 20 Sep 2024 62,948.00 | 10 Oct 2024 60,636.00 | 20 | −$434.18 |
| long | 15 Oct 2024 66,084.00 | 23 Dec 2024 95,186.30 | 69 | $5,014.74 |
| long | 7 Jan 2025 102,236.00 | 5 Feb 2025 97,763.10 | 29 | −$717.53 |
| long | 23 Apr 2025 93,443.00 | 6 Jun 2025 101,509.00 | 44 | $1,353.83 |
| long | 30 Jun 2025 108,357.00 | 1 Aug 2025 115,764.00 | 32 | $1,164.71 |
| long | 14 Aug 2025 123,306.00 | 20 Aug 2025 112,873.00 | 6 | −$1,540.23 |
| long | 13 Sep 2025 116,029.00 | 26 Sep 2025 108,994.00 | 13 | −$1,010.27 |
| long | 4 Oct 2025 122,232.00 | 16 Oct 2025 110,763.00 | 12 | −$1,468.66 |
| long | 10 Dec 2025 92,678.80 | 15 Dec 2025 88,172.20 | 5 | −$689.71 |
| long | 3 Jan 2026 89,995.10 | 21 Jan 2026 88,427.70 | 18 | −$235.03 |
| long | 5 Mar 2026 72,666.80 | 28 Mar 2026 66,407.30 | 23 | −$1,142.13 |
| long | 10 Apr 2026 71,788.00 | 20 May 2026 76,834.40 | 40 | $851.77 |
| long | 15 Jul 2026 65,044.00 | 14 Aug 2026 63,490.90 | 30 | −$309.67 |
| long | 18 Aug 2026 64,532.10 | 11 Sep 2026 76,568.70 | 24 | $2,361.18 |
| long | 20 Sep 2026 81,250.00 | 9 Oct 2026 81,754.50 | 19 | $93.26 |
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 |
|---|---|---|---|---|---|
| KAMA Strategy - Kaufman's Adaptive Moving Average this page | — | v5 strategy | +51.1% | 20 | 34.6% |
| Trend #2 - BB+EMA by zxcv55602 | ema stdev sar | v4 strategy | −0.6% | 14 | 3.9% |
| Trend Following with Bollinger Bands by Robrecht99 | stdev ema atr | v5 strategy | +2.5% | 7 | 1.7% |
| [KL] BOLL + MACD Strategy v2 (published) by DojiEmoji | stdev ema atr | v4 strategy | −32.1% | 28 | 54.0% |
| [EURUSD60] BB Expansion Strategy by kasaism | stdev ema atr | v4 strategy | No trades on this market | ||
| 888 BOT #backtest by UnknownUnicorn2151907 | ema sar stdev atr | v4 strategy | +3.0% | 12 | 0.9% |
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.
- 2 strategy calls use the when = argument (line 124, 127). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 32 settings use the untyped input() (line 13, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 91, 115, 116, 117). 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 20 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 13: input() → input.source()
- Line 15: input() → input.bool()
- Line 16: input() → input.bool()
- Line 17: input() → input.bool()
- Line 19: input() → input.int()
- Line 20: input() → input.int()
28 more edits
- Line 21: input() → input.int()
- Line 22: input() → input.int()
- Line 23: input() → input.int()
- Line 24: input() → input.int()
- Line 25: input() → input.int()
- Line 26: input() → input.int()
- Line 27: input() → input.int()
- Line 28: input() → input.int()
- Line 29: input() → input.int()
- Line 30: input() → input.int()
- Line 32: input() → input.int()
- Line 33: input() → input.int()
- Line 34: input() → input.int()
- Line 35: input() → input.int()
- Line 36: input() → input.int()
- Line 37: input() → input.int()
- Line 38: input() → input.int()
- Line 39: input() → input.int()
- Line 40: input() → input.int()
- Line 42: input() → input.int()
- Line 43: input() → input.int()
- Line 44: input() → input.int()
- Line 91: input() → input.int()
- Line 115: input() → input.float()
- Line 116: input() → input.float()
- Line 117: input() → input.float()
- Line 124: when = long → if block
- Line 127: when = longClose → if block
⋯strategy(title='KAMA Strategy - Kaufman\'s Adaptive Moving Average', shorttitle='KAMA Strategy', overlay=true, default_qty_value=100, default_qty_type=strategy.percent_of_equity, initial_capital=10000, max_bars_back=1000)-src = input(title='Source', defval=close)+src = input.source(title='Source', defval=close)// Exits-KAMA1SL = input(title='KAMA 1 Stop Loss', defval=true)+KAMA1SL = input.bool(title='KAMA 1 Stop Loss', defval=true)-ATRTSL = input(title='ATR Trailing Stop Loss', defval=false)+ATRTSL = input.bool(title='ATR Trailing Stop Loss', defval=false)-PSARSL = input(title='PSAR Stop Loss', defval=false)+PSARSL = input.bool(title='PSAR Stop Loss', defval=false)// KAMA 1 (Fastest)-length1 = input(title='KAMA 1: Length', defval=14)+length1 = input.int(title='KAMA 1: Length', defval=14)-fastLength1 = input(title='KAMA 1: Fast KAMA Length', defval=2)+fastLength1 = input.int(title='KAMA 1: Fast KAMA Length', defval=2)-slowLength1 = input(title='KAMA 1: Slow KAMA Length', defval=20)+slowLength1 = input.int(title='KAMA 1: Slow KAMA Length', defval=20)-length2 = input(title='KAMA 2: Length 2', defval=15)+length2 = input.int(title='KAMA 2: Length 2', defval=15)-fastLength2 = input(title='KAMA 2: Fast KAMA Length', defval=3)+fastLength2 = input.int(title='KAMA 2: Fast KAMA Length', defval=3)-slowLength2 = input(title='KAMA 2: Slow KAMA Length', defval=22)+slowLength2 = input.int(title='KAMA 2: Slow KAMA Length', defval=22)-length3 = input(title='KAMA 3: Length 3', defval=16)+length3 = input.int(title='KAMA 3: Length 3', defval=16)-fastLength3 = input(title='KAMA 3: Fast KAMA Length', defval=4)+fastLength3 = input.int(title='KAMA 3: Fast KAMA Length', defval=4)-slowLength3 = input(title='KAMA 3: Slow KAMA Length', defval=24)+slowLength3 = input.int(title='KAMA 3: Slow KAMA Length', defval=24)-length4 = input(title='KAMA 4: Length', defval=17)+length4 = input.int(title='KAMA 4: Length', defval=17)-fastLength4 = input(title='KAMA 4: Fast KAMA Length', defval=5)+fastLength4 = input.int(title='KAMA 4: Fast KAMA Length', defval=5)-slowLength4 = input(title='KAMA 4: Slow KAMA Length', defval=26)+slowLength4 = input.int(title='KAMA 4: Slow KAMA Length', defval=26)// KAMA 5 (Medium)-length5 = input(title='KAMA 5: Length', defval=18)+length5 = input.int(title='KAMA 5: Length', defval=18)-fastLength5 = input(title='KAMA 5: Fast KAMA Length', defval=6)+fastLength5 = input.int(title='KAMA 5: Fast KAMA Length', defval=6)-slowLength5 = input(title='KAMA 5: Slow KAMA Length', defval=28)+slowLength5 = input.int(title='KAMA 5: Slow KAMA Length', defval=28)-length6 = input(title='KAMA 6: Length', defval=19)+length6 = input.int(title='KAMA 6: Length', defval=19)-fastLength6 = input(title='KAMA 6: Fast KAMA Length', defval=7)+fastLength6 = input.int(title='KAMA 6: Fast KAMA Length', defval=7)-slowLength6 = input(title='KAMA 6: Slow KAMA Length', defval=30)+slowLength6 = input.int(title='KAMA 6: Slow KAMA Length', defval=30)-length7 = input(title='KAMA 7: Length', defval=20)+length7 = input.int(title='KAMA 7: Length', defval=20)-fastLength7 = input(title='KAMA 7: Fast KAMA Length', defval=8)+fastLength7 = input.int(title='KAMA 7: Fast KAMA Length', defval=8)-slowLength7 = input(title='KAMA 7: Slow KAMA Length', defval=32)+slowLength7 = input.int(title='KAMA 7: Slow KAMA Length', defval=32)// KAMA 8 (Slowest)-length8 = input(title='KAMA 8: Length', defval=21)+length8 = input.int(title='KAMA 8: Length', defval=21)-fastLength8 = input(title='KAMA 8: Fast KAMA Length', defval=9)+fastLength8 = input.int(title='KAMA 8: Fast KAMA Length', defval=9)-slowLength8 = input(title='KAMA 8: Slow KAMA Length', defval=34)+slowLength8 = input.int(title='KAMA 8: Slow KAMA Length', defval=34)// Kaufman's Adaptive Moving Average⋯// Inputs-atrLookback = input(defval=14, title='Trailing ATR Lookback Period')+atrLookback = input.int(defval=14, title='Trailing ATR Lookback Period')multiplier = input.float(defval=3, title='Trailing ATR Multiplier', step=0.1, minval=0.5, maxval=4)⋯//====================================== PSAR STOP ==========================================-start = input(0.02, 'PSAR Start')+start = input.float(0.02, 'PSAR Start')-increment = input(0.02, 'PSAR Increment')+increment = input.float(0.02, 'PSAR Increment')-maximum = input(0.2, 'PSAR Max Value')+maximum = input.float(0.2, 'PSAR Max Value')psar = ta.sar(start, increment, maximum)⋯long = kama1delta > 0 and kama1delta > entryMAAF-strategy.entry('Buy', strategy.long, when=long)+if long+ strategy.entry('Buy', strategy.long)// CloselongClose = (PSARSL ? ta.crossunder(close, psar) : na) or (KAMA1SL ? kama1delta < 0 and math.abs(kama1delta) > exitMAAF : na) or (ATRTSL ? supportHit : na)-strategy.close('Buy', when=longClose, comment='Sell')+if longClose+ strategy.close('Buy', comment='Sell')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: 20 before, 18 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.
-//@version=5+//@version=6// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 35 ms | Yes 1,000 bars in 142 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
“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.
//@version=5// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © BenHampson// Credit to:// - ChuckBanger for much of the KAMA code// - cheatcountry for the KAMA Filter code// - millerrh for much of the ATR Stop code// - racer8 for much of the Position Sizing code// I have combined aspects of their work and built upon it to form a strategy I like. // The KAMA, with its filter, is used for entry.// An ATR trailing stop loss, PSAR, and the KAMA can all optionally be used as exits, or you can use a combination of the three.strategy(title='KAMA Strategy - Kaufman\'s Adaptive Moving Average', shorttitle='KAMA Strategy', overlay=true, default_qty_value=100, default_qty_type=strategy.percent_of_equity, initial_capital=10000, max_bars_back=1000)src = input(title='Source', defval=close)// ExitsKAMA1SL = input(title='KAMA 1 Stop Loss', defval=true)ATRTSL = input(title='ATR Trailing Stop Loss', defval=false)PSARSL = input(title='PSAR Stop Loss', defval=false)// KAMA 1 (Fastest)length1 = input(title='KAMA 1: Length', defval=14)fastLength1 = input(title='KAMA 1: Fast KAMA Length', defval=2)slowLength1 = input(title='KAMA 1: Slow KAMA Length', defval=20)length2 = input(title='KAMA 2: Length 2', defval=15)fastLength2 = input(title='KAMA 2: Fast KAMA Length', defval=3)slowLength2 = input(title='KAMA 2: Slow KAMA Length', defval=22)length3 = input(title='KAMA 3: Length 3', defval=16)fastLength3 = input(title='KAMA 3: Fast KAMA Length', defval=4)slowLength3 = input(title='KAMA 3: Slow KAMA Length', defval=24)length4 = input(title='KAMA 4: Length', defval=17)fastLength4 = input(title='KAMA 4: Fast KAMA Length', defval=5)slowLength4 = input(title='KAMA 4: Slow KAMA Length', defval=26)// KAMA 5 (Medium)length5 = input(title='KAMA 5: Length', defval=18)fastLength5 = input(title='KAMA 5: Fast KAMA Length', defval=6)slowLength5 = input(title='KAMA 5: Slow KAMA Length', defval=28)length6 = input(title='KAMA 6: Length', defval=19)fastLength6 = input(title='KAMA 6: Fast KAMA Length', defval=7)slowLength6 = input(title='KAMA 6: Slow KAMA Length', defval=30)length7 = input(title='KAMA 7: Length', defval=20)fastLength7 = input(title='KAMA 7: Fast KAMA Length', defval=8)slowLength7 = input(title='KAMA 7: Slow KAMA Length', defval=32)// KAMA 8 (Slowest)length8 = input(title='KAMA 8: Length', defval=21)fastLength8 = input(title='KAMA 8: Fast KAMA Length', defval=9)slowLength8 = input(title='KAMA 8: Slow KAMA Length', defval=34)// Kaufman's Adaptive Moving AveragegetKAMA(src, length1, fastLength1, slowLength1) => mom = math.abs(ta.change(src, length1)) volatility = math.sum(math.abs(ta.change(src)), length1) // Efficiency Ratio er = volatility != 0 ? mom / volatility : 0 fastAlpha = 2 / (fastLength1 + 1) slowAlpha = 2 / (slowLength1 + 1) // KAMA Alpha sc = math.pow(er * (fastAlpha - slowAlpha) + slowAlpha, 2) kama = 0.0 kama := sc * src + (1 - sc) * nz(kama[1]) kamakama1 = getKAMA(src, length1, fastLength1, slowLength1)kama2 = getKAMA(src, length2, fastLength2, slowLength2)kama3 = getKAMA(src, length3, fastLength3, slowLength3)kama4 = getKAMA(src, length4, fastLength4, slowLength4)kama5 = getKAMA(src, length5, fastLength5, slowLength5)kama6 = getKAMA(src, length6, fastLength6, slowLength6)kama7 = getKAMA(src, length7, fastLength7, slowLength7)kama8 = getKAMA(src, length8, fastLength8, slowLength8)//If the kama1 has increased...kama1delta = kama1[0] - kama1[1]kama3delta = kama3[0] - kama3[1]kama8delta = kama8[0] - kama8[1]// KAMA Plotsplot(kama1, title='KAMA 1', color=color.new(#e91e63, 0), display=display.all, linewidth=2)plot(kama2, title='KAMA 2', color=color.new(color.red, 0), display=display.all)plot(kama3, title='KAMA 3', color=color.new(color.red, 0), display=display.all)plot(kama4, title='KAMA 4', color=color.new(color.orange, 0), display=display.all)plot(kama5, title='KAMA 5', color=color.new(color.orange, 0), display=display.all)plot(kama6, title='KAMA 6', color=color.new(color.yellow, 0), display=display.all)plot(kama7, title='KAMA 7', color=color.new(color.yellow, 0), display=display.all)plot(kama8, title='KAMA 8', color=color.new(color.white, 0), display=display.all)//========================================= KAMA FILTER ===========================================// Copyright (c) 2019-present, Franklin Moormann (cheatcountry)// Moving Average Adaptive Filter [CC] script may be freely distributed under the MIT license.entryFilter = input.float(title='KAMA Entry Filter', defval=1, minval=0.01)exitFilter = input.float(title='KAMA Exit Filter', defval=0.5, minval=0.01)entryMAAF = entryFilter * ta.stdev(kama1delta, length1)exitMAAF = exitFilter * ta.stdev(kama1delta, length1)srcEma = ta.ema(src, length1)//========================================= TRAILING ATR STOP ====================================// The following is an adaptation of Trailing ATR Stops by @millerrh// He based it on scripts by @garethyeo & @SimpleCryptoLife// InputsatrLookback = input(defval=14, title='Trailing ATR Lookback Period')multiplier = input.float(defval=3, title='Trailing ATR Multiplier', step=0.1, minval=0.5, maxval=4)trailMode = input.string(title='Trail Mode', defval='Trailing', options=['Running', 'Trailing'])trigInput = input.string(title='Trigger Trailing Stop On', defval='Wick', options=['Close', 'Wick'])// Calculate ATRatrValue = ta.atr(atrLookback)atrMultiplied = atrValue * multiplier// Plot the price minus the ATRatrLow = low - atrMultiplied// Calculate the low trailing ATRs every time. The trailing stop loss never goes down.// Set them to something to start withtrailAtrLow = atrLow// If the ATR Low has gone up AND it has gone above the trail, the low trailing ATR should also go up. If the ATR Low has gone up or down, but not below the trail, the ATR trail stays where it istrailAtrLow := na(trailAtrLow[1]) ? trailAtrLow : atrLow >= trailAtrLow[1] ? atrLow : trailAtrLow[1]// Trigger stop based on candle close or lowtrigSupport = trigInput == 'Close' ? close : trigInput == 'Wick' ? low : na// Determine if price is below supportsupportHit = trigSupport <= trailAtrLow// If price is below support, reset the trailing ATRtrailAtrLow := supportHit ? atrLow : trailAtrLow// Plot LinesplotLow = ATRTSL ? trailAtrLow : naplot(plotLow, title='ATR Low', color=color.new(color.white, 50), style=plot.style_linebr, linewidth=1, display=display.all)//====================================== PSAR STOP ==========================================start = input(0.02, 'PSAR Start')increment = input(0.02, 'PSAR Increment')maximum = input(0.2, 'PSAR Max Value')psar = ta.sar(start, increment, maximum)psarPlot = PSARSL ? psar : naplot(psarPlot, 'Parabolic SAR', style=plot.style_cross, color=color.new(#3A6CA8, 0), display=display.all)//========================================= ENTRY & EXITS =====================================================// Entrylong = kama1delta > 0 and kama1delta > entryMAAFstrategy.entry('Buy', strategy.long, when=long)// CloselongClose = (PSARSL ? ta.crossunder(close, psar) : na) or (KAMA1SL ? kama1delta < 0 and math.abs(kama1delta) > exitMAAF : na) or (ATRTSL ? supportHit : na)strategy.close('Buy', when=longClose, comment='Sell')