Pine Script v5 · Strategy
Moving Average Crossover Strategy
A Pine Script v5 strategy built on SMA (close, 13), EMA (5) and Crossover (Fast, Slow). It has 6 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 10% of equity and pays no commission. 30 settings; defaults include Fast EMA 5, Slow SMA 10 and Trend Determining SMA 13.
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 Moving Average Crossover Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 24 of 24 trades.
- How did Moving Average Crossover Strategy do on BTCUSDT 1D with default settings?
- +0.02%
- From 26 Jan 2024 to 21 Oct 2026, over 75 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Moving Average Crossover Strategy run in Pine Script v6?
- Yes
- Same 75 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 Moving Average Crossover Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 139, 141.
- Does Moving Average Crossover Strategy run in Zeal Terminal?
- Yes
- It compiles in 43 ms and runs 1,000 bars in 67 ms, in the browser.
BTCUSDT · 1D · default settings
- SMA
- EMA
- SMA
- Crossover
- Crossunder
- Crossover
- Crossunder
- Crossover
- Crossunder
- ATR
- SMA
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +0.02%
- $17.92
- Buy and hold
- +102.5%
- same period
- Closed trades
- 75
- 37 won · 38 lost
- Profitable
- 49.3%
- of closed trades
- Profit factor
- 1.00
- gross profit ÷ gross loss
- Max drawdown
- 4.30%
- −$4,347.17
One run with the author's default settings, $100,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.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
4 settings have no label
In the settings dialog usevol (line 54), useatr (line 61), usersi (line 68), rsitrhs1 (line 70) show an empty name. The labels on this page come from the variable names.
-
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 |
|---|---|---|---|
| Moving Averages: | |||
| Fast EMA | 5 | int · min 1 | Fast |
| Dynamic Exponential Moving Average?Changes the source of the MA based on trend | true | bool | Fast |
| Slow SMA | 10 | int · min 1 | Slow |
| Dynamic Simple Moving Average?Changes the source of the MA based on trend | false | bool | Slow |
| Trend Determining SMA | 13 | int · min 1 | Trend |
| Allowed Entries: | |||
| Long | true | bool | golong entry long line 130 |
| Short | true | bool | goshort entry short line 132 |
| Entry Conditions: | |||
| Buy when | Fast-Slow Crossing | string · options ["Fast-Slow Crossing", "Fast-Trend Crossing","Slow-Trend Crossing"] | crosslong crossshort |
| In trendIn trend if price is above SMA 2 | true | bool | crosslong |
| Sell when | Fast-Slow Crossing | string · options ["Fast-Slow Crossing", "Fast-Trend Crossing","Slow-Trend Crossing"] | crossshort |
| In trendIn trend if price is below SMA 2? | true | bool | crossshort |
| Exit Conditions: | |||
| Close long when | Fast-Slow Crossing | string · options ["Fast-Slow Crossing", "Fast-Trend Crossing","Slow-Trend Crossing"] | crossexitlong |
| Close short when | Fast-Slow Crossing | string · options ["Fast-Slow Crossing", "Fast-Trend Crossing","Slow-Trend Crossing"] | crossexitshort |
| Apply Filters to | |||
| Long Entries | true | bool | golong |
| Short Entries | true | bool | goshort |
| Exits | true | bool | endlong endshort |
| Relative Volume Filter: | |||
| Usevol (no label in the script) | false | bool | volumefilter |
| Volume > | 1 | int | volumefilter |
| Avg. Volume Over PeriodThe current volume must be greater than N times the M-period average volume. | 30 | int · min 1 | volumefilter |
| Volatility Filter: | |||
| Useatr (no label in the script) | false | bool | volatilityfilter |
| ATR | 5 | int · min 1 | volatilityfilter |
| > ATRThe N-period ATR must be greater than the M-period ATR. | 30 | int · min 1 | volatilityfilter |
| Overbought/Oversold Filter: | |||
| Usersi (no label in the script) | false | bool | rsifilter |
| Rsitrhs1 (no label in the script) | 0 | int · min 0 · max 100 | rsifilter |
| < RSI (14) <RSI(14) must be in the range between N and M. | 100 | int · min 0 · max 100 | rsifilter |
| Stop Loss / Take Profit: | |||
| SL | false | bool | exit sl/tp long exit sl/tp short |
| , % | 10 | float · min 0.0 | stoploss |
| Trailing | false | bool | line 125 line 127 |
| TP | false | bool | exit sl/tp long exit sl/tp short |
| , % | 20 | float | takeprofit |
What it calculates
- SMAclose, Trend Determining SMA (13)
Trend · line 18 - EMAisDynamicEMA ? (close > Trend ? low : high) : close, Fast EMA (5)
Fast · line 19 - SMAisDynamicSMA ? (close > Trend ? low : high) : close, Slow SMA (10)
Slow · line 20 - CrossoverFast, Slow
fscrossup · line 87 - CrossunderFast, Slow
fscrossdw · line 88 - CrossoverFast, Trend
ftcrossup · line 89 - CrossunderFast, Trend
ftcrossdw · line 90 - CrossoverSlow, Trend
stcrossup · line 91 - CrossunderSlow, Trend
stcrossdw · line 92 - RSIclose, 14
rsifilter · line 109 - ATRATR (5)
volatilityfilter · line 110 - SMAvolume, Avg. Volume Over Period (30)
volumefilter · line 111
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 long long | golong and islong | On | 131 |
| Open short short | goshort and isshort | On | 133 |
| Close long | endlong | On | 135 |
| Close short | endshort | On | 137 |
| Exit sl/tp long stop and limit | every bar | On | 139 |
| Exit sl/tp short stop and limit | every bar | On | 141 |
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 | +0.0% | +102.5% | 75 | 49% | 1.00 | 4.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.5% | +5.5% | 83 | 58% | 1.35 | 1.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +1.4% | +9.8% | 67 | 52% | 1.09 | 6.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +4.1% | +8.4% | 80 | 54% | 2.16 | 0.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +4.6% | +16.7% | 72 | 54% | 1.34 | 6.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +2.5% | +21.5% | 70 | 49% | 1.61 | 1.3% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −5.4% | +148.3% | 79 | 39% | 0.71 | 10.3% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +2.5% | +17.1% | 76 | 50% | 1.84 | 1.1% | 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 | 23 | 48% | −$94.53 |
| Sideways market | 30 | 43% | −$460.49 |
| Falling market | 22 | 59% | $572.94 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
6.4%
The 95th percentile of the worst drawdown when the same 75 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 3.0% to 6.4%; the tick is what actually happened, 3.9%. Order luck alone moves it this much.
All 75 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 29 Feb 2024 62,432.10 | 6 Mar 2024 63,724.00 | 6 | $206.93 |
| long | 10 Mar 2024 68,313.30 | 12 Mar 2024 72,078.10 | 2 | $552.25 |
| short | 10 Apr 2024 69,146.00 | 13 Apr 2024 67,116.50 | 3 | $295.74 |
| long | 13 Apr 2024 67,116.50 | 15 Apr 2024 65,661.90 | 2 | −$219.02 |
| long | 1 May 2024 60,672.00 | 2 May 2024 58,365.00 | 1 | −$383.43 |
| short | 2 May 2024 58,365.00 | 4 May 2024 62,882.00 | 2 | −$777.43 |
| long | 15 May 2024 61,577.50 | 16 May 2024 66,206.50 | 1 | $749.30 |
| short | 16 May 2024 66,206.50 | 17 May 2024 65,235.20 | 1 | $147.33 |
| short | 4 Jun 2024 68,809.90 | 6 Jun 2024 71,108.00 | 2 | −$335.89 |
| long | 13 Jun 2024 68,264.00 | 14 Jun 2024 66,773.00 | 1 | −$218.93 |
| short | 25 Jun 2024 60,293.30 | 29 Jun 2024 60,427.80 | 4 | −$22.32 |
| short | 1 Jul 2024 62,772.00 | 2 Jul 2024 62,900.00 | 1 | −$20.39 |
| short | 6 Jul 2024 56,628.80 | 11 Jul 2024 57,725.80 | 5 | −$193.68 |
| short | 4 Aug 2024 60,698.00 | 9 Aug 2024 61,686.00 | 5 | −$162.42 |
| long | 19 Aug 2024 58,427.30 | 20 Aug 2024 59,438.50 | 1 | $172.40 |
| short | 20 Aug 2024 59,438.50 | 21 Aug 2024 59,013.80 | 1 | $71.30 |
| short | 8 Sep 2024 54,160.90 | 10 Sep 2024 57,042.00 | 2 | −$531.23 |
| short | 12 Sep 2024 57,338.00 | 13 Sep 2024 58,132.30 | 1 | −$137.60 |
| long | 21 Sep 2024 63,201.10 | 27 Sep 2024 65,174.00 | 6 | $309.65 |
| long | 30 Sep 2024 65,602.00 | 4 Oct 2024 60,752.70 | 4 | −$735.52 |
| long | 16 Oct 2024 67,074.10 | 23 Oct 2024 67,426.00 | 7 | $51.81 |
| long | 31 Oct 2024 72,344.80 | 3 Nov 2024 69,374.70 | 3 | −$405.69 |
| long | 9 Nov 2024 76,509.80 | 20 Nov 2024 92,310.80 | 11 | $2,032.46 |
| long | 23 Nov 2024 98,892.00 | 28 Nov 2024 95,863.10 | 5 | −$307.65 |
| short | 5 Dec 2024 98,587.30 | 10 Dec 2024 97,276.50 | 5 | $133.15 |
| long | 10 Dec 2024 97,276.50 | 12 Dec 2024 101,125.00 | 2 | $396.70 |
| long | 17 Dec 2024 106,059.00 | 22 Dec 2024 97,292.00 | 5 | −$832.11 |
| short | 3 Jan 2025 96,984.80 | 4 Jan 2025 98,174.20 | 1 | −$122.43 |
| long | 29 Jan 2025 101,336.00 | 30 Jan 2025 103,733.00 | 1 | $235.94 |
| long | 1 Feb 2025 102,430.00 | 27 Feb 2025 84,250.10 | 26 | −$1,773.93 |
| short | 27 Feb 2025 84,250.10 | 4 Mar 2025 86,221.20 | 5 | −$229.69 |
| long | 26 Mar 2025 87,392.90 | 27 Mar 2025 86,909.20 | 1 | −$54.21 |
| long | 29 Mar 2025 84,424.40 | 31 Mar 2025 82,390.00 | 2 | −$235.89 |
| long | 3 Apr 2025 82,516.30 | 10 Apr 2025 82,615.20 | 7 | $11.71 |
| short | 10 Apr 2025 82,615.20 | 14 Apr 2025 83,760.00 | 4 | −$135.34 |
| long | 24 Apr 2025 93,691.10 | 1 May 2025 94,172.00 | 7 | $50.06 |
| long | 5 May 2025 94,277.60 | 7 May 2025 96,834.00 | 2 | $264.60 |
| long | 10 May 2025 102,972.00 | 16 May 2025 103,764.00 | 6 | $75.23 |
| short | 24 Jun 2025 105,334.00 | 27 Jun 2025 106,947.00 | 3 | −$149.96 |
| long | 12 Jul 2025 117,528.00 | 19 Jul 2025 117,925.00 | 7 | $33.04 |
| short | 3 Aug 2025 112,546.00 | 7 Aug 2025 114,992.00 | 4 | −$212.56 |
| short | 8 Aug 2025 117,472.00 | 9 Aug 2025 116,675.00 | 1 | $66.24 |
| short | 31 Aug 2025 108,816.00 | 3 Sep 2025 111,240.00 | 3 | −$217.52 |
| long | 13 Sep 2025 116,029.00 | 17 Sep 2025 116,789.00 | 4 | $63.79 |
| long | 11 Oct 2025 112,774.00 | 12 Oct 2025 110,644.00 | 1 | −$184.17 |
| short | 6 Nov 2025 103,885.00 | 11 Nov 2025 106,011.00 | 5 | −$199.16 |
| short | 17 Nov 2025 94,261.40 | 26 Nov 2025 87,370.00 | 9 | $710.06 |
| short | 27 Nov 2025 90,484.00 | 28 Nov 2025 91,333.90 | 1 | −$91.89 |
| long | 6 Dec 2025 89,330.00 | 8 Dec 2025 90,395.30 | 2 | $116.56 |
| short | 17 Dec 2025 87,863.40 | 18 Dec 2025 86,243.20 | 1 | $180.45 |
| long | 18 Dec 2025 86,243.20 | 9 Jan 2026 91,100.00 | 22 | $552.09 |
| long | 16 Jan 2026 95,604.80 | 20 Jan 2026 92,631.00 | 4 | −$306.66 |
| short | 1 Feb 2026 78,741.10 | 10 Feb 2026 70,138.00 | 9 | $1,073.81 |
| short | 26 Feb 2026 67,988.00 | 28 Feb 2026 65,872.10 | 2 | $309.22 |
| short | 1 Mar 2026 66,973.30 | 6 Mar 2026 70,891.00 | 5 | −$583.01 |
| long | 6 Mar 2026 70,891.00 | 7 Mar 2026 68,114.00 | 1 | −$388.13 |
| long | 9 Mar 2026 65,971.20 | 10 Mar 2026 68,432.20 | 1 | $368.16 |
| long | 25 Mar 2026 70,556.70 | 26 Mar 2026 71,336.50 | 1 | $109.48 |
| short | 26 Mar 2026 71,336.50 | 27 Mar 2026 68,820.30 | 1 | $349.80 |
| short | 30 Mar 2026 66,010.90 | 31 Mar 2026 66,797.40 | 1 | −$118.57 |
| long | 9 Apr 2026 71,069.90 | 14 Apr 2026 74,418.00 | 5 | $468.28 |
| long | 19 Apr 2026 75,691.80 | 20 Apr 2026 73,801.80 | 1 | −$249.37 |
| long | 30 Apr 2026 75,780.00 | 1 May 2026 76,346.60 | 1 | $74.48 |
| short | 5 May 2026 79,861.00 | 6 May 2026 80,905.50 | 1 | −$130.40 |
| long | 6 May 2026 80,905.50 | 11 May 2026 82,210.10 | 5 | $160.54 |
| short | 30 May 2026 73,460.80 | 11 Jun 2026 61,511.00 | 12 | $1,622.24 |
| long | 23 Jun 2026 64,020.00 | 27 Jun 2026 60,097.30 | 4 | −$621.00 |
| short | 27 Jun 2026 60,097.30 | 2 Jul 2026 60,024.00 | 5 | $12.28 |
| long | 14 Jul 2026 62,334.50 | 15 Jul 2026 65,044.00 | 1 | $437.88 |
| long | 25 Jul 2026 64,140.00 | 26 Jul 2026 64,375.00 | 1 | $37.07 |
| short | 16 Aug 2026 63,086.00 | 21 Aug 2026 73,027.00 | 5 | −$1,594.93 |
| long | 21 Aug 2026 73,027.00 | 29 Aug 2026 77,845.90 | 8 | $657.38 |
| long | 10 Sep 2026 78,306.40 | 13 Sep 2026 77,278.70 | 3 | −$131.60 |
| short | 19 Sep 2026 80,883.90 | 20 Sep 2026 81,250.00 | 1 | −$45.33 |
| long | 8 Oct 2026 83,321.80 | 10 Oct 2026 82,635.60 | 2 | −$82.44 |
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 |
|---|---|---|---|---|---|
| Moving Average Crossover Strategy this page | — | v5 strategy | +0.0% | 75 | 4.3% |
| 3 x EMA + Stochastic RSI + ATR by tomimarson | ema rsi sma atr | v4 strategy | −42.3% | 18 | 44.8% |
| Atrend by OTS by prim722 | ema rsi sma atr | v4 strategy | +12.1% | 76 | 34.4% |
| EMA crossover (daily TF) by tradeswithashish | ema sma rsi atr | v5 strategy | +11.0% | 12 | 11.6% |
| EMA Stoch Strategy For ProfitView by treigen | rsi sma ema cross | v5 strategy | −69.8% | 5 | 80.6% |
| Delta-RSI Strategy (with filters) by tbiktag | rsi ema atr sma | v4 strategy | −28.8% | 71 | 83.8% |
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 75 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.
⋯// © Decam9-//@version=5+//@version=6strategy(title = "Moving Average Crossover", shorttitle = "MA Crossover Strategy", overlay=true,⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 43 ms | With a workaround 1,000 bars in 67 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/// © Decam9//@version=5strategy(title = "Moving Average Crossover", shorttitle = "MA Crossover Strategy", overlay=true, initial_capital = 100000,default_qty_type = strategy.percent_of_equity, default_qty_value = 10)//Moving Average InputsEMA1 = input.int(title="Fast EMA", group = "Moving Averages:", inline = "EMAs", defval=5, minval = 1)isDynamicEMA = input.bool(title = "Dynamic Exponential Moving Average?", defval = true, inline = "EMAs", group = "Moving Averages:", tooltip = "Changes the source of the MA based on trend")SMA1 = input.int(title = "Slow SMA", group = "Moving Averages:", inline = "SMAs", defval = 10, minval = 1)isDynamicSMA = input.bool(title = "Dynamic Simple Moving Average?", defval = false, inline = "SMAs", group = "Moving Averages:", tooltip = "Changes the source of the MA based on trend")SMA2 = input.int(title="Trend Determining SMA", group = "Moving Averages:", inline = "MAs", defval=13, minval = 1)//Moving AveragesTrend = ta.sma(close, SMA2)Fast = ta.ema(isDynamicEMA ? (close > Trend ? low : high) : close, EMA1)Slow = ta.sma(isDynamicSMA ? (close > Trend ? low : high) : close, SMA1)//Allowed Entriesislong = input.bool(title = "Long", group = "Allowed Entries:", inline = "Entries",defval = true)isshort = input.bool(title = "Short", group = "Allowed Entries:", inline = "Entries", defval= true)//Entry Long Conditionsbuycond = input.string(title="Buy when", group = "Entry Conditions:", inline = "Conditions",defval="Fast-Slow Crossing", options=["Fast-Slow Crossing", "Fast-Trend Crossing","Slow-Trend Crossing"]) intrendbuy = input.bool(title = "In trend", defval = true, group = "Entry Conditions:", inline = "Conditions", tooltip = "In trend if price is above SMA 2")//Entry Short Conditionssellcond = input.string(title="Sell when", group = "Entry Conditions:", inline = "Conditions2",defval="Fast-Slow Crossing", options=["Fast-Slow Crossing", "Fast-Trend Crossing","Slow-Trend Crossing"]) intrendsell = input.bool(title = "In trend",defval = true, group = "Entry Conditions:", inline = "Conditions2", tooltip = "In trend if price is below SMA 2?")//Exit Long Conditionsclosebuy = input.string(title="Close long when", group = "Exit Conditions:", defval="Fast-Slow Crossing", options=["Fast-Slow Crossing", "Fast-Trend Crossing","Slow-Trend Crossing"])//Exit Short Conditionscloseshort = input.string(title="Close short when", group = "Exit Conditions:", defval="Fast-Slow Crossing", options=["Fast-Slow Crossing", "Fast-Trend Crossing","Slow-Trend Crossing"]) //Filtersfilterlong =input.bool(title = "Long Entries", inline = 'linefilt', group = 'Apply Filters to', defval = true)filtershort =input.bool(title = "Short Entries", inline = 'linefilt', group = 'Apply Filters to', defval = true)filterend =input.bool(title = "Exits", inline = 'linefilt', group = 'Apply Filters to', defval = true)usevol =input.bool(title = "", inline = 'linefiltvol', group = 'Relative Volume Filter:', defval = false)rvol = input.int(title = "Volume >", inline = 'linefiltvol', group = 'Relative Volume Filter:', defval = 1)len_vol = input.int(title = "Avg. Volume Over Period", inline = 'linefiltvol', group = 'Relative Volume Filter:', defval = 30, minval = 1, tooltip="The current volume must be greater than N times the M-period average volume.")useatr =input.bool(title = "", inline = 'linefiltatr', group = 'Volatility Filter:', defval = false)len_atr1 = input.int(title = "ATR", inline = 'linefiltatr', group = 'Volatility Filter:', defval = 5, minval = 1)len_atr2 = input.int(title = "> ATR", inline = 'linefiltatr', group = 'Volatility Filter:', defval = 30, minval = 1, tooltip="The N-period ATR must be greater than the M-period ATR.")usersi =input.bool(title = "", inline = 'linersi', group = 'Overbought/Oversold Filter:', defval = false)rsitrhs1 = input.int(title = "", inline = 'linersi', group = 'Overbought/Oversold Filter:', defval = 0, minval=0, maxval=100)rsitrhs2 = input.int(title = "< RSI (14) <", inline = 'linersi', group = 'Overbought/Oversold Filter:', defval = 100, minval=0, maxval=100, tooltip="RSI(14) must be in the range between N and M.")issl = input.bool(title = "SL", inline = 'linesl1', group = 'Stop Loss / Take Profit:', defval = false)slpercent = input.float(title = ", %", inline = 'linesl1', group = 'Stop Loss / Take Profit:', defval = 10, minval=0.0)istrailing = input.bool(title = "Trailing", inline = 'linesl1', group = 'Stop Loss / Take Profit:', defval = false)istp = input.bool(title = "TP", inline = 'linetp1', group = 'Stop Loss / Take Profit:', defval = false)tppercent = input.float(title = ", %", inline = 'linetp1', group = 'Stop Loss / Take Profit:', defval = 20) //Conditions for Crossingfscrossup = ta.crossover(Fast,Slow)fscrossdw = ta.crossunder(Fast,Slow)ftcrossup = ta.crossover(Fast,Trend)ftcrossdw = ta.crossunder(Fast,Trend)stcrossup = ta.crossover(Slow,Trend)stcrossdw = ta.crossunder(Slow,Trend)//Defining in trenduptrend = Fast >= Slow and Slow >= Trenddowntrend = Fast <= Slow and Slow <= TrendjustCrossed = ta.cross(Fast,Slow) or ta.cross(Slow,Trend)//Entry Signalscrosslong = if intrendbuy (buycond =="Fast-Slow Crossing" and uptrend ? fscrossup:(buycond =="Fast-Trend Crossing" and uptrend ? ftcrossup:(buycond == "Slow-Trend Crossing" and uptrend ? stcrossup : na))) else (buycond =="Fast-Slow Crossing"?fscrossup:(buycond=="Fast-Trend Crossing"?ftcrossup:stcrossup))crossshort = if intrendsell (sellcond =="Fast-Slow Crossing" and downtrend ? fscrossdw:(sellcond =="Fast-Trend Crossing" and downtrend ? ftcrossdw:(sellcond == "Slow-Trend Crossing" and downtrend ? stcrossdw : na))) else (sellcond =="Fast-Slow Crossing"?fscrossdw:(buycond=="Fast-Trend Crossing"?ftcrossdw:stcrossdw))crossexitlong = (closebuy =="Fast-Slow Crossing"?fscrossdw:(closebuy=="Fast-Trend Crossing"?ftcrossdw:stcrossdw))crossexitshort = (closeshort =="Fast-Slow Crossing"?fscrossup:(closeshort=="Fast-Trend Crossing"?ftcrossup:stcrossup))// Filtersrsifilter = usersi?(ta.rsi(close,14) > rsitrhs1 and ta.rsi(close,14) < rsitrhs2):truevolatilityfilter = useatr?(ta.atr(len_atr1) > ta.atr(len_atr2)):truevolumefilter = usevol?(volume > rvol*ta.sma(volume,len_vol)):truetotalfilter = volatilityfilter and volumefilter and rsifilter//Filtered signalsgolong = crosslong and islong and (filterlong?totalfilter:true) goshort = crossshort and isshort and (filtershort?totalfilter:true)endlong = crossexitlong and (filterend?totalfilter:true)endshort = crossexitshort and (filterend?totalfilter:true)// Entry price and TPstartprice = ta.valuewhen(condition=golong or goshort, source=close, occurrence=0)pm = golong?1:goshort?-1:1/math.sign(strategy.position_size)takeprofit = startprice*(1+pm*tppercent*0.01)// fixed stop lossstoploss = startprice * (1-pm*slpercent*0.01)// trailing stop lossif istrailing and strategy.position_size>0 stoploss := math.max(close*(1 - slpercent*0.01),stoploss[1])else if istrailing and strategy.position_size<0 stoploss := math.min(close*(1 + slpercent*0.01),stoploss[1]) if golong and islong strategy.entry("long", strategy.long )if goshort and isshort strategy.entry("short", strategy.short)if endlong strategy.close("long")if endshort strategy.close("short")// Exit via SL or TPstrategy.exit(id="sl/tp long", from_entry="long", stop=issl?stoploss:na, limit=istp?takeprofit:na)strategy.exit(id="sl/tp short",from_entry="short",stop=issl?stoploss:na, limit=istp?takeprofit:na)