Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

Stochastic RSI + WMA + SMA strat

A Pine Script v4 strategy built on RSI (close, 14), SMA or Stoch (3) and SMA (k, 3). It has 4 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.04 commission. 14 settings; defaults include Swing Lo/Hi Lookback 20, SL Expander 10 and TP Expander 30.

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 Stochastic RSI + WMA + SMA strat repaint?
No
No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
How did Stochastic RSI + WMA + SMA strat do on BTCUSDT 1D with default settings?
−11.93%
From 26 Jan 2024 to 21 Oct 2026, over 4 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can Stochastic RSI + WMA + SMA strat be converted to Pine Script v5?
Yes
The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
Does Stochastic RSI + WMA + SMA strat use a stop loss?
Yes
strategy.exit sets a stop on line 93, 94.
Does Stochastic RSI + WMA + SMA strat run in Zeal Terminal?
Yes
It compiles in 44 ms and runs 1,000 bars in 97 ms, in the browser.

BTCUSDT · 1D · default settings

  • HLine
  • HLine
  • Plot
  • Plot
  • Plot
  • Plot
  • Winning trade
  • Losing trade
  • Equity
  • Buy and hold
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$6,563$20,253Equity from $10,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 long = not i_reverse?true:false, long = not i_reverse?false:true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 88, 90) so the engine trades the way the script says. 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. The live re-run is not offered here: the script requests BINANCE:BTCUSDT 60, which this page cannot load in your browser.

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
−11.93%
−$1,193.01
Buy and hold
+102.5%
same period
Closed trades
4
1 won · 3 lost
Profitable
25.0%
of closed trades
Profit factor
0.62
gross profit ÷ gross loss
Max drawdown
37.88%
−$3,788.28

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

  • Likely bug

    sma(close, 5) ignores the settings line 41

    Line 41 hard-codes 5 where line 34 uses “Smooth K”. Changing those settings moves one band but not this one.

    sma(close, 5)sma(close, smoothK)
  • Likely bug

    sma(close, 5) ignores the settings line 43

    Line 43 hard-codes 5 where line 34 uses “Smooth K”. Changing those settings moves one band but not this one.

    sma(close, 5)sma(close, smoothK)
  • Check

    Every trade uses all the equity

    default_qty_value is 100% of equity.

  • Note

    4 settings have no label

    In the settings dialog smoothK (line 29), smoothD (line 30), lengthRSI (line 31), lengthStoch (line 32) 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 long =, which v5 renamed line 88

    long = on line 88, 90. v5 writes it as direction = strategy.long or strategy.short.

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 rsi → ta.rsi, sma → ta.sma, stoch → ta.stoch, security → request.security, wma → ta.wma, crossover → ta.crossover, crossunder → ta.crossunder, lowest → ta.lowest, highest → ta.highest, valuewhen → ta.valuewhen, atr → ta.atr.

v4 → v5

Verified

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

  • Line 15: //@version=4 → //@version=5
  • Line 33: rsi → ta.rsi
  • Line 34: sma → ta.sma
  • Line 34: stoch → ta.stoch
  • Line 35: sma → ta.sma
  • Line 40: security → request.security
25 more edits
  • Line 40: wma → ta.wma
  • Line 41: security → request.security
  • Line 41: sma → ta.sma
  • Line 42: wma → ta.wma
  • Line 43: sma → ta.sma
  • Line 45: crossover → ta.crossover
  • Line 46: crossunder → ta.crossunder
  • Line 50: lowest → ta.lowest
  • Line 51: highest → ta.highest
  • Line 53: valuewhen → ta.valuewhen
  • Line 53: valuewhen → ta.valuewhen
  • Line 53: atr → ta.atr
  • Line 54: valuewhen → ta.valuewhen
  • Line 54: valuewhen → ta.valuewhen
  • Line 54: atr → ta.atr
  • Line 55: valuewhen → ta.valuewhen
  • Line 55: valuewhen → ta.valuewhen
  • Line 55: atr → ta.atr
  • Line 56: valuewhen → ta.valuewhen
  • Line 56: valuewhen → ta.valuewhen
  • Line 56: atr → ta.atr
  • Line 60: valuewhen → ta.valuewhen
  • Line 72: valuewhen → ta.valuewhen
  • Line 88: long = not i_reverse?true:false → direction = not i_reverse?strategy.long:strategy.short
  • Line 90: long = not i_reverse?false:true → direction = not i_reverse?strategy.short:strategy.long
⋯// in the overbought region of the stochastic. Viceversa for longs"-//@version=4+//@version=5strategy("Stoch + WMA + SMA strat", overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value = 0.04, initial_capital=10000)⋯lengthStoch = input(14, minval=1)-rsi1 = rsi(src4, lengthRSI)+rsi1 = ta.rsi(src4, lengthRSI)-k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)+k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)-d = sma(k, smoothD)+d = ta.sma(k, smoothD)h0 = hline(80, linestyle=hline.style_dotted)⋯wmalen=input(defval=144, title="WMA Length")-WMA = security(syminfo.tickerid, "60", wma(close, wmalen))+WMA = request.security(syminfo.tickerid, "60", ta.wma(close, wmalen))-SMA = security(syminfo.tickerid, "60", sma(close, 5))+SMA = request.security(syminfo.tickerid, "60", ta.sma(close, 5))-minWMA = wma(close, wmalen)+minWMA = ta.wma(close, wmalen)-minSMA = sma(close, 5)+minSMA = ta.sma(close, 5)//Entry Logic-stobuy = crossover(k, d) and k < stochOS+stobuy = ta.crossover(k, d) and k < stochOS-stosell = crossunder(k, d) and k > stochOB+stosell = ta.crossunder(k, d) and k > stochOBmabuy = minSMA > minWMA⋯//SL & TP Calculations-SwingLow=lowest(i_SwingLookback)+SwingLow=ta.lowest(i_SwingLookback)-SwingHigh=highest(i_SwingLookback)+SwingHigh=ta.highest(i_SwingLookback)bought=strategy.position_size != strategy.position_size[1]-LSL=valuewhen(bought, SwingLow, 0)-((valuewhen(bought, atr(14), 0)/5)*i_SLExpander)+LSL=ta.valuewhen(bought, SwingLow, 0)-((ta.valuewhen(bought, ta.atr(14), 0)/5)*i_SLExpander)-SSL=valuewhen(bought, SwingHigh, 0)+((valuewhen(bought, atr(14), 0)/5)*i_SLExpander)+SSL=ta.valuewhen(bought, SwingHigh, 0)+((ta.valuewhen(bought, ta.atr(14), 0)/5)*i_SLExpander)-lTP=(strategy.position_avg_price + (strategy.position_avg_price-(valuewhen(bought, SwingLow, 0)))+((valuewhen(bought, atr(14), 0)/5)*i_TPExpander))+lTP=(strategy.position_avg_price + (strategy.position_avg_price-(ta.valuewhen(bought, SwingLow, 0)))+((ta.valuewhen(bought, ta.atr(14), 0)/5)*i_TPExpander))-sTP=(strategy.position_avg_price - (valuewhen(bought, SwingHigh, 0) - strategy.position_avg_price))-((valuewhen(bought, atr(14), 0)/5)*i_TPExpander)+sTP=(strategy.position_avg_price - (ta.valuewhen(bought, SwingHigh, 0) - strategy.position_avg_price))-((ta.valuewhen(bought, ta.atr(14), 0)/5)*i_TPExpander)islong=strategy.position_size > 0⋯//TrailingStop-dif=(valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0))+dif=(ta.valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0)) -strategy.position_avg_price⋯var Ststop = float(na)-Sdif=strategy.position_avg_price-(valuewhen(strategy.position_size<0 +Sdif=strategy.position_avg_price-(ta.valuewhen(strategy.position_size<0  and strategy.position_size[1]>=0, low,0))⋯if stobuy and mabuy and daymabuy-    strategy.entry("long", long=not i_reverse?true:false)+    strategy.entry("long", direction = not i_reverse?strategy.long:strategy.short)if stosell and not mabuy and not daymabuy-    strategy.entry("short", long=not i_reverse?false:true)+    strategy.entry("short", direction = not i_reverse?strategy.short:strategy.long)//Exit⋯

Clean v5

VerifiedDownload the v5 script

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

  • Line 18: input() → input.bool()
  • Line 19: input() → input.int()
  • Line 20: input() → input.int()
  • Line 21: input() → input.int()
  • Line 22: input() → input.bool()
  • Line 23: input() → input.bool()
8 more edits
  • Line 25: input() → input.source()
  • Line 26: input() → input.int()
  • Line 27: input() → input.int()
  • Line 29: input() → input.int()
  • Line 30: input() → input.int()
  • Line 31: input() → input.int()
  • Line 32: input() → input.int()
  • Line 39: input() → input.int()
⋯//SL & TP Inputs-i_SL=input(true, title="Use Swing Lo/Hi Stop Loss & Take Profit")+i_SL=input.bool(true, title="Use Swing Lo/Hi Stop Loss & Take Profit")-i_SwingLookback=input(20, title="Swing Lo/Hi Lookback")+i_SwingLookback=input.int(20, title="Swing Lo/Hi Lookback")-i_SLExpander=input(defval=10, step=1, title="SL Expander")+i_SLExpander=input.int(defval=10, step=1, title="SL Expander")-i_TPExpander=input(defval=30, step=1, title="TP Expander")+i_TPExpander=input.int(defval=30, step=1, title="TP Expander")-i_reverse=input(false, title="Reverse Trades")+i_reverse=input.bool(false, title="Reverse Trades")-i_TStop =input(false, title="Use Trailing Stop")+i_TStop =input.bool(false, title="Use Trailing Stop")//Strategy Inputs-src4 = input(close, title="RSI Source")+src4 = input.source(close, title="RSI Source")-stochOS=input(defval=20, step=5, title="Stochastics Oversold Level")+stochOS=input.int(defval=20, step=5, title="Stochastics Oversold Level")-stochOB=input(defval=80, step=5, title="Stochastics Overbought Level")+stochOB=input.int(defval=80, step=5, title="Stochastics Overbought Level")//Stoch rsi Calculations-smoothK = input(3, minval=1)+smoothK = input.int(3, minval=1)-smoothD = input(3, minval=1)+smoothD = input.int(3, minval=1)-lengthRSI = input(14, minval=1)+lengthRSI = input.int(14, minval=1)-lengthStoch = input(14, minval=1)+lengthStoch = input.int(14, minval=1)rsi1 = ta.rsi(src4, lengthRSI)⋯//MA-wmalen=input(defval=144, title="WMA Length")+wmalen=input.int(defval=144, title="WMA Length")WMA = request.security(syminfo.tickerid, "60", ta.wma(close, wmalen))⋯

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: 4 before, 22 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.

⋯// in the overbought region of the stochastic. Viceversa for longs"-//@version=5+//@version=6strategy("Stoch + WMA + SMA strat", overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value = 0.04, initial_capital=10000)⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Use Swing Lo/Hi Stop Loss & Take Profittrueboolexit longexit exit shortexit line 92 plot
Swing Lo/Hi Lookback20intSwingLow SwingHigh
SL Expander10int · step 1LSL SSL
TP Expander30int · step 1lTP sTP
Reverse Tradesfalseboolentry long entry short
Use Trailing Stopfalseboolline 83
RSI Sourceclosesourcersi1
Stochastics Oversold Level20int · step 5stobuy
Stochastics Overbought Level80int · step 5stosell
Smooth K (no label in the script)3int · min 1k
Smooth D (no label in the script)3int · min 1d
Length RSI (no label in the script)14int · min 1rsi1
Length Stoch (no label in the script)14int · min 1k
WMA Length144intWMA minWMA

What it calculates

  • RSI (rsi, ta.rsi in v5)RSI Source (close), Length RSI (14)rsi1 · line 33
  • SMA / Stoch (sma, ta.sma in v5)stoch(rsi1, rsi1, rsi1, lengthStoch), Smooth K (3)k · line 34
  • SMA (sma, ta.sma in v5)k, Smooth D (3)d · line 35
  • WMA (wma, ta.wma in v5)close, WMA Length (144)WMA · line 40
  • SMA (sma, ta.sma in v5)close, 5SMA · line 41
  • WMA (wma, ta.wma in v5)close, WMA Length (144)minWMA · line 42
  • SMA (sma, ta.sma in v5)close, 5minSMA · line 43
  • Lowest (lowest, ta.lowest in v5)Swing Lo/Hi Lookback (20)SwingLow · line 50
  • Highest (highest, ta.highest in v5)Swing Lo/Hi Lookback (20)SwingHigh · line 51
  • Valuewhen / ATR (valuewhen, ta.valuewhen in v5)bought, SwingLow, 0LSL · line 53
  • Valuewhen / ATR (valuewhen, ta.valuewhen in v5)bought, SwingHigh, 0SSL · line 54
  • Valuewhen / ATR (valuewhen, ta.valuewhen in v5)bought, SwingLow, 0lTP · line 55
  • Valuewhen / ATR (valuewhen, ta.valuewhen in v5)bought, SwingHigh, 0sTP · line 56

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 datalengthRSILength RSIsetting · 14lengthStochLength Stochsetting · 14smoothKSmooth Ksetting · 3smoothDSmooth Dsetting · 3wmalenWMA Lengthsetting · 144i_SwingLookbackSwing Lo/Hi Lookbacksetting · 20i_SLExpanderSL Expandersetting · 10i_TPExpanderTP Expandersetting · 30highhighprice datalowlowprice datastochOSStochastics Oversold Le…setting · 20stochOBStochastics Overbought …setting · 80rsi1RSI→ rsi1kSMA / Stoch→ kdSMA→ dWMAWMA→ WMASMASMA→ SMAminWMAWMA→ minWMAminSMASMA→ minSMASwingLowLowest→ SwingLowSwingHighHighest→ SwingHighLSLValuewhen / ATR→ LSLSSLValuewhen / ATR→ SSLlTPValuewhen / ATR→ lTPsTPValuewhen / ATR→ sTPtrailOffsettrailOffsetvaluetstoptstopvalueStrailOffsetStrailOffsetvalueStstopStstopvalueSLSLvalueTPTPvaluestobuystobuyconditionstosellstosellconditionmabuymabuyconditiondaymabuydaymabuyconditionbought, islong, isshortbought · islong +13 conditionsdifdifconditionSdifSdifconditionentry:longOpen longstrategy.entryentry:shortOpen shortstrategy.entryexit:longexitexit longexitstrategy.exitexit:shortexitexit shortexitstrategy.exithline:36hlinehlinehline:37hlinehlineplot:96plotplotplot:97plotplotplot:98plotplotplot:99plotplot

Order rules

OrderFires whenBy defaultLine
Open long not i_reverse?true:falsestobuy and mabuy and daymabuyOn88
Open short not i_reverse?false:truestosell and not mabuy and not daymabuyOn90
Exit longexit stop and limiti_SLOn “Use Swing Lo/Hi Stop Loss & Take Profit”93
Exit shortexit stop and limiti_SLOn “Use Swing Lo/Hi Stop Loss & Take Profit”94

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.

Not measuredNo trades to compareThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, but neither the live nor the historical run produced any trades in them, so the replay says nothing either way.
  • ClearHigher-timeframe data2 security call(s), none with lookahead on.
  • 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.

The trades

By market direction

At entry, last 90 barsTradesWonP&L
Rising market20%−$2,408.82
Sideways market250%$1,219.33
Falling market0—$0.00

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

How bad could the drawdown be?

31.5%

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

0%36%
All 4 trades
SideEntryExitBarsP&L
short15 Feb 2024 51,795.2027 Feb 2024 55,957.0012−$811.52
short8 May 2024 62,312.1029 Oct 2024 73,094.40174−$1,597.30
short28 Mar 2025 87,232.0025 Apr 2025 95,671.6028−$740.51
short29 Oct 2025 112,898.0031 Jan 2026 80,510.2094$1,959.84

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
Stochastic RSI + WMA + SMA strat this page—v4 strategy−11.9%437.9%
Correlation Strategy by tweakerIDlowest highest valuewhen atr rsi smav4 strategydoes not run in Zeal Terminal
Delayed RSI Strategy by tweakerIDlowest highest valuewhen atr rsiv4 strategy−40.4%742.2%
CCI Scalping Strategy by tweakerIDlowest highest valuewhen atr wmav4 strategy−66.2%4173.0%
Money Flow Index 5 min Strategy by tweakerIDlowest highest valuewhen atr smav4 strategy−6.8%1828.4%
Double Bollinger Strategy by tweakerIDlowest highest valuewhen atr rsiv4 strategy−29.6%6150.1%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 44 msWith a workaround 1,000 bars in 97 msNot measured
PineTSNo v5 and v6 only—Not measured

Zeal Terminal's engine currently reads an order side written as long = not i_reverse?true:false, long = not i_reverse?false:true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 88, 90) so the engine trades the way the script says. 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. 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/// © bufirolas// Works well with a wide stop with 20 bars lookback// for the SL level and a 2:1 reward ratio Take Profit .// These parameters can be modified in the Inputs section of the strategy panel.// "an entry signal it's a cross down or up on// the stochastics. if you're in a downtrend// on the hourly time frame you// must also be in a downtrend on the five// minute so the five period has to be below the 144// as long as the five period is still trading below// the 144 period on both the hourly and the five minutes// we are looking for these short signals crosses down// in the overbought region of the stochastic. Viceversa for longs"//@version=4strategy("Stoch + WMA + SMA strat", overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value = 0.04, initial_capital=10000)//SL & TP Inputsi_SL=input(true, title="Use Swing Lo/Hi Stop Loss & Take Profit")i_SwingLookback=input(20, title="Swing Lo/Hi Lookback")i_SLExpander=input(defval=10, step=1, title="SL Expander")i_TPExpander=input(defval=30, step=1, title="TP Expander")i_reverse=input(false, title="Reverse Trades")i_TStop =input(false, title="Use Trailing Stop")//Strategy Inputssrc4 = input(close, title="RSI Source")stochOS=input(defval=20, step=5, title="Stochastics Oversold Level")stochOB=input(defval=80, step=5, title="Stochastics Overbought Level")//Stoch rsi CalculationssmoothK = input(3, minval=1)smoothD = input(3, minval=1)lengthRSI = input(14, minval=1)lengthStoch = input(14, minval=1)rsi1 = rsi(src4, lengthRSI)k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)d = sma(k, smoothD)h0 = hline(80, linestyle=hline.style_dotted)h1 = hline(20, linestyle=hline.style_dotted)//MAwmalen=input(defval=144, title="WMA Length")WMA = security(syminfo.tickerid, "60", wma(close, wmalen))SMA = security(syminfo.tickerid, "60", sma(close, 5))minWMA = wma(close, wmalen)minSMA = sma(close, 5)//Entry Logicstobuy = crossover(k, d) and k < stochOSstosell = crossunder(k, d) and k > stochOBmabuy = minSMA > minWMAdaymabuy = SMA > WMA//SL & TP CalculationsSwingLow=lowest(i_SwingLookback)SwingHigh=highest(i_SwingLookback)bought=strategy.position_size != strategy.position_size[1]LSL=valuewhen(bought, SwingLow, 0)-((valuewhen(bought, atr(14), 0)/5)*i_SLExpander)SSL=valuewhen(bought, SwingHigh, 0)+((valuewhen(bought, atr(14), 0)/5)*i_SLExpander)lTP=(strategy.position_avg_price + (strategy.position_avg_price-(valuewhen(bought, SwingLow, 0)))+((valuewhen(bought, atr(14), 0)/5)*i_TPExpander))sTP=(strategy.position_avg_price - (valuewhen(bought, SwingHigh, 0) - strategy.position_avg_price))-((valuewhen(bought, atr(14), 0)/5)*i_TPExpander)islong=strategy.position_size > 0isshort=strategy.position_size < 0//TrailingStopdif=(valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0)) -strategy.position_avg_pricetrailOffset     = strategy.position_avg_price - LSLvar tstop = float(na)if strategy.position_size > 0    tstop := high- trailOffset - dif    if tstop<tstop[1]        tstop:=tstop[1]else    tstop := naStrailOffset     = SSL - strategy.position_avg_pricevar Ststop = float(na)Sdif=strategy.position_avg_price-(valuewhen(strategy.position_size<0  and strategy.position_size[1]>=0, low,0))if strategy.position_size < 0    Ststop := low+ StrailOffset + Sdif    if Ststop>Ststop[1]        Ststop:=Ststop[1]else    Ststop := na    //Stop SelectorSL= islong ? LSL : isshort ? SSL : naif i_TStop     SL:= islong ? tstop : isshort ? Ststop : naTP= islong ? lTP : isshort ? sTP : na//Entriesif stobuy and mabuy and daymabuy    strategy.entry("long", long=not i_reverse?true:false)if stosell and not mabuy and not daymabuy    strategy.entry("short", long=not i_reverse?false:true)//Exitif i_SL    strategy.exit("longexit", "long", stop=SL, limit=TP)    strategy.exit("shortexit", "short", stop=SL, limit=TP)//Plotsplot(i_SL ? SL : na, color=color.red, style=plot.style_cross)plot(i_SL ? TP : na, color=color.green, style=plot.style_cross)plot(minWMA)plot(minSMA, color=color.green)