Pine Script v4 · Strategy
Ultimate Oscillator [Long] Strategy
A Pine Script v4 strategy built on SMA (src, length), VWAP (hlc3) and EMA (ultOscVal, 5). It has 4 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 20% of equity and pays no commission. 11 settings; defaults include Length1 5, Length2 10 and Length3 15.
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 Ultimate Oscillator [Long] Strategy repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Ultimate Oscillator [Long] Strategy 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; it opened a long position that was still open on the last bar, so there is no result to report. On ETHUSDT 1D it did trade; that row of the markets table below has its figures. A backtest, not investment advice.
- Can Ultimate Oscillator [Long] Strategy 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 Ultimate Oscillator [Long] Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Ultimate Oscillator [Long] Strategy run in Zeal Terminal?
- Yes
- It compiles in 19 ms and runs 1,000 bars in 71 ms, in the browser.
BTCUSDT · 1D · default settings
- Middle Line 60 [Profit Exit Here]
- Overbought
- Oversold
- rsiUO
- emaUO [signal line]
- Winning trade
- Losing trade
- Equity
- Buy and hold
- 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
- 49.87%
- −$4,986.77
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. A long 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
crossover(rsiUO, 30) ignores the settings line 61
Line 61 hard-codes 30 where line 61 uses “Buy Line (UO crossing up oversold at ) ”. Changing those settings moves one band but not this one.
crossover(rsiUO, 30)crossover(rsiUO, buyLine) -
Likely bug
crossover(rsiUO, 60) ignores the settings line 82
Line 82 hard-codes 60 where line 61 uses “Buy Line (UO crossing up oversold at ) ”. Changing those settings moves one band but not this one.
crossover(rsiUO, 60)crossover(rsiUO, buyLine) -
Check
1 setting does nothing
“Signal length” is declared but never read.
-
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
3 settings have no label
In the settings dialog length1 (line 8), length2 (line 8), length3 (line 8) 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 an argument v6 removed
when = on line 69, 85, 87, 91. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 80
transp = on line 80. v5 folds the transparency into the colour with color.new(c, transp).
-
Note
Uses long =, which v5 renamed line 69
long = on line 69. 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 sum → math.sum, max → math.max, min → math.min, sma → ta.sma, vwap → ta.vwap, ema → ta.ema, crossover → ta.crossover, barssince → ta.barssince, abs → math.abs, crossunder → ta.crossunder, tostring → str.tostring.
- 4 strategy calls use the when = argument (line 69, 85, 87, 91). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 27 trades and the same P&L on ETHUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 19: sum → math.sum
- Line 19: sum → math.sum
- Line 20: max → math.max
- Line 21: min → math.min
- Line 34: sma → ta.sma
17 more edits
- Line 42: vwap → ta.vwap
- Line 53: ema → ta.ema
- Line 61: crossover → ta.crossover
- Line 61: crossover → ta.crossover
- Line 69: long = true → direction = strategy.long
- Line 69: barssince → ta.barssince
- Line 78: barssince → ta.barssince
- Line 80: abs → math.abs
- Line 80: transp = 70 → color.new(abs(strategy.position_size)>=1 ? color.blue : na, 70)
- Line 82: crossover → ta.crossover
- Line 85: abs → math.abs
- Line 85: crossunder → ta.crossunder
- Line 87: tostring → str.tostring
- Line 87: abs → math.abs
- Line 87: crossunder → ta.crossunder
- Line 91: tostring → str.tostring
- Line 91: abs → math.abs
⋯// © mohanee-//@version=4+//@version=5strategy(title="Ultimate Oscillator [Long] Strategy", shorttitle="UO" , overlay=false, pyramiding=2, default_qty_type=strategy.percent_of_equity, default_qty_value=20, initial_capital=10000, currency=currency.USD) //default_qty_value=10, default_qty_type=strategy.fixed,⋯//showUO=input(false, "show Ultimate Oscialltor")-average(bp, tr_, length) => sum(bp, length) / sum(tr_, length)+average(bp, tr_, length) => math.sum(bp, length) / math.sum(tr_, length)-high_ = max(high, close[1])+high_ = math.max(high, close[1])-low_ = min(low, close[1])+low_ = math.min(low, close[1])bp = close - low_⋯ smma = 0.0- smma := na(smma[1]) ? sma(src, length) : (smma[1] * (length - 1) + src) / length+ smma := na(smma[1]) ? ta.sma(src, length) : (smma[1] * (length - 1) + src) / length smma⋯/////////////////////////////////////////////////////////////////////////////////-myVwap= vwap(hlc3)+myVwap= ta.vwap(hlc3)//drawings⋯//signal line-emaUO = ema(ultOscVal , 5) // ema(ultOscVal / rsiUO, 9)+emaUO = ta.ema(ultOscVal , 5) // ema(ultOscVal / rsiUO, 9)//ultPlot=plot(showUO==true? ultOscVal : na, color=color.green, title="Oscillator")⋯/////////////////////////////////////////////////////////////////////////////////-longCond= crossover(rsiUO, buyLine) or crossover(rsiUO, 30)+longCond= ta.crossover(rsiUO, buyLine) or ta.crossover(rsiUO, 30)//longCond= ( ema10>ema20 and crossover(rsiUO, buyLine) ) or ( ema10 < ema20 and crossover(rsiUO, 75) )⋯//strategy.entry(id="LERSIofUO", long=true, qty=qty1, when = close > open and barssince(longCond)<=3 and strategy.position_size<1 ) //and sma1 > sma3) // and close>open and rsiUO >= 25 ) //and-strategy.entry(id="LEUO", long=true, qty=qty1, when = close > open and barssince(longCond)<=3 and strategy.position_size<1 and sma2 > sma3) // and close>open and rsiUO >= 25 ) //and+strategy.entry(id="LEUO", direction = strategy.long, qty=qty1, when = close > open and ta.barssince(longCond)<=3 and strategy.position_size<1 and sma2 > sma3) // and close>open and rsiUO >= 25 ) //and//Add⋯//change the bar color to yellow , indicating startegy will trigger BUY-barcolor( close > open and barssince(longCond)<=3 and strategy.position_size<1 and sma2 > sma3 ? color.orange : na)+barcolor( close > open and ta.barssince(longCond)<=3 and strategy.position_size<1 and sma2 > sma3 ? color.orange : na)//barcolor(abs(strategy.position_size)>=1 ? color.blue : na )-bgcolor(abs(strategy.position_size)>=1 ? color.blue : na , transp=70)+bgcolor(color.new(math.abs(strategy.position_size)>=1 ? color.blue : na, 70) )//signal for addition to existing position -barcolor( strategy.position_size>=1 and close < strategy.position_avg_price and crossover(rsiUO, 60) ? color.yellow : na)+barcolor( strategy.position_size>=1 and close < strategy.position_avg_price and ta.crossover(rsiUO, 60) ? color.yellow : na)//bgcolor( strategy.position_size>=1 and close < strategy.position_avg_price and crossover(rsiUO, 60) ? color.yellow : na, transp=30)//partial exit-strategy.close(id="LEUO", comment="PExit", qty=strategy.position_size/3, when= takeProfit and abs(strategy.position_size)>=1 and close > strategy.position_avg_price and crossunder(rsiUO,profitExitLine) )+strategy.close(id="LEUO", comment="PExit", qty=strategy.position_size/3, when= takeProfit and math.abs(strategy.position_size)>=1 and close > strategy.position_avg_price and ta.crossunder(rsiUO,profitExitLine) )//close the Long order-strategy.close(id="LEUO", comment="Profit is "+tostring(close - strategy.position_avg_price, "###.##"), when=abs(strategy.position_size)>=1 and crossunder(rsiUO,exitLine) ) //and close > strategy.position_avg_price )+strategy.close(id="LEUO", comment="Profit is "+str.tostring(close - strategy.position_avg_price, "###.##"), when=math.abs(strategy.position_size)>=1 and ta.crossunder(rsiUO,exitLine) ) //and close > strategy.position_avg_price )//strategy.close(id="LEUO", comment="CloseAll", when=abs(strategy.position_size)>=1 and crossunder(rsiUO2,40) ) //and close > strategy.position_avg_price )⋯stopLossVal = strategy.position_size>=1 ? strategy.position_avg_price * ( 1 - (stopLoss/100) ) : 0.00-strategy.close(id="LEUO", comment="SL exit Loss is "+tostring(close - strategy.position_avg_price, "###.##") , when=abs(strategy.position_size)>=1 and close < stopLossVal and rsiUO < exitLine) +strategy.close(id="LEUO", comment="SL exit Loss is "+str.tostring(close - strategy.position_avg_price, "###.##") , when=math.abs(strategy.position_size)>=1 and close < stopLossVal and rsiUO < exitLine) //reason to rsiUO <30 is if price is going down , indicator should reflect it ... but indicator is above 30 means it showing divergence... so hold on it until it crossdown 30 ...that way even Stop Loss less than predefined ...⋯Same 27 trades and the same P&L on ETHUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 8: input() → input.int()
- Line 8: input() → input.int()
- Line 8: input() → input.int()
- Line 10: input() → input.int()
- Line 11: input() → input.int()
- Line 12: input() → input.int()
9 more edits
- Line 13: input() → input.int()
- Line 14: input() → input.int()
- Line 15: input() → input.bool()
- Line 16: input() → input.int()
- Line 17: input() → input.bool()
- Line 69: when = close > open and ta.barssince(longCond)<=3 and strategy.position_size<1 and sma2 > sma3 → if block
- Line 85: when = takeProfit and math.abs(strategy.position_size)>=1 and close > strategy.position_avg_price and ta.crossunder(rsiUO,profitExitLine) → if block
- Line 87: when = math.abs(strategy.position_size)>=1 and ta.crossunder(rsiUO,exitLine) → if block
- Line 91: when = math.abs(strategy.position_size)>=1 and close < stopLossVal and rsiUO < exitLine → if block
⋯/////////////////////////////////////////////////////////////////////////////////-length1 = input(5, minval=1), length2 = input(10, minval=1), length3 = input(15, minval=1)+length1 = input.int(5, minval=1), length2 = input.int(10, minval=1), length3 = input.int(15, minval=1)//rsiUOLength = input(7, title="RSI UO length", minval=1)-signalLength = input(9, title="Signal length", minval=1)+signalLength = input.int(9, title="Signal length", minval=1)-buyLine = input (45, title="Buy Line (UO crossing up oversold at ) ") //crossover+buyLine = input.int (45, title="Buy Line (UO crossing up oversold at ) ") //crossover-exitLine = input (70, title="Exit Line (UO crsossing down overbought at) ") //crossunder+exitLine = input.int (70, title="Exit Line (UO crsossing down overbought at) ") //crossunder-riskCapital = input(title="Risk % of capital", defval=10, minval=1)+riskCapital = input.int(title="Risk % of capital", defval=10, minval=1)-stopLoss=input(3,title="Stop Loss",minval=1)+stopLoss=input.int(3,title="Stop Loss",minval=1)-takeProfit=input(false, title="Take Profit")+takeProfit=input.bool(false, title="Take Profit")-profitExitLine = input (75, title="Take Profit at RSIofUO crossing below this value ") //crossunder+profitExitLine = input.int (75, title="Take Profit at RSIofUO crossing below this value ") //crossunder-showSignalLine=input(true, "show Signal Line")+showSignalLine=input.bool(true, "show Signal Line")//showUO=input(false, "show Ultimate Oscialltor")⋯//strategy.entry(id="LERSIofUO", long=true, qty=qty1, when = close > open and barssince(longCond)<=3 and strategy.position_size<1 ) //and sma1 > sma3) // and close>open and rsiUO >= 25 ) //and-strategy.entry(id="LEUO", direction = strategy.long, qty=qty1, when = close > open and ta.barssince(longCond)<=3 and strategy.position_size<1 and sma2 > sma3) // and close>open and rsiUO >= 25 ) //and+if close > open and ta.barssince(longCond)<=3 and strategy.position_size<1 and sma2 > sma3+ strategy.entry(id="LEUO", direction = strategy.long, qty=qty1) // and close>open and rsiUO >= 25 ) //and//Add⋯//partial exit-strategy.close(id="LEUO", comment="PExit", qty=strategy.position_size/3, when= takeProfit and math.abs(strategy.position_size)>=1 and close > strategy.position_avg_price and ta.crossunder(rsiUO,profitExitLine) )+if takeProfit and math.abs(strategy.position_size)>=1 and close > strategy.position_avg_price and ta.crossunder(rsiUO,profitExitLine)+ strategy.close(id="LEUO", comment="PExit", qty=strategy.position_size/3 )//close the Long order-strategy.close(id="LEUO", comment="Profit is "+str.tostring(close - strategy.position_avg_price, "###.##"), when=math.abs(strategy.position_size)>=1 and ta.crossunder(rsiUO,exitLine) ) //and close > strategy.position_avg_price )+if math.abs(strategy.position_size)>=1 and ta.crossunder(rsiUO,exitLine)+ strategy.close(id="LEUO", comment="Profit is "+str.tostring(close - strategy.position_avg_price, "###.##") ) //and close > strategy.position_avg_price )//strategy.close(id="LEUO", comment="CloseAll", when=abs(strategy.position_size)>=1 and crossunder(rsiUO2,40) ) //and close > strategy.position_avg_price )⋯stopLossVal = strategy.position_size>=1 ? strategy.position_avg_price * ( 1 - (stopLoss/100) ) : 0.00-strategy.close(id="LEUO", comment="SL exit Loss is "+str.tostring(close - strategy.position_avg_price, "###.##") , when=math.abs(strategy.position_size)>=1 and close < stopLossVal and rsiUO < exitLine) +if math.abs(strategy.position_size)>=1 and close < stopLossVal and rsiUO < exitLine+ strategy.close(id="LEUO", comment="SL exit Loss is "+str.tostring(close - strategy.position_avg_price, "###.##") ) //reason to rsiUO <30 is if price is going down , indicator should reflect it ... but indicator is above 30 means it showing divergence... so hold on it until it crossdown 30 ...that way even Stop Loss less than predefined ...⋯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: 27 before, 25 after on ETHUSDT 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.
⋯// © mohanee-//@version=5+//@version=6strategy(title="Ultimate Oscillator [Long] Strategy", shorttitle="UO" , overlay=false, pyramiding=2, default_qty_type=strategy.percent_of_equity, default_qty_value=20, initial_capital=10000, currency=currency.USD) //default_qty_value=10, default_qty_type=strategy.fixed,⋯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 |
|---|---|---|---|
| Length1 (no label in the script) | 5 | int · min 1 | avg7 |
| Length2 (no label in the script) | 10 | int · min 1 | avg14 |
| Length3 (no label in the script) | 15 | int · min 1 | avg28 |
| Signal length | 9 | int · min 1 | unused |
| Buy Line (UO crossing up oversold at ) | 45 | int | Oversold longCond |
| Exit Line (UO crsossing down overbought at) | 70 | int | Overbought close LEUO |
| Risk % of capital | 10 | int · min 1 | qty1 |
| Stop Loss | 3 | int · min 1 | qty1 stopLossVal |
| Take Profit | false | bool | close LEUO |
| Take Profit at RSIofUO crossing below this value | 75 | int | Middle Line 60 [Profit Exit Here] close LEUO |
| show Signal Line | true | bool | emaUO [signal line] |
What it calculates
- SMA (
sma,ta.smain v5)src, lengthsmma · line 34 - VWAP (
vwap,ta.vwapin v5)hlc3myVwap · line 42 - EMA (
ema,ta.emain v5)ultOscVal, 5emaUO · line 53
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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open LEUO long | close > open and barssince(longCond)<=3 and strategy.position_size<1 and sma2 > sma3 | On | 69 |
| Close LEUO | takeProfit and abs(strategy.position_size)>=1 and close > strategy.position_avg_price and crossunder(rsiUO,profitExitLine) | On | 85 |
| Close LEUO | abs(strategy.position_size)>=1 and crossunder(rsiUO,exitLine) | On | 87 |
| Close LEUO | abs(strategy.position_size)>=1 and close < stopLossVal and rsiUO < exitLine | On | 91 |
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.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | 0.0% | +102.5% | 0 | 0% | — | 49.9% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 4.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −44.0% | +9.8% | 27 | 19% | 0.46 | 49.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +34.4% | +8.4% | 13 | 62% | 2.41 | 8.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −7.7% | +16.7% | 27 | 19% | 0.93 | 34.5% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −1.4% | +21.5% | 12 | 50% | 0.95 | 12.6% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +13.0% | +148.3% | 20 | 25% | 1.16 | 27.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +5.1% | +17.1% | 10 | 40% | 1.31 | 11.4% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
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 |
|---|---|---|---|---|---|
| Ultimate Oscillator [Long] Strategy this page | — | v4 strategy | 0.0% | 0 | 49.9% |
| ZVWAP strategy by mohanee | sma ema vwap | v4 strategy | +614.4% | 13 | 748.5% |
| JBravo Swing with GoGo Juice by bradvaughn | sma ema vwap | v4 strategy | +68.7% | 93 | 22.3% |
| VWAP and BB strategy [EEMANI] by mohanee | ema vwap sma | v4 strategy | No trades on this market | ||
| Buying Selling Volume Strategy by exlux99 | vwap ema sma | v5 strategy | No trades on this market | ||
| Long only strategy VWAP with BB and Golden Cross EMA50-200 by SoftKill21 | ema vwap sma | v4 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 19 ms | With a workaround 1,000 bars in 71 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 69) 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. 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/// © mohanee//@version=4strategy(title="Ultimate Oscillator [Long] Strategy", shorttitle="UO" , overlay=false, pyramiding=2, default_qty_type=strategy.percent_of_equity, default_qty_value=20, initial_capital=10000, currency=currency.USD) //default_qty_value=10, default_qty_type=strategy.fixed, //Ultimate Oscillator logic copied from TradingView builtin indicator/////////////////////////////////////////////////////////////////////////////////length1 = input(5, minval=1), length2 = input(10, minval=1), length3 = input(15, minval=1)//rsiUOLength = input(7, title="RSI UO length", minval=1)signalLength = input(9, title="Signal length", minval=1)buyLine = input (45, title="Buy Line (UO crossing up oversold at ) ") //crossoverexitLine = input (70, title="Exit Line (UO crsossing down overbought at) ") //crossunderriskCapital = input(title="Risk % of capital", defval=10, minval=1)stopLoss=input(3,title="Stop Loss",minval=1)takeProfit=input(false, title="Take Profit")profitExitLine = input (75, title="Take Profit at RSIofUO crossing below this value ") //crossundershowSignalLine=input(true, "show Signal Line")//showUO=input(false, "show Ultimate Oscialltor")average(bp, tr_, length) => sum(bp, length) / sum(tr_, length)high_ = max(high, close[1])low_ = min(low, close[1])bp = close - low_tr_ = high_ - low_avg7 = average(bp, tr_, length1)avg14 = average(bp, tr_, length2)avg28 = average(bp, tr_, length3)ultOscVal = 100 * (4*avg7 + 2*avg14 + avg28)/7//Ultimate Oscillator ///////////////////////////////////////////////////////////////////////////////////Willimas Alligator copied from TradingView built in Indicator/////////////////////////////////////////////////////////////////////////////////smma(src, length) => smma = 0.0 smma := na(smma[1]) ? sma(src, length) : (smma[1] * (length - 1) + src) / length smma//moving averages logic copied from Willimas Alligator -- builtin indicator in TradingViewsma1=smma(hl2,5)sma2=smma(hl2,20)sma3=smma(hl2,50)//Willimas Alligator/////////////////////////////////////////////////////////////////////////////////myVwap= vwap(hlc3)//drawings/////////////////////////////////////////////////////////////////////////////////hline(profitExitLine, title="Middle Line 60 [Profit Exit Here]", color=color.purple , linestyle=hline.style_dashed)obLevelPlot = hline(exitLine, title="Overbought", color=color.red , linestyle=hline.style_dashed)osLevelPlot = hline(buyLine, title="Oversold", color=color.blue, linestyle=hline.style_dashed)//fill(obLevelPlot, osLevelPlot, title="Background", color=color.blue, transp=90)//rsiUO = rsi(ultOscVal,rsiUOLength)rsiUO=ultOscVal//emaUO = ema(rsiUO, 9)//signal lineemaUO = ema(ultOscVal , 5) // ema(ultOscVal / rsiUO, 9)//ultPlot=plot(showUO==true? ultOscVal : na, color=color.green, title="Oscillator")plot(rsiUO, title = "rsiUO" , color=color.purple)plot(showSignalLine ? emaUO : na , title = "emaUO [signal line]" , color=color.blue) //emaUO//drawings///////////////////////////////////////////////////////////////////////////////////Strategy Logic /////////////////////////////////////////////////////////////////////////////////longCond= crossover(rsiUO, buyLine) or crossover(rsiUO, 30)//longCond= ( ema10>ema20 and crossover(rsiUO, buyLine) ) or ( ema10 < ema20 and crossover(rsiUO, 75) )//Entry--//Echeck how many units can be purchased based on risk manage ment and stop lossqty1 = (strategy.equity * riskCapital / 100 ) / (close*stopLoss/100) //check if cash is sufficient to buy qty1 , if capital not available use the available capital onlyqty1:= (qty1 * close >= strategy.equity ) ? (strategy.equity / close) : qty1//strategy.entry(id="LERSIofUO", long=true, qty=qty1, when = close > open and barssince(longCond)<=3 and strategy.position_size<1 ) //and sma1 > sma3) // and close>open and rsiUO >= 25 ) //andstrategy.entry(id="LEUO", long=true, qty=qty1, when = close > open and barssince(longCond)<=3 and strategy.position_size<1 and sma2 > sma3) // and close>open and rsiUO >= 25 ) //and//Add//strategy.entry(id="LEUO", comment="Add" , qty=qty1/2 , long=true, when = strategy.position_size>=1 and close < strategy.position_avg_price and crossover(rsiUO, 60) ) //and sma1 > sma3) // and close>open and rsiUO >= 25 ) //and//strategy.entry(id="LEUO", long=true, qty=qty1, when = close > open and barssince(longCond)<=10 and valuewhen(longCond , close , 1) > close and rsiUO>=30) // and close>open and rsiUO >= 25 ) //and //for Later versions//also check for divergence ... later version//also check if close above vwap session//strategy.entry(id="LEUO", long=false, when = sma1< sma2 and crossunder(rsiUO,60) )//change the bar color to yellow , indicating startegy will trigger BUYbarcolor( close > open and barssince(longCond)<=3 and strategy.position_size<1 and sma2 > sma3 ? color.orange : na)//barcolor(abs(strategy.position_size)>=1 ? color.blue : na )bgcolor(abs(strategy.position_size)>=1 ? color.blue : na , transp=70)//signal for addition to existing position barcolor( strategy.position_size>=1 and close < strategy.position_avg_price and crossover(rsiUO, 60) ? color.yellow : na)//bgcolor( strategy.position_size>=1 and close < strategy.position_avg_price and crossover(rsiUO, 60) ? color.yellow : na, transp=30)//partial exitstrategy.close(id="LEUO", comment="PExit", qty=strategy.position_size/3, when= takeProfit and abs(strategy.position_size)>=1 and close > strategy.position_avg_price and crossunder(rsiUO,profitExitLine) )//close the Long orderstrategy.close(id="LEUO", comment="Profit is "+tostring(close - strategy.position_avg_price, "###.##"), when=abs(strategy.position_size)>=1 and crossunder(rsiUO,exitLine) ) //and close > strategy.position_avg_price )//strategy.close(id="LEUO", comment="CloseAll", when=abs(strategy.position_size)>=1 and crossunder(rsiUO2,40) ) //and close > strategy.position_avg_price )// stop loss exitstopLossVal = strategy.position_size>=1 ? strategy.position_avg_price * ( 1 - (stopLoss/100) ) : 0.00strategy.close(id="LEUO", comment="SL exit Loss is "+tostring(close - strategy.position_avg_price, "###.##") , when=abs(strategy.position_size)>=1 and close < stopLossVal and rsiUO < exitLine) //reason to rsiUO <30 is if price is going down , indicator should reflect it ... but indicator is above 30 means it showing divergence... so hold on it until it crossdown 30 ...that way even Stop Loss less than predefined ...//Strategy Logic /////////////////////////////////////////////////////////////////////////////////