Pine Script v4 · Strategy
Pivot Moving Average steteggy
A Pine Script v4 strategy built on Pivothigh (5, 5), Pivotlow (5, 5) and ATR or Highest or Lowest (1). It has 3 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 15 settings; defaults include Left Bars 5, Right Bars 5 and Trad Amount 100.
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 Pivot Moving Average steteggy repaint?
- Yes
- At least one pattern in the source can repaint: higher-timeframe data.
- How did Pivot Moving Average steteggy do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its entry conditions never all held on this data. It closed no trades on the other markets either. A backtest, not investment advice.
- Can Pivot Moving Average steteggy be converted to Pine Script v5?
- Not tested
- The steps are shown below, but neither version placed a trade on any market tested, so there was nothing to compare and no download is offered.
- Does Pivot Moving Average steteggy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Pivot Moving Average steteggy run in Zeal Terminal?
- Yes
- It compiles in 57 ms and runs 1,000 bars in 220 ms, in the browser.
BTCUSDT · 1D · default settings
- Higher Low
- Higher High
- Lower Low
- Lower High
- ema3
- ema5
- ema8
- ema13
- ema21
- ema34
- ema55
- Winning trade
- Losing trade
- Equity
- Buy and hold
Needs BINANCE:BTCUSDT 60 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Values that depend on it are empty on this chart.
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
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
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
5 settings have no label
In the settings dialog supcol (line 10), rescol (line 11), srlinewidth (line 13), bcolup (line 15), bcoldn (line 16) 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.
-
Note
Uses iff(), which v5 removed line 22
iff() on line 22, 22, 23, 23, 24, 25, 26, 27, 28, 30, 44, 73, 74, 76, 76, 77, 78, 93, 93. v5 writes it as the ternary c ? a : b.
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames pivothigh → ta.pivothigh, pivotlow → ta.pivotlow, valuewhen → ta.valuewhen, ema → ta.ema, log10 → math.log10, sum → math.sum, atr → ta.atr, highest → ta.highest, lowest → ta.lowest, rsi → ta.rsi, security → request.security, bb → ta.bb, stdev → ta.stdev, supertrend → ta.supertrend, cross → ta.cross, tostring → str.tostring.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 3: //@version=4 → //@version=5
- Line 20: pivothigh → ta.pivothigh
- Line 21: pivotlow → ta.pivotlow
- Line 22: iff(c, a, b) → (c ? a : b)
- Line 22: iff(c, a, b) → (c ? a : b)
- Line 23: iff(c, a, b) → (c ? a : b)
49 more edits
- Line 23: iff(c, a, b) → (c ? a : b)
- Line 24: iff(c, a, b) → (c ? a : b)
- Line 24: valuewhen → ta.valuewhen
- Line 24: valuewhen → ta.valuewhen
- Line 25: iff(c, a, b) → (c ? a : b)
- Line 25: valuewhen → ta.valuewhen
- Line 25: valuewhen → ta.valuewhen
- Line 26: iff(c, a, b) → (c ? a : b)
- Line 26: valuewhen → ta.valuewhen
- Line 26: valuewhen → ta.valuewhen
- Line 27: iff(c, a, b) → (c ? a : b)
- Line 27: valuewhen → ta.valuewhen
- Line 27: valuewhen → ta.valuewhen
- Line 28: iff(c, a, b) → (c ? a : b)
- Line 30: iff(c, a, b) → (c ? a : b)
- Line 44: iff(c, a, b) → (c ? a : b)
- Line 73: iff(c, a, b) → (c ? a : b)
- Line 74: iff(c, a, b) → (c ? a : b)
- Line 76: iff(c, a, b) → (c ? a : b)
- Line 76: iff(c, a, b) → (c ? a : b)
- Line 77: iff(c, a, b) → (c ? a : b)
- Line 78: iff(c, a, b) → (c ? a : b)
- Line 93: iff(c, a, b) → (c ? a : b)
- Line 93: iff(c, a, b) → (c ? a : b)
- Line 98: ema → ta.ema
- Line 100: ema → ta.ema
- Line 101: ema → ta.ema
- Line 102: ema → ta.ema
- Line 103: ema → ta.ema
- Line 104: ema → ta.ema
- Line 105: ema → ta.ema
- Line 106: ema → ta.ema
- Line 107: ema → ta.ema
- Line 110: ema → ta.ema
- Line 111: log10 → math.log10
- Line 111: sum → math.sum
- Line 111: atr → ta.atr
- Line 111: highest → ta.highest
- Line 111: lowest → ta.lowest
- Line 111: log10 → math.log10
- Line 112: rsi → ta.rsi
- Line 113: security → request.security
- Line 114: bb → ta.bb
- Line 115: stdev → ta.stdev
- Line 116: supertrend → ta.supertrend
- Line 117: security → request.security
- Line 130: cross → ta.cross
- Line 161: tostring → str.tostring
- Line 167: tostring → str.tostring
⋯// © LonesomeThecolor.blue-//@version=4+//@version=5strategy("Pivot Moving Average Strategy", overlay =true, max_lines_count = 500, shorttitle="PMAS")⋯factor = input(3.0, "Factor", step = 0.01)-ph = pivothigh(lb, rb)+ph = ta.pivothigh(lb, rb)-pl = pivotlow(lb, rb)+pl = ta.pivotlow(lb, rb)-hl = iff(ph, 1, iff(pl, -1, na)) // Trend direction+hl = (ph ? 1 : (pl ? -1 : na)) // Trend direction-zz = iff(ph, ph, iff(pl, pl, na)) // similar to zigzag but may have multiple highs/lows+zz = (ph ? ph : (pl ? pl : na)) // similar to zigzag but may have multiple highs/lows-zz :=iff(pl and hl == -1 and valuewhen(hl, hl, 1) == -1 and pl > valuewhen(zz, zz, 1), na, zz)+zz :=(pl and hl == -1 and ta.valuewhen(hl, hl, 1) == -1 and pl > ta.valuewhen(zz, zz, 1) ? na : zz)-zz :=iff(ph and hl == 1 and valuewhen(hl, hl, 1) == 1 and ph < valuewhen(zz, zz, 1), na, zz)+zz :=(ph and hl == 1 and ta.valuewhen(hl, hl, 1) == 1 and ph < ta.valuewhen(zz, zz, 1) ? na : zz)-hl := iff(hl==-1 and valuewhen(hl, hl, 1)==1 and zz > valuewhen(zz, zz, 1), na, hl)+hl := (hl==-1 and ta.valuewhen(hl, hl, 1)==1 and zz > ta.valuewhen(zz, zz, 1) ? na : hl)-hl := iff(hl==1 and valuewhen(hl, hl, 1)==-1 and zz < valuewhen(zz, zz, 1), na, hl)+hl := (hl==1 and ta.valuewhen(hl, hl, 1)==-1 and zz < ta.valuewhen(zz, zz, 1) ? na : hl)-zz := iff(na(hl), na, zz)+zz := (na(hl) ? na : zz)findprevious()=> // finds previous three points (b, c, d, e)- ehl = iff(hl==1, -1, 1)+ ehl = (hl==1 ? -1 : 1) loc1 = 0.0, loc2 = 0.0, loc3 = 0.0, loc4 = 0.0⋯ break- ehl := iff(hl==1, -1, 1)+ ehl := (hl==1 ? -1 : 1) for x=xx to 1000⋯float res = na, float sup = na-res := iff(_lh, zz, res[1])+res := (_lh ? zz : res[1])-sup := iff(_hl, zz, sup[1])+sup := (_hl ? zz : sup[1])var int trend = na-trend := iff(close > res, 1, iff(close < sup, -1, nz(trend[1])))+trend := (close > res ? 1 : (close < sup ? -1 : nz(trend[1])))-res := iff((trend == 1 and _hh) or (trend == -1 and _lh), zz, res)+res := ((trend == 1 and _hh) or (trend == -1 and _lh) ? zz : res)-sup := iff((trend == 1 and _hl) or (trend == -1 and _ll), zz, sup)+sup := ((trend == 1 and _hl) or (trend == -1 and _ll) ? zz : sup)rechange = res != res[1]⋯ supline := line.new(x1 = bar_index - rb, y1 = sup, x2 = bar_index, y2 = sup, color = supcol, extend = extend.right, style = srlinestyle, width = srlinewidth) -barcolor(color = iff(changebarcol, iff(trend == 1, bcolup, bcoldn), na))+barcolor(color = (changebarcol ? (trend == 1 ? bcolup : bcoldn) : na))var float chopiness = na, float rsi = na⋯// ema200 = security(syminfo.tickerid, "D", ema(close, 200), barmerge.gaps_off, barmerge.lookahead_on)-ema200 = ema(close, 200)+ema200 = ta.ema(close, 200)movingAverageTrad := ema200 < close-ema3 := ema(close, 3)+ema3 := ta.ema(close, 3)-ema5 := ema(close, 5)+ema5 := ta.ema(close, 5)-ema8 := ema(close, 8)+ema8 := ta.ema(close, 8)-ema13 := ema(close, 13)+ema13 := ta.ema(close, 13)-ema21 := ema(close, 21)+ema21 := ta.ema(close, 21)-ema34 := ema(close, 34)+ema34 := ta.ema(close, 34)-ema55 := ema(close, 55)+ema55 := ta.ema(close, 55)-ema100 := ema(close, 100)+ema100 := ta.ema(close, 100)movingAverageUpTrad := ema3 > ema5 and ema5 > ema8 and ema8 > ema13 and ema13 > ema21 and ema21 > ema34 and ema34 > ema200movingAverageDownTrad := ema3 < ema5 and ema5 < ema8 and ema8 < ema13 and ema13 < ema21 and ema21 < ema34 and ema34 < ema200-highVolume := ema(volume, 14) < volume+highVolume := ta.ema(volume, 14) < volume-chopiness := 100 * log10(sum(atr(1), length) / (highest(length) - lowest(length))) / log10(length)+chopiness := 100 * math.log10(math.sum(ta.atr(1), length) / (ta.highest(length) - ta.lowest(length))) / math.log10(length)-rsiT = rsi(close, 14)+rsiT = ta.rsi(close, 14)-rsi := security(syminfo.tickerid, "60", rsiT, barmerge.gaps_off, barmerge.lookahead_on)+rsi := request.security(syminfo.tickerid, "60", rsiT, barmerge.gaps_off, barmerge.lookahead_on)-[middle, upper, lower] = bb(close,length=20, mult=2)+[middle, upper, lower] = ta.bb(close,length=20, mult=2)-stdev = stdev(close, 14)+stdev = ta.stdev(close, 14)-[supertrendT, directionT] = supertrend(factor, atrPeriod)+[supertrendT, directionT] = ta.supertrend(factor, atrPeriod)-[supertrend, direction] = security(syminfo.tickerid, "60", [supertrendT, directionT], barmerge.gaps_off, barmerge.lookahead_on)+[supertrend, direction] = request.security(syminfo.tickerid, "60", [supertrendT, directionT], barmerge.gaps_off, barmerge.lookahead_on)plot(ema3, "ema3", color=color.red)⋯var float entryPrice = na, bool isOpenPosition = false, string position = na, string closeComments = na-if cross(ema3, ema8)+if ta.cross(ema3, ema8) closeFull := true⋯ closeComments := "Trailing Stop"-message = '{"symbol":"{{ticker}}","action":"' + tostring(position) +'","quantity":"{{strategy.order.contracts}}"}'+message = '{"symbol":"{{ticker}}","action":"' + str.tostring(position) +'","quantity":"{{strategy.order.contracts}}"}'if position== "SELL" and isOpenPosition and not isOpenPosition[1]⋯if closeFull and isOpenPosition[1]- messageClose = '{"symbol":"{{ticker}}","close":' + tostring(closeFull) + '}'+ messageClose = '{"symbol":"{{ticker}}","close":' + str.tostring(closeFull) + '}' strategy.close(position, comment = closeComments, alert_message = messageClose)Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 5: input() → input.int()
- Line 6: input() → input.int()
- Line 7: input() → input.int()
- Line 8: input() → input.int()
- Line 9: input() → input.bool()
- Line 10: input() → input.color()
8 more edits
- Line 11: input() → input.color()
- Line 13: input() → input.int()
- Line 14: input() → input.bool()
- Line 15: input() → input.color()
- Line 16: input() → input.color()
- Line 17: input() → input.int()
- Line 18: input() → input.int()
- Line 19: input() → input.float()
⋯strategy("Pivot Moving Average Strategy", overlay =true, max_lines_count = 500, shorttitle="PMAS")-lb = input(5, title="Left Bars", minval = 1)+lb = input.int(5, title="Left Bars", minval = 1)-rb = input(5, title="Right Bars", minval = 1)+rb = input.int(5, title="Right Bars", minval = 1)-trad_amount = input(100, title="Trad Amount", minval = 1)+trad_amount = input.int(100, title="Trad Amount", minval = 1)-minimum_profite = input(-2, title="Minimum Profite")+minimum_profite = input.int(-2, title="Minimum Profite")-showsupres = input(true, title="Support/Resistance", inline = "srcol")+showsupres = input.bool(true, title="Support/Resistance", inline = "srcol")-supcol = input(color.lime, title ="", inline = "srcol")+supcol = input.color(color.lime, title ="", inline = "srcol")-rescol = input(color.red, title ="", inline = "srcol")+rescol = input.color(color.red, title ="", inline = "srcol")srlinestyle = input(line.style_dotted, title = "Line Style/Width", options = [line.style_solid, line.style_dashed, line.style_dotted], inline ="style")-srlinewidth = input(1, title = "", minval = 1, maxval = 5, inline ="style")+srlinewidth = input.int(1, title = "", minval = 1, maxval = 5, inline ="style")-changebarcol = input(true, title="Change Bar Color", inline = "bcol")+changebarcol = input.bool(true, title="Change Bar Color", inline = "bcol")-bcolup = input(color.blue, title ="", inline = "bcol")+bcolup = input.color(color.blue, title ="", inline = "bcol")-bcoldn = input(color.black, title ="", inline = "bcol")+bcoldn = input.color(color.black, title ="", inline = "bcol")-length = input(14, title="Chopiness Bars", minval=1)+length = input.int(14, title="Chopiness Bars", minval=1)-atrPeriod = input(10, "ATR Length")+atrPeriod = input.int(10, "ATR Length")-factor = input(3.0, "Factor", step = 0.01)+factor = input.float(3.0, "Factor", step = 0.01)ph = ta.pivothigh(lb, rb)⋯Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L95:C0: a 'bool' cannot be 'na' in Pine Script v6 — declare it as true or false. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © LonesomeThecolor.blue-//@version=5+//@version=6strategy("Pivot Moving Average Strategy", overlay =true, max_lines_count = 500, shorttitle="PMAS")⋯Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Left Bars | 5 | int · min 1 | ph pl |
| Right Bars | 5 | int · min 1 | ph pl resline supline Higher Low Higher High Lower Low Lower High line 149 |
| Trad Amount | 100 | int · min 1 | entry Short entry Long |
| Minimum Profite | -2 | int | line 132 line 134 line 157 |
| Support/Resistance | true | bool | line 83 |
| Supcol (no label in the script) | color.lime | value | supline line 149 |
| Rescol (no label in the script) | color.red | value | resline |
| Line Style/Width | line.style_dotted | value · options [line.style_solid, line.style_dashed, line.style_dotted] | resline supline |
| Srlinewidth (no label in the script) | 1 | int · min 1 · max 5 | resline supline |
| Change Bar Color | true | bool | barcolor |
| Bcolup (no label in the script) | color.blue | value | barcolor |
| Bcoldn (no label in the script) | color.black | value | barcolor |
| Chopiness Bars | 14 | int · min 1 | chopiness |
| ATR Length | 10 | int | supertrendT directionT |
| Factor | 3.0 | float · step 0.01 | supertrendT directionT |
What it calculates
- Pivothigh (
pivothigh,ta.pivothighin v5)Left Bars (5), Right Bars (5)ph · line 20 - Pivotlow (
pivotlow,ta.pivotlowin v5)Left Bars (5), Right Bars (5)pl · line 21 - ATR / Highest / Lowest (
atr,ta.atrin v5)1chopiness · line 111 - EMA (
ema,ta.emain v5)close, 3ema3 · line 100 - EMA (
ema,ta.emain v5)close, 5ema5 · line 101 - EMA (
ema,ta.emain v5)close, 8ema8 · line 102 - EMA (
ema,ta.emain v5)close, 13ema13 · line 103 - EMA (
ema,ta.emain v5)close, 21ema21 · line 104 - EMA (
ema,ta.emain v5)close, 34ema34 · line 105 - EMA (
ema,ta.emain v5)close, 55ema55 · line 106 - EMA (
ema,ta.emain v5)close, 100ema100 · line 107 - EMA (
ema,ta.emain v5)close, 200ema200 · line 98 - RSI (
rsi,ta.rsiin v5)close, 14rsiT · line 112 - BB (
bb,ta.bbin v5)close, length = 20, mult = 2middle · line 114 - BB (
bb,ta.bbin v5)close, length = 20, mult = 2upper · line 114 - BB (
bb,ta.bbin v5)close, length = 20, mult = 2lower · line 114 - Stdev (
stdev,ta.stdevin v5)close, 14stdev · line 115 - Supertrend (
supertrend,ta.supertrendin v5)Factor (3.0), ATR Length (10)supertrendT · line 116 - Supertrend (
supertrend,ta.supertrendin v5)Factor (3.0), ATR Length (10)directionT · line 116
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 68 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Short short | position== "SELL" and isOpenPosition and not isOpenPosition[1] | On | 163 |
| Open Long long | position== "BUY" and isOpenPosition and not isOpenPosition[1] | On | 165 |
| Close position | closeFull and isOpenPosition[1] | On | 168 |
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.
- CheckHigher-timeframe data2 security call(s) use lookahead_on (line 113, 117). Without a [1] offset this reads future bars in history.
- 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
Not run on other markets: the script requests BINANCE:BTCUSDT 60, and that data is not available here (a lower timeframe than the chart; the engine does not evaluate intrabar data). Its results would rest on empty values.
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Pivot Moving Average steteggy this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| MACD 5 iN 1 [Pro-Tool] by HamidBox | ema rsi pivotlow pivothigh highest lowest | v4 strategy | No trades on this market | ||
| VXD Supercycle by Vvaz_ | supertrend ema pivothigh pivotlow highest lowest rsi | v5 strategy | does not run in Zeal Terminal | ||
| X48 - Strategy - BreakOut & Consecutive (11in1) + Alert - V.1.2 by X4815162342 | ema highest lowest stdev pivothigh pivotlow atr rsi | v5 strategy | does not run in Zeal Terminal | ||
| M0PB (Momentum Pullback) by Marcns_ | rsi ema highest lowest atr | v5 strategy | +51.3% | 3 | 17.5% |
| Simple SuperTrend Strategy for BTCUSD 4H by StrategiesForEveryone | rsi supertrend atr highest lowest | v5 strategy | +1.1% | 72 | 19.5% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 57 ms | Yes 1,000 bars in 220 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
“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. PineTS runs Pine Script v5 and v6 only.
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/// © LonesomeThecolor.blue//@version=4strategy("Pivot Moving Average Strategy", overlay =true, max_lines_count = 500, shorttitle="PMAS")lb = input(5, title="Left Bars", minval = 1)rb = input(5, title="Right Bars", minval = 1)trad_amount = input(100, title="Trad Amount", minval = 1)minimum_profite = input(-2, title="Minimum Profite")showsupres = input(true, title="Support/Resistance", inline = "srcol")supcol = input(color.lime, title ="", inline = "srcol")rescol = input(color.red, title ="", inline = "srcol")srlinestyle = input(line.style_dotted, title = "Line Style/Width", options = [line.style_solid, line.style_dashed, line.style_dotted], inline ="style")srlinewidth = input(1, title = "", minval = 1, maxval = 5, inline ="style")changebarcol = input(true, title="Change Bar Color", inline = "bcol")bcolup = input(color.blue, title ="", inline = "bcol")bcoldn = input(color.black, title ="", inline = "bcol")length = input(14, title="Chopiness Bars", minval=1)atrPeriod = input(10, "ATR Length")factor = input(3.0, "Factor", step = 0.01)ph = pivothigh(lb, rb)pl = pivotlow(lb, rb)hl = iff(ph, 1, iff(pl, -1, na)) // Trend directionzz = iff(ph, ph, iff(pl, pl, na)) // similar to zigzag but may have multiple highs/lowszz :=iff(pl and hl == -1 and valuewhen(hl, hl, 1) == -1 and pl > valuewhen(zz, zz, 1), na, zz)zz :=iff(ph and hl == 1 and valuewhen(hl, hl, 1) == 1 and ph < valuewhen(zz, zz, 1), na, zz)hl := iff(hl==-1 and valuewhen(hl, hl, 1)==1 and zz > valuewhen(zz, zz, 1), na, hl)hl := iff(hl==1 and valuewhen(hl, hl, 1)==-1 and zz < valuewhen(zz, zz, 1), na, hl)zz := iff(na(hl), na, zz)findprevious()=> // finds previous three points (b, c, d, e) ehl = iff(hl==1, -1, 1) loc1 = 0.0, loc2 = 0.0, loc3 = 0.0, loc4 = 0.0 xx = 0 for x=1 to 1000 if hl[x]==ehl and not na(zz[x]) loc1 := zz[x] xx := x + 1 break ehl := hl for x=xx to 1000 if hl[x]==ehl and not na(zz[x]) loc2 := zz[x] xx := x + 1 break ehl := iff(hl==1, -1, 1) for x=xx to 1000 if hl[x]==ehl and not na(zz[x]) loc3 := zz[x] xx := x + 1 break ehl := hl for x=xx to 1000 if hl[x]==ehl and not na(zz[x]) loc4 := zz[x] break [loc1, loc2, loc3, loc4]float a = na, float b = na, float c = na, float d = na, float e = naif not na(hl) [loc1, loc2, loc3, loc4] = findprevious() a := zz b := loc1 c := loc2 d := loc3 e := loc4_hh = zz and (a > b and a > c and c > b and c > d)_ll = zz and (a < b and a < c and c < b and c < d)_hl = zz and ((a >= c and (b > c and b > d and d > c and d > e)) or (a < b and a > c and b < d))_lh = zz and ((a <= c and (b < c and b < d and d < c and d < e)) or (a > b and a < c and b > d))plotshape(_hl, text="HL", title="Higher Low", style=shape.labelup, color=color.lime, textcolor=color.black, location=location.belowbar, offset = -rb)plotshape(_hh, text="HH", title="Higher High", style=shape.labeldown, color=color.lime, textcolor=color.black, location=location.abovebar, offset = -rb)plotshape(_ll, text="LL", title="Lower Low", style=shape.labelup, color=color.red, textcolor=color.white, location=location.belowbar, offset = -rb)plotshape(_lh, text="LH", title="Lower High", style=shape.labeldown, color=color.red, textcolor=color.white, location=location.abovebar, offset = -rb)float res = na, float sup = nares := iff(_lh, zz, res[1])sup := iff(_hl, zz, sup[1])var int trend = natrend := iff(close > res, 1, iff(close < sup, -1, nz(trend[1])))res := iff((trend == 1 and _hh) or (trend == -1 and _lh), zz, res)sup := iff((trend == 1 and _hl) or (trend == -1 and _ll), zz, sup)rechange = res != res[1]suchange = sup != sup[1]var line resline = navar line supline = naif showsupres if rechange line.set_x2(resline, bar_index) line.set_extend(resline, extend = extend.none) resline := line.new(x1 = bar_index - rb, y1 = res, x2 = bar_index, y2 = res, color = rescol, extend = extend.right, style = srlinestyle, width = srlinewidth) if suchange line.set_x2(supline, bar_index) line.set_extend(supline, extend = extend.none) supline := line.new(x1 = bar_index - rb, y1 = sup, x2 = bar_index, y2 = sup, color = supcol, extend = extend.right, style = srlinestyle, width = srlinewidth) barcolor(color = iff(changebarcol, iff(trend == 1, bcolup, bcoldn), na))var float chopiness = na, float rsi = navar bool movingAverageTrad = na, bool movingAverageUpTrad = na, bool movingAverageDownTrad = na, bool highVolume = navar float ema3 = na, float ema5 = na, float ema8 = na, float ema13 = na, float ema21 = na, float ema34 = na, float ema55 = na, float ema100 = na// ema200 = security(syminfo.tickerid, "D", ema(close, 200), barmerge.gaps_off, barmerge.lookahead_on)ema200 = ema(close, 200)movingAverageTrad := ema200 < closeema3 := ema(close, 3)ema5 := ema(close, 5)ema8 := ema(close, 8)ema13 := ema(close, 13)ema21 := ema(close, 21)ema34 := ema(close, 34)ema55 := ema(close, 55)ema100 := ema(close, 100)movingAverageUpTrad := ema3 > ema5 and ema5 > ema8 and ema8 > ema13 and ema13 > ema21 and ema21 > ema34 and ema34 > ema200movingAverageDownTrad := ema3 < ema5 and ema5 < ema8 and ema8 < ema13 and ema13 < ema21 and ema21 < ema34 and ema34 < ema200highVolume := ema(volume, 14) < volumechopiness := 100 * log10(sum(atr(1), length) / (highest(length) - lowest(length))) / log10(length)rsiT = rsi(close, 14)rsi := security(syminfo.tickerid, "60", rsiT, barmerge.gaps_off, barmerge.lookahead_on)[middle, upper, lower] = bb(close,length=20, mult=2)stdev = stdev(close, 14)[supertrendT, directionT] = supertrend(factor, atrPeriod)[supertrend, direction] = security(syminfo.tickerid, "60", [supertrendT, directionT], barmerge.gaps_off, barmerge.lookahead_on)plot(ema3, "ema3", color=color.red)plot(ema5, "ema5", color=color.blue)plot(ema8, "ema8", color=color.orange)plot(ema13, "ema13", color=color.yellow)plot(ema21, "ema21", color=color.red)plot(ema34, "ema34", color=color.blue)plot(ema55, "ema55", color=color.red, linewidth=2)// plot(ema200, "ema200D", color=color.red)var bool buy = na, bool sell = na, bool closeFull = na, bool closeHaft = nabuy := trend == 1 and movingAverageUpTrad and direction < 0// and stdev > stdev[1]sell := trend != 1 and movingAverageDownTrad and direction > 0// and stdev > stdev[1]var float entryPrice = na, bool isOpenPosition = false, string position = na, string closeComments = naif cross(ema3, ema8) closeFull := trueelse if (position[1]== "SELL" and ema3 > ema5 and strategy.openprofit < minimum_profite) closeFull := trueelse if (position[1]== "BUY" and ema3 < ema5 and strategy.openprofit < minimum_profite) closeFull := trueelse closeFull := falseif not na(position[1]) position := closeFull ? na : position[1] entryPrice := closeFull ? na : entryPrice[1]else if buy position := "BUY" entryPrice := (close[1] + open[1])/2else if sell position := "SELL" entryPrice := (close[1] + open[1])/2 if buy or sell line.new(x1 = bar_index - rb, y1 = entryPrice, x2 = bar_index, y2 = entryPrice, color = supcol, extend = extend.none, color=color.red, style = line.style_solid, width = 1) if isOpenPosition[1] isOpenPosition := closeFull ? false : isOpenPosition[1] else if position == "SELL" and rsi[1] < rsi[2] and supertrendT != supertrendT[1] isOpenPosition := trueelse if position == "BUY" and rsi[1] > rsi[2] and supertrendT != supertrendT[1] isOpenPosition := true if strategy.openprofit < minimum_profite closeComments := "Exit"else closeComments := "Trailing Stop"message = '{"symbol":"{{ticker}}","action":"' + tostring(position) +'","quantity":"{{strategy.order.contracts}}"}'if position== "SELL" and isOpenPosition and not isOpenPosition[1] strategy.entry("Short", strategy.short, trad_amount, alert_message = message)if position== "BUY" and isOpenPosition and not isOpenPosition[1] strategy.entry("Long", strategy.long, trad_amount, alert_message = message)if closeFull and isOpenPosition[1] messageClose = '{"symbol":"{{ticker}}","close":' + tostring(closeFull) + '}' strategy.close(position, comment = closeComments, alert_message = messageClose)