Zeal Terminal Open in Zeal Terminal

Pine Script v5 · Strategy

5 Minute EMA Cross Strategy

A Pine Script v5 strategy built on EMA or SMA or RMA or WMA (close, 20), EMA (hl2, 300) and Lowest (low, 3). 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 100% of equity and pays 0.05% commission. 16 settings; defaults include Fast Length 20, indent_4+"Type" EMA and indent_4+"Source" 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 5 Minute EMA Cross Strategy repaint?
Yes
At least one pattern in the source can repaint: intrabar recalculation.
How did 5 Minute EMA Cross Strategy do on BTCUSDT 1D with default settings?
+3.43%
From 26 Jan 2024 to 21 Oct 2026, over 9 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Does 5 Minute EMA Cross Strategy 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: 9 before, 14 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 5 Minute EMA Cross Strategy use a stop loss?
Yes
strategy.exit sets a stop on line 101, 105.
Does 5 Minute EMA Cross Strategy run in Zeal Terminal?
Yes
It compiles in 32 ms and runs 1,000 bars in 100 ms, in the browser.

BTCUSDT · 1D · default settings

  • Fast EMA
  • Slow EMA
  • Bars Since Entry
  • Plot
  • Plot
  • Short Entry Symbol
  • Long Entry Symbol
  • Entry
  • 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 (16)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$103,433$202,527Equity from $100,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 99, 103) so the engine trades the way the script says.
Net profit
+3.43%
$3,432.21
Buy and hold
+102.5%
same period
Closed trades
9
5 won · 4 lost
Profitable
55.6%
of closed trades
Profit factor
1.34
gross profit ÷ gross loss
Max drawdown
7.52%
−$8,184.84

One run with the author's default settings, $100,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

    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 99, 100, 101, 103, 104, 105. 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
EMA Settings
Fast Length20int · min 1fast_EMA
indent_4+"Type"EMAstring · options ["EMA", "SMA", "RMA", "WMA"]fast_EMA
indent_4+"Source"closesourcefast_EMA
Slow Length100int · min 1slow_EMA
indent_4+"Type"EMAstring · options ["EMA", "SMA", "RMA", "WMA"]slow_EMA
indent_4+"Source"closesourceslow_EMA
Trade Filters
Use EMA Filterfalseboollong_conditions_met short_conditions_met EMA Macro Filter
indent_4+"Timeframe"timeframeema_filter_smoothed
indent_4+"Length"300int · min 1 · step 10ema_filter
indent_4+"Source"hl2sourceema_filter
Entry Settings
Stop Loss (%)2.0float · step 0.1long_stop_loss short_stop_loss
Take Profit (%)2.0float · step 0.1long_profit_target short_profit_target
Long Entry Limit Lookback3int · min 1 · step 1limit_order_long_price
Short Entry Limit Lookback3int · min 1 · step 1limit_order_short_price
Start Trailing After (%)1float · step 0.1long_start_trailing_val short_start_trailing_val
Trail Behind (%)1float · step 0.1long_trail_behind_val short_trail_behind_val long_trailing_stop short_trailing_stop

What it calculates

  • EMA / SMA / RMA / WMAindent_4+"Source" (close), Fast Length (20) · chosen by indent_4+"Type"fast_EMA · line 31
  • EMA / SMA / RMA / WMAindent_4+"Source" (close), Slow Length (100) · chosen by indent_4+"Type"slow_EMA · line 41
  • EMAindent_4+"Source" (hl2), indent_4+"Length" (300)ema_filter · line 53
  • Lowestlow, Long Entry Limit Lookback (3)limit_order_long_price · line 61
  • Highesthigh, Short Entry Limit Lookback (3)limit_order_short_price · line 62
  • Highesthigh, bars_since_entrylong_run_up · line 77
  • Lowestlow, bars_since_entryshort_run_up · line 81
  • Crossunderfast_EMA, slow_EMAfast_EMA_cross_down_slow_EMA · line 86
  • Crossoverfast_EMA, slow_EMAfast_EMA_cross_up_slow_EMA · line 87

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 datafast_EMA_typeindent_4+"Type"setting · EMAfast_EMA_lengthFast Lengthsetting · 20slow_EMA_typeindent_4+"Type"setting · EMAslow_EMA_lengthSlow Lengthsetting · 100hl2hl2price dataEMA_filter_lengthindent_4+"Length"setting · 300lowlowprice datalong_entry_limit_lookbackLong Entry Limit Lookba…setting · 3highhighprice datashort_entry_limit_lookbackShort Entry Limit Lookb…setting · 3EMA_filter_timeframeindent_4+"Timeframe"setting · start_trailing_afterStart Trailing After (%)setting · 1trail_behindTrail Behind (%)setting · 1take_profit_valTake Profit (%)setting · 2.0stop_loss_valStop Loss (%)setting · 2.0fast_EMAEMA / SMA / RMA / WMA→ fast_EMA · by indent_4+"Typ…slow_EMAEMA / SMA / RMA / WMA→ slow_EMA · by indent_4+"Typ…ema_filterEMA→ ema_filterlimit_order_long_priceLowest→ limit_order_long_pricelimit_order_short_priceHighest→ limit_order_short_pricelong_run_upHighest→ long_run_upshort_run_upLowest→ short_run_upfast_EMA_cross_down_slow_EMACrossunder→ fast_EMA_cross_down_slow_EMAfast_EMA_cross_up_slow_EMACrossover→ fast_EMA_cross_up_slow_EMAema_filter_smoothedema_filter_smoothedvaluelong_start_trailing_val, short_start_trailing_vallong_start_trailing_val…2 valueslong_trail_behind_val, short_trail_behind_vallong_trail_behind_val ·…2 valueslong_profit_target, short_profit_targetlong_profit_target · sh…2 valueslong_stop_loss, short_stop_losslong_stop_loss · short_…2 valuesbars_since_entrybars_since_entryvaluelong_trailing_stoplong_trailing_stopvalueshort_trailing_stopshort_trailing_stopvaluelong_conditions_metlong_conditions_metvalueshort_conditions_metshort_conditions_metvaluelong_position_take_profitlong_position_take_prof…conditionshort_position_take_profitshort_position_take_pro…conditioncurrently_in_a_long_postion, currently_in_a_short_postioncurrently_in_a_long_pos…2 conditionsfast_EMA_is_above_slow_EMA, fast_EMA_is_below_slow_EMAfast_EMA_is_above_slow_…2 conditionsema_macro_filter_longs_only, ema_macro_filter_shorts_onlyema_macro_filter_longs_…2 conditionsocean_blue, sky_blue, green, red, light_blue, light_green, light_red, light_yellow, white, light_gray, transparentColourocean_blue · sky_blue +9entry:LongOpen Longstrategy.entrycancel:Cancel Longcancel Cancel Longstrategy.cancelexit:Close Longexit Close Longstrategy.exitentry:ShortOpen Shortstrategy.entrycancel:Cancel Shortcancel Cancel Shortstrategy.cancelexit:Close Shortexit Close Shortstrategy.exitplot:37Fast EMAplotplot:47Slow EMAplotplot:55EMA Macro Filterplotplot:76Bars Since Entryplotplot:80plotplotplot:84plotplotplotshape:88Short Entry Symbolplotshapeplotshape:89Long Entry Symbolplotshapeplot:106Entryplotfill:107Fillfill × 2fill:108Fillfill × 2

Order rules

OrderFires whenBy defaultLine
Open Long longlong_conditions_metOn99
cancel Cancel Longta.crossover(fast_EMA,slow_EMA)On100
Exit Close Long stop and limitlong_position_take_profitta.cross(close, long_trailing_stop) or close > long_profit_targetOn101
Open Short shortshort_conditions_metOn103
cancel Cancel Shortta.crossunder(fast_EMA,slow_EMA)On104
Exit Close Short stop and limitshort_position_take_profitta.cross(close, short_trailing_stop) or close > short_profit_targetOn105

Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.

Does it repaint?

Yes

At least one pattern in the source can repaint; it is marked Check below.

2/2trades 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. On 1 of them the position changed while the bar was forming before it settled. Anyone watching live sees those changes; once the bar closes it matches history.
  • ClearHigher-timeframe data1 request.security call(s), none with lookahead on.
  • ClearFuture-bar referencesNo negative history offsets such as close[-1].
  • CheckIntrabar recalculationreads realtime-only state. Live values can differ from history.
  • 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+3.4%+102.5%956%1.347.5%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.9%+5.5%633%0.663.2%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+14.6%+9.8%475%132.4437.5%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+1.7%+8.4%560%1.723.6%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+9.2%+16.7%956%2.0731.9%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+7.1%+21.5%1070%2.976.7%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+4.2%+148.3%757%1.598.6%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+1.9%+17.1%367%11.0518.2%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 barsTradesWonP&L
Rising market0—$0.00
Sideways market863%$3,535.02
Falling market10%−$102.81

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

9.0%

The 95th percentile of the worst drawdown when the same 9 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 5.5% to 9.0%; the tick is what actually happened, 5.5%. Order luck alone moves it this much.

0%10%
All 9 trades
SideEntryExitBarsP&L
short30 Jun 2024 62,389.201 Jul 2024 62,772.001−$713.50
long23 Jul 2024 65,777.0027 Jul 2024 67,908.004$3,114.17
short8 Aug 2024 62,198.209 Aug 2024 61,686.001$740.95
long1 Oct 2024 62,538.8015 Oct 2024 66,084.0014$5,738.05
short2 Mar 2025 89,414.103 Mar 2025 94,270.001−$6,021.78
short21 Oct 2025 111,706.0024 Oct 2025 110,078.003$1,395.67
long17 Nov 2025 91,660.0017 Nov 2025 91,660.000−$102.81
long23 May 2026 74,937.5024 May 2026 76,752.001$2,415.21
short21 Aug 2026 76,174.1022 Aug 2026 78,338.001−$3,133.73

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
5 Minute EMA Cross Strategy this page—v5 strategy+3.4%97.5%
SPX Scalping Strategy by connor2279sma ema lowest wma rma crossv5 strategy+1.5%1095.0%
MACD Willy Strategy by PtGamblerrma sma ema highest lowest wmav5 strategyNo trades on this market
VuManChu Cipher B + Divergences Strategy by nattichai001ema cross lowest highest sma rma wmav4 strategy+76.6%17518.4%
Bagheri IG Ether by Bagheri_IGema highest lowest rma smav4 strategy+0.0%30.0%
Bagheri IG Ether v2 by Bagheri_IGema highest lowest rma smav4 strategy−0.0%20.0%

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.

  • 6 strategy calls use the when = argument (line 99, 100, 101, 103, 104, 105). 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 9 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 99: when = long_conditions_met → if block
  • Line 100: when = ta.crossover(fast_EMA,slow_EMA) → if block
  • Line 101: when = long_position_take_profit → if block
  • Line 103: when = short_conditions_met → if block
  • Line 104: when = ta.crossunder(fast_EMA,slow_EMA) → if block
  • Line 105: when = short_position_take_profit → if block
⋯// Long Entry-strategy.entry(id="Long", direction=strategy.long, limit=limit_order_long_price, when=long_conditions_met)+if long_conditions_met+    strategy.entry(id="Long", direction=strategy.long, limit=limit_order_long_price)-strategy.cancel(id="Cancel Long", when=ta.crossover(fast_EMA,slow_EMA))+if ta.crossover(fast_EMA,slow_EMA)+    strategy.cancel(id="Cancel Long")-strategy.exit(id="Close Long", from_entry="Long", stop=long_trailing_stop, limit=long_profit_target, when=long_position_take_profit)+if long_position_take_profit+    strategy.exit(id="Close Long", from_entry="Long", stop=long_trailing_stop, limit=long_profit_target)// Short Entry -strategy.entry(id="Short", direction=strategy.short, limit=limit_order_short_price, when=short_conditions_met)+if short_conditions_met+    strategy.entry(id="Short", direction=strategy.short, limit=limit_order_short_price)-strategy.cancel(id="Cancel Short", when=ta.crossunder(fast_EMA,slow_EMA))+if ta.crossunder(fast_EMA,slow_EMA)+    strategy.cancel(id="Cancel Short")-strategy.exit(id="Close Short", from_entry="Short", stop=short_trailing_stop, limit=short_profit_target, when=short_position_take_profit)+if short_position_take_profit+    strategy.exit(id="Close Short", from_entry="Short", stop=short_trailing_stop, limit=short_profit_target)entry = plot(strategy.position_avg_price, editable=false, title="Entry", style=plot.style_stepline, color=currently_in_a_long_postion or currently_in_a_short_postion ? color.blue : transparent, linewidth=1)⋯

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: 9 before, 14 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.

⋯// © jordanfray-//@version=5+//@version=6strategy(title="5 Minute EMA Strategy", overlay=true, max_bars_back=5000, default_qty_type=strategy.percent_of_equity, default_qty_value=100,initial_capital=100000, commission_type=strategy.commission.percent, commission_value=0.05, backtest_fill_limits_assumption=2)⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 32 msWith a workaround 1,000 bars in 100 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 99, 103) 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.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © jordanfray//@version=5strategy(title="5 Minute EMA Strategy", overlay=true, max_bars_back=5000, default_qty_type=strategy.percent_of_equity, default_qty_value=100,initial_capital=100000, commission_type=strategy.commission.percent, commission_value=0.05, backtest_fill_limits_assumption=2)max_bars_back(time, 5000)// Indenting Classsindent_1 = " "indent_2 = "  "indent_3 = "   "indent_4 = "    "// Group Titlesgroup_one_title = "EMA Settings"group_two_title = "Entry Settings"group_three_title = "Trade Filters"// Input Tipsocean_blue = color.new(#0C6090,0)sky_blue = color.new(#00A5FF,0)green = color.new(#2DBD85,0)red = color.new(#E02A4A,0)light_blue = color.new(#00A5FF,85)light_green = color.new(#2DBD85,85)light_red = color.new(#E02A4A,85)light_yellow = color.new(#FFF900,85)white = color.new(#ffffff,0)light_gray = color.new(#000000,70)transparent = color.new(#000000,100)// Strategy Settings - EMAfast_EMA_length = input.int(defval=20, minval=1, title="Fast Length", group=group_one_title)fast_EMA_type = input.string(defval="EMA", options = ["EMA", "SMA", "RMA", "WMA"], title=indent_4+"Type", group=group_one_title)fast_EMA_source = input.source(defval=close, title=indent_4+"Source", group=group_one_title)fast_EMA = switch fast_EMA_type    "EMA" => ta.ema(fast_EMA_source, fast_EMA_length)    "SMA" => ta.sma(fast_EMA_source, fast_EMA_length)    "RMA" => ta.rma(fast_EMA_source, fast_EMA_length)    "WMA" => ta.wma(fast_EMA_source, fast_EMA_length)    => naplot(fast_EMA, title="Fast EMA", linewidth=1, color=green, editable=true)slow_EMA_length = input.int(defval=100, minval=1, title="Slow Length", group=group_one_title)slow_EMA_type = input.string(defval="EMA", options = ["EMA", "SMA", "RMA", "WMA"], title=indent_4+"Type", group=group_one_title)slow_EMA_source = input.source(defval=close, title=indent_4+"Source", group=group_one_title)slow_EMA = switch slow_EMA_type    "EMA" => ta.ema(slow_EMA_source, slow_EMA_length)    "SMA" => ta.sma(slow_EMA_source, slow_EMA_length)    "RMA" => ta.rma(slow_EMA_source, slow_EMA_length)    "WMA" => ta.wma(slow_EMA_source, slow_EMA_length)    => naplot(slow_EMA, title="Slow EMA", linewidth=1, color=sky_blue, editable=true)// EMA Macro Filterenable_EMA_filter = input.bool(defval=false, title="Use EMA Filter", group=group_three_title)EMA_filter_timeframe = input.timeframe(defval="", title=indent_4+"Timeframe", group=group_three_title)EMA_filter_length = input.int(defval=300, minval=1, step=10, title=indent_4+"Length", group=group_three_title)EMA_filter_source = input.source(defval=hl2, title=indent_4+"Source", group=group_three_title)ema_filter = ta.ema(EMA_filter_source, EMA_filter_length)ema_filter_smoothed = request.security(syminfo.tickerid, EMA_filter_timeframe, ema_filter[barstate.isrealtime ? 1 : 0], gaps=barmerge.gaps_on)plot(enable_EMA_filter ? ema_filter_smoothed: na, title="EMA Macro Filter", linewidth=2, color=white, editable=true)// Entry Settingsstop_loss_val = input.float(defval=2.0, title="Stop Loss (%)", step=0.1, group=group_two_title)/100take_profit_val = input.float(defval=2.0, title="Take Profit (%)", step=0.1, group=group_two_title)/100long_entry_limit_lookback = input.int(defval=3, title="Long Entry Limit Lookback", minval=1, step=1, group=group_two_title)short_entry_limit_lookback = input.int(defval=3, title="Short Entry Limit Lookback", minval=1, step=1, group=group_two_title)limit_order_long_price = ta.lowest(low, long_entry_limit_lookback)limit_order_short_price = ta.highest(high, short_entry_limit_lookback)start_trailing_after = input.float(defval=1, title="Start Trailing After (%)", step=0.1, group=group_two_title)/100trail_behind = input.float(defval=1, title="Trail Behind (%)", step=0.1, group=group_two_title)/100long_start_trailing_val = strategy.position_avg_price + (strategy.position_avg_price * start_trailing_after)short_start_trailing_val = strategy.position_avg_price - (strategy.position_avg_price * start_trailing_after)long_trail_behind_val = close - (strategy.position_avg_price * (trail_behind/100))short_trail_behind_val = close + (strategy.position_avg_price * (trail_behind/100))currently_in_a_long_postion = strategy.position_size > 0currently_in_a_short_postion = strategy.position_size < 0long_profit_target = strategy.position_avg_price * (1 + take_profit_val)long_stop_loss = strategy.position_avg_price * (1.0 - stop_loss_val)short_profit_target = strategy.position_avg_price * (1 - take_profit_val)short_stop_loss = strategy.position_avg_price * (1 + stop_loss_val)bars_since_entry = currently_in_a_long_postion or currently_in_a_short_postion ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) + 1 : 5plot(bars_since_entry, editable=false, title="Bars Since Entry", color=green)long_run_up = ta.highest(high, bars_since_entry)long_trailing_stop = currently_in_a_long_postion and bars_since_entry > 0 and long_run_up > long_start_trailing_val ? long_run_up - (long_run_up * trail_behind) : long_stop_loss// long_run_up_line = plot(long_run_up, style=plot.style_stepline, editable=false, color=currently_in_a_long_postion ? green : transparent)long_trailing_stop_line = plot(long_trailing_stop, style=plot.style_stepline, editable=false, color=currently_in_a_long_postion ? long_trailing_stop > strategy.position_avg_price ? green : red : transparent)short_run_up = ta.lowest(low, bars_since_entry)short_trailing_stop = currently_in_a_short_postion and bars_since_entry > 0 and short_run_up < short_start_trailing_val ? short_run_up + (short_run_up * trail_behind) : short_stop_loss// short_run_up_line = plot(short_run_up, style=plot.style_stepline, editable=false, color=currently_in_a_short_postion ? green : transparent)short_trailing_stop_line = plot(short_trailing_stop, style=plot.style_stepline, editable=false, color=currently_in_a_short_postion ? short_trailing_stop < strategy.position_avg_price ? green : red : transparent)// Trade Conditionsfast_EMA_cross_down_slow_EMA = ta.crossunder(fast_EMA,slow_EMA)fast_EMA_cross_up_slow_EMA = ta.crossover(fast_EMA,slow_EMA)plotshape(fast_EMA_cross_down_slow_EMA ? close : na, title="Short Entry Symbol", color=red, style=shape.triangledown, location=location.belowbar)plotshape(fast_EMA_cross_up_slow_EMA ? close : na, title="Long Entry Symbol", color=green, style=shape.triangleup, location=location.abovebar)fast_EMA_is_above_slow_EMA = fast_EMA > slow_EMAfast_EMA_is_below_slow_EMA = fast_EMA < slow_EMAema_macro_filter_longs_only = fast_EMA > ema_filter_smoothed and slow_EMA > ema_filter_smoothedema_macro_filter_shorts_only = fast_EMA < ema_filter_smoothed and slow_EMA < ema_filter_smoothedlong_position_take_profit = ta.cross(close, long_trailing_stop) or close > long_profit_targetshort_position_take_profit = ta.cross(close, short_trailing_stop) or close > short_profit_targetlong_conditions_met = enable_EMA_filter ? ema_macro_filter_longs_only and fast_EMA_cross_up_slow_EMA and fast_EMA_is_above_slow_EMA and not currently_in_a_short_postion : fast_EMA_cross_up_slow_EMA and not currently_in_a_short_postionshort_conditions_met = enable_EMA_filter ? ema_macro_filter_shorts_only and fast_EMA_cross_down_slow_EMA and fast_EMA_is_below_slow_EMA and not currently_in_a_long_postion : fast_EMA_cross_down_slow_EMA and fast_EMA_is_below_slow_EMA and not currently_in_a_long_postion// Long Entrystrategy.entry(id="Long", direction=strategy.long, limit=limit_order_long_price, when=long_conditions_met)strategy.cancel(id="Cancel Long", when=ta.crossover(fast_EMA,slow_EMA))strategy.exit(id="Close Long", from_entry="Long", stop=long_trailing_stop, limit=long_profit_target, when=long_position_take_profit)// Short Entry strategy.entry(id="Short", direction=strategy.short, limit=limit_order_short_price, when=short_conditions_met)strategy.cancel(id="Cancel Short", when=ta.crossunder(fast_EMA,slow_EMA))strategy.exit(id="Close Short", from_entry="Short", stop=short_trailing_stop, limit=short_profit_target, when=short_position_take_profit)entry = plot(strategy.position_avg_price, editable=false, title="Entry", style=plot.style_stepline, color=currently_in_a_long_postion or currently_in_a_short_postion ? color.blue : transparent, linewidth=1)fill(entry,long_trailing_stop_line, editable=false, color=currently_in_a_long_postion ? long_trailing_stop > strategy.position_avg_price ? light_green : light_red : transparent)fill(entry,short_trailing_stop_line, editable=false, color=currently_in_a_short_postion ? short_trailing_stop < strategy.position_avg_price ? light_green : light_red : transparent)//ltp = plot(currently_in_a_long_postion ? long_profit_target : na, style=plot.style_stepline, title="Take Profit", color=currently_in_a_long_postion ? green : transparent, linewidth=1)//lsl = plot(currently_in_a_long_postion ? long_stop_loss : na, style=plot.style_stepline, title="Take Profit", color=currently_in_a_long_postion ? red : transparent, linewidth=1)//fill(entry,ltp, color= currently_in_a_long_postion ? light_green : light_red)//fill(entry,lsl, color= currently_in_a_long_postion ? light_red : light_green)//stp = plot(currently_in_a_short_postion ? short_profit_target : na, style=plot.style_stepline, title="Take Profit", color=currently_in_a_short_postion ? green : transparent, linewidth=1)//ssl = plot(currently_in_a_short_postion ? short_stop_loss : na, style=plot.style_stepline, title="Take Profit", color=currently_in_a_short_postion ? red : transparent, linewidth=1)//fill(entry,stp, color= currently_in_a_short_postion ? light_green : light_red)//fill(entry,ssl, color= currently_in_a_short_postion ? light_red : light_green)