Pine Script v5 · Strategy
Davin's 10-200MA Pullback on SPY Strategy v2.0
A Pine Script v5 strategy built on SMA (close, 200), EMA (close, 9) and Highest (high, 52). 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 10% of equity and pays 0.1 commission. 10 settings; defaults include MA Length 1 200, MA Length 2 10 and MA Length 3 50.
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 Davin's 10-200MA Pullback on SPY Strategy v2.0 repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 9 of 9 trades.
- How did Davin's 10-200MA Pullback on SPY Strategy v2.0 do on BTCUSDT 1D with default settings?
- +6.52%
- From 26 Jan 2024 to 21 Oct 2026, over 32 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Davin's 10-200MA Pullback on SPY Strategy v2.0 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: 32 before, 32 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does Davin's 10-200MA Pullback on SPY Strategy v2.0 use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Davin's 10-200MA Pullback on SPY Strategy v2.0 run in Zeal Terminal?
- Yes
- It compiles in 19 ms and runs 1,000 bars in 45 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +6.52%
- $652.44
- Buy and hold
- +102.5%
- same period
- Closed trades
- 32
- 22 won · 10 lost
- Profitable
- 68.8%
- of closed trades
- Profit factor
- 1.97
- gross profit ÷ gross loss
- Max drawdown
- 4.79%
- −$523.83
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.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Strategy Parameters | |||
| MA Length 1Long-term MA 200 | 200 | int · step 10 | ma1 |
| MA Length 2Short-term MA 10 | 10 | int · step 10 | ma2 |
| MA Length 3MA for crossover signals` | 50 | int · step 1 | ma3 |
| Stop Loss PercentHard stop loss of 10% | 0.15 | float · step 0.01 | stopPrice stopCondition |
| Exit on lower closeWait for lower close after above 10SMA before exiting | true | bool | sellCondition |
| Buy whenever more than x% drawdownBuy the dip! Whenever x% or more drawdown on SPY | true | bool | buyCondition |
| Trigger % drop to buy the dip% drop to trigger contrarian Buy the Dip! | 14 | int · step 1 | buyCondition |
| Include stop condition using MA crossoverSell when crossover of MA2/1 happens | false | bool | stopCondition |
| Time filter | |||
| Start filterStart date and time to begin | timestamp("01 Jan 2013 13:30 +0000") | time | f_dateFilter |
| End filterEnd date and time to stop | timestamp("01 Jan 2099 19:30 +0000") | time | f_dateFilter |
What it calculates
- SMAclose, MA Length 1 (200)
ma1 · line 34 - SMAclose, MA Length 2 (10)
ma2 · line 35 - SMAclose, MA Length 3 (50)
ma3 · line 36 - EMAclose, 9
ma_9 · line 37 - EMAclose, 20
ma_20 · line 38 - Highesthigh, 52
highest52 · line 42
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 Long long | buyCondition | On | 54 |
| Close Long | sellCondition or stopCondition | On | 60 |
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 | +6.5% | +102.5% | 32 | 69% | 1.97 | 4.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +2.8% | +5.5% | 22 | 59% | 2.55 | 1.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.2% | +9.8% | 41 | 56% | 0.99 | 8.9% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +3.1% | +8.4% | 12 | 75% | 2.38 | 1.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −1.5% | +16.7% | 42 | 48% | 0.95 | 11.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +4.1% | +21.5% | 17 | 53% | 2.17 | 1.9% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +3.1% | +148.3% | 31 | 58% | 1.24 | 6.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.5% | +17.1% | 17 | 53% | 1.29 | 1.2% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Settings heatmap
Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (MA Length 1 200, MA Length 2 10) made +7.6% before 26 Dec 2025 and −1.1% after it, against −1.1% for the defaults. 100% of the 64 combinations made money in-sample and 50% out-of-sample.
| MA Length 2 ↓ · MA Length 1 → | 100 | 120 | 140 | 170 | 200 | 240 | 280 | 340 |
|---|---|---|---|---|---|---|---|---|
| 5 | +5% | +5% | +5% | +5% | +5% | +5% | +5% | +4% |
| 6 | +4% | +4% | +4% | +5% | +5% | +6% | +6% | +3% |
| 7 | +3% | +2% | +3% | +4% | +4% | +4% | +4% | +1% |
| 9 | +2% | +2% | +2% | +3% | +4% | +4% | +4% | +1% |
| 10 | +6% | +5% | +5% | +6% | +8% | +8% | +7% | +4% |
| 12 | +5% | +5% | +5% | +6% | +7% | +7% | +7% | +4% |
| 14 | +4% | +5% | +5% | +5% | +7% | +7% | +6% | +3% |
| 17 | +4% | +4% | +4% | +4% | +5% | +5% | +5% | +2% |
| MA Length 2 ↓ · MA Length 1 → | 100 | 120 | 140 | 170 | 200 | 240 | 280 | 340 |
|---|---|---|---|---|---|---|---|---|
| 5 | +1% | +1% | +1% | +1% | +1% | +1% | +1% | +1% |
| 6 | +1% | +1% | +1% | +1% | +3% | +3% | +3% | +3% |
| 7 | +1% | +1% | +1% | +1% | +3% | +3% | +3% | +3% |
| 9 | +1% | +1% | +1% | +1% | +2% | +2% | +2% | +1% |
| 10 | −2% | −2% | −2% | −2% | −1% | −1% | −1% | −2% |
| 12 | −1% | −1% | −2% | −2% | −1% | −1% | −1% | −2% |
| 14 | −1% | −1% | −2% | −2% | −1% | −1% | −1% | −2% |
| 17 | −2% | −2% | −2% | −2% | −1% | −1% | −1% | −2% |
Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. 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 | 11 | 82% | $489.52 |
| Sideways market | 17 | 65% | $302.28 |
| Falling market | 4 | 50% | −$139.36 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
4.3%
The 95th percentile of the worst drawdown when the same 32 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 2.0% to 4.3%; the tick is what actually happened, 4.1%. Order luck alone moves it this much.
All 32 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 20 Mar 2024 61,937.40 | 8 May 2024 62,312.10 | 49 | $6.05 |
| long | 9 May 2024 61,193.00 | 20 May 2024 66,274.00 | 11 | $83.08 |
| long | 25 Jun 2024 60,293.30 | 3 Jul 2024 62,135.50 | 8 | $30.82 |
| long | 4 Jul 2024 60,208.60 | 24 Jul 2024 65,936.00 | 20 | $96.26 |
| long | 5 Aug 2024 58,161.00 | 27 Aug 2024 62,834.00 | 22 | $82.08 |
| long | 28 Aug 2024 59,415.00 | 16 Sep 2024 59,132.00 | 19 | −$4.91 |
| long | 17 Sep 2024 58,214.00 | 22 Oct 2024 67,377.50 | 35 | $162.03 |
| long | 24 Oct 2024 66,668.60 | 1 Nov 2024 70,292.00 | 8 | $56.82 |
| long | 3 Nov 2024 69,374.70 | 18 Nov 2024 89,856.00 | 15 | $310.35 |
| long | 26 Nov 2024 93,010.00 | 8 Jan 2025 96,954.60 | 43 | $45.90 |
| long | 9 Jan 2025 95,060.60 | 20 Jan 2025 101,332.00 | 11 | $71.69 |
| long | 27 Jan 2025 102,620.00 | 26 Feb 2025 88,680.40 | 30 | −$148.61 |
| long | 27 Feb 2025 84,250.10 | 16 Apr 2025 83,644.00 | 48 | −$7.77 |
| long | 5 May 2025 94,277.60 | 13 May 2025 102,791.00 | 8 | $97.38 |
| long | 18 May 2025 103,127.00 | 24 May 2025 107,318.00 | 6 | $44.23 |
| long | 29 May 2025 107,782.00 | 1 Jul 2025 107,147.00 | 33 | −$6.44 |
| long | 2 Jul 2025 105,681.00 | 5 Jul 2025 107,984.00 | 3 | $23.79 |
| long | 20 Jul 2025 117,840.00 | 15 Aug 2025 118,295.00 | 26 | $4.22 |
| long | 16 Aug 2025 117,342.00 | 7 Sep 2025 110,188.00 | 22 | −$66.74 |
| long | 20 Sep 2025 115,632.00 | 8 Oct 2025 121,333.00 | 18 | $53.64 |
| long | 11 Oct 2025 112,774.00 | 29 Oct 2025 112,898.00 | 18 | $1.20 |
| long | 30 Oct 2025 110,021.00 | 15 Nov 2025 94,594.00 | 16 | −$153.34 |
| long | 16 Nov 2025 95,596.20 | 19 Jan 2026 93,673.10 | 64 | −$21.69 |
| long | 1 Feb 2026 78,741.10 | 6 Feb 2026 62,909.90 | 5 | −$216.34 |
| long | 7 Feb 2026 70,580.30 | 13 Apr 2026 70,741.60 | 65 | $2.41 |
| long | 3 Jun 2026 66,760.80 | 18 Jun 2026 64,509.40 | 15 | −$35.57 |
| long | 19 Jun 2026 62,958.00 | 9 Jul 2026 62,290.00 | 20 | −$11.16 |
| long | 10 Jul 2026 63,230.00 | 13 Jul 2026 63,780.00 | 3 | $9.13 |
| long | 14 Jul 2026 62,334.50 | 17 Jul 2026 63,830.20 | 3 | $25.21 |
| long | 18 Jul 2026 63,931.70 | 24 Jul 2026 65,099.00 | 6 | $19.23 |
| long | 31 Aug 2026 77,682.00 | 8 Sep 2026 79,112.00 | 8 | $19.42 |
| long | 9 Sep 2026 78,455.80 | 24 Sep 2026 84,397.60 | 15 | $80.07 |
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 |
|---|---|---|---|---|---|
| Davin's 10-200MA Pullback on SPY Strategy v2.0 this page | — | v5 strategy | +6.5% | 32 | 4.8% |
| Slight Swing Momentum Strategy. by fzj20020403 | sma highest ema | v5 strategy | +3.1% | 7 | 0.6% |
| Forex bot full strategy with risk management by exlux99 | highest ema sma | v4 strategy | −26.4% | 11 | 38.9% |
| LowFinder_PyraMider_V2 by A3Sh | ema sma highest | v5 strategy | +1.9% | 15 | 0.8% |
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
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: 32 before, 32 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.
⋯//Recommended tickers: SPY (D), QQQ (D) and big indexes, AAPL (4H)-//@version=5+//@version=6strategy("Davin's 10/200MA Pullback on SPY Strategy v2.0",⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 19 ms | With a workaround 1,000 bars in 45 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
Zeal Terminal's engine currently reads an order side written as direction = strategy.long as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 54) 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.
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/// © Gold_D_Roger//note: spreading 1 statement over multiple lines needs 1 apce + 1 tab | multi line function is 1 tab//Recommended tickers: SPY (D), QQQ (D) and big indexes, AAPL (4H)//@version=5strategy("Davin's 10/200MA Pullback on SPY Strategy v2.0", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, // 10% of equity on each trade commission_type=strategy.commission.cash_per_contract, commission_value=0.1) //Insert your broker's rate, IB is 0.005USD or tiered//Best parameters// SPY D// Stop loss 0.15// commission of 0.005 USD using Interactive brokers// Exit on lower close // Buy more when x% down --> 14%// DO NOT include stop condition using MA crossover// Get User Inputi_ma1 = input.int(title="MA Length 1", defval=200, step=10, group="Strategy Parameters", tooltip="Long-term MA 200")i_ma2 = input.int(title="MA Length 2", defval=10, step=10, group="Strategy Parameters", tooltip="Short-term MA 10")i_ma3 = input.int(title="MA Length 3", defval=50, step=1, group="Strategy Parameters", tooltip="MA for crossover signals`")i_stopPercent = input.float(title="Stop Loss Percent", defval=0.15, step=0.01, group="Strategy Parameters", tooltip="Hard stop loss of 10%")i_startTime = input.time(title="Start filter", defval=timestamp("01 Jan 2013 13:30 +0000"), group="Time filter", tooltip="Start date and time to begin")i_endTime = input.time(title="End filter", defval=timestamp("01 Jan 2099 19:30 +0000"), group="Time filter", tooltip="End date and time to stop")i_lowerClose = input.bool(title="Exit on lower close", defval=true, group="Strategy Parameters", tooltip="Wait for lower close after above 10SMA before exiting") // optimise exit strat, boolean type creates tickbox type inputsi_contrarianBuyTheDip = input.bool(title="Buy whenever more than x% drawdown", defval=true, group="Strategy Parameters", tooltip="Buy the dip! Whenever x% or more drawdown on SPY")i_contrarianTrigger = input.int(title="Trigger % drop to buy the dip", defval=14, step=1, group="Strategy Parameters", tooltip="% drop to trigger contrarian Buy the Dip!") //14% to be best for SPY 1D//20% best for AMZN 1Di_stopByCrossover_MA2_3 = input.bool(title="Include stop condition using MA crossover", defval=false, group="Strategy Parameters", tooltip="Sell when crossover of MA2/1 happens")// Get indicator valuesma1 = ta.sma(close,i_ma1) //param 1ma2 = ta.sma(close,i_ma2) //param 2ma3 = ta.sma(close,i_ma3) //param 3ma_9 = ta.ema(close,9) //param 2ma_20 = ta.ema(close,20) //param 3// Check filter(s)f_dateFilter = time >+ i_startTime and time <= i_endTime //make sure date entries are within acceptable range// Highest price of the prev 52 days: https://www.tradingcode.net/tradingview/largest-maximum-value/#:~:text=()%20versus%20ta.-,highest(),max()%20and%20ta.highest52 = ta.highest(high,52)overall_change = ((highest52 - close[0]) / highest52) * 100// Check buy/sell conditionsvar float buyPrice = 0 //intialise buyPrice, this will change when we enter a trade ; float = decimal number data type 0.0buyCondition = (close > ma1 and close < ma2 and strategy.position_size == 0 and f_dateFilter) or (strategy.position_size == 0 and i_contrarianBuyTheDip==true and overall_change > i_contrarianTrigger and f_dateFilter) // higher than 200sma, lower than short term ma (pullback) + avoid pyramiding positionssellCondition = close > ma2 and strategy.position_size > 0 and (not i_lowerClose or close < low[1]) //check if we already in trade + close above 10MA; // third condition: EITHER i_lowerClose not turned on OR closing price has to be < previous candle's LOW [1]stopDistance = strategy.position_size > 0 ? ((buyPrice - close)/close) : na // check if in trade > calc % drop dist from entry, if not nastopPrice = strategy.position_size > 0 ? (buyPrice - (buyPrice * i_stopPercent)) : na // calc SL price if in trade, if not, nastopCondition = (strategy.position_size > 0 and stopDistance > i_stopPercent) or (strategy.position_size > 0 and (i_stopByCrossover_MA2_3==true and ma3 < ma1))// Enter positionsif buyCondition strategy.entry(id="Long", direction=strategy.long) //long only if buyCondition[1] // if buyCondition is true prev candle buyPrice := open // entry price = current bar opening price// Exit positionif sellCondition or stopCondition strategy.close(id="Long", comment = "Exit" + (stopCondition ? "Stop loss=true" : "")) // if condition? "Value for true" : "value for false" buyPrice := na //reset buyPrice// Plotplot(buyPrice, color=color.lime, style=plot.style_linebr)plot(stopPrice, color=color.red, style=plot.style_linebr, offset = -1)plot(ma1, color=color.blue) //defval=200plot(ma2, color=color.white) //defval=10plot(ma3, color=color.yellow) // defval=50