Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (30)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOctHistogram Long$1,563$2,025Equity from $1,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots. 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.
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.

SettingDefaultType and rangeFeeds
inputs long
Allow Longtrueboolline 95 line 117 line 122
Fast Length Long13intfast_ma_long
Slow Length Long19intslow_ma_long
Source Longclosesourcefast_ma_long slow_ma_long
Signal Smoothing Long9int · min 1 · max 50signal_long
Oscillator MA Type LongEMAstring · options ["SMA", "EMA"]fast_ma_long slow_ma_long
Signal Line MA Type LongEMAstring · options ["SMA", "EMA"]signal_long
Cross Point Long0intcrossover_signal_long crossunder_signal_long crossover_macd_long crossunder_macd_long line 79 line 81
MacD Cross Delay Long0intlongMacDCondition
Signal Must Also Cross Longfalseboolentry long entry long close long close long line 96 line 118
Signal Cross Delay Long0intlongSignalCondition
inputs short
Allow Shorttrueboolline 106 line 117 line 122
Fast Length Short11intfast_ma_short
Slow Length Short20intslow_ma_short
Source Shortclosesourcefast_ma_short slow_ma_short
Signal Smoothing Short9int · min 1 · max 50signal_short
Oscillator MA Type ShortEMAstring · options ["SMA", "EMA"]fast_ma_short slow_ma_short
Signal Line MA Type ShortEMAstring · options ["SMA", "EMA"]signal_short
Cross Point Short0intcrossunder_signal_short crossover_signal_short crossunder_macd_short crossover_macd_short line 74 line 76
MacD Cross Delay Short1intshortMacDCondition
Signal Must Also Cross Shortfalseboolentry short entry short close short close short line 107 line 123
Signal Cross Delay Short0intshortSignalCondition
Stop/Profit Long
Use Stop Loss Longfalseboolexit TP/SL Long
Stop Loss % Long1float · min 0.0 · step 0.1stop_loss_value_long
Use Take Profit Longfalseboolexit TP/SL Long
Take Profit % Long1float · min 0.0 · step 0.1take_profit_value_long
Stop/Profit Short
Use Stop Loss Shorttrueboolexit TP/SL Short
Stop Loss % Short21float · min 0.0 · step 0.1stop_loss_value_short
Use Take Profit Shorttrueboolexit TP/SL Short
Take Profit % Short20float · min 0.0 · step 0.1take_profit_value_short
Color Settings
MACD Line Long#2962FFcolorMACD Long
Signal Line Long#FF6D00colorSignal Long
MACD Line Short#B03DFFcolorMACD Short
Signal Line Short#00FFE8colorSignal Short
Histogram Color Settings
Grow Above Long#26A69AcolorHistogram Long
Fall Above Long#B2DFDBcolorHistogram Long
Grow Below Long#FFCDD2colorHistogram Long
Fall Below Long#FF5252colorHistogram Long
Grow Above Short#D95965colorHistogram Short
Fall Above Short#4D2024colorHistogram Short
Grow Below Short#00322DcolorHistogram Short
Fall Below Short#00ADADcolorHistogram Short

What it calculates

  • SMA / EMASource Long (close), Fast Length Long (13) · chosen by Oscillator MA Type Longfast_ma_long · line 53
  • SMA / EMASource Long (close), Slow Length Long (19) · chosen by Oscillator MA Type Longslow_ma_long · line 54
  • SMA / EMAmacd_long, Signal Smoothing Long (9) · chosen by Signal Line MA Type Longsignal_long · line 56
  • SMA / EMASource Short (close), Fast Length Short (11) · chosen by Oscillator MA Type Shortfast_ma_short · line 59
  • SMA / EMASource Short (close), Slow Length Short (20) · chosen by Oscillator MA Type Shortslow_ma_short · line 60
  • SMA / EMAmacd_short, Signal Smoothing Short (9) · chosen by Signal Line MA Type Shortsignal_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.

closecloseprice datasma_source_longOscillator MA Type Longsetting · EMAfast_length_longFast Length Longsetting · 13slow_length_longSlow Length Longsetting · 19sma_signal_longSignal Line MA Type Longsetting · EMAsignal_length_longSignal Smoothing Longsetting · 9sma_source_shortOscillator MA Type Shortsetting · EMAfast_length_shortFast Length Shortsetting · 11slow_length_shortSlow Length Shortsetting · 20sma_signal_shortSignal Line MA Type Sho…setting · EMAsignal_length_shortSignal Smoothing Shortsetting · 9cross_point_longCross Point Longsetting · 0cross_point_shortCross Point Shortsetting · 0cross_delay_macd_longMacD Cross Delay Longsetting · 0cross_delay_macd_shortMacD Cross Delay Shortsetting · 1stop_loss_long_percentageStop Loss % Longsetting · 1take_profit_long_percentageTake Profit % Longsetting · 1stop_loss_short_percentageStop Loss % Shortsetting · 21take_profit_short_percentageTake Profit % Shortsetting · 20cross_delay_signal_longSignal Cross Delay Longsetting · 0cross_delay_signal_shortSignal Cross Delay Shortsetting · 0fast_ma_longSMA / EMA→ fast_ma_long · by Oscillato…slow_ma_longSMA / EMA→ slow_ma_long · by Oscillato…signal_longSMA / EMA→ signal_long · by Signal Lin…fast_ma_shortSMA / EMA→ fast_ma_short · by Oscillat…slow_ma_shortSMA / EMA→ slow_ma_short · by Oscillat…signal_shortSMA / EMA→ signal_short · by Signal Li…crossover_signal_longCrossover→ crossover_signal_longcrossunder_signal_longCrossunder→ crossunder_signal_longcrossunder_signal_shortCrossunder→ crossunder_signal_shortcrossover_signal_shortCrossover→ crossover_signal_shortcrossover_macd_longCrossover→ crossover_macd_longcrossunder_macd_longCrossunder→ crossunder_macd_longcrossunder_macd_shortCrossunder→ crossunder_macd_shortcrossover_macd_shortCrossover→ crossover_macd_shortmacd_longmacd_longvaluehist_longhist_longvaluemacd_shortmacd_shortvaluehist_shorthist_shortvaluelongMacDConditionlongMacDConditionvalueshortMacDConditionshortMacDConditionvaluestop_loss_value_longstop_loss_value_longvaluetake_profit_value_longtake_profit_value_longvaluestop_loss_value_shortstop_loss_value_shortvaluetake_profit_value_shorttake_profit_value_shortvaluelongSignalConditionlongSignalConditionconditionshortSignalConditionshortSignalConditionconditionentry:longOpen longstrategy.entryentry:shortOpen shortstrategy.entryclose:longClose longstrategy.closeclose:shortClose shortstrategy.closeexit:TP/SL Longexit TP/SL Longstrategy.exitexit:TP/SL Shortexit TP/SL Shortstrategy.exitplot:65Histogram Longplotplot:66MACD Longplotplot:67Signal Longplotplot:69Histogram Shortplotplot:70MACD Shortplotplot:71Signal Shortplot

Order rules

OrderFires whenBy defaultLine
Open long longlongSignalConditiondetectedLongCrossOver==true and crossover_signal_long[cross_delay_signal_long]On98
Open long longlongMacDConditionOn103
Open short shortshortSignalConditiondetectedShortCrossUnder and crossunder_signal_short[cross_delay_signal_short]On109
Open short shortshortMacDConditionOn114
Close longdetectedLongCrossOver==false and crossunder_signal_longOn119
Close longcrossunder_macd_longOn121
Close shortdetectedShortCrossUnder==false and crossover_signal_shortOn124
Close shortcrossover_macd_shortOn126
Exit TP/SL Long stop and limit(strategy.position_size>0)On132
Exit TP/SL Short stop and limit(strategy.position_size<0)On134

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.

11/11trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+62.5%+102.5%3631%1.4830.9%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.4%+5.5%4726%0.9916.8%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+75.6%+9.8%3231%1.4438.9%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−3.1%+8.4%4626%0.9630.8%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+2.0%+16.7%3435%1.0159.4%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+50.7%+21.5%3330%1.8516.9%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−16.6%+148.3%3628%0.8746.8%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+5.1%+17.1%4137%1.1312.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.

Before 26 Dec 2025 · in-sample
Signal Smoothing Short ↓ · Signal Smoothing Long →5689111315
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%
From 26 Dec 2025 · out-of-sample
Signal Smoothing Short ↓ · Signal Smoothing Long →5689111315
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 barsTradesWonP&L
Rising market1020%−$116.21
Sideways market1735%$571.80
Falling market933%$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.

0%57%
All 36 trades
SideEntryExitBarsP&L
short16 Apr 2024 63,420.0018 May 2024 67,024.0032−$56.83
long18 May 2024 67,024.0016 Jun 2024 66,228.3029−$11.20
short16 Jun 2024 66,228.3018 Jul 2024 64,088.0032$30.12
long18 Jul 2024 64,088.005 Aug 2024 58,161.0018−$88.98
short5 Aug 2024 58,161.0025 Aug 2024 64,157.0020−$90.01
long25 Aug 2024 64,157.0031 Aug 2024 59,124.006−$61.43
short31 Aug 2024 59,124.0018 Sep 2024 60,314.0018−$14.53
long18 Sep 2024 60,314.0012 Oct 2024 62,540.0024$26.10
short12 Oct 2024 62,540.0013 Oct 2024 63,206.201−$7.81
long13 Oct 2024 63,206.2028 Dec 2024 94,299.0076$356.86
short28 Dec 2024 94,299.007 Jan 2025 102,236.0010−$91.09
long7 Jan 2025 102,236.0011 Jan 2025 94,726.104−$72.81
short11 Jan 2025 94,726.1017 Jan 2025 99,987.306−$51.01
long17 Jan 2025 99,987.306 Feb 2025 96,612.4020−$29.28
short6 Feb 2025 96,612.4011 Mar 2025 77,289.9033$167.62
long18 Apr 2025 84,947.9021 Jun 2025 103,298.0064$217.25
short21 Jun 2025 103,298.0027 Jun 2025 106,947.006−$43.20
long27 Jun 2025 106,947.005 Aug 2025 115,055.0039$89.44
short5 Aug 2025 115,055.009 Aug 2025 116,675.004−$17.87
long9 Aug 2025 116,675.0021 Aug 2025 114,271.0012−$25.78
short21 Aug 2025 114,271.0013 Sep 2025 116,029.0023−$18.86
long13 Sep 2025 116,029.0027 Sep 2025 109,643.0014−$66.41
short27 Sep 2025 109,643.002 Oct 2025 118,595.005−$93.10
long2 Oct 2025 118,595.0016 Oct 2025 110,763.0014−$69.16
short16 Oct 2025 110,763.0019 Nov 2025 88,610.6034$195.61
long5 Jan 2026 91,529.7025 Jan 2026 89,225.3020−$29.55
short25 Jan 2026 89,225.305 Feb 2026 71,380.3011$228.82
long14 Mar 2026 70,930.0029 Mar 2026 66,377.0015−$88.13
short29 Mar 2026 66,377.009 Apr 2026 71,069.9011−$90.84
long9 Apr 2026 71,069.9022 May 2026 77,615.5043$109.97
short22 May 2026 77,615.504 Jun 2026 62,092.4013$260.79
long12 Jul 2026 63,819.003 Aug 2026 63,570.0022−$6.11
short3 Aug 2026 63,570.007 Aug 2026 64,323.604−$18.48
long7 Aug 2026 64,323.6014 Aug 2026 63,490.907−$19.94
short14 Aug 2026 63,490.9020 Aug 2026 69,334.806−$139.93
long20 Aug 2026 69,334.8017 Oct 2026 81,615.9058$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.

ScriptSharesTypeNetTradesMax drawdown
MacD Short and-or Long with Bi-Directional TP and SL this page—v5 strategy+62.5%3630.9%
MACD Long Strat by TheGrindTodayema smav4 strategy+24.0%7920.1%
Stock trending strategy by exlux99ema smav4 strategy+35.3%2114.7%
4-Hour Stochastic EMA Trend by slymnturkogluema smav4 strategyNo trades on this market
4X EMA and volume strategy by SoftKill21ema smav4 strategy+1.8%325.8%
Simple Momentum Strategy Based on SMA, EMA and Volume by slip_streamsma emav4 strategy+7.4%1827.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.

Clean v5

VerifiedDownload the v5 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 48 msWith a workaround 1,000 bars in 98 msNot measured
PineTSNot testedNot testedNot 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)