Pine Script v4 · Strategy
Chirag Strategy SMA with StopLoss
A Pine Script v4 strategy built on Crossover or SMA (close, 10, close, 13). It has 6 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade is one contract and pays no commission. 2 settings; defaults include Long Stop Loss (%) 1 and Short Stop Loss (%) 1.
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 Chirag Strategy SMA with StopLoss repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 34 of 34 trades.
- How did Chirag Strategy SMA with StopLoss do on BTCUSDT 1D with default settings?
- −19.36%
- From 26 Jan 2024 to 21 Oct 2026, over 91 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Chirag Strategy SMA with StopLoss 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 Chirag Strategy SMA with StopLoss use a stop loss?
- Yes
- strategy.exit sets a stop on line 23, 25.
- Does Chirag Strategy SMA with StopLoss run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 25 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Long StopLoss
- Short StopLoss
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −19.36%
- −$19,360.40
- Buy and hold
- +102.5%
- same period
- Closed trades
- 91
- 14 won · 77 lost
- Profitable
- 15.4%
- of closed trades
- Profit factor
- 0.80
- gross profit ÷ gross loss
- Max drawdown
- 42.76%
- −$48,201.30
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
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 sma → ta.sma, crossover → ta.crossover.
Same 91 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 5: sma → ta.sma
- Line 6: sma → ta.sma
- Line 7: crossover → ta.crossover
- Line 7: sma → ta.sma
- Line 7: sma → ta.sma
5 more edits
- Line 8: crossover → ta.crossover
- Line 8: sma → ta.sma
- Line 8: sma → ta.sma
- Line 10: input(type = input.float) → input.float()
- Line 11: input(type = input.float) → input.float()
⋯// © chiragchopra91-//@version=4+//@version=5strategy(title='Chirag Strategy SMA', shorttitle='CHIRAGSMA', overlay=true)-plot(sma(close,10), color=color.green, linewidth=2)+plot(ta.sma(close,10), color=color.green, linewidth=2)-plot(sma(close,13), color=color.red, linewidth=2)+plot(ta.sma(close,13), color=color.red, linewidth=2)-longCondition = crossover(sma(close, 10), sma(close, 13))+longCondition = ta.crossover(ta.sma(close, 10), ta.sma(close, 13))-shortCondition = crossover(sma(close, 13), sma(close, 10))+shortCondition = ta.crossover(ta.sma(close, 13), ta.sma(close, 10))// Set stop loss level with input options-longLossPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1) * 0.01+longLossPerc = input.float(title="Long Stop Loss (%)", minval=0.0, step=0.1, defval=1) * 0.01-shortLossPerc = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1) * 0.01+shortLossPerc = input.float(title="Short Stop Loss (%)", minval=0.0, step=0.1, defval=1) * 0.01longStopPrice = strategy.position_avg_price * (1 - longLossPerc)⋯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: 91 before, 78 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.
⋯// © chiragchopra91-//@version=5+//@version=6strategy(title='Chirag Strategy SMA', shorttitle='CHIRAGSMA', overlay=true)⋯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 |
|---|---|---|---|
| Long Stop Loss (%) | 1 | float · min 0.0 · step 0.1 | longStopPrice |
| Short Stop Loss (%) | 1 | float · min 0.0 · step 0.1 | shortStopPrice |
What it calculates
- Crossover / SMA (
crossover,ta.crossoverin v5)sma(close, 10), sma(close, 13)longCondition · line 7 - Crossover / SMA (
crossover,ta.crossoverin v5)sma(close, 13), sma(close, 10)shortCondition · line 8
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 |
|---|---|---|---|
| Close Short | strategy.position_size < 0 | On | 16 |
| Open Long long | longCondition | On | 17 |
| Close Long | strategy.position_size > 0 | On | 20 |
| Open Short short | shortCondition | On | 21 |
| Exit LONG SL stop | strategy.position_size > 0 | On | 23 |
| Exit SHORT SL stop | strategy.position_size < 0 | On | 25 |
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 | −19.4% | +102.5% | 91 | 15% | 0.80 | 42.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +7.6% | +5.5% | 95 | 27% | 1.18 | 11.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.1% | +9.8% | 112 | 15% | 1.02 | 1.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.3% | +8.4% | 101 | 26% | 0.80 | 0.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.1% | +16.7% | 102 | 15% | 0.73 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 99 | 23% | 0.65 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.2% | +148.3% | 104 | 21% | 0.77 | 0.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 93 | 29% | 1.01 | 0.1% | 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 | 33 | 9% | −$27,078.70 |
| Sideways market | 29 | 24% | $14,521.10 |
| Falling market | 29 | 14% | −$6,802.71 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
48.8%
The 95th percentile of the worst drawdown when the same 91 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 27.3% to 48.8%; the tick is what actually happened, 42.4%. Order luck alone moves it this much.
All 91 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 22 Mar 2024 65,501.30 | 24 Mar 2024 66,156.30 | 2 | −$655.02 |
| long | 30 Mar 2024 69,850.50 | 1 Apr 2024 69,152.00 | 2 | −$698.51 |
| short | 6 Apr 2024 67,820.60 | 7 Apr 2024 68,896.00 | 1 | −$1,075.37 |
| long | 13 Apr 2024 67,116.50 | 14 Apr 2024 63,924.50 | 1 | −$3,192.00 |
| short | 17 Apr 2024 63,793.40 | 19 Apr 2024 64,431.30 | 2 | −$637.94 |
| long | 26 Apr 2024 64,498.30 | 27 Apr 2024 63,770.00 | 1 | −$728.33 |
| short | 2 May 2024 58,365.00 | 3 May 2024 59,060.60 | 1 | −$695.63 |
| long | 12 May 2024 60,826.00 | 16 May 2024 66,206.50 | 4 | $5,380.52 |
| short | 16 May 2024 66,206.50 | 17 May 2024 66,868.60 | 1 | −$662.07 |
| long | 19 May 2024 66,915.20 | 20 May 2024 66,246.00 | 1 | −$669.16 |
| short | 1 Jun 2024 67,540.00 | 2 Jun 2024 68,215.40 | 1 | −$675.41 |
| long | 8 Jun 2024 69,355.60 | 11 Jun 2024 68,662.00 | 3 | −$693.56 |
| short | 15 Jun 2024 66,044.00 | 16 Jun 2024 66,704.40 | 1 | −$660.44 |
| long | 16 Jul 2024 64,724.10 | 17 Jul 2024 64,076.80 | 1 | −$647.25 |
| short | 1 Aug 2024 64,628.00 | 2 Aug 2024 65,354.00 | 1 | −$726.01 |
| long | 16 Aug 2024 57,541.10 | 20 Aug 2024 59,438.50 | 4 | $1,897.45 |
| short | 20 Aug 2024 59,438.50 | 21 Aug 2024 60,032.90 | 1 | −$594.39 |
| long | 24 Aug 2024 64,037.20 | 26 Aug 2024 63,396.90 | 2 | −$640.38 |
| short | 3 Sep 2024 59,132.10 | 13 Sep 2024 59,723.40 | 10 | −$591.33 |
| long | 16 Sep 2024 59,132.00 | 17 Sep 2024 58,214.00 | 1 | −$918.01 |
| short | 5 Oct 2024 62,086.00 | 6 Oct 2024 62,706.90 | 1 | −$620.86 |
| long | 13 Oct 2024 63,206.20 | 14 Oct 2024 62,574.20 | 1 | −$632.07 |
| short | 3 Dec 2024 95,840.60 | 4 Dec 2024 96,799.00 | 1 | −$958.41 |
| long | 7 Dec 2024 99,740.80 | 8 Dec 2024 98,743.40 | 1 | −$997.41 |
| short | 24 Dec 2024 94,881.50 | 25 Dec 2024 98,663.60 | 1 | −$3,782.11 |
| long | 8 Jan 2025 96,954.60 | 9 Jan 2025 95,060.60 | 1 | −$1,893.99 |
| short | 14 Jan 2025 94,536.10 | 15 Jan 2025 96,560.90 | 1 | −$2,024.74 |
| long | 20 Jan 2025 101,332.00 | 21 Jan 2025 100,318.00 | 1 | −$1,013.32 |
| short | 2 Feb 2025 100,636.00 | 3 Feb 2025 101,642.00 | 1 | −$1,006.36 |
| long | 18 Feb 2025 95,780.00 | 23 Feb 2025 96,551.00 | 5 | $771.00 |
| short | 23 Feb 2025 96,551.00 | 10 Mar 2025 80,734.50 | 15 | $15,816.50 |
| long | 10 Mar 2025 80,734.50 | 11 Mar 2025 78,595.90 | 1 | −$2,138.62 |
| short | 13 Mar 2025 83,680.10 | 14 Mar 2025 84,516.90 | 1 | −$836.81 |
| long | 21 Mar 2025 84,223.40 | 29 Mar 2025 83,381.10 | 8 | −$842.24 |
| short | 4 Apr 2025 83,213.10 | 5 Apr 2025 84,045.20 | 1 | −$832.14 |
| long | 17 Apr 2025 84,030.40 | 2 Jun 2025 105,643.00 | 46 | $21,612.50 |
| short | 2 Jun 2025 105,643.00 | 3 Jun 2025 106,699.00 | 1 | −$1,056.43 |
| long | 11 Jun 2025 110,274.00 | 12 Jun 2025 108,645.00 | 1 | −$1,629.26 |
| short | 20 Jun 2025 104,659.00 | 23 Jun 2025 105,705.00 | 3 | −$1,046.59 |
| long | 30 Jun 2025 108,357.00 | 1 Jul 2025 107,147.00 | 1 | −$1,210.42 |
| short | 25 Jul 2025 118,341.00 | 27 Jul 2025 119,524.00 | 2 | −$1,183.41 |
| long | 31 Jul 2025 117,840.00 | 1 Aug 2025 115,764.00 | 1 | −$2,076.22 |
| short | 2 Aug 2025 113,298.00 | 3 Aug 2025 114,431.00 | 1 | −$1,132.98 |
| long | 12 Aug 2025 118,686.00 | 14 Aug 2025 117,499.00 | 2 | −$1,186.86 |
| short | 22 Aug 2025 112,500.00 | 23 Aug 2025 116,936.00 | 1 | −$4,436.00 |
| long | 10 Sep 2025 111,546.00 | 24 Sep 2025 111,999.00 | 14 | $452.42 |
| short | 24 Sep 2025 111,999.00 | 25 Sep 2025 113,307.00 | 1 | −$1,308.21 |
| long | 4 Oct 2025 122,232.00 | 7 Oct 2025 121,010.00 | 3 | −$1,222.33 |
| short | 14 Oct 2025 115,166.00 | 27 Oct 2025 116,318.00 | 13 | −$1,151.66 |
| long | 28 Oct 2025 114,108.00 | 29 Oct 2025 112,898.00 | 1 | −$1,209.21 |
| short | 5 Nov 2025 101,497.00 | 6 Nov 2025 103,885.00 | 1 | −$2,387.93 |
| long | 2 Dec 2025 86,286.00 | 9 Dec 2025 90,634.40 | 7 | $4,348.34 |
| short | 9 Dec 2025 90,634.40 | 10 Dec 2025 92,678.80 | 1 | −$2,044.46 |
| long | 11 Dec 2025 92,015.40 | 12 Dec 2025 91,095.20 | 1 | −$920.16 |
| short | 15 Dec 2025 88,172.20 | 17 Dec 2025 89,053.90 | 2 | −$881.73 |
| long | 18 Dec 2025 86,243.20 | 19 Dec 2025 85,516.40 | 1 | −$726.82 |
| short | 19 Dec 2025 85,516.40 | 20 Dec 2025 88,136.90 | 1 | −$2,620.54 |
| long | 27 Dec 2025 87,369.60 | 1 Jan 2026 87,648.20 | 5 | $278.65 |
| short | 1 Jan 2026 87,648.20 | 2 Jan 2026 88,839.10 | 1 | −$1,190.84 |
| long | 4 Jan 2026 90,628.00 | 8 Jan 2026 89,721.70 | 4 | −$906.29 |
| short | 17 Jan 2026 95,550.90 | 18 Jan 2026 95,147.80 | 1 | $403.17 |
| long | 18 Jan 2026 95,147.80 | 19 Jan 2026 93,673.10 | 1 | −$1,474.63 |
| short | 25 Jan 2026 89,225.30 | 28 Jan 2026 90,117.60 | 3 | −$892.26 |
| long | 18 Feb 2026 67,503.50 | 19 Feb 2026 66,461.00 | 1 | −$1,042.52 |
| short | 19 Feb 2026 66,461.00 | 20 Feb 2026 67,125.60 | 1 | −$664.61 |
| long | 23 Feb 2026 67,643.40 | 24 Feb 2026 64,656.00 | 1 | −$2,987.38 |
| short | 25 Feb 2026 64,058.20 | 26 Feb 2026 67,988.00 | 1 | −$3,929.89 |
| long | 6 Mar 2026 70,891.00 | 7 Mar 2026 68,114.00 | 1 | −$2,777.00 |
| short | 15 Mar 2026 71,211.90 | 16 Mar 2026 72,815.30 | 1 | −$1,603.30 |
| long | 18 Mar 2026 73,909.40 | 19 Mar 2026 71,246.60 | 1 | −$2,662.82 |
| short | 26 Mar 2026 71,336.50 | 7 Apr 2026 72,049.90 | 12 | −$713.37 |
| long | 8 Apr 2026 71,924.20 | 9 Apr 2026 71,069.90 | 1 | −$854.29 |
| short | 4 May 2026 78,568.60 | 5 May 2026 79,861.00 | 1 | −$1,292.43 |
| long | 6 May 2026 80,905.50 | 7 May 2026 80,096.50 | 1 | −$809.06 |
| short | 17 May 2026 78,148.10 | 16 Jun 2026 66,328.70 | 30 | $11,819.30 |
| long | 16 Jun 2026 66,328.70 | 17 Jun 2026 65,665.40 | 1 | −$663.29 |
| short | 25 Jun 2026 61,078.00 | 2 Jul 2026 61,688.80 | 7 | −$610.78 |
| long | 7 Jul 2026 64,042.90 | 8 Jul 2026 63,364.00 | 1 | −$678.93 |
| short | 31 Jul 2026 64,780.00 | 9 Aug 2026 64,962.60 | 9 | −$182.57 |
| long | 9 Aug 2026 64,962.60 | 10 Aug 2026 64,901.60 | 1 | −$61.01 |
| short | 10 Aug 2026 64,901.60 | 11 Aug 2026 63,970.00 | 1 | $931.58 |
| long | 11 Aug 2026 63,970.00 | 12 Aug 2026 63,330.30 | 1 | −$639.71 |
| short | 16 Aug 2026 63,086.00 | 17 Aug 2026 63,716.90 | 1 | −$630.87 |
| long | 21 Aug 2026 73,027.00 | 6 Sep 2026 79,831.80 | 16 | $6,804.73 |
| short | 6 Sep 2026 79,831.80 | 9 Sep 2026 78,455.80 | 3 | $1,375.95 |
| long | 9 Sep 2026 78,455.80 | 10 Sep 2026 77,671.20 | 1 | −$784.56 |
| short | 14 Sep 2026 76,842.00 | 15 Sep 2026 78,189.20 | 1 | −$1,347.19 |
| long | 21 Sep 2026 81,178.00 | 3 Oct 2026 84,518.00 | 12 | $3,339.99 |
| short | 3 Oct 2026 84,518.00 | 4 Oct 2026 85,363.20 | 1 | −$845.18 |
| long | 6 Oct 2026 85,766.90 | 7 Oct 2026 84,909.20 | 1 | −$857.67 |
| short | 13 Oct 2026 81,850.00 | 14 Oct 2026 83,169.00 | 1 | −$1,319.00 |
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 |
|---|---|---|---|---|---|
| Chirag Strategy SMA with StopLoss this page | — | v4 strategy | −19.4% | 91 | 42.8% |
| Follow the Crypto Shorts by xtradernet | sma | v4 strategy | No trades on this market | ||
| buy down and sell high trend 200 sma and ma cross by willi130503 | sma | v4 strategy | No trades on this market | ||
| Mayer Multiple Strategy by xtradernet | sma | v4 strategy | No trades on this market | ||
| Moving Average Cross Strategy early Closing by Geduldtrader | sma | v4 strategy | +73.8% | 20 | 151.5% |
| MFI Simple Strategy by OztheWoz | sma | v5 strategy | −0.2% | 28 | 3.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | Yes 1,000 bars in 25 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/// © chiragchopra91//@version=4strategy(title='Chirag Strategy SMA', shorttitle='CHIRAGSMA', overlay=true)plot(sma(close,10), color=color.green, linewidth=2)plot(sma(close,13), color=color.red, linewidth=2)longCondition = crossover(sma(close, 10), sma(close, 13))shortCondition = crossover(sma(close, 13), sma(close, 10))// Set stop loss level with input optionslongLossPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1) * 0.01shortLossPerc = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1) * 0.01longStopPrice = strategy.position_avg_price * (1 - longLossPerc)shortStopPrice = strategy.position_avg_price * (1 + shortLossPerc)if longCondition if strategy.position_size < 0 strategy.close('Short', comment="SHORT EXIT") strategy.entry('Long', strategy.long, comment="BUY")if shortCondition if strategy.position_size > 0 strategy.close('Long', comment="BUY EXIT") strategy.entry('Short', strategy.short, comment="SHORT")if strategy.position_size > 0 strategy.exit('LONG SL', stop=longStopPrice, comment="LONG SL EXIT")if strategy.position_size < 0 strategy.exit('SHORT SL', stop=shortStopPrice, comment="SHORT SL EXIT") plot(longStopPrice, title="Long StopLoss", color=color.red)plot(shortStopPrice, title="Short StopLoss", color=color.red)