Pine Script v5 · Strategy
MFI Simple Strategy
A Pine Script v5 strategy built on SMA or MFI (close, len, 18) and SMA (close, 200). It has 3 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss. Each trade is one contract and pays no commission. 1 setting; defaults include Length 18.
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 MFI Simple Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 2 of 2 trades.
- How did MFI Simple Strategy do on BTCUSDT 1D with default settings?
- −0.21%
- From 26 Jan 2024 to 21 Oct 2026, over 28 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does MFI Simple Strategy 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: 28 before, 27 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 MFI Simple Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 12.
- Does MFI Simple Strategy run in Zeal Terminal?
- Yes
- It compiles in 11 ms and runs 1,000 bars in 36 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −0.21%
- −$2,132.46
- Buy and hold
- +102.5%
- same period
- Closed trades
- 28
- 10 won · 18 lost
- Profitable
- 35.7%
- of closed trades
- Profit factor
- 0.96
- gross profit ÷ gross loss
- Max drawdown
- 3.23%
- −$33,011.80
One run with the author's default settings, $1,000,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.
-
Likely bug
ta.sma(close, 200) ignores the settings line 9
Line 9 hard-codes 200 where line 6 uses “Length”. Changing those settings moves one band but not this one.
ta.sma(close, 200)ta.sma(close, len) -
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.
-
Note
Uses an argument v6 removed
when = on line 11, 13. The upgraded version below moves each call inside an if block.
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 |
|---|---|---|---|
| Length | 18 | int | mfi |
What it calculates
- SMA / MFIta.mfi(close, len), Length (18)
mfi · line 6 - SMAclose, 200
goldsma · line 9
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 | mfiup(mfi[1] < mfi) and (mfi[2] < mfi[1]) | On | 11 |
| Exit Long stop | every bar | On | 12 |
| Close Long | mfidn(mfi[1] > mfi) and (mfi[2] < mfi[1]) | On | 13 |
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 | −0.2% | +102.5% | 28 | 36% | 0.96 | 3.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.4% | +5.5% | 35 | 29% | 1.26 | 0.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.0% | +9.8% | 13 | 31% | 0.83 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.1% | +8.4% | 27 | 48% | 2.37 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 20 | 40% | 1.41 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 33 | 39% | 2.30 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.0% | +148.3% | 45 | 29% | 0.86 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 37 | 22% | 1.62 | 0.0% | 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 | 12 | 58% | $20,061.10 |
| Sideways market | 15 | 20% | −$21,728.70 |
| Falling market | 1 | 0% | −$464.82 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.8%
The 95th percentile of the worst drawdown when the same 28 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.9% to 3.8%; the tick is what actually happened, 3.2%. Order luck alone moves it this much.
All 28 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 24 Aug 2024 64,037.20 | 26 Aug 2024 63,324.00 | 2 | −$713.23 |
| long | 25 Sep 2024 64,262.70 | 25 Sep 2024 63,904.50 | 0 | −$358.16 |
| long | 27 Sep 2024 65,174.00 | 30 Sep 2024 63,763.00 | 3 | −$1,411.02 |
| long | 21 Oct 2024 69,032.00 | 22 Oct 2024 67,377.50 | 1 | −$1,654.50 |
| long | 27 Oct 2024 67,092.80 | 4 Nov 2024 68,776.00 | 8 | $1,683.23 |
| long | 10 Nov 2024 76,677.50 | 16 Nov 2024 91,032.10 | 6 | $14,354.60 |
| long | 20 Nov 2024 92,310.80 | 27 Nov 2024 91,965.20 | 7 | −$345.64 |
| long | 30 Dec 2024 93,738.20 | 4 Jan 2025 98,174.20 | 5 | $4,435.98 |
| long | 12 Jan 2025 94,600.00 | 13 Jan 2025 94,545.10 | 1 | −$54.92 |
| long | 16 Jan 2025 100,497.00 | 6 Feb 2025 96,612.40 | 21 | −$3,884.91 |
| long | 16 Mar 2025 84,338.40 | 16 Mar 2025 83,873.60 | 0 | −$464.82 |
| long | 20 Mar 2025 86,845.90 | 20 Mar 2025 84,371.90 | 0 | −$2,474.04 |
| long | 26 Mar 2025 87,392.90 | 28 Mar 2025 85,552.90 | 2 | −$1,839.99 |
| long | 23 Apr 2025 93,443.00 | 11 May 2025 104,810.00 | 18 | $11,366.50 |
| long | 5 Jun 2025 104,697.00 | 6 Jun 2025 101,509.00 | 1 | −$3,188.17 |
| long | 8 Jun 2025 105,552.00 | 9 Jun 2025 105,734.00 | 1 | $181.86 |
| long | 18 Jun 2025 104,551.00 | 21 Jun 2025 103,298.00 | 3 | −$1,253.19 |
| long | 25 Jun 2025 106,083.00 | 29 Jun 2025 107,297.00 | 4 | $1,213.79 |
| long | 1 Jul 2025 107,147.00 | 5 Jul 2025 107,984.00 | 4 | $837.74 |
| long | 10 Jul 2025 111,234.00 | 27 Jul 2025 117,920.00 | 17 | $6,685.99 |
| long | 19 Aug 2025 116,227.00 | 1 Sep 2025 108,246.00 | 13 | −$7,980.69 |
| long | 3 Sep 2025 111,240.00 | 8 Sep 2025 111,137.00 | 5 | −$102.66 |
| long | 13 Sep 2025 116,029.00 | 30 Sep 2025 114,312.00 | 17 | −$1,717.44 |
| long | 7 Oct 2025 124,659.00 | 10 Oct 2025 106,442.00 | 3 | −$18,216.80 |
| long | 11 Oct 2025 112,774.00 | 12 Oct 2025 110,644.00 | 1 | −$2,130.09 |
| long | 15 Oct 2025 113,028.00 | 16 Oct 2025 110,763.00 | 1 | −$2,264.85 |
| long | 21 Aug 2026 73,027.00 | 8 Sep 2026 79,112.00 | 18 | $6,084.98 |
| long | 28 Sep 2026 84,472.00 | 7 Oct 2026 85,549.90 | 9 | $1,077.93 |
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 |
|---|---|---|---|---|---|
| MFI Simple Strategy this page | — | v5 strategy | −0.2% | 28 | 3.2% |
| [VJ]Thor for MFI by vikris | mfi sma | v4 strategy | No trades on this market | ||
| CCU MFI + RSI + STOCH RSI by Bangsoul | mfi sma | v4 strategy | +41.8% | 6 | 37.4% |
| 3 Indicator Strategy (StochRSI, MFI & EMA) With Safety Orders by ggekko21 | sma mfi | v5 strategy | No trades on this market | ||
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.
- 2 strategy calls use the when = argument (line 11, 13). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 1 setting uses the untyped input() (line 5). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 28 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.int()
- Line 11: when = mfiup → if block
- Line 13: when = mfidn → if block
⋯strategy("MFI Simple Strategy", overlay=false)-len = input(18, "Length")+len = input.int(18, "Length")mfi = ta.sma(ta.mfi(close, len), len)⋯if goldsma < close- strategy.entry("Long", strategy.long, when=mfiup)+ if mfiup+ strategy.entry("Long", strategy.long)strategy.exit("Long", stop=goldsma)-strategy.close("Long", when=mfidn)+if mfidn+ strategy.close("Long")a = plot(mfi)⋯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: 28 before, 27 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.
⋯// © OztheWoz-//@version=5+//@version=6strategy("MFI Simple Strategy", overlay=false)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 11 ms | Yes 1,000 bars in 36 ms | Not measured |
| PineTS | Not tested | Not tested | 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.
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/// © OztheWoz//@version=5strategy("MFI Simple Strategy", overlay=false)len = input(18, "Length")mfi = ta.sma(ta.mfi(close, len), len)mfiup = (mfi[1] < mfi) and (mfi[2] < mfi[1])mfidn = (mfi[1] > mfi) and (mfi[2] < mfi[1])goldsma = ta.sma(close, 200)if goldsma < close strategy.entry("Long", strategy.long, when=mfiup)strategy.exit("Long", stop=goldsma)strategy.close("Long", when=mfidn)a = plot(mfi)b = plot(80, color=color.gray)c = plot(20, color=color.gray)fillclr = color.new(color.blue, 70)fill(a, b, color=(mfi > 80) ? fillclr : na)// fill(a, c)