Pine Script v4 · Strategy
Sentiment Oscillator
A Pine Script v4 strategy built on EMA (priceChange, 49), Crossover (macd, signal) and Crossunder (macd, signal). It has 2 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 100% of equity and pays 0.08% commission. 21 settings; defaults include Market Sentiment Lookback Length 49, Fast EMA Length 40 and Slow EMA Length 204.
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 Sentiment Oscillator repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 1 of 1 trades.
- How did Sentiment Oscillator do on BTCUSDT 1D with default settings?
- +1.38%
- From 26 Jan 2024 to 21 Oct 2026, over 5 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Sentiment Oscillator 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 Sentiment Oscillator use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Sentiment Oscillator run in Zeal Terminal?
- Yes
- It compiles in 34 ms and runs 1,000 bars in 65 ms, in the browser.
BTCUSDT · 1D · default settings
- MACD
- Signal
- Histogram
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +1.38%
- $1.38
- Buy and hold
- +102.5%
- same period
- Closed trades
- 5
- 2 won · 3 lost
- Profitable
- 40.0%
- of closed trades
- Profit factor
- 1.05
- gross profit ÷ gross loss
- Max drawdown
- 20.62%
- −$23.39
One run with the author's default settings, $100 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.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 143, 144. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 123
transp = on line 123, 125, 127, 130, 131, 132. v5 folds the transparency into the colour with color.new(c, transp).
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 ema → ta.ema, crossover → ta.crossover, crossunder → ta.crossunder, valuewhen → ta.valuewhen.
- 2 strategy calls use the when = argument (line 143, 144). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 5 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 6: input(type = input.integer) → input.int()
- Line 7: input(type = input.integer) → input.int()
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.integer) → input.int()
- Line 10: input(type = input.bool) → input.bool()
30 more edits
- Line 11: input(type = input.bool) → input.bool()
- Line 12: input(type = input.bool) → input.bool()
- Line 13: input(type = input.bool) → input.bool()
- Line 14: input(type = input.bool) → input.bool()
- Line 15: input(type = input.bool) → input.bool()
- Line 16: input(type = input.bool) → input.bool()
- Line 17: input(type = input.bool) → input.bool()
- Line 18: input(type = input.bool) → input.bool()
- Line 25: input(type = input.bool) → input.bool()
- Line 26: input(type = input.bool) → input.bool()
- Line 30: ema → ta.ema
- Line 31: ema → ta.ema
- Line 32: ema → ta.ema
- Line 33: ema → ta.ema
- Line 34: ema → ta.ema
- Line 36: ema → ta.ema
- Line 101: crossover → ta.crossover
- Line 103: crossunder → ta.crossunder
- Line 106: crossover → ta.crossover
- Line 108: crossunder → ta.crossunder
- Line 111: crossover → ta.crossover
- Line 112: crossunder → ta.crossunder
- Line 113: valuewhen → ta.valuewhen
- Line 114: valuewhen → ta.valuewhen
- Line 123: transp = mcTheme ? 15 : 0 → color.new(mcTheme ? #6f62e3 : col_macd, mcTheme ? 15 : 0)
- Line 125: transp = mcTheme ? 85 : 0 → color.new(mcTheme ? (signal > 0 ? color.blue : color.orange) : col_signal, mcTheme ? 85 : 0)
- Line 127: transp = mcTheme ? 15 : 20 → color.new(mcTheme ? (hist > 0 ? color.teal : color.red) : (hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)), mcTheme ? 15 : 20)
- Line 130: transp = 40 → color.new(color.lime, 40)
- Line 131: transp = 40 → color.new(color.red, 40)
- Line 132: transp = 80 → color.new(color.white, 80)
⋯// © dannylimardi-//@version=4+//@version=5strategy("Sentiment Oscillator with Backtest", "Sentiment Oscillator", overlay=false, initial_capital=100, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.08)//Inputs-msLen = input(49, type=input.integer, title="Market Sentiment Lookback Length", group="Parameters")+msLen = input.int(49, title="Market Sentiment Lookback Length", group="Parameters")-emaLen1 = input(40, type=input.integer, title="Fast EMA Length", group="Parameters")+emaLen1 = input.int(40, title="Fast EMA Length", group="Parameters")-emaLen2 = input(204, type=input.integer, title="Slow EMA Length", group="Parameters")+emaLen2 = input.int(204, title="Slow EMA Length", group="Parameters")-signalLen = input(20, type=input.integer, title="Signal Length", group="Parameters")+signalLen = input.int(20, title="Signal Length", group="Parameters")-showMs = input(false, type=input.bool, title="Show Market Sentiment Line?", group="Plots")+showMs = input.bool(false, title="Show Market Sentiment Line?", group="Plots")-showHist = input(true, type=input.bool, title="Show Momentum Histogram?", group="Plots")+showHist = input.bool(true, title="Show Momentum Histogram?", group="Plots")-showMacd = input(true, type=input.bool, title="Show MACD Line?", group="Plots")+showMacd = input.bool(true, title="Show MACD Line?", group="Plots")-showSignal = input(true, type=input.bool, title="Show Signal Line?", group="Plots")+showSignal = input.bool(true, title="Show Signal Line?", group="Plots")-showDots = input(true, type=input.bool, title="Plot Dots when MACD and Signal Line cross?", group="Plots")+showDots = input.bool(true, title="Plot Dots when MACD and Signal Line cross?", group="Plots")-showCpv = input(false, type=input.bool, title="[Show Change/Volume of Each Bar?]", group="Plots")+showCpv = input.bool(false, title="[Show Change/Volume of Each Bar?]", group="Plots")-showEma1 = input(false, type=input.bool, title="[Show Fast EMA?]", group="Plots")+showEma1 = input.bool(false, title="[Show Fast EMA?]", group="Plots")-showEma2 = input(false, type=input.bool, title="[Show Slow EMA?]", group="Plots")+showEma2 = input.bool(false, title="[Show Slow EMA?]", group="Plots")-longStrategy = input(true, type=input.bool, title="Backtest Long Strategy?", group="Long Strategy")+longStrategy = input.bool(true, title="Backtest Long Strategy?", group="Long Strategy")entryVar1 = input(title="Long Entry Variable 1",defval="Histogram", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram"], group="Long Strategy")⋯exitVar2 = input(title="Long Exit Variable 2", defval="Zero Line", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram", "Zero Line"], group="Long Strategy")-useCPV = input(false, type=input.bool, title="Use Alternate Calculation Method?", group="Other Settings", tooltip="If checked, the Market Sentiment will be the EMA of Change Per Volume of each bar, instead of the default calculation method (Price Change EMA divided by Volume EMA). The alternate method may be slightly more responsive, but will result in bigger fluctuations when there is a huge change in volume. If this method is checked, I recommend changing the Long Exit Strategy to 'Signal Line Crossing Under Zero'.")+useCPV = input.bool(false, title="Use Alternate Calculation Method?", group="Other Settings", tooltip="If checked, the Market Sentiment will be the EMA of Change Per Volume of each bar, instead of the default calculation method (Price Change EMA divided by Volume EMA). The alternate method may be slightly more responsive, but will result in bigger fluctuations when there is a huge change in volume. If this method is checked, I recommend changing the Long Exit Strategy to 'Signal Line Crossing Under Zero'.")-mcTheme = input(false, type=input.bool, title="Use Alternate Color Scheme?", group="Other Settings", tooltip="If checked, the MACD, Signal, and Histogram will all be plotted as areas and histograms")+mcTheme = input.bool(false, title="Use Alternate Color Scheme?", group="Other Settings", tooltip="If checked, the MACD, Signal, and Histogram will all be plotted as areas and histograms")//Calculations⋯changePerVolume = (priceChange/volume) * 10000000 // (The 1000000 doesn't have any significance, it's just to avoid color-change errors when the values are too small.)-priceChangeEma = ema(priceChange, msLen)+priceChangeEma = ta.ema(priceChange, msLen)-volumeEma = ema(volume, msLen)+volumeEma = ta.ema(volume, msLen)-marketSentiment = useCPV ? ema(changePerVolume, msLen) : priceChangeEma/volumeEma * 1000000000+marketSentiment = useCPV ? ta.ema(changePerVolume, msLen) : priceChangeEma/volumeEma * 1000000000-msEma1 = ema(marketSentiment, emaLen1)+msEma1 = ta.ema(marketSentiment, emaLen1)-msEma2 = ema(marketSentiment, emaLen2)+msEma2 = ta.ema(marketSentiment, emaLen2)macd = msEma1-msEma2-signal = ema(macd, signalLen)+signal = ta.ema(macd, signalLen)hist = macd-signal⋯ if entryCond == "Crossing Over"- crossover(entryVar1_, entryVar2_)+ ta.crossover(entryVar1_, entryVar2_) else if entryCond == "Crossing Under"- crossunder(entryVar1_, entryVar2_)+ ta.crossunder(entryVar1_, entryVar2_)exitCond_(exitVar1_, exitVar2_) => if exitCond == "Crossing Over"- crossover(exitVar1_, exitVar2_)+ ta.crossover(exitVar1_, exitVar2_) if exitCond == "Crossing Under"- crossunder(exitVar1_, exitVar2_)+ ta.crossunder(exitVar1_, exitVar2_)longEntry = entryCond_(entryVar1_, entryVar2_)longExit = exitCond_(exitVar1_, exitVar2_)-up = crossover(macd, signal)+up = ta.crossover(macd, signal)-down = crossunder(macd, signal)+down = ta.crossunder(macd, signal)-greenDot = up ? valuewhen(up, signal, 0) : na+greenDot = up ? ta.valuewhen(up, signal, 0) : na-redDot = down ? valuewhen(down, signal, 0) : na+redDot = down ? ta.valuewhen(down, signal, 0) : na//Plot colors⋯//Drawings-plot(showMacd ? macd : na, title="MACD", color=mcTheme ? #6f62e3 : col_macd, transp=mcTheme ? 15 : 0,+plot(showMacd ? macd : na, title="MACD", color=color.new(mcTheme ? #6f62e3 : col_macd, mcTheme ? 15 : 0), style=mcTheme ? plot.style_histogram : plot.style_line)-plot(showSignal ? signal : na, title="Signal", color=mcTheme ? (signal > 0 ? color.blue : color.orange) : col_signal, transp=mcTheme ? 85 : 0,+plot(showSignal ? signal : na, title="Signal", color=color.new(mcTheme ? (signal > 0 ? color.blue : color.orange) : col_signal, mcTheme ? 85 : 0), style=mcTheme ? plot.style_area : plot.style_line)plot(showHist ? hist : na, title="Histogram", style=mcTheme ? plot.style_columns : plot.style_area, - color=mcTheme ? (hist > 0 ? color.teal : color.red) : (hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)), - transp=mcTheme ? 15 : 20)+ color=color.new(mcTheme ? (hist > 0 ? color.teal : color.red) : (hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)), mcTheme ? 15 : 20))-plot(showDots ? greenDot : na, color=color.lime, style=plot.style_circles, linewidth=5, transp=40)+plot(showDots ? greenDot : na, color=color.new(color.lime, 40), style=plot.style_circles, linewidth=5)-plot(showDots ? redDot : na, color=color.red, style=plot.style_circles, linewidth=5, transp=40)+plot(showDots ? redDot : na, color=color.new(color.red, 40), style=plot.style_circles, linewidth=5)-plot(0, color=color.white, transp=80)+plot(0, color=color.new(color.white, 80))plot(showEma1 ? msEma1 : na, color=color.aqua)⋯Same 5 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 19: input() → input.string()
- Line 20: input() → input.string()
- Line 21: input() → input.string()
- Line 22: input() → input.string()
- Line 23: input() → input.string()
- Line 24: input() → input.string()
- Line 142: when = longEntry → if block
- Line 143: when = longExit → if block
⋯longStrategy = input.bool(true, title="Backtest Long Strategy?", group="Long Strategy")-entryVar1 = input(title="Long Entry Variable 1",defval="Histogram", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram"], group="Long Strategy")+entryVar1 = input.string(title="Long Entry Variable 1",defval="Histogram", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram"], group="Long Strategy")-entryCond = input(title="Long Entry Condition", defval="Crossing Over", options=["Crossing Over", "Crossing Under"], group="Long Strategy")+entryCond = input.string(title="Long Entry Condition", defval="Crossing Over", options=["Crossing Over", "Crossing Under"], group="Long Strategy")-entryVar2 = input(title="Long Entry Variable 2", defval="Zero Line", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram", "Zero Line"], group="Long Strategy")+entryVar2 = input.string(title="Long Entry Variable 2", defval="Zero Line", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram", "Zero Line"], group="Long Strategy")-exitVar1 = input(title="Long Exit Variable 1",defval="MACD", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram"], group="Long Strategy")+exitVar1 = input.string(title="Long Exit Variable 1",defval="MACD", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram"], group="Long Strategy")-exitCond = input(title="Long Exit Condition", defval="Crossing Under", options=["Crossing Over", "Crossing Under"], group="Long Strategy")+exitCond = input.string(title="Long Exit Condition", defval="Crossing Under", options=["Crossing Over", "Crossing Under"], group="Long Strategy")-exitVar2 = input(title="Long Exit Variable 2", defval="Zero Line", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram", "Zero Line"], group="Long Strategy")+exitVar2 = input.string(title="Long Exit Variable 2", defval="Zero Line", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram", "Zero Line"], group="Long Strategy")useCPV = input.bool(false, title="Use Alternate Calculation Method?", group="Other Settings", tooltip="If checked, the Market Sentiment will be the EMA of Change Per Volume of each bar, instead of the default calculation method (Price Change EMA divided by Volume EMA). The alternate method may be slightly more responsive, but will result in bigger fluctuations when there is a huge change in volume. If this method is checked, I recommend changing the Long Exit Strategy to 'Signal Line Crossing Under Zero'.")⋯if longStrategy- strategy.entry("Buy", strategy.long, when=longEntry)+ if longEntry+ strategy.entry("Buy", strategy.long)- strategy.close("Buy", when=longExit)+ if longExit+ strategy.close("Buy")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: 5 before, 8 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.
⋯// © dannylimardi-//@version=5+//@version=6strategy("Sentiment Oscillator with Backtest", "Sentiment Oscillator", overlay=false, initial_capital=100, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.08)⋯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 |
|---|---|---|---|
| Parameters | |||
| Market Sentiment Lookback Length | 49 | int (written input.integer) | priceChangeEma volumeEma marketSentiment |
| Fast EMA Length | 40 | int (written input.integer) | msEma1 |
| Slow EMA Length | 204 | int (written input.integer) | msEma2 |
| Signal Length | 20 | int (written input.integer) | signal |
| Plots | |||
| Show Market Sentiment Line? | false | bool | plot |
| Show Momentum Histogram? | true | bool | Histogram |
| Show MACD Line? | true | bool | MACD |
| Show Signal Line? | true | bool | Signal |
| Plot Dots when MACD and Signal Line cross? | true | bool | plot |
| [Show Change/Volume of Each Bar?] | false | bool | plot |
| [Show Fast EMA?] | false | bool | plot |
| [Show Slow EMA?] | false | bool | plot |
| Long Strategy | |||
| Backtest Long Strategy? | true | bool | entry Buy close Buy line 142 |
| Long Entry Variable 1 | Histogram | string · options ["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram"] | line 43 line 45 line 47 line 49 line 51 line 53 |
| Long Entry Condition | Crossing Over | string · options ["Crossing Over", "Crossing Under"] | line 100 line 102 |
| Long Entry Variable 2 | Zero Line | string · options ["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram", "Zero Line"] | line 56 line 58 line 60 line 62 line 64 line 66 line 68 |
| Long Exit Variable 1 | MACD | string · options ["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram"] | line 71 line 73 line 75 line 77 line 79 line 81 |
| Long Exit Condition | Crossing Under | string · options ["Crossing Over", "Crossing Under"] | line 105 line 107 |
| Long Exit Variable 2 | Zero Line | string · options ["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram", "Zero Line"] | line 84 line 86 line 88 line 90 line 92 line 94 line 96 |
| Other Settings | |||
| Use Alternate Calculation Method?If checked, the Market Sentiment will be the EMA of Change Per Volume of each bar, instead of the default calculation method (Price Change EMA divided by Volume EMA). The alternate method may be slightly more responsive, but will result in bigger fluctuations when there is a huge change in volume. If this method is checked, I recommend changing the Long Exit Strategy to 'Signal Line Crossing Under Zero'. | false | bool | marketSentiment |
| Use Alternate Color Scheme?If checked, the MACD, Signal, and Histogram will all be plotted as areas and histograms | false | bool | MACD Signal Histogram |
What it calculates
- EMA (
ema,ta.emain v5)priceChange, Market Sentiment Lookback Length (49)priceChangeEma · line 30 - EMA (
ema,ta.emain v5)volume, Market Sentiment Lookback Length (49)volumeEma · line 31 - EMA (
ema,ta.emain v5)changePerVolume, Market Sentiment Lookback Length (49)marketSentiment · line 32 - EMA (
ema,ta.emain v5)marketSentiment, Fast EMA Length (40)msEma1 · line 33 - EMA (
ema,ta.emain v5)marketSentiment, Slow EMA Length (204)msEma2 · line 34 - EMA (
ema,ta.emain v5)macd, Signal Length (20)signal · line 36 - Crossover (
crossover,ta.crossoverin v5)macd, signalup · line 111 - Crossunder (
crossunder,ta.crossunderin v5)macd, signaldown · line 112 - Valuewhen (
valuewhen,ta.valuewhenin v5)up, signal, 0greenDot · line 113 - Valuewhen (
valuewhen,ta.valuewhenin v5)down, signal, 0redDot · line 114
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 Buy long | longEntry | On “Backtest Long Strategy?” | 143 |
| Close Buy | longExit | On “Backtest Long Strategy?” | 144 |
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, and the live replay matched history.
- 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 | +1.4% | +102.5% | 5 | 40% | 1.05 | 20.6% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +33.3% | +5.5% | 5 | 60% | 13.42 | 7.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +106.4% | +9.8% | 3 | 67% | 11.09 | 30.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +50.0% | +8.4% | 5 | 80% | 11.64 | 14.7% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −35.8% | +16.7% | 5 | 20% | 0.27 | 47.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +41.2% | +21.5% | 5 | 60% | 5.87 | 10.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +4.2% | +148.3% | 3 | 67% | 1.08 | 36.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +19.2% | +17.1% | 7 | 71% | 5.84 | 4.7% | 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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 1 | 0% | −$6.82 |
| Sideways market | 2 | 50% | $21.10 |
| Falling market | 2 | 50% | −$12.82 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
21.9%
The 95th percentile of the worst drawdown when the same 5 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 11.9% to 21.9%; the tick is what actually happened, 16.0%. Order luck alone moves it this much.
All 5 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 26 Jan 2025 104,747.00 | 5 Feb 2025 97,763.10 | 10 | −$6.82 |
| long | 26 Mar 2025 87,392.90 | 25 Aug 2025 113,494.00 | 152 | $27.64 |
| long | 17 Sep 2025 116,789.00 | 12 Oct 2025 110,644.00 | 25 | −$6.54 |
| long | 7 Dec 2025 89,236.80 | 24 Jan 2026 89,600.30 | 48 | $0.28 |
| long | 3 Mar 2026 68,830.10 | 6 Jun 2026 61,056.50 | 95 | −$13.10 |
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 |
|---|---|---|---|---|---|
| Sentiment Oscillator this page | — | v4 strategy | +1.4% | 5 | 20.6% |
| MACD 50x Leveraged Short Strategy with Real Equity by Noldo | ema valuewhen | v4 strategy | +15,109.2% | 39 | 2,974.0% |
| MACD 50x Leveraged Long Strategy Results with Real Equity by Noldo | ema valuewhen | v4 strategy | +40,225.0% | 38 | 1,581.6% |
| Yesterday's High v.17.07 by tumiza999 | valuewhen ema | v5 strategy | +10.1% | 127 | 1.8% |
| PCT Trailing Stoploss-Strategy by Thumpyr | ema valuewhen | v5 strategy | No trades on this market | ||
| Initial template by trading_mentalist | ema valuewhen | v4 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 34 ms | Yes 1,000 bars in 65 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/// © dannylimardi//@version=4strategy("Sentiment Oscillator with Backtest", "Sentiment Oscillator", overlay=false, initial_capital=100, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.08)//InputsmsLen = input(49, type=input.integer, title="Market Sentiment Lookback Length", group="Parameters")emaLen1 = input(40, type=input.integer, title="Fast EMA Length", group="Parameters")emaLen2 = input(204, type=input.integer, title="Slow EMA Length", group="Parameters")signalLen = input(20, type=input.integer, title="Signal Length", group="Parameters")showMs = input(false, type=input.bool, title="Show Market Sentiment Line?", group="Plots")showHist = input(true, type=input.bool, title="Show Momentum Histogram?", group="Plots")showMacd = input(true, type=input.bool, title="Show MACD Line?", group="Plots")showSignal = input(true, type=input.bool, title="Show Signal Line?", group="Plots")showDots = input(true, type=input.bool, title="Plot Dots when MACD and Signal Line cross?", group="Plots")showCpv = input(false, type=input.bool, title="[Show Change/Volume of Each Bar?]", group="Plots")showEma1 = input(false, type=input.bool, title="[Show Fast EMA?]", group="Plots")showEma2 = input(false, type=input.bool, title="[Show Slow EMA?]", group="Plots")longStrategy = input(true, type=input.bool, title="Backtest Long Strategy?", group="Long Strategy")entryVar1 = input(title="Long Entry Variable 1",defval="Histogram", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram"], group="Long Strategy")entryCond = input(title="Long Entry Condition", defval="Crossing Over", options=["Crossing Over", "Crossing Under"], group="Long Strategy")entryVar2 = input(title="Long Entry Variable 2", defval="Zero Line", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram", "Zero Line"], group="Long Strategy")exitVar1 = input(title="Long Exit Variable 1",defval="MACD", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram"], group="Long Strategy")exitCond = input(title="Long Exit Condition", defval="Crossing Under", options=["Crossing Over", "Crossing Under"], group="Long Strategy")exitVar2 = input(title="Long Exit Variable 2", defval="Zero Line", options=["Market Sentiment", "Fast EMA", "Slow EMA", "MACD", "Signal Line", "Histogram", "Zero Line"], group="Long Strategy")useCPV = input(false, type=input.bool, title="Use Alternate Calculation Method?", group="Other Settings", tooltip="If checked, the Market Sentiment will be the EMA of Change Per Volume of each bar, instead of the default calculation method (Price Change EMA divided by Volume EMA). The alternate method may be slightly more responsive, but will result in bigger fluctuations when there is a huge change in volume. If this method is checked, I recommend changing the Long Exit Strategy to 'Signal Line Crossing Under Zero'.")mcTheme = input(false, type=input.bool, title="Use Alternate Color Scheme?", group="Other Settings", tooltip="If checked, the MACD, Signal, and Histogram will all be plotted as areas and histograms")//CalculationspriceChange = close - close[1]changePerVolume = (priceChange/volume) * 10000000 // (The 1000000 doesn't have any significance, it's just to avoid color-change errors when the values are too small.)priceChangeEma = ema(priceChange, msLen)volumeEma = ema(volume, msLen)marketSentiment = useCPV ? ema(changePerVolume, msLen) : priceChangeEma/volumeEma * 1000000000msEma1 = ema(marketSentiment, emaLen1)msEma2 = ema(marketSentiment, emaLen2)macd = msEma1-msEma2signal = ema(macd, signalLen)hist = macd-signal//Strategy Function and String Definitionsvar entryVar1_ = 0.0var entryVar2_ = 0.0var exitVar1_ = 0.0var exitVar2_ = 0.0if entryVar1 == "Market Sentiment" entryVar1_ := marketSentimentelse if entryVar1 == "Fast EMA" entryVar1_ := msEma1else if entryVar1 =="Slow EMA" entryVar1_ := msEma2else if entryVar1 == "MACD" entryVar1_ := macdelse if entryVar1 == "Signal Line" entryVar1_ := signalelse if entryVar1 == "Histogram" entryVar1_ := hist if entryVar2 == "Market Sentiment" entryVar2_ := marketSentimentelse if entryVar2 == "Fast EMA" entryVar2_ := msEma1else if entryVar2 =="Slow EMA" entryVar2_ := msEma2else if entryVar2 == "MACD" entryVar2_ := macdelse if entryVar2 == "Signal Line" entryVar2_ := signalelse if entryVar2 == "Histogram" entryVar2_ := histelse if entryVar2 == "Zero Line" entryVar2_ := 0 if exitVar1 == "Market Sentiment" exitVar1_ := marketSentimentelse if exitVar1 == "Fast EMA" exitVar1_ := msEma1else if exitVar1 =="Slow EMA" exitVar1_ := msEma2else if exitVar1 == "MACD" exitVar1_ := macdelse if exitVar1 == "Signal Line" exitVar1_ := signalelse if exitVar1 == "Histogram" exitVar1_ := hist if exitVar2 == "Market Sentiment" exitVar2_ := marketSentimentelse if exitVar2 == "Fast EMA" exitVar2_ := msEma1else if exitVar2 =="Slow EMA" exitVar2_ := msEma2else if exitVar2 == "MACD" exitVar2_ := macdelse if exitVar2 == "Signal Line" exitVar2_ := signalelse if exitVar2 == "Histogram" exitVar2_ := histelse if exitVar2 == "Zero Line" exitVar2_ := 0 entryCond_(entryVar1_, entryVar2_) => if entryCond == "Crossing Over" crossover(entryVar1_, entryVar2_) else if entryCond == "Crossing Under" crossunder(entryVar1_, entryVar2_)exitCond_(exitVar1_, exitVar2_) => if exitCond == "Crossing Over" crossover(exitVar1_, exitVar2_) if exitCond == "Crossing Under" crossunder(exitVar1_, exitVar2_)longEntry = entryCond_(entryVar1_, entryVar2_)longExit = exitCond_(exitVar1_, exitVar2_)up = crossover(macd, signal)down = crossunder(macd, signal)greenDot = up ? valuewhen(up, signal, 0) : naredDot = down ? valuewhen(down, signal, 0) : na//Plot colorscol_grow_above = #26A69Acol_grow_below = #FFCDD2col_fall_above = #B2DFDBcol_fall_below = #EF5350col_macd = #0094ffcol_signal = #ff6a00//Drawingsplot(showMacd ? macd : na, title="MACD", color=mcTheme ? #6f62e3 : col_macd, transp=mcTheme ? 15 : 0, style=mcTheme ? plot.style_histogram : plot.style_line)plot(showSignal ? signal : na, title="Signal", color=mcTheme ? (signal > 0 ? color.blue : color.orange) : col_signal, transp=mcTheme ? 85 : 0, style=mcTheme ? plot.style_area : plot.style_line)plot(showHist ? hist : na, title="Histogram", style=mcTheme ? plot.style_columns : plot.style_area, color=mcTheme ? (hist > 0 ? color.teal : color.red) : (hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)), transp=mcTheme ? 15 : 20)plot(showDots ? greenDot : na, color=color.lime, style=plot.style_circles, linewidth=5, transp=40)plot(showDots ? redDot : na, color=color.red, style=plot.style_circles, linewidth=5, transp=40)plot(0, color=color.white, transp=80)plot(showEma1 ? msEma1 : na, color=color.aqua)plot(showEma2 ? msEma2 : na, color=color.yellow)plot(showMs ? marketSentiment : na, color=color.lime)plot(showCpv ? changePerVolume : na, color=changePerVolume > changePerVolume[1] ? color.teal : color.red)// if longEntry and valuewhen(longEntry, bar_index, 1) < valuewhen(longExit, bar_index, 0)// label.new(bar_index, signal, "BUY", yloc=yloc.price, style=label.style_none, color=color.green, textcolor=color.blue)// if longExit and valuewhen(longExit, bar_index, 1) < valuewhen(longEntry, bar_index, 0)// label.new(bar_index, signal, "SELL", yloc=yloc.price, style=label.style_none, color=color.red, textcolor=color.red)//Strategyif longStrategy strategy.entry("Buy", strategy.long, when=longEntry) strategy.close("Buy", when=longExit)