Pine Script v5 · Strategy
Multi MA MTF SandBox Strategy
A Pine Script v5 strategy built on SMA or EMA or WMA or HMA or ALMA (src, length), Change (src, length) and EMA (src, length). It has 7 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 3500 in cash and pays no commission. 22 settings; defaults include What trades should be taken: LONG, First Slow moving average EMA and First MA Length 6.
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 Multi MA MTF SandBox Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 15 of 15 trades.
- How did Multi MA MTF SandBox Strategy do on BTCUSDT 1D with default settings?
- +17.35%
- From 26 Jan 2024 to 21 Oct 2026, over 48 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Multi MA MTF SandBox Strategy run in Pine Script v6?
- Yes
- Same 48 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 Multi MA MTF SandBox Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 179, 181.
- Does Multi MA MTF SandBox Strategy run in Zeal Terminal?
- Yes
- It compiles in 48 ms and runs 1,000 bars in 128 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +17.35%
- $1,735.27
- Buy and hold
- +102.5%
- same period
- Closed trades
- 48
- 17 won · 31 lost
- Profitable
- 35.4%
- of closed trades
- Profit factor
- 1.54
- gross profit ÷ gross loss
- Max drawdown
- 9.46%
- −$1,012.05
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
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 |
|---|---|---|---|
| What trades should be taken: | LONG | string · options ['LONG', 'SHORT', 'BOTH'] | long short |
| Act as indicator (will disable strategy functionality) | false | bool | longShape shortShape entry long entry short line 167 line 169 |
| Start Date | timestamp('1 JAN 2020 09:30:00 +0000') | time | line 26 |
| End Date | timestamp('1 JAN 2030 09:30:00 +0000') | time | line 26 |
| Moving Average 1 (Blue) | |||
| First Slow moving average | EMA | string · options ['SMA', 'EMA', 'WMA', 'HMA', 'JMA', 'KAMA', 'TMA', 'VAMA', 'SMMA', 'DEMA', 'VMA', 'WWMA', 'EMA_NO_LAG', 'TSF', 'ALMA'] | ma_fast |
| First Time Frame | | timeframe | maFastStep |
| First MA Length | 6 | int | ma_fast |
| Moving Average 2 (Yellow) | |||
| Second Fast moving average | JMA | string · options ['SMA', 'EMA', 'WMA', 'HMA', 'JMA', 'KAMA', 'TMA', 'VAMA', 'SMMA', 'DEMA', 'VMA', 'WWMA', 'EMA_NO_LAG', 'TSF', 'ALMA'] | ma_slow |
| Second time frame | | timeframe | maSlowStep |
| Second MA length | 14 | int | ma_slow |
| Visual Settings | |||
| Line Width | 2 | int | ma1_plot ma2_plot plot |
| Fast Line Color (or Buy) | color.new(#26c6da, 40) | value | colors_setting ma1_plot plot |
| Slow Line Color (or Sell) | color.new(color.yellow, 40) | value | colors_setting ma2_plot plot |
| Change line color based on signalChange line color based on Buy or Sell Signal. Effected by Fast/Slow Line Color settings above. | true | bool | ma1_plot ma2_plot plot |
| Fill Color | true | bool | fill |
| Moving Average Parameters | |||
| Alma Offset (only for ALMA) | 0.85 | float · step 0.05 | ma |
| Volatility lookback (only for VAMA) | 12 | int | vol_up vol_down |
| KAMA\'s alpha (only for KAMA) | 1.25 | float · min 1 · step 0.25 | fastAlpha |
| Stop Loss | |||
| Use Stop Loss | false | bool | exit Take Profit exit Stop loss exit Take Profit/Stop Loss line 175 line 176 line 178 line 180 |
| Stop Loss % | 2.5 | float · step 0.1 | stop_level |
| Take Profit | |||
| Use Take Profit | false | bool | exit Take Profit exit Stop loss exit Take Profit/Stop Loss line 175 line 176 line 178 line 180 |
| Take Profit % | 5 | float · step 0.1 | take_level |
What it calculates
- SMA / EMA / WMA / HMA / ALMAsrc, length
ma · line 47 - Changesrc, length
momentum · line 74 - Changesrc
volatility · line 75 - EMAsrc, length
EMA1 · line 103 - EMAEMA1, length
EMA2 · line 104 - Linregsrc, length, 0
lrc · line 109 - Linregsrc, length, 1
lrc1 · line 110 - Linregsrc, length, 0
TSF · line 112 - EMAsrc, length
mid · line 116 - Highestdev, Volatility lookback (only for VAMA) (12)
vol_up · line 118 - Lowestdev, Volatility lookback (only for VAMA) (12)
vol_down · line 119 - SMAsrc, length
smaval · line 124 - EMAsrc, length
e1 · line 129 - EMAe1, length
e2 · line 130 - CrossundermaFastStep, maSlowStep
longClose · line 150 - CrossovermaFastStep, maSlowStep
shortClose · line 153
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 74 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 long long | long and window() and act_like_indicator==false | On | 168 |
| Open short short | short and window() and act_like_indicator==false | On | 170 |
| Close long | (longClose) | On | 172 |
| Close short | (shortClose) | On | 174 |
| Exit Take Profit limit | (useTakeprofit and useStopLoss==false) | On | 177 |
| Exit Stop loss stop | (useTakeprofit==false and useStopLoss) | On | 179 |
| Exit Take Profit/Stop Loss stop and limit | (useTakeprofit and useStopLoss) | On | 181 |
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 data2 request.security call(s), none with lookahead on.
- 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 | +17.4% | +102.5% | 48 | 35% | 1.54 | 9.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +2.6% | +5.5% | 51 | 27% | 1.20 | 3.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +7.0% | +9.8% | 49 | 33% | 1.11 | 16.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +1.5% | +8.4% | 50 | 22% | 1.08 | 6.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +9.5% | +16.7% | 44 | 39% | 1.15 | 22.5% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +8.4% | +21.5% | 47 | 32% | 1.61 | 4.2% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −1.5% | +148.3% | 47 | 36% | 0.96 | 17.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +5.0% | +17.1% | 48 | 33% | 1.47 | 2.8% | 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 | 17 | 35% | $774.43 |
| Sideways market | 21 | 43% | $1,250.77 |
| Falling market | 10 | 20% | −$289.92 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
14.4%
The 95th percentile of the worst drawdown when the same 48 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 6.2% to 14.4%; the tick is what actually happened, 9.0%. Order luck alone moves it this much.
All 48 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 27 Feb 2024 54,476.50 | 17 Mar 2024 65,300.60 | 19 | $695.43 |
| long | 26 Mar 2024 69,880.00 | 3 Apr 2024 65,464.00 | 8 | −$221.18 |
| long | 8 Apr 2024 69,360.40 | 13 Apr 2024 67,116.50 | 5 | −$113.23 |
| long | 23 Apr 2024 66,819.30 | 27 Apr 2024 63,770.00 | 4 | −$159.72 |
| long | 6 May 2024 64,012.00 | 9 May 2024 61,193.00 | 3 | −$154.13 |
| long | 10 May 2024 63,074.00 | 11 May 2024 60,800.00 | 1 | −$126.19 |
| long | 16 May 2024 66,206.50 | 30 May 2024 67,652.40 | 14 | $76.44 |
| long | 5 Jun 2024 70,537.80 | 12 Jun 2024 67,314.20 | 7 | −$159.95 |
| long | 3 Jul 2024 62,135.50 | 4 Jul 2024 60,208.60 | 1 | −$108.54 |
| long | 14 Jul 2024 59,204.00 | 1 Aug 2024 64,628.00 | 18 | $320.65 |
| long | 14 Aug 2024 60,587.20 | 16 Aug 2024 57,541.10 | 2 | −$175.97 |
| long | 20 Aug 2024 59,438.50 | 29 Aug 2024 59,034.90 | 9 | −$23.77 |
| long | 13 Sep 2024 58,132.30 | 2 Oct 2024 60,804.90 | 19 | $160.91 |
| long | 13 Oct 2024 63,206.20 | 27 Oct 2024 67,092.80 | 14 | $215.21 |
| long | 28 Oct 2024 68,021.70 | 5 Nov 2024 67,850.00 | 8 | −$8.83 |
| long | 7 Nov 2024 75,572.00 | 4 Dec 2024 95,849.70 | 27 | $939.13 |
| long | 5 Dec 2024 98,587.30 | 20 Dec 2024 97,461.90 | 15 | −$39.96 |
| long | 4 Jan 2025 98,174.20 | 10 Jan 2025 92,552.50 | 6 | −$200.42 |
| long | 16 Jan 2025 100,497.00 | 29 Jan 2025 101,336.00 | 13 | $29.19 |
| long | 31 Jan 2025 104,723.00 | 1 Feb 2025 102,430.00 | 1 | −$76.65 |
| long | 16 Feb 2025 97,569.70 | 17 Feb 2025 96,118.10 | 1 | −$52.07 |
| long | 21 Feb 2025 98,305.00 | 24 Feb 2025 96,258.00 | 3 | −$72.88 |
| long | 7 Mar 2025 89,931.90 | 8 Mar 2025 86,801.70 | 1 | −$121.82 |
| long | 20 Mar 2025 86,845.90 | 30 Mar 2025 82,648.50 | 10 | −$169.16 |
| long | 13 Apr 2025 85,276.90 | 30 May 2025 105,590.00 | 47 | $833.70 |
| long | 10 Jun 2025 110,263.00 | 15 Jun 2025 105,415.00 | 5 | −$153.90 |
| long | 26 Jun 2025 107,341.00 | 26 Jul 2025 117,614.00 | 30 | $334.99 |
| long | 28 Jul 2025 119,416.00 | 29 Jul 2025 118,062.00 | 1 | −$39.66 |
| long | 9 Aug 2025 116,675.00 | 18 Aug 2025 117,405.00 | 9 | $21.91 |
| long | 6 Sep 2025 110,660.00 | 23 Sep 2025 112,651.00 | 17 | $62.97 |
| long | 1 Oct 2025 114,049.00 | 11 Oct 2025 112,774.00 | 10 | −$39.11 |
| long | 26 Oct 2025 111,646.00 | 31 Oct 2025 108,323.00 | 5 | −$104.19 |
| long | 28 Nov 2025 91,333.90 | 15 Dec 2025 88,172.20 | 17 | −$121.16 |
| long | 29 Dec 2025 87,952.70 | 30 Dec 2025 87,237.10 | 1 | −$28.48 |
| long | 31 Dec 2025 88,485.50 | 21 Jan 2026 88,427.70 | 21 | −$2.29 |
| long | 23 Feb 2026 67,643.40 | 24 Feb 2026 64,656.00 | 1 | −$154.57 |
| long | 1 Mar 2026 66,973.30 | 2 Mar 2026 65,776.50 | 1 | −$62.54 |
| long | 3 Mar 2026 68,830.10 | 9 Mar 2026 65,971.20 | 6 | −$145.37 |
| long | 11 Mar 2026 69,948.60 | 21 Mar 2026 70,510.70 | 10 | $28.13 |
| long | 6 Apr 2026 69,034.20 | 30 Apr 2026 75,780.00 | 24 | $342.01 |
| long | 3 May 2026 78,686.80 | 16 May 2026 79,113.20 | 13 | $18.96 |
| long | 15 Jun 2026 65,746.40 | 22 Jun 2026 63,312.00 | 7 | −$129.60 |
| long | 4 Jul 2026 62,583.30 | 26 Jul 2026 64,375.00 | 22 | $100.20 |
| long | 27 Jul 2026 65,400.00 | 28 Jul 2026 63,755.90 | 1 | −$87.99 |
| long | 6 Aug 2026 64,665.20 | 12 Aug 2026 63,600.00 | 6 | −$57.66 |
| long | 19 Aug 2026 64,725.40 | 3 Sep 2026 77,340.00 | 15 | $682.13 |
| long | 4 Sep 2026 81,270.40 | 9 Sep 2026 78,455.80 | 5 | −$121.21 |
| long | 19 Sep 2026 80,883.90 | 8 Oct 2026 83,321.80 | 19 | $105.50 |
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 |
|---|---|---|---|---|---|
| Multi MA MTF SandBox Strategy this page | — | v5 strategy | +17.4% | 48 | 9.5% |
| Ultra Moving Average Rating Trend Strategy by exlux99 | ema wma highest lowest sma alma linreg hma | v5 strategy | +2.8% | 5 | 2.4% |
| Multi Trend Cross Strategy Template by TradeAutomation | alma ema hma linreg sma wma highest lowest | v5 strategy | +33.0% | 3 | 7.3% |
| Multi MA Trend Following Strategy Template by TradeAutomation | alma ema hma linreg sma wma highest lowest | v5 strategy | +12.3% | 3 | 26.9% |
| Swing Multi Moving Averages Crypto and Stocks Strategy by exlux99 | ema wma highest lowest sma | v5 strategy | +60.2% | 22 | 13.1% |
| BTC 15 min by RafaelZioni | sma ema wma linreg highest lowest | v4 strategy | −25.7% | 119 | 38.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.
- 9 settings use the untyped input() (line 29, 32, 33, 34, 35, 37, 39, 159, 161). 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 48 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 29: input() → input.int()
- Line 32: input() → input.int()
- Line 33: input() → input.int()
- Line 37: input() → input.bool()
- Line 39: input() → input.int()
- Line 159: input() → input.bool()
- Line 161: input() → input.bool()
⋯resma_fast = input.timeframe(title='First Time Frame', defval='',group="Moving Average 1 (Blue)")-lenma_fast = input(title='First MA Length', defval=6,group="Moving Average 1 (Blue)")+lenma_fast = input.int(title='First MA Length', defval=6,group="Moving Average 1 (Blue)")ma_select2 = input.string(title='Second Fast moving average', defval='JMA', options=['SMA', 'EMA', 'WMA', 'HMA', 'JMA', 'KAMA', 'TMA', 'VAMA', 'SMMA', 'DEMA', 'VMA', 'WWMA', 'EMA_NO_LAG', 'TSF', 'ALMA'],group="Moving Average 2 (Yellow)")resma_slow = input.timeframe(title='Second time frame', defval='',group="Moving Average 2 (Yellow)")-lenma_slow = input(title='Second MA length', defval=14,group="Moving Average 2 (Yellow)")+lenma_slow = input.int(title='Second MA length', defval=14,group="Moving Average 2 (Yellow)")-lineWidth = input(2, title='Line Width', group="Visual Settings")+lineWidth = input.int(2, title='Line Width', group="Visual Settings")color_fast = input(title='Fast Line Color (or Buy)', defval=color.new(#26c6da, 40), group="Visual Settings")⋯change_line_color = input.bool(true, title='Change line color based on signal', tooltip='Change line color based on Buy or Sell Signal. Effected by Fast/Slow Line Color settings above.', group="Visual Settings")-fillColor = input(title='Fill Color', defval=true, group="Visual Settings")+fillColor = input.bool(title='Fill Color', defval=true, group="Visual Settings")offset = input.float(title='Alma Offset (only for ALMA)', defval=0.85, step=0.05, group="Moving Average Parameters")-volatility_lookback = input(title='Volatility lookback (only for VAMA)', defval=12, group="Moving Average Parameters")+volatility_lookback = input.int(title='Volatility lookback (only for VAMA)', defval=12, group="Moving Average Parameters")i_fastAlpha = input.float(1.25, 'KAMA\'s alpha (only for KAMA)', minval=1, step=0.25, group="Moving Average Parameters")⋯// === Stop LOSS ===-useStopLoss = input(false, title='Use Stop Loss',group="Stop Loss")+useStopLoss = input.bool(false, title='Use Stop Loss',group="Stop Loss")sl_inp = input.float(2.5, title='Stop Loss %', step=0.1, group="Stop Loss") // / 100-useTakeprofit = input(false, title='Use Take Profit',group="Take Profit")+useTakeprofit = input.bool(false, title='Use Take Profit',group="Take Profit")tp_inp = input.float(5, title='Take Profit %', step=0.1,group="Take Profit") // / 100⋯Does it run in Pine Script v6?
Yes
Same 48 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.
⋯// Can also act as an indicator by settings 'Act as indicator' to true.-//@version=5+//@version=6strategy('Multi MA MTF SandBox Strategy', 'Multi MA SandBox', overlay=true, default_qty_type=strategy.cash, default_qty_value=3500, initial_capital=10000)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 48 ms | Yes 1,000 bars in 128 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.
// This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License https://creativecommons.org/licenses/by-sa/4.0/// © dman103// A moving averages SandBox strategy where you can experiment with two moving averages (like KAMA, ALMA, HMA, JMA, VAMA, and more) on different time frames to generate BUY and SELL signals, when they cross.// Great sandbox for experimenting with different moving averages and different time frames.//// == How to use ==// We select two types of moving averages on two different time frames://// First is the FAST moving average which should be at the same time frame or higher.// Second is the SLOW moving average that should be on the same time frame or higher.// When FAST moving average cross over the SLOW moving average we have a BUY signal (for LONG)// When FAST moving average cross under the SLOW moving average we have a SELL signal (for SHORT)// WARNING: Using a lower time frame than your chart time frame will result in unrealistic results in your backtesting and bar replay.// == NOTES ==// You can select BOTH, LONG and SHORT in the strategy settings.// You can also enable Stop Loss and Take Profit.// More sandboxes to come, Follow to get notified.// Can also act as an indicator by settings 'Act as indicator' to true.//@version=5strategy('Multi MA MTF SandBox Strategy', 'Multi MA SandBox', overlay=true, default_qty_type=strategy.cash, default_qty_value=3500, initial_capital=10000)tradeType = input.string('LONG', title='What trades should be taken:', options=['LONG', 'SHORT', 'BOTH'])act_like_indicator = input.bool(false,'Act as indicator (will disable strategy functionality)')start_date = input.time(title='Start Date', defval=timestamp('1 JAN 2020 09:30:00 +0000'))end_date = input.time(title='End Date', defval=timestamp('1 JAN 2030 09:30:00 +0000'))window() => time_close >= start_date and time_close <= end_date ? true : falsema_select1 = input.string(title='First Slow moving average', defval='EMA', options=['SMA', 'EMA', 'WMA', 'HMA', 'JMA', 'KAMA', 'TMA', 'VAMA', 'SMMA', 'DEMA', 'VMA', 'WWMA', 'EMA_NO_LAG', 'TSF', 'ALMA'],group="Moving Average 1 (Blue)")resma_fast = input.timeframe(title='First Time Frame', defval='',group="Moving Average 1 (Blue)")lenma_fast = input(title='First MA Length', defval=6,group="Moving Average 1 (Blue)")ma_select2 = input.string(title='Second Fast moving average', defval='JMA', options=['SMA', 'EMA', 'WMA', 'HMA', 'JMA', 'KAMA', 'TMA', 'VAMA', 'SMMA', 'DEMA', 'VMA', 'WWMA', 'EMA_NO_LAG', 'TSF', 'ALMA'],group="Moving Average 2 (Yellow)")resma_slow = input.timeframe(title='Second time frame', defval='',group="Moving Average 2 (Yellow)")lenma_slow = input(title='Second MA length', defval=14,group="Moving Average 2 (Yellow)")lineWidth = input(2, title='Line Width', group="Visual Settings")color_fast = input(title='Fast Line Color (or Buy)', defval=color.new(#26c6da, 40), group="Visual Settings")color_slow = input(title='Slow Line Color (or Sell)', defval=color.new(color.yellow, 40), group="Visual Settings")change_line_color = input.bool(true, title='Change line color based on signal', tooltip='Change line color based on Buy or Sell Signal. Effected by Fast/Slow Line Color settings above.', group="Visual Settings")fillColor = input(title='Fill Color', defval=true, group="Visual Settings")offset = input.float(title='Alma Offset (only for ALMA)', defval=0.85, step=0.05, group="Moving Average Parameters")volatility_lookback = input(title='Volatility lookback (only for VAMA)', defval=12, group="Moving Average Parameters")i_fastAlpha = input.float(1.25, 'KAMA\'s alpha (only for KAMA)', minval=1, step=0.25, group="Moving Average Parameters")fastAlpha = 2.0 / (i_fastAlpha + 1)slowAlpha = 2.0 / 31///////Moving AveragesMA_selector(src, length, ma_select) => ma = 0.0 if ma_select == 'SMA' ma := ta.sma(src, length) ma if ma_select == 'EMA' ma := ta.ema(src, length) ma if ma_select == 'WMA' ma := ta.wma(src, length) ma if ma_select == 'HMA' ma := ta.hma(src, length) ma if ma_select == 'JMA' beta = 0.45 * (length - 1) / (0.45 * (length - 1) + 2) alpha = beta tmp0 = 0.0 tmp1 = 0.0 tmp2 = 0.0 tmp3 = 0.0 tmp4 = 0.0 tmp0 := (1 - alpha) * src + alpha * nz(tmp0[1]) tmp1 := (src - tmp0[0]) * (1 - beta) + beta * nz(tmp1[1]) tmp2 := tmp0[0] + tmp1[0] tmp3 := (tmp2[0] - nz(tmp4[1])) * (1 - alpha) * (1 - alpha) + alpha * alpha * nz(tmp3[1]) tmp4 := nz(tmp4[1]) + tmp3[0] ma := tmp4 ma if ma_select == 'KAMA' momentum = math.abs(ta.change(src, length)) volatility = math.sum(math.abs(ta.change(src)), length) efficiencyRatio = volatility != 0 ? momentum / volatility : 0 smoothingConstant = math.pow(efficiencyRatio * (fastAlpha - slowAlpha) + slowAlpha, 2) var kama = 0.0 kama := nz(kama[1], src) + smoothingConstant * (src - nz(kama[1], src)) ma := kama ma if ma_select == 'TMA' ma := ta.sma(ta.sma(src, math.ceil(length / 2)), math.floor(length / 2) + 1) ma if ma_select == 'VMA' valpha = 2 / (length + 1) vud1 = src > src[1] ? src - src[1] : 0 vdd1 = src < src[1] ? src[1] - src : 0 vUD = math.sum(vud1, 9) vDD = math.sum(vdd1, 9) vCMO = nz((vUD - vDD) / (vUD + vDD)) VAR = 0.0 VAR := nz(valpha * math.abs(vCMO) * src) + (1 - valpha * math.abs(vCMO)) * nz(VAR[1]) ma := VAR ma if ma_select == 'WWMA' wwalpha = 1 / length WWMA = 0.0 WWMA := wwalpha * src + (1 - wwalpha) * nz(WWMA[1]) ma := WWMA ma if ma_select == 'EMA_NO_LAG' EMA1 = ta.ema(src, length) EMA2 = ta.ema(EMA1, length) Difference = EMA1 - EMA2 ma := EMA1 + Difference ma if ma_select == 'TSF' lrc = ta.linreg(src, length, 0) lrc1 = ta.linreg(src, length, 1) lrs = lrc - lrc1 TSF = ta.linreg(src, length, 0) + lrs ma := TSF ma if ma_select == 'VAMA' // Volatility Adjusted from @fractured mid = ta.ema(src, length) dev = src - mid vol_up = ta.highest(dev, volatility_lookback) vol_down = ta.lowest(dev, volatility_lookback) ma := mid + math.avg(vol_up, vol_down) ma if ma_select == 'SMMA' smma = float(0.0) smaval = ta.sma(src, length) smma := na(smma[1]) ? smaval : (smma[1] * (length - 1) + src) / length ma := smma ma if ma_select == 'DEMA' e1 = ta.ema(src, length) e2 = ta.ema(e1, length) ma := 2 * e1 - e2 ma if ma_select == 'ALMA' ma := ta.alma(src, length, offset, 6) ma ma// Calculate EMAma_fast = MA_selector(close, lenma_fast, ma_select1)ma_slow = MA_selector(close, lenma_slow, ma_select2)maFastStep = request.security(syminfo.tickerid, resma_fast, ma_fast)maSlowStep = request.security(syminfo.tickerid, resma_slow, ma_slow)colors = ma_fast > ma_slow ? color.new(color.green, 70) : color.new(color.red, 70)colors_setting = ma_fast > ma_slow ? color_fast : color_slowma1_plot = plot(maFastStep, color=change_line_color == false ? color_fast : colors_setting, linewidth=lineWidth)ma2_plot = plot(maSlowStep, color=change_line_color == false ? color_slow : colors_setting, linewidth=lineWidth)fill(ma1_plot, ma2_plot, color=fillColor ? colors : na)closeStatus = strategy.openprofit > 0 ? 'win' : 'lose'////////Long Ruleslong = ta.crossover(maFastStep, maSlowStep) and (tradeType == 'LONG' or tradeType == 'BOTH')longClose = ta.crossunder(maFastStep, maSlowStep) //and falling(maSlowStep,1) ///////Short Rulesshort = ta.crossunder(maFastStep, maSlowStep) and (tradeType == 'SHORT' or tradeType == 'BOTH')shortClose = ta.crossover(maFastStep, maSlowStep)longShape = ta.crossover(maFastStep, maSlowStep) and act_like_indicatorshortShape = ta.crossunder(maFastStep, maSlowStep) and act_like_indicatorplotshape(longShape, style=shape.triangleup, location=location.belowbar, color=color.new(color.lime, 0), size=size.small)plotshape(shortShape, style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.small)// === Stop LOSS ===useStopLoss = input(false, title='Use Stop Loss',group="Stop Loss")sl_inp = input.float(2.5, title='Stop Loss %', step=0.1, group="Stop Loss") // / 100useTakeprofit = input(false, title='Use Take Profit',group="Take Profit")tp_inp = input.float(5, title='Take Profit %', step=0.1,group="Take Profit") // / 100percentToTicks(pcnt) => strategy.position_size != 0 ? math.round(pcnt / 100.0 * strategy.position_avg_price / syminfo.mintick) : float(na)float stop_level = percentToTicks(sl_inp)float take_level = percentToTicks(tp_inp)if long and window() and act_like_indicator==false strategy.entry('long', strategy.long)if short and window() and act_like_indicator==false strategy.entry('short', strategy.short)if (longClose) strategy.close('long', comment=closeStatus)if (shortClose) strategy.close('short', comment=closeStatus)if useStopLoss or useTakeprofit if (useTakeprofit and useStopLoss==false) strategy.exit('Take Profit', profit=take_level, comment='tp '+closeStatus) if (useTakeprofit==false and useStopLoss) strategy.exit('Stop loss', loss=stop_level, comment='sl') if (useTakeprofit and useStopLoss) strategy.exit('Take Profit/Stop Loss', profit=take_level,loss=stop_level, comment_profit ='tp '+closeStatus,comment_loss='sl')