Pine Script v5 · Strategy
MacD Short and-or Long with Bi-Directional TP and SL
A Pine Script v5 strategy built on SMA or EMA (close, 13), Crossover (signal_long, 0) and Crossunder (signal_long, 0). It has 10 order rules, 4 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 100% of equity and pays no commission. 42 settings; defaults include Fast Length Long 13, Slow Length Long 19 and Source Long close.
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 MacD Short and-or Long with Bi-Directional TP and SL repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 11 of 11 trades.
- How did MacD Short and-or Long with Bi-Directional TP and SL do on BTCUSDT 1D with default settings?
- +62.48%
- From 26 Jan 2024 to 21 Oct 2026, over 36 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does MacD Short and-or Long with Bi-Directional TP and SL 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: 36 before, 26 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 MacD Short and-or Long with Bi-Directional TP and SL use a stop loss?
- Yes
- strategy.exit sets a stop on line 132, 134.
- Does MacD Short and-or Long with Bi-Directional TP and SL run in Zeal Terminal?
- Yes
- It compiles in 48 ms and runs 1,000 bars in 98 ms, in the browser.
BTCUSDT · 1D · default settings
- Histogram Long
- MACD Long
- Signal Long
- Histogram Short
- MACD Short
- Signal Short
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +62.48%
- $624.77
- Buy and hold
- +102.5%
- same period
- Closed trades
- 36
- 11 won · 25 lost
- Profitable
- 30.6%
- of closed trades
- Profit factor
- 1.48
- gross profit ÷ gross loss
- Max drawdown
- 30.89%
- −$368.55
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.
-
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 98, 103, 109, 114, 119, 121, 124, 126. 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 |
|---|---|---|---|
| inputs long | |||
| Allow Long | true | bool | line 95 line 117 line 122 |
| Fast Length Long | 13 | int | fast_ma_long |
| Slow Length Long | 19 | int | slow_ma_long |
| Source Long | close | source | fast_ma_long slow_ma_long |
| Signal Smoothing Long | 9 | int · min 1 · max 50 | signal_long |
| Oscillator MA Type Long | EMA | string · options ["SMA", "EMA"] | fast_ma_long slow_ma_long |
| Signal Line MA Type Long | EMA | string · options ["SMA", "EMA"] | signal_long |
| Cross Point Long | 0 | int | crossover_signal_long crossunder_signal_long crossover_macd_long crossunder_macd_long line 79 line 81 |
| MacD Cross Delay Long | 0 | int | longMacDCondition |
| Signal Must Also Cross Long | false | bool | entry long entry long close long close long line 96 line 118 |
| Signal Cross Delay Long | 0 | int | longSignalCondition |
| inputs short | |||
| Allow Short | true | bool | line 106 line 117 line 122 |
| Fast Length Short | 11 | int | fast_ma_short |
| Slow Length Short | 20 | int | slow_ma_short |
| Source Short | close | source | fast_ma_short slow_ma_short |
| Signal Smoothing Short | 9 | int · min 1 · max 50 | signal_short |
| Oscillator MA Type Short | EMA | string · options ["SMA", "EMA"] | fast_ma_short slow_ma_short |
| Signal Line MA Type Short | EMA | string · options ["SMA", "EMA"] | signal_short |
| Cross Point Short | 0 | int | crossunder_signal_short crossover_signal_short crossunder_macd_short crossover_macd_short line 74 line 76 |
| MacD Cross Delay Short | 1 | int | shortMacDCondition |
| Signal Must Also Cross Short | false | bool | entry short entry short close short close short line 107 line 123 |
| Signal Cross Delay Short | 0 | int | shortSignalCondition |
| Stop/Profit Long | |||
| Use Stop Loss Long | false | bool | exit TP/SL Long |
| Stop Loss % Long | 1 | float · min 0.0 · step 0.1 | stop_loss_value_long |
| Use Take Profit Long | false | bool | exit TP/SL Long |
| Take Profit % Long | 1 | float · min 0.0 · step 0.1 | take_profit_value_long |
| Stop/Profit Short | |||
| Use Stop Loss Short | true | bool | exit TP/SL Short |
| Stop Loss % Short | 21 | float · min 0.0 · step 0.1 | stop_loss_value_short |
| Use Take Profit Short | true | bool | exit TP/SL Short |
| Take Profit % Short | 20 | float · min 0.0 · step 0.1 | take_profit_value_short |
| Color Settings | |||
| MACD Line Long | #2962FF | color | MACD Long |
| Signal Line Long | #FF6D00 | color | Signal Long |
| MACD Line Short | #B03DFF | color | MACD Short |
| Signal Line Short | #00FFE8 | color | Signal Short |
| Histogram Color Settings | |||
| Grow Above Long | #26A69A | color | Histogram Long |
| Fall Above Long | #B2DFDB | color | Histogram Long |
| Grow Below Long | #FFCDD2 | color | Histogram Long |
| Fall Below Long | #FF5252 | color | Histogram Long |
| Grow Above Short | #D95965 | color | Histogram Short |
| Fall Above Short | #4D2024 | color | Histogram Short |
| Grow Below Short | #00322D | color | Histogram Short |
| Fall Below Short | #00ADAD | color | Histogram Short |
What it calculates
- SMA / EMASource Long (close), Fast Length Long (13) · chosen by Oscillator MA Type Long
fast_ma_long · line 53 - SMA / EMASource Long (close), Slow Length Long (19) · chosen by Oscillator MA Type Long
slow_ma_long · line 54 - SMA / EMAmacd_long, Signal Smoothing Long (9) · chosen by Signal Line MA Type Long
signal_long · line 56 - SMA / EMASource Short (close), Fast Length Short (11) · chosen by Oscillator MA Type Short
fast_ma_short · line 59 - SMA / EMASource Short (close), Slow Length Short (20) · chosen by Oscillator MA Type Short
slow_ma_short · line 60 - SMA / EMAmacd_short, Signal Smoothing Short (9) · chosen by Signal Line MA Type Short
signal_short · line 62 - Crossoversignal_long, Cross Point Long (0)
crossover_signal_long · line 83 - Crossundersignal_long, Cross Point Long (0)
crossunder_signal_long · line 84 - Crossundersignal_short, Cross Point Short (0)
crossunder_signal_short · line 85 - Crossoversignal_short, Cross Point Short (0)
crossover_signal_short · line 86 - Crossovermacd_long, Cross Point Long (0)
crossover_macd_long · line 87 - Crossundermacd_long, Cross Point Long (0)
crossunder_macd_long · line 88 - Crossundermacd_short, Cross Point Short (0)
crossunder_macd_short · line 89 - Crossovermacd_short, Cross Point Short (0)
crossover_macd_short · line 90
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 | longSignalConditiondetectedLongCrossOver==true and crossover_signal_long[cross_delay_signal_long] | On | 98 |
| Open long long | longMacDCondition | On | 103 |
| Open short short | shortSignalConditiondetectedShortCrossUnder and crossunder_signal_short[cross_delay_signal_short] | On | 109 |
| Open short short | shortMacDCondition | On | 114 |
| Close long | detectedLongCrossOver==false and crossunder_signal_long | On | 119 |
| Close long | crossunder_macd_long | On | 121 |
| Close short | detectedShortCrossUnder==false and crossover_signal_short | On | 124 |
| Close short | crossover_macd_short | On | 126 |
| Exit TP/SL Long stop and limit | (strategy.position_size>0) | On | 132 |
| Exit TP/SL Short stop and limit | (strategy.position_size<0) | On | 134 |
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 | +62.5% | +102.5% | 36 | 31% | 1.48 | 30.9% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.4% | +5.5% | 47 | 26% | 0.99 | 16.8% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +75.6% | +9.8% | 32 | 31% | 1.44 | 38.9% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −3.1% | +8.4% | 46 | 26% | 0.96 | 30.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +2.0% | +16.7% | 34 | 35% | 1.01 | 59.4% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +50.7% | +21.5% | 33 | 30% | 1.85 | 16.9% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −16.6% | +148.3% | 36 | 28% | 0.87 | 46.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +5.1% | +17.1% | 41 | 37% | 1.13 | 12.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 (Signal Smoothing Long 5, Signal Smoothing Short 5) made +17.4% before 26 Dec 2025 and +45.1% after it, against +45.1% for the defaults. 100% of the 49 combinations made money in-sample and 100% out-of-sample.
| Signal Smoothing Short ↓ · Signal Smoothing Long → | 5 | 6 | 8 | 9 | 11 | 13 | 15 |
|---|---|---|---|---|---|---|---|
| 5 | +17% | +17% | +17% | +17% | +17% | +17% | +17% |
| 6 | +17% | +17% | +17% | +17% | +17% | +17% | +17% |
| 8 | +17% | +17% | +17% | +17% | +17% | +17% | +17% |
| 9 | +17% | +17% | +17% | +17% | +17% | +17% | +17% |
| 11 | +17% | +17% | +17% | +17% | +17% | +17% | +17% |
| 13 | +17% | +17% | +17% | +17% | +17% | +17% | +17% |
| 15 | +17% | +17% | +17% | +17% | +17% | +17% | +17% |
| Signal Smoothing Short ↓ · Signal Smoothing Long → | 5 | 6 | 8 | 9 | 11 | 13 | 15 |
|---|---|---|---|---|---|---|---|
| 5 | +45% | +45% | +45% | +45% | +45% | +45% | +45% |
| 6 | +45% | +45% | +45% | +45% | +45% | +45% | +45% |
| 8 | +45% | +45% | +45% | +45% | +45% | +45% | +45% |
| 9 | +45% | +45% | +45% | +45% | +45% | +45% | +45% |
| 11 | +45% | +45% | +45% | +45% | +45% | +45% | +45% |
| 13 | +45% | +45% | +45% | +45% | +45% | +45% | +45% |
| 15 | +45% | +45% | +45% | +45% | +45% | +45% | +45% |
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 | 10 | 20% | −$116.21 |
| Sideways market | 17 | 35% | $571.80 |
| Falling market | 9 | 33% | $169.19 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
50.0%
The 95th percentile of the worst drawdown when the same 36 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 24.6% to 50.0%; the tick is what actually happened, 29.3%. Order luck alone moves it this much.
All 36 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 16 Apr 2024 63,420.00 | 18 May 2024 67,024.00 | 32 | −$56.83 |
| long | 18 May 2024 67,024.00 | 16 Jun 2024 66,228.30 | 29 | −$11.20 |
| short | 16 Jun 2024 66,228.30 | 18 Jul 2024 64,088.00 | 32 | $30.12 |
| long | 18 Jul 2024 64,088.00 | 5 Aug 2024 58,161.00 | 18 | −$88.98 |
| short | 5 Aug 2024 58,161.00 | 25 Aug 2024 64,157.00 | 20 | −$90.01 |
| long | 25 Aug 2024 64,157.00 | 31 Aug 2024 59,124.00 | 6 | −$61.43 |
| short | 31 Aug 2024 59,124.00 | 18 Sep 2024 60,314.00 | 18 | −$14.53 |
| long | 18 Sep 2024 60,314.00 | 12 Oct 2024 62,540.00 | 24 | $26.10 |
| short | 12 Oct 2024 62,540.00 | 13 Oct 2024 63,206.20 | 1 | −$7.81 |
| long | 13 Oct 2024 63,206.20 | 28 Dec 2024 94,299.00 | 76 | $356.86 |
| short | 28 Dec 2024 94,299.00 | 7 Jan 2025 102,236.00 | 10 | −$91.09 |
| long | 7 Jan 2025 102,236.00 | 11 Jan 2025 94,726.10 | 4 | −$72.81 |
| short | 11 Jan 2025 94,726.10 | 17 Jan 2025 99,987.30 | 6 | −$51.01 |
| long | 17 Jan 2025 99,987.30 | 6 Feb 2025 96,612.40 | 20 | −$29.28 |
| short | 6 Feb 2025 96,612.40 | 11 Mar 2025 77,289.90 | 33 | $167.62 |
| long | 18 Apr 2025 84,947.90 | 21 Jun 2025 103,298.00 | 64 | $217.25 |
| short | 21 Jun 2025 103,298.00 | 27 Jun 2025 106,947.00 | 6 | −$43.20 |
| long | 27 Jun 2025 106,947.00 | 5 Aug 2025 115,055.00 | 39 | $89.44 |
| short | 5 Aug 2025 115,055.00 | 9 Aug 2025 116,675.00 | 4 | −$17.87 |
| long | 9 Aug 2025 116,675.00 | 21 Aug 2025 114,271.00 | 12 | −$25.78 |
| short | 21 Aug 2025 114,271.00 | 13 Sep 2025 116,029.00 | 23 | −$18.86 |
| long | 13 Sep 2025 116,029.00 | 27 Sep 2025 109,643.00 | 14 | −$66.41 |
| short | 27 Sep 2025 109,643.00 | 2 Oct 2025 118,595.00 | 5 | −$93.10 |
| long | 2 Oct 2025 118,595.00 | 16 Oct 2025 110,763.00 | 14 | −$69.16 |
| short | 16 Oct 2025 110,763.00 | 19 Nov 2025 88,610.60 | 34 | $195.61 |
| long | 5 Jan 2026 91,529.70 | 25 Jan 2026 89,225.30 | 20 | −$29.55 |
| short | 25 Jan 2026 89,225.30 | 5 Feb 2026 71,380.30 | 11 | $228.82 |
| long | 14 Mar 2026 70,930.00 | 29 Mar 2026 66,377.00 | 15 | −$88.13 |
| short | 29 Mar 2026 66,377.00 | 9 Apr 2026 71,069.90 | 11 | −$90.84 |
| long | 9 Apr 2026 71,069.90 | 22 May 2026 77,615.50 | 43 | $109.97 |
| short | 22 May 2026 77,615.50 | 4 Jun 2026 62,092.40 | 13 | $260.79 |
| long | 12 Jul 2026 63,819.00 | 3 Aug 2026 63,570.00 | 22 | −$6.11 |
| short | 3 Aug 2026 63,570.00 | 7 Aug 2026 64,323.60 | 4 | −$18.48 |
| long | 7 Aug 2026 64,323.60 | 14 Aug 2026 63,490.90 | 7 | −$19.94 |
| short | 14 Aug 2026 63,490.90 | 20 Aug 2026 69,334.80 | 6 | −$139.93 |
| long | 20 Aug 2026 69,334.80 | 17 Oct 2026 81,615.90 | 58 | $244.49 |
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 |
|---|---|---|---|---|---|
| MacD Short and-or Long with Bi-Directional TP and SL this page | — | v5 strategy | +62.5% | 36 | 30.9% |
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
| 4-Hour Stochastic EMA Trend by slymnturkoglu | ema sma | v4 strategy | No trades on this market | ||
| 4X EMA and volume strategy by SoftKill21 | ema sma | v4 strategy | +1.8% | 3 | 25.8% |
| Simple Momentum Strategy Based on SMA, EMA and Volume by slip_stream | sma ema | v4 strategy | +7.4% | 18 | 27.4% |
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.
- 8 strategy calls use the when = argument (line 98, 103, 109, 114, 119, 121, 124, 126). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Same 36 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 98: when = longSignalCondition → if block
- Line 103: when = longMacDCondition → if block
- Line 109: when = shortSignalCondition → if block
- Line 114: when = shortMacDCondition → if block
- Line 119: when = detectedLongCrossOver==false and crossunder_signal_long → if block
- Line 121: when = crossunder_macd_long → if block
- Line 124: when = detectedShortCrossUnder==false and crossover_signal_short → if block
- Line 126: when = crossover_macd_short → if block
⋯ longSignalCondition = detectedLongCrossOver==true and crossover_signal_long[cross_delay_signal_long]- strategy.entry(id="long", direction=strategy.long, when=longSignalCondition)+ if longSignalCondition+ strategy.entry(id="long", direction=strategy.long) if(longSignalCondition)⋯ longMacDCondition = crossover_macd_long[cross_delay_macd_long]- strategy.entry(id="long", direction=strategy.long, when=longMacDCondition)+ if longMacDCondition+ strategy.entry(id="long", direction=strategy.long) if(longMacDCondition)⋯ shortSignalCondition = detectedShortCrossUnder and crossunder_signal_short[cross_delay_signal_short]- strategy.entry(id="short", direction=strategy.short, when=shortSignalCondition)+ if shortSignalCondition+ strategy.entry(id="short", direction=strategy.short) if(shortSignalCondition)⋯ shortMacDCondition = crossunder_macd_short[cross_delay_macd_short]- strategy.entry(id="short", direction=strategy.short, when=shortMacDCondition)+ if shortMacDCondition+ strategy.entry(id="short", direction=strategy.short) if(shortMacDCondition)⋯ if(signal_must_cross_long==true)- strategy.close(id="long", when=detectedLongCrossOver==false and crossunder_signal_long)+ if detectedLongCrossOver==false and crossunder_signal_long+ strategy.close(id="long") else- strategy.close(id="long", when=crossunder_macd_long)+ if crossunder_macd_long+ strategy.close(id="long") if(strategy.position_size < 0 and allow_short==true and allow_long==false) if(signal_must_cross_short==true)- strategy.close(id="short", when=detectedShortCrossUnder==false and crossover_signal_short)+ if detectedShortCrossUnder==false and crossover_signal_short+ strategy.close(id="short") else- strategy.close(id="short", when=crossover_macd_short)+ if crossover_macd_short+ strategy.close(id="short")stop_loss_value_long = strategy.position_avg_price*(1 - stop_loss_long_percentage)⋯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: 36 before, 26 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 48 ms | With a workaround 1,000 bars in 98 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
Zeal Terminal's engine currently reads an order side written as direction = strategy.long, direction = strategy.short as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 98, 103, 109, 114) so the engine trades the way the script says.
“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/// © Gentleman-Goat & TradingTools.Software/Optimizerstrategy(title="MACD Short/Long Strategy for TradingView Input Optimizer", shorttitle="MACD Short/Long TVIO", initial_capital=1000, default_qty_value=100, default_qty_type=strategy.percent_of_equity)// Get Inputs Longallow_long = input.bool(title="Allow Long", defval=true, group="inputs long")fast_length_long = input.int(title="Fast Length Long", defval=13, group="inputs long")slow_length_long = input.int(title="Slow Length Long", defval=19, group="inputs long")src_long = input.source(title="Source Long", defval=close, group="inputs long")signal_length_long = input.int(title="Signal Smoothing Long", minval = 1, maxval = 50, defval = 9, group="inputs long")sma_source_long = input.string(title="Oscillator MA Type Long", defval="EMA", options=["SMA", "EMA"], group="inputs long")sma_signal_long = input.string(title="Signal Line MA Type Long", defval="EMA", options=["SMA", "EMA"], group="inputs long")cross_point_long = input.int(title="Cross Point Long", defval=0, group="inputs long")cross_delay_macd_long = input.int(title="MacD Cross Delay Long", defval=0, group="inputs long")signal_must_cross_long = input.bool(title="Signal Must Also Cross Long", defval=false, group="inputs long")cross_delay_signal_long = input.int(title="Signal Cross Delay Long", defval=0, group="inputs long")//Get Inputs Shortallow_short = input.bool(title="Allow Short", defval=true, group="inputs short")fast_length_short = input.int(title="Fast Length Short", defval=11, group="inputs short")slow_length_short = input.int(title="Slow Length Short", defval=20, group="inputs short")src_short = input.source(title="Source Short", defval=close, group="inputs short")signal_length_short = input.int(title="Signal Smoothing Short", minval = 1, maxval = 50, defval = 9, group="inputs short")sma_source_short = input.string(title="Oscillator MA Type Short", defval="EMA", options=["SMA", "EMA"], group="inputs short")sma_signal_short = input.string(title="Signal Line MA Type Short", defval="EMA", options=["SMA", "EMA"], group="inputs short")cross_point_short = input.int(title="Cross Point Short", defval=0, group="inputs short")cross_delay_macd_short = input.int(title="MacD Cross Delay Short", defval=1, group="inputs short")signal_must_cross_short = input.bool(title="Signal Must Also Cross Short", defval=false, group="inputs short")cross_delay_signal_short = input.int(title="Signal Cross Delay Short", defval=0, group="inputs short")use_stop_loss_long = input.bool(defval=false,title="Use Stop Loss Long", group="Stop/Profit Long")stop_loss_long_percentage = input.float(defval=1,title="Stop Loss % Long",minval=0.0,step=0.1, group="Stop/Profit Long") * .01use_take_profit_long = input.bool(defval=false,title="Use Take Profit Long", group="Stop/Profit Long")take_profit_long_percentage = input.float(defval=1,title="Take Profit % Long",minval=0.0,step=0.1, group="Stop/Profit Long") * .01use_stop_loss_short = input.bool(defval=true,title="Use Stop Loss Short", group="Stop/Profit Short")stop_loss_short_percentage = input.float(defval=21,title="Stop Loss % Short",minval=0.0,step=0.1, group="Stop/Profit Short") * .01use_take_profit_short = input.bool(defval=true,title="Use Take Profit Short", group="Stop/Profit Short")take_profit_short_percentage= input.float(defval=20,title="Take Profit % Short",minval=0.0,step=0.1, group="Stop/Profit Short") * .01//------------------------------------------------------------------------------// Plot colors Longcol_macd_long = input.color(#2962FF, "MACD Line Long", group="Color Settings", inline="MACD")col_signal_long = input.color(#FF6D00, "Signal Line Long", group="Color Settings", inline="Signal")col_grow_above_long = input.color(#26A69A, "Grow Above Long", group="Histogram Color Settings", inline="Above Long")col_fall_above_long = input.color(#B2DFDB, "Fall Above Long", group="Histogram Color Settings", inline="Above Long")col_grow_below_long = input.color(#FFCDD2, "Grow Below Long", group="Histogram Color Settings", inline="Below Long")col_fall_below_long = input.color(#FF5252, "Fall Below Long", group="Histogram Color Settings", inline="Below Long")// Plot colors Shortcol_macd_short = input.color(#B03DFF, "MACD Line Short", group="Color Settings", inline="MACD")col_signal_short = input.color(#00FFE8, "Signal Line Short", group="Color Settings", inline="Signal")col_grow_above_short = input.color(#D95965, "Grow Above Short", group="Histogram Color Settings", inline="Above Short")col_fall_above_short = input.color(#4D2024, "Fall Above Short", group="Histogram Color Settings", inline="Above Short")col_grow_below_short = input.color(#00322D, "Grow Below Short", group="Histogram Color Settings", inline="Below Short")col_fall_below_short = input.color(#00ADAD, "Fall Below Short", group="Histogram Color Settings", inline="Below Short")// Calculate Longfast_ma_long = sma_source_long == "SMA" ? ta.sma(src_long, fast_length_long) : ta.ema(src_long, fast_length_long)slow_ma_long = sma_source_long == "SMA" ? ta.sma(src_long, slow_length_long) : ta.ema(src_long, slow_length_long)macd_long = fast_ma_long - slow_ma_longsignal_long = sma_signal_long == "SMA" ? ta.sma(macd_long, signal_length_long) : ta.ema(macd_long, signal_length_long)hist_long = macd_long - signal_long// Calculate Shortfast_ma_short = sma_source_short == "SMA" ? ta.sma(src_short, fast_length_short) : ta.ema(src_short, fast_length_short)slow_ma_short = sma_source_short == "SMA" ? ta.sma(src_short, slow_length_short) : ta.ema(src_short, slow_length_short)macd_short = fast_ma_short - slow_ma_shortsignal_short = sma_signal_short == "SMA" ? ta.sma(macd_short, signal_length_short) : ta.ema(macd_short, signal_length_short)hist_short = macd_short - signal_short//Plot Longplot(hist_long, title="Histogram Long", style=plot.style_columns, color=(hist_long>=0 ? (hist_long[1] < hist_long ? col_grow_above_long : col_fall_above_long) : (hist_long[1] < hist_long ? col_grow_below_long : col_fall_below_long)))plot(macd_long, title="MACD Long", color=col_macd_long)plot(signal_long, title="Signal Long", color=col_signal_long)//Plot Shortplot(hist_short, title="Histogram Short", style=plot.style_columns, color=(hist_short>=0 ? (hist_short[1] < hist_short ? col_grow_above_short : col_fall_above_short) : (hist_short[1] < hist_short ? col_grow_below_short : col_fall_below_short)))plot(macd_short, title="MACD Short", color=col_macd_short)plot(signal_short, title="Signal Short", color=col_signal_short)var detectedLongCrossOver = falsevar detectedShortCrossUnder = falseif(ta.crossunder(macd_short,cross_point_short)) detectedShortCrossUnder := trueif(ta.crossover(macd_short,cross_point_short)) detectedShortCrossUnder := false if(ta.crossover(macd_long,cross_point_long)) detectedLongCrossOver := trueif(ta.crossunder(macd_long,cross_point_long)) detectedLongCrossOver := falsecrossover_signal_long = ta.crossover(signal_long,cross_point_long)crossunder_signal_long = ta.crossunder(signal_long,cross_point_long)crossunder_signal_short = ta.crossunder(signal_short,cross_point_short)crossover_signal_short = ta.crossover(signal_short,cross_point_short)crossover_macd_long = ta.crossover(macd_long,cross_point_long)crossunder_macd_long = ta.crossunder(macd_long,cross_point_long)crossunder_macd_short = ta.crossunder(macd_short,cross_point_short)crossover_macd_short = ta.crossover(macd_short,cross_point_short)inEntry = false//Strategy Entriesif (strategy.equity > 0) //This is required for the input optimizer to work since it will fail if the strategy fails to succeed by not having enough equity. if (strategy.position_size <= 0 and allow_long==true and inEntry==false) if(signal_must_cross_long==true) longSignalCondition = detectedLongCrossOver==true and crossover_signal_long[cross_delay_signal_long] strategy.entry(id="long", direction=strategy.long, when=longSignalCondition) if(longSignalCondition) inEntry:=true else longMacDCondition = crossover_macd_long[cross_delay_macd_long] strategy.entry(id="long", direction=strategy.long, when=longMacDCondition) if(longMacDCondition) inEntry:=true if (strategy.position_size >= 0 and allow_short==true and inEntry==false) if(signal_must_cross_short==true) shortSignalCondition = detectedShortCrossUnder and crossunder_signal_short[cross_delay_signal_short] strategy.entry(id="short", direction=strategy.short, when=shortSignalCondition) if(shortSignalCondition) inEntry:=true else shortMacDCondition = crossunder_macd_short[cross_delay_macd_short] strategy.entry(id="short", direction=strategy.short, when=shortMacDCondition) if(shortMacDCondition) inEntry:=true if(strategy.position_size > 0 and allow_long==true and allow_short==false) if(signal_must_cross_long==true) strategy.close(id="long", when=detectedLongCrossOver==false and crossunder_signal_long) else strategy.close(id="long", when=crossunder_macd_long) if(strategy.position_size < 0 and allow_short==true and allow_long==false) if(signal_must_cross_short==true) strategy.close(id="short", when=detectedShortCrossUnder==false and crossover_signal_short) else strategy.close(id="short", when=crossover_macd_short)stop_loss_value_long = strategy.position_avg_price*(1 - stop_loss_long_percentage)take_profit_value_long = strategy.position_avg_price*(1 + take_profit_long_percentage)stop_loss_value_short = strategy.position_avg_price*(1 + stop_loss_short_percentage)take_profit_value_short = strategy.position_avg_price*(1 - take_profit_short_percentage)if(strategy.position_size>0) //Long positions only strategy.exit(id="TP/SL Long",from_entry="long", limit=use_take_profit_long ? take_profit_value_long : na, stop=use_stop_loss_long ? stop_loss_value_long : na)if(strategy.position_size<0) //Short positions only strategy.exit(id="TP/SL Short",from_entry="short", limit=use_take_profit_short ? take_profit_value_short : na, stop=use_stop_loss_short ? stop_loss_value_short : na)