Zeal Terminal Open in Zeal Terminal

Pine Script v5 · Strategy

Elliot Wave - Impulse Strategy

A Pine Script v5 strategy built on Pivothigh (Phase, 1), Pivotlow (Phase, 1) and RSI (close, 1). It has 8 order rules, 1 entry among them, and trades long and short with the default settings. Exits use a stop loss. Each trade commits 75% of equity and pays 0.04% commission. 23 settings; defaults include Start Day 0, Start Month 0 and Start Year 0.

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 Elliot Wave - Impulse Strategy repaint?
Yes
At least one pattern in the source can repaint: intrabar recalculation.
How did Elliot Wave - Impulse Strategy do on BTCUSDT 1D with default settings?
+0.49%
From 26 Jan 2024 to 21 Oct 2026, over 7 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Does Elliot Wave - Impulse Strategy run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L78:C7: cannot use 'float' as a condition — Pine Script v6 does not cast 'int' or 'float' to 'bool' implicitly. Wrap it in bool(). The v6 version is shown for reference and is not offered as a download.
Does Elliot Wave - Impulse Strategy use a stop loss?
Yes
strategy.exit sets a stop on line 398.
Does Elliot Wave - Impulse Strategy run in Zeal Terminal?
Yes
It compiles in 137 ms and runs 1,000 bars in 245 ms, in the browser.

BTCUSDT · 1D · default settings

  • Pivothigh
  • Pivotlow
  • Highestbars
  • Lowestbars
  • Change
  • 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 (23)
Sell invalidated at SLBuy invalidated at SL6739200 / 120Buy invalidated at SL4492800 / 120Buy invalidated at SLBuy invalidated at SLSell invalidated at SLSell invalidated at SLSell invalidated at SL19440000 / 1203110400 / 1201987200 / 120Sell invalidated at SL3283200 / 120Sell invalidated at SLSell invalidated at SLBuy invalidated at SLBuy invalidated at SLSell invalidated at SLBuy invalidated at SLBuy invalidated at SL46137600 / 120Entry : 83956.156Stop : 87220T.Stop : 84747.844Target - 1 : 77947.24408Target - 2 : 76435.12Target - 3 : 73988.80408Target - 4 : 71542.488160.0050,000100,000AprJulOct2025AprJulOct2026AprJulOctRSI$100$203Equity from $100
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. The script plots nothing itself; the lines and the lower pane are its own calculations, drawn for reference. Lines, boxes and labels are the script's own drawings, the most recent ones its limits keep. 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.
Table drawn by the script, as it stood on the last bar
BullishBearish
3932
Net profit
+0.49%
$0.49
Buy and hold
+102.5%
same period
Closed trades
7
2 won · 5 lost
Profitable
28.6%
of closed trades
Profit factor
1.06
gross profit ÷ gross loss
Max drawdown
7.05%
−$7.05

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

    1 setting does nothing

    “MESA offset” is declared but never read.

  • Note

    6 settings have no label

    In the settings dialog zigzagLength (line 15), errorPercent (line 16), entryPercent (line 17), zigzagWidth (line 18), zigzagStyle (line 19), target2reach (line 20) 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.

SettingDefaultType and rangeFeeds
General
Show 'invalidated at SL' Labelstrueboolline 392
Time-Limit BackTesting (0 disable)
Use Time-Limited Backtestingfalseboolline 41
Start Day0int · min 0 · max 31inDateRange line 41
Start Month0int · min 0 · max 12inDateRange line 41
Start Year0int · min 0 · max 2100inDateRange line 41
End Day0int · min 0 · max 31inDateRange line 41
End Month0int · min 0 · max 12inDateRange line 41
End Year0int · min 0 · max 2100inDateRange line 41
Elliot Waves Options
Zigzag Length (no label in the script)2int · min 2 · step 1line 354
Error Percent (no label in the script)10int · min 2 · max 20 · step 1err_min err_max
Entry Percent (no label in the script)10int · min 10 · max 100 · step 1entry_range
Zigzag Width (no label in the script)2int · min 1 · step 1w1 w2 waveFormed
Zigzag Style (no label in the script)line.style_solidstring · options [line.style_dashed, line.style_dotted, line.style_solid]w1 w2 waveFormed
Target2reach (no label in the script)TP4 (3.236 fibo)string · options ["TP1 (1.618 fibo)", "TP2 (2.0 fibo)", "TP3 (2.618 fibo)", "TP4 (3.236 fibo)"]target_real
cRSI Options
Trade by cRSI directiontrueboolline 134 line 136 line 243
Show cRSI Pivotsfalseboolline 124
cRSI EMA period1intrsi ph pl
cRSI Sourceclosesourcersi
MESA Pivots Options
Open trades by MESA Pivotstrueboolentry trade_type exit trade_type line 385 line 395
Show MESA PivotsfalseboolPivot High Pivot Low
MESA offset0intunused
MESA pivot zone upper0.4floatout_zone
MESA pivot zone lower-0.4floatout_zone

What it calculates

  • PivothighPhase, 1, 1ph_mesa · line 60
  • PivotlowPhase, 1, 1pl_mesa · line 61
  • RSIcRSI Source (close), cRSI EMA period (1)rsi · line 72
  • Highestbarshigh, cRSI EMA period (1)ph · line 75
  • Lowestbarslow, cRSI EMA period (1)pl · line 76
  • Changedirdirchanged · line 166

Signal flow

How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.

The script has 78 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.

Order rules

OrderFires whenBy defaultLine
Close Buyclose >= strategy.opentrades.entry_price( strategy.opentrades - 1 )On307
Close Buyclose >= strategy.opentrades.entry_price( strategy.opentrades - 1 )On309
Close Sellclose <= strategy.opentrades.entry_price( strategy.opentrades - 1 )On313
Close Sellclose <= strategy.opentrades.entry_price( strategy.opentrades - 1 )On315
Close trade_typeexit_price > 0 and strategy.opentrades > 0 and exit_boolOn365
Open trade_type trade_type == "Buy" ? strategy.long : strategy.short( inDateRange and ( time - last_trade_ts ) / 1000 >= ( 60 * min_trade_gap_minutes ) and ( not i_use_mesa or ( trade_type == "Buy" and ( ( ph_mesa and out_zone ) ? ph_mesa : na ) ) or ( trade_type == "Sell" and ( ( pl_mesa and out_zone ) ? pl_mesa : na ) ) ) )On397
Exit trade_type stop( inDateRange and ( time - last_trade_ts ) / 1000 >= ( 60 * min_trade_gap_minutes ) and ( not i_use_mesa or ( trade_type == "Buy" and ( ( ph_mesa and out_zone ) ? ph_mesa : na ) ) or ( trade_type == "Sell" and ( ( pl_mesa and out_zone ) ? pl_mesa : na ) ) ) )On398
Close (not inDateRange)On418

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.

0/1trades 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 0 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 dataThe script requests no other symbol or timeframe.
  • ClearFuture-bar referencesNo negative history offsets such as close[-1].
  • CheckIntrabar recalculationcalc_on_every_tick = true. Live values can differ from history.
  • ClearOrder timingprocess_orders_on_close = true: orders fill at the signal bar's close.

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+0.5%+102.5%729%1.067.1%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+3.0%+5.5%850%3.431.2%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+47.0%+9.8%922%7.164.4%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.0%+8.4%425%1.031.8%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−3.8%+16.7%520%0.064.1%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−5.4%+21.5%1010%0.025.4%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−10.7%+148.3%1436%0.3815.4%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−0.5%+17.1%633%0.461.8%8 May 2026 – 21 Oct 2026

Backtests are simulations on historical data and are not investment advice.

The trades

By market direction

At entry, last 90 barsTradesWonP&L
Rising market333%−$4.74
Sideways market333%$7.55
Falling market10%−$2.33

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

How bad could the drawdown be?

8.4%

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

0%10%
All 7 trades
SideEntryExitBarsP&L
long13 Apr 2024 63,924.5013 Apr 2024 63,924.500−$0.06
short4 Jun 2024 70,537.805 Jun 2024 70,687.601−$0.22
short15 Jan 2025 100,497.0020 Jan 2025 108,353.005−$5.91
short20 Feb 2025 98,305.004 Mar 2025 87,282.0012$7.83
short15 Mar 2025 84,338.4019 Mar 2025 86,845.904−$2.33
short22 Apr 2025 93,443.0022 Apr 2025 93,443.000−$0.06
long8 Oct 2026 81,754.4013 Oct 2026 83,169.005$1.23

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
Elliot Wave - Impulse Strategy this page—v5 strategy+0.5%77.1%
RSI Centered Pivots by OztheWozrsi pivotlow pivothighv4 strategy+98.0%4160.2%
RSI5_50 with Divergence by mohaneersi pivotlow pivothighv4 strategy+443.3%25411.3%
Aroon Oscillator Strategy by mohaneersi highestbars lowestbarsv4 strategyNo trades on this market
RSI Divergence X Ichimoku Cloud X 200EMA by tradethrillsrsi pivotlow pivothighv4 strategy−196,793.0%71,854.6%
EMA_HMA_RSI_Strategy by mohaneersi highestbars lowestbarsv4 strategyNo trades on this market

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.

  • 4 settings use the untyped input() (line 24, 28, 29, 30). It still compiles in v6; input.int, input.float or input.source state the type.
  • No v6-only features. v6 is not needed to run this script.

Clean v5

VerifiedDownload the v5 script

Same 7 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 24: input() → input.int()
  • Line 28: input() → input.int()
  • Line 29: input() → input.float()
  • Line 30: input() → input.float()
⋯i_show_crsi = input.bool( false, "Show cRSI Pivots", group = "cRSI Options" )-i_i_len   = input( 1, "cRSI EMA period", group = "cRSI Options" )+i_i_len   = input.int( 1, "cRSI EMA period", group = "cRSI Options" )i_src     = input.source( close, 'cRSI Source',  group = "cRSI Options" )⋯i_show_mesa = input.bool( false, "Show MESA Pivots", group = "MESA Pivots Options" )-offsetNum = input( 0, "MESA offset", group = "MESA Pivots Options")+offsetNum = input.int( 0, "MESA offset", group = "MESA Pivots Options")-pivot_zone_upper = input( 0.4, "MESA pivot zone upper", group = "MESA Pivots Options" )+pivot_zone_upper = input.float( 0.4, "MESA pivot zone upper", group = "MESA Pivots Options" )-pivot_zone_lower = input( -0.4, "MESA pivot zone lower", group = "MESA Pivots Options" )+pivot_zone_lower = input.float( -0.4, "MESA pivot zone lower", group = "MESA Pivots Options" )var cRSI_high_extreme_found = 0.0⋯

Does it run in Pine Script v6?

No

The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L78:C7: cannot use 'float' as a condition — Pine Script v6 does not cast 'int' or 'float' to 'bool' implicitly. Wrap it in bool(). The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.

⋯// © HeWhoMustNotBeNamed-//@version=5+//@version=6strategy("Elliot Wave - Impulse Strategy", shorttitle="EW - Impulse Strategy", overlay=true, process_orders_on_close = true, calc_on_order_fills = true, calc_on_every_tick = true, pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 75, commission_type = strategy.commission.percent, commission_value = 0.04, initial_capital = 100, max_lines_count=500, max_labels_count=500)⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 137 msWith a workaround 1,000 bars in 245 msNot measured
PineTSNot testedNot testedNot 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/// © HeWhoMustNotBeNamed//@version=5strategy("Elliot Wave - Impulse Strategy", shorttitle="EW - Impulse Strategy", overlay=true, process_orders_on_close = true, calc_on_order_fills = true, calc_on_every_tick = true, pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 75, commission_type = strategy.commission.percent, commission_value = 0.04, initial_capital = 100, max_lines_count=500, max_labels_count=500)show_sl_invalidation_labels = input.bool( true, "Show 'invalidated at SL' Labels", group = "General" )i_use_time_limited_backtesting = input.bool( false, "Use Time-Limited Backtesting", group = "Time-Limit BackTesting (0 = disable)" )startDay = input.int(0, "Start Day", minval = 0, maxval = 31, group = "Time-Limit BackTesting (0 = disable)")startMonth = input.int(0, "Start Month", minval = 0, maxval = 12, group = "Time-Limit BackTesting (0 = disable)")startYear = input.int(0, "Start Year", minval = 0, maxval = 2100, group = "Time-Limit BackTesting (0 = disable)")endDay = input.int(0, "End Day", minval = 0, maxval = 31, group = "Time-Limit BackTesting (0 = disable)")endMonth = input.int(0, "End Month", minval = 0, maxval = 12, group = "Time-Limit BackTesting (0 = disable)")endYear = input.int(0, "End Year", minval = 0, maxval = 2100, group = "Time-Limit BackTesting (0 = disable)")//source = input(close, group = "Elliot Waves Options")source = closezigzagLength = input.int(2, minval=2, step=1, group = "Elliot Waves Options")errorPercent = input.int(10, minval=2, step=1, maxval=20, group = "Elliot Waves Options")entryPercent = input.int(10, minval=10, step=1, maxval=100, group = "Elliot Waves Options")zigzagWidth = input.int(2, step=1, minval=1, group = "Elliot Waves Options")zigzagStyle = input.string(line.style_solid, options=[line.style_dashed, line.style_dotted, line.style_solid], group = "Elliot Waves Options")target2reach = input.string("TP4 (3.236 fibo)", options=["TP1 (1.618 fibo)", "TP2 (2.0 fibo)", "TP3 (2.618 fibo)", "TP4 (3.236 fibo)"], group = "Elliot Waves Options")waitForConfirmation = truei_use_crsi = input.bool( true, "Trade by cRSI direction", group = "cRSI Options" )i_show_crsi = input.bool( false, "Show cRSI Pivots", group = "cRSI Options" )i_i_len   = input( 1, "cRSI EMA period", group = "cRSI Options" )i_src     = input.source( close, 'cRSI Source',  group = "cRSI Options" )i_use_mesa = input.bool( true, "Open trades by MESA Pivots", group = "MESA Pivots Options" )i_show_mesa = input.bool( false, "Show MESA Pivots", group = "MESA Pivots Options" )offsetNum = input( 0, "MESA offset", group = "MESA Pivots Options")pivot_zone_upper = input( 0.4, "MESA pivot zone upper", group = "MESA Pivots Options" )pivot_zone_lower = input( -0.4, "MESA pivot zone lower", group = "MESA Pivots Options" )var cRSI_high_extreme_found = 0.0var cRSI_high_extreme_valid = truevar cRSI_low_extreme_found = 0.0var cRSI_low_extreme_valid = truevar last_cRSI_extreme = ""var cRSI_trend_direction = ""var last_trade_ts = timevar min_trade_gap_minutes = 2 inDateRange = true// Look if the close time of the current bar falls inside the date rangeif i_use_time_limited_backtesting and startDay != 0 and startMonth != 0 and startYear != 0 and endDay != 0 and endMonth != 0 and endYear != 0    inDateRange := ( time >= timestamp( syminfo.timezone, startYear, startMonth, startDay, 0, 0 ) ) and ( time < timestamp( syminfo.timezone, endYear, endMonth, endDay, 0, 0 ) )//------------------------------------------------------------------------------// MESA calculations from the MESA indicator//------------------------------------------------------------------------------// Basic//------------------------------------------------------------------------------DomCycle = 15RealPart = 0.0ImagPart = 0.0for J = 0 to DomCycle - 1    Weight = (close[J] + close[J] + high[J] + low[J]) * 10000    if DomCycle != 0        RealPart := RealPart + math.cos(90 * J / DomCycle) * Weight * 2        ImagPart := ((ImagPart + math.sin(90 * J / DomCycle) * Weight) + (ImagPart + math.sin(180 * J / DomCycle) * Weight)) / 2Phase = ((math.atan(ImagPart / RealPart)) - 0.685) * 100//------------------------------------------------------------------------------//Pivot//------------------------------------------------------------------------------ph_mesa = ta.pivothigh(Phase, 1, 1)pl_mesa = ta.pivotlow(Phase, 1, 1)out_zone = Phase[2] < pivot_zone_lower or Phase[2] > pivot_zone_upperplotshape(i_show_mesa and ph_mesa and out_zone ? ph_mesa : na, title = "Pivot High", color=#f23737, style = shape.circle, location = location.abovebar, offset = -1, size = size.tiny)plotshape(i_show_mesa and pl_mesa and out_zone ? pl_mesa : na, title = "Pivot Low", color=#169788, style = shape.circle, location = location.belowbar, offset = -1, size = size.tiny)//------------------------------------------------------------------------------// cRSI indicator code//------------------------------------------------------------------------------crsi       = 0.0vibration  = 10torque     = 0.618 / (vibration + 1)phasingLag = (vibration - 1) / 0.618rsi        = ta.rsi(i_src, i_i_len)crsi      := torque * (2 * rsi - rsi[phasingLag]) + (1 - torque) * nz(crsi[1])float osc = crsifloat ph = ta.highestbars(high, i_i_len) == 0 ? osc : nafloat pl = ta.lowestbars(low,   i_i_len) == 0 ? osc : navar int dir = 0dir := ph and na(pl) ? 1 : pl and na(ph) ? -1 : dirvar max_array_size = 10var arr_zz         = array.new_float(0)older_zz           = array.copy(arr_zz)dirchanged         = ta.change(dir)add_to_zigzag(_id, float value, int bindex) =>    array.unshift(_id, bindex)    array.unshift(_id, value)    if array.size(_id) > max_array_size        array.pop(_id)        array.pop(_id)update_zigzag(_id, float value, int bindex, int dir) =>    if array.size(_id) == 0        add_to_zigzag(_id, value, bindex)    else        if dir == 1 and value > array.get(_id, 0) or dir == -1 and value < array.get(_id, 0)            array.set(_id, 0, value)            array.set(_id, 1, bindex)        0.if ph or pl    if dirchanged        add_to_zigzag(arr_zz, dir == 1 ? ph : pl, bar_index)    else        update_zigzag(arr_zz, dir == 1 ? ph : pl, bar_index, dir)if array.size(arr_zz) >= 6    // Variables    var label label_zz = na    // Bools for or    bool bool_or_1 = array.get(arr_zz, 0) != array.get(older_zz, 0)    bool bool_or_2 = array.get(arr_zz, 1) != array.get(older_zz, 1)        // Bools for and    bool bool_n_1 = array.get(arr_zz, 2) == array.get(older_zz, 2)    bool bool_n_2 = array.get(arr_zz, 3) == array.get(older_zz, 3)        // Bools for more than and less than    bool bool_0_mt_4 = array.get(arr_zz, 0) > array.get(arr_zz, 4)    bool bool_0_lt_4 = array.get(arr_zz, 0) < array.get(arr_zz, 4)        if bool_or_1 or bool_or_2        if bool_n_1 and bool_n_2            label.delete(label_zz)        str_label = dir == 1 ?   bool_0_mt_4 ? '▼'     : '◍' :                                 bool_0_lt_4 ? '▲'     : '◍'        col_label = dir == 1 ?   bool_0_mt_4 ? color.red  : color.teal :                                 bool_0_lt_4 ? color.teal : color.red        if i_show_crsi            label_zz := label.new(bar_index, high, text = str_label, color = color.new(color.blue, 100), textcolor = col_label, style=dir == 1 ? label.style_label_down : label.style_label_up )        if dir == 1            cRSI_high_extreme_found := close            cRSI_low_extreme_found := 0.0            last_cRSI_extreme := "high"        else            cRSI_low_extreme_found := close            cRSI_high_extreme_found := 0.0            last_cRSI_extreme := "low"if i_use_crsi and last_cRSI_extreme == "high" and cRSI_high_extreme_found > 0.0    cRSI_trend_direction := "sell"if i_use_crsi and last_cRSI_extreme == "low" and cRSI_low_extreme_found > 0.0    cRSI_trend_direction := "buy"var order_prices = array.new_float(3, 0.0) // entry pricearray.push(order_prices, 0.0) // exit pricearray.push(order_prices, 0.0) // stop pricevar order_types = array.new_string(1, "") // order typevar our_active_orders_count = array.new_int(1, 0)max_pivot_size = 10zigzagColor = color.blackshowZigZag = truevar zigzagpivots = array.new_float(0)var zigzagpivotbars = array.new_int(0)var zigzagpivotdirs = array.new_int(0)var waveLinesArray = array.new_line(2)var targetLabels = array.new_label(0)var targetLines = array.new_line(0)var wavelines = array.new_line(0)transparent = color.new(#FFFFFF, 100)err_min = (100-errorPercent)/100err_max = (100+errorPercent)/100entry_range = (entryPercent)/100var patterncount = array.new_int(2,0)pivots(length)=>    float phigh = ta.highestbars(high, length) == 0 ? high : na    float plow = ta.lowestbars(low, length) == 0 ? low : na    dir = 0    dir := phigh and na(plow) ? 1 : plow and na(phigh) ? -1 : dir[1]    [dir, phigh, plow]zigzag(length, zigzagpivots, zigzagpivotbars, zigzagpivotdirs)=>    [dir, phigh, plow] = pivots(length)    dirchanged = ta.change(dir)        if(phigh or plow)        value = (dir == 1? phigh : plow)        bar = bar_index        if(not dirchanged and array.size(zigzagpivots) >=1)            pivot = array.shift(zigzagpivots)            pivotbar = array.shift(zigzagpivotbars)            pivotdir = array.shift(zigzagpivotdirs)            value:= value*pivotdir < pivot*pivotdir? pivot : value            bar:= value*pivotdir < pivot*pivotdir? pivotbar : bar                if(array.size(zigzagpivots) >=2)            LastPoint = array.get(zigzagpivots,1)            dir := dir*value > dir*LastPoint? dir*2 : dir                array.unshift(zigzagpivots, value = value)        array.unshift(zigzagpivotbars, bar)        array.unshift(zigzagpivotdirs, dir)                if(array.size(zigzagpivots) > max_pivot_size)            array.pop(zigzagpivots)            array.pop(zigzagpivotbars)            array.pop(zigzagpivotdirs)draw_zigzag(zigzagpivots, zigzagpivotbars, zigzagcolor, zigzagwidth, zigzagstyle, showZigZag)=>    if(array.size(zigzagpivots) > 2 and showZigZag)        for i=0 to array.size(zigzagpivots)-2            y1 = array.get(zigzagpivots, i)            y2 = array.get(zigzagpivots, i+1)            x1 = array.get(zigzagpivotbars, i)            x2 = array.get(zigzagpivotbars, i+1)            zline = line.new(x1=x1, y1=y1,                          x2 = x2, y2=y2,                         color=zigzagcolor, width=zigzagwidth, style=zigzagstyle)//////////////////////////////////// Draw Lines with labels //////////////////////////////////////////////////f_drawLinesWithLabels(target, lbl, linecolor)=>    timeDiffEnd = time - time[10]    timeDiffLabel = time - time[5]        line_x1 = time    line_x2 = time+timeDiffEnd    label_x = time+timeDiffLabel    targetLine = line.new(x1=line_x1, y1=target, x2=line_x2, y2=target, color=linecolor, xloc=xloc.bar_time)    targetLabel = label.new(x=label_x, y=target, text=lbl + " : "+str.tostring(target), xloc=xloc.bar_time, style=label.style_none, textcolor=color.black, size=size.normal)    [targetLine,targetLabel]//////////////////////////////////// Delete Lines with labels //////////////////////////////////////////////////f_deleteLinesWithLabels(targetLine, targetLabel)=>    line.delete(targetLine)    label.delete(targetLabel)    ew_impulse(zigzagpivots, zigzagpivotbars, zigzagpivotdirs, zigzagWidth, zigzagStyle, showZigZag, waveLinesArray, targetLines, targetLabels)=>    start = waitForConfirmation? 1: 0    waveFound = false    if(array.size(zigzagpivots) >= 3+start and showZigZag)        Point2 = array.get(zigzagpivots, start)        Point2Bar = array.get(zigzagpivotbars, start)        Point2Dir = array.get(zigzagpivotdirs, start)                Point1 = array.get(zigzagpivots, start+1)        Point1Bar = array.get(zigzagpivotbars, start+1)        Point1Dir = array.get(zigzagpivotdirs, start+1)                Point0 = array.get(zigzagpivots, start+2)        Point0Bar = array.get(zigzagpivotbars, start+2)        Point0Dir = array.get(zigzagpivotdirs, start+2)                W1Length = math.abs(Point1-Point0)        W2Length = math.abs(Point2-Point1)        r2 = W2Length/W1Length                    existingW1 = array.get(waveLinesArray, 0)        existingW2 = array.get(waveLinesArray, 1)                    existing0 = line.get_y1(existingW1)        existing1 = line.get_y1(existingW2)        existing2 = line.get_y2(existingW2)        dir_crsi = 0        if i_use_crsi            if cRSI_trend_direction != ""                if cRSI_trend_direction == "buy"                    dir_crsi := 1                else if cRSI_trend_direction == "sell"                    dir_crsi := -1                else                     dir_crsi := Point0 > Point1 ? -1 : 1            else                dir_crsi := Point0 > Point1 ? -1 : 1        else            dir_crsi := Point0 > Point1? -1 : 1        dir = Point0 > Point1? -1 : 1        //label.new(bar_index, high, cRSI_trend_direction + ", " + str.tostring( dir_crsi ) + " / " + str.tostring( dir ) )        if dir_crsi == dir            entry = Point2 + dir*entry_range*W2Length            stop = Point0            tstop = Point2 - dir*entry_range*W2Length                        target1 = Point2 + dir*1.618*W2Length            target2 = Point2 + dir*2.0*W2Length            target3 = Point2 + dir*2.618*W2Length            target4 = Point2 + dir*3.236*W2Length                    stopColor = dir == 1? color.red : color.green            targetColor = dir == 1? color.green : color.red                        ignore = false            patternMatched = false            dirMatched = false            if(existing0 == Point0 or existing1 == Point1 or existing2 == Point2)                ignore := true                if(r2 > 0.50*err_min and r2 < 0.50*err_max) or (r2 > 0.618*err_min and r2 < 0.618*err_max) or                     (r2 > 0.764*err_min and r2 < 0.764*err_max) or (r2 > 0.854*err_min and r2 < 0.854*err_max)                patternMatched := true                if(Point1Dir == 2 and Point2Dir == -1) or (Point1Dir == -2 and Point2Dir == 1)                dirMatched := true            directionColor = Point1 > Point2 ? color.green : color.red            if(not ignore and patternMatched and dirMatched)                w1 = line.new(y1=Point0, y2=Point1, x1=Point0Bar, x2=Point1Bar, color=directionColor, width=zigzagWidth, style=zigzagStyle)                w2 = line.new(y1=Point1, y2=Point2, x1=Point1Bar, x2=Point2Bar, color=directionColor, width=zigzagWidth, style=zigzagStyle)                array.set(waveLinesArray,0,w1)                array.set(waveLinesArray,1,w2)                                if(array.size(targetLines) > 0)                    for i=0 to array.size(targetLines)-1                        line.delete(array.shift(targetLines))                    for i=0 to array.size(targetLabels)-1                        label.delete(array.shift(targetLabels))                                [entryLine,entryLabel] = f_drawLinesWithLabels(entry, "Entry", color.blue)                [stopLine,stopLabel] = f_drawLinesWithLabels(stop, "Stop", stopColor)                [tstopLine,tstopLabel] = f_drawLinesWithLabels(tstop, "T.Stop", stopColor)                [t1Line,t1Label] = f_drawLinesWithLabels(target1, "Target - 1", targetColor)                [t2Line,t2Label] = f_drawLinesWithLabels(target2, "Target - 2", targetColor)                [t3Line,t3Label] = f_drawLinesWithLabels(target3, "Target - 3", targetColor)                [t4Line,t4Label] = f_drawLinesWithLabels(target4, "Target - 4", targetColor)                    // store info for a new trade and also cancel any old ones, since we found a new wave here                if strategy.opentrades > 0                    if strategy.opentrades.entry_id( strategy.opentrades - 1 ) == "Buy"                        if close >= strategy.opentrades.entry_price( strategy.opentrades - 1 )                            strategy.close( "Buy", comment = "close buy (new wave), profit" )                        else                            strategy.close( "Buy", comment = "close buy (new wave), loss" )                        if strategy.opentrades.entry_id( strategy.opentrades - 1 ) == "Sell"                        if close <= strategy.opentrades.entry_price( strategy.opentrades - 1 )                            strategy.close( "Sell", comment = "close sell (new wave), profit" )                        else                            strategy.close( "Sell", comment = "close sell (new wave), loss" )                    tradeType = ( Point1 > Point2 ? "Buy" : "Sell" )                array.set(order_prices, 0, entry)                                target_real = switch target2reach                    "TP1 (1.618 fibo)" => target1                    "TP2 (2.0 fibo)" => target2                    "TP3 (2.618 fibo)" => target3                    "TP4 (3.236 fibo)" => target4                array.set(order_prices, 1, target_real)                array.set(order_prices, 2, stop)                array.set(order_types, 0, tradeType)                array.set( our_active_orders_count, 0, 0 )                    array.unshift(targetLines, entryLine)                array.unshift(targetLines, stopLine)                array.unshift(targetLines, tstopLine)                array.unshift(targetLines, t1Line)                array.unshift(targetLines, t2Line)                array.unshift(targetLines, t3Line)                array.unshift(targetLines, t4Line)                                array.unshift(targetLabels, entryLabel)                array.unshift(targetLabels, stopLabel)                array.unshift(targetLabels, tstopLabel)                array.unshift(targetLabels, t1Label)                array.unshift(targetLabels, t2Label)                array.unshift(targetLabels, t3Label)                array.unshift(targetLabels, t4Label)                                count_index = dir==1?0:1                array.set(patterncount, count_index, array.get(patterncount, count_index)+1)                waveFound := true                if(existing0 == Point0 and existing1 == Point1 and existing2 == Point2) and waveFound[1]                    line.delete(existingW1)                    line.delete(existingW2)                    array.set(patterncount, count_index, array.get(patterncount, count_index)-1)    waveFoundzigzag(zigzagLength, zigzagpivots, zigzagpivotbars, zigzagpivotdirs)waveFormed = ew_impulse(zigzagpivots, zigzagpivotbars, zigzagpivotdirs, zigzagWidth, zigzagStyle, showZigZag, waveLinesArray, targetLines, targetLabels)var stats = table.new(position = position.top_right, columns = 3, rows = max_pivot_size+2, border_width = 1)trade_type = array.get(order_types, 0)entry_price = array.get(order_prices, 0)exit_price = array.get(order_prices, 1)stop_price = array.get(order_prices, 2)// close trades when price reaches our targetexit_bool = array.get( our_active_orders_count, 0 ) > 0 and (trade_type == "Buy" ? close >= exit_price : close <= exit_price )//label.new(bar_index, high, trade_type + ", " + str.tostring( close ) + " / " + str.tostring( exit_price ) )if exit_price > 0 and strategy.opentrades > 0 and exit_bool    strategy.close( trade_type, comment = trade_type + " TP reached" )    array.set(order_prices, 0, 0.0)    array.set(order_prices, 1, 0.0)    array.set(order_prices, 2, 0.0)    array.set(order_types, 0, "")    array.set( our_active_orders_count, 0, 0 )// remove trades from internal cache when they reach SLsl_bool = array.get( our_active_orders_count, 0 ) > 0 and (trade_type == "Buy" ? close <= stop_price : close >= stop_price )//label.new(bar_index, high, trade_type + ", " + str.tostring( close ) + " / " + str.tostring( exit_price ) )if stop_price > 0 and strategy.opentrades > 0 and sl_bool    array.set(order_prices, 0, 0.0)    array.set(order_prices, 1, 0.0)    array.set(order_prices, 2, 0.0)    array.set(order_types, 0, "")    array.set( our_active_orders_count, 0, 0 )// open trades when the price reached our entryentry_bool = array.get( our_active_orders_count, 0 ) == 0 and (trade_type == "Buy" ? close <= entry_price : close >= entry_price )if entry_price > 0 and ( strategy.opentrades == 0 or strategy.opentrades.entry_id( strategy.opentrades - 1 ) != trade_type ) and entry_bool    // check if we're also using MESA to wait for a better entry    // first check that we've not beyond our SL...    if i_use_mesa and (trade_type == "Buy" ? close <= stop_price : close >= stop_price )        // SL reached, invalidate this trade and mark it on chart        array.set(order_prices, 0, 0.0)        array.set(order_prices, 1, 0.0)        array.set(order_prices, 2, 0.0)        array.set(order_types, 0, "")        array.set( our_active_orders_count, 0, 0 )        if inDateRange and show_sl_invalidation_labels            label.new(bar_index, high, trade_type + " invalidated at SL", color = color.white, textcolor = color.black )    // now check that we have a better entry through MESa    if ( inDateRange and ( time - last_trade_ts ) / 1000 >= ( 60 * min_trade_gap_minutes ) and ( not i_use_mesa or ( trade_type == "Buy" and ( ( ph_mesa and out_zone ) ? ph_mesa : na ) ) or ( trade_type == "Sell" and ( ( pl_mesa and out_zone ) ? pl_mesa : na ) ) ) )        label.new(bar_index, high, str.tostring(( time - last_trade_ts ) / 1000) + " / " + str.tostring( 60 * min_trade_gap_minutes ) )        strategy.entry( trade_type, trade_type == "Buy" ? strategy.long : strategy.short )        strategy.exit( trade_type, stop = stop_price, comment = "closed by SL" )        array.set( our_active_orders_count, 0, 1 )        last_trade_ts := time        if trade_type == "Buy"            cRSI_low_extreme_found := 0.0        else            cRSI_high_extreme_found := 0.0if(barstate.islast)    table.cell(table_id = stats, column = 1, row = 0 , text = "Bullish", bgcolor=color.black, text_color=color.white)    table.cell(table_id = stats, column = 2, row = 0 , text = "Bearish", bgcolor=color.black, text_color=color.white)    dtColor = color.white    for type = 1 to (array.size(patterncount)/2)        for direction = 0 to 1            count_index = (type*2 - 2) + direction            row = type            column = direction+1            txt = str.tostring(array.get(patterncount, count_index))            table.cell(table_id = stats, column = column, row = row, text = txt, bgcolor=dtColor)// Exit open market position when date range endsif (not inDateRange)    strategy.close_all()