Pine Script v4 · Strategy
Money Flow Index 5 min Strategy
A Pine Script v4 strategy built on Lowest (10), Highest (10) and Valuewhen (bought, LL). It has 11 order rules, 8 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 100% of equity and pays 0.04 commission. 21 settings; defaults include Strategy Direction 0, MFI Length 3 and Overbought Level 100.
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 Money Flow Index 5 min Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 4 of 4 trades.
- How did Money Flow Index 5 min Strategy do on BTCUSDT 1D with default settings?
- −6.79%
- From 26 Jan 2024 to 21 Oct 2026, over 18 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Money Flow Index 5 min 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 Money Flow Index 5 min Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 133, 134.
- Does Money Flow Index 5 min Strategy run in Zeal Terminal?
- Yes
- It compiles in 38 ms and runs 1,000 bars in 101 ms, in the browser.
BTCUSDT · 1D · default settings
- Trade Day Start
- SL
- SSL
- TP
- STP
- Bullish Setup
- Bearish Setup
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −6.79%
- −$678.93
- Buy and hold
- +102.5%
- same period
- Closed trades
- 18
- 10 won · 8 lost
- Profitable
- 55.6%
- of closed trades
- Profit factor
- 0.88
- gross profit ÷ gross loss
- Max drawdown
- 28.44%
- −$3,229.99
One run with the author's default settings, $10,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
2 settings do nothing
“-----------------Strategy Inputs-------------------”, “-----------------General Inputs-------------------” are declared but never read.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
Uses an argument v6 removed
when = on line 94, 95, 97, 98, 101, 102, 104, 105, 107, 133, 134. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 68
transp = on line 68, 87. 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 lowest → ta.lowest, highest → ta.highest, valuewhen → ta.valuewhen, atr → ta.atr, mfi → ta.mfi, sma → ta.sma.
- 11 strategy calls use the when = argument (line 94, 95, 97, 98, 101, 102, 104, 105, 107, 133, 134). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 18 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 12: //@version=4 → //@version=5
- Line 21: input(type = input.integer) → input.int()
- Line 48: lowest → ta.lowest
- Line 49: highest → ta.highest
- Line 50: valuewhen → ta.valuewhen
- Line 51: valuewhen → ta.valuewhen
12 more edits
- Line 57: atr → ta.atr
- Line 60: valuewhen → ta.valuewhen
- Line 61: valuewhen → ta.valuewhen
- Line 68: transp = 80 → color.new(color.silver, 80)
- Line 69: valuewhen → ta.valuewhen
- Line 70: valuewhen → ta.valuewhen
- Line 74: mfi → ta.mfi
- Line 78: sma → ta.sma
- Line 80: valuewhen → ta.valuewhen
- Line 87: transp = 100 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 113: valuewhen → ta.valuewhen
- Line 125: valuewhen → ta.valuewhen
⋯// © tweakerID-//@version=4+//@version=5strategy("Money Flow Index 5 min Strategy", ⋯ slippage=0)-direction = input(0, title = "Strategy Direction", type=input.integer, minval=-1, maxval=1)+direction = input.int(0, title = "Strategy Direction", minval=-1, maxval=1)strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))⋯// Price Action Stop and Take Profit-LL=(lowest(i_SPL))*(1-i_PercIncrement)+LL=(ta.lowest(i_SPL))*(1-i_PercIncrement)-HH=(highest(i_SPL))*(1+i_PercIncrement)+HH=(ta.highest(i_SPL))*(1+i_PercIncrement)-LL_price = valuewhen(bought, LL, 0)+LL_price = ta.valuewhen(bought, LL, 0)-HH_price = valuewhen(bought, HH, 0)+HH_price = ta.valuewhen(bought, HH, 0)entry_LL_price = strategy.position_size > 0 ? LL_price : na ⋯// ATR Stop-ATR=atr(i_ATR)*i_ATRMult+ATR=ta.atr(i_ATR)*i_ATRMultATRLong = ohlc4 - ATRATRShort = ohlc4 + ATR-ATRLongStop = valuewhen(bought, ATRLong, 0)+ATRLongStop = ta.valuewhen(bought, ATRLong, 0)-ATRShortStop = valuewhen(bought, ATRShort, 0)+ATRShortStop = ta.valuewhen(bought, ATRShort, 0)LongSL_ATR_price = strategy.position_size > 0 ? ATRLongStop : na ⋯DayStart = time == timestamp("UTC", year, month, dayofmonth, 0, 0, 0)-plot(DayStart ? 1e9 : na, style=plot.style_columns, color=color.silver, transp=80, title="Trade Day Start")+plot(DayStart ? 1e9 : na, style=plot.style_columns, color=color.new(color.silver, 80), title="Trade Day Start")-float LongStop = valuewhen(DayStart,low,0)*(1-i_PercIncrement)+float LongStop = ta.valuewhen(DayStart,low,0)*(1-i_PercIncrement)-float ShortStop = valuewhen(DayStart,high,0)*(1+i_PercIncrement)+float ShortStop = ta.valuewhen(DayStart,high,0)*(1+i_PercIncrement)float StratTP = strategy.position_avg_price + (strategy.position_avg_price - LongStop)*i_TPRRR⋯/////////////////////// STRATEGY LOGIC /////////////////////////////////////////-MFI=mfi(close,i_MFI)+MFI=ta.mfi(close,i_MFI)barsize=high-low⋯shortwicksbar=barbodysize>barsize*barsizeThreshold-SMA=sma(close, i_MALen)+SMA=ta.sma(close, i_MALen)MAFilter=close > SMA-timesinceentry=(time - valuewhen(bought, time, 0)) / 60000+timesinceentry=(time - ta.valuewhen(bought, time, 0)) / 60000timedexit=timesinceentry == 60⋯//Debugging Plots-plot(timesinceentry, transp=100, title="Time Since Entry")+plot(timesinceentry, title="Time Since Entry")//Trading Inputs⋯//TrailingStop-dif=(valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0))+dif=(ta.valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0)) -strategy.position_avg_price⋯var Ststop = float(na)-Sdif=strategy.position_avg_price-(valuewhen(strategy.position_size<0 +Sdif=strategy.position_avg_price-(ta.valuewhen(strategy.position_size<0 and strategy.position_size[1]>=0, low,0))⋯Same 18 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 24: input() → input.bool()
- Line 25: input() → input.int()
- Line 26: input() → input.int()
- Line 27: input() → input.int()
- Line 28: input() → input.float()
- Line 29: input() → input.bool()
25 more edits
- Line 30: input() → input.int()
- Line 31: input() → input.bool()
- Line 32: input() → input.bool()
- Line 34: input() → input.bool()
- Line 36: input() → input.bool()
- Line 37: input() → input.string()
- Line 38: input() → input.int()
- Line 39: input() → input.int()
- Line 40: input() → input.int()
- Line 41: input() → input.int()
- Line 42: input() → input.float()
- Line 43: input() → input.bool()
- Line 89: input() → input.bool()
- Line 90: input() → input.bool()
- Line 94: when = SELL and strategy.position_size == 0 → if block
- Line 95: when = BUY and strategy.position_size == 0 → if block
- Line 97: when = SELL → if block
- Line 98: when = BUY → if block
- Line 101: when = BUY and strategy.position_size == 0 → if block
- Line 102: when = SELL and strategy.position_size == 0 → if block
- Line 104: when = BUY → if block
- Line 105: when = SELL → if block
- Line 107: when = timedexit → if block
- Line 133: when = i_SL → if block
- Line 134: when = i_SL → if block
⋯/////////////////////// STRATEGY INPUTS ////////////////////////////////////////-title1=input(true, "-----------------Strategy Inputs-------------------") +title1=input.bool(true, "-----------------Strategy Inputs-------------------") -i_MFI = input(3, title="MFI Length")+i_MFI = input.int(3, title="MFI Length")-OB=input(100, title="Overbought Level")+OB=input.int(100, title="Overbought Level")-OS=input(0, title="Oversold Level")+OS=input.int(0, title="Oversold Level")-barsizeThreshold=input(.5, step=.05, minval=.1, maxval=1, title="Bar Body Size, 1=No Wicks")+barsizeThreshold=input.float(.5, step=.05, minval=.1, maxval=1, title="Bar Body Size, 1=No Wicks")-i_MAFilter = input(true, title="Use MA Trend Filter")+i_MAFilter = input.bool(true, title="Use MA Trend Filter")-i_MALen = input(80, title="MA Length")+i_MALen = input.int(80, title="MA Length")-i_timedexit=input(false, title="Use 60 minutes exit rule")+i_timedexit=input.bool(false, title="Use 60 minutes exit rule")-short=input(true, title="Use Mirrored logic for Shorts")+short=input.bool(true, title="Use Mirrored logic for Shorts")/////////////////////// BACKTESTER /////////////////////////////////////////////-title2=input(true, "-----------------General Inputs-------------------") +title2=input.bool(true, "-----------------General Inputs-------------------") // Backtester General Inputs-i_SL=input(true, title="Use Stop Loss and Take Profit")+i_SL=input.bool(true, title="Use Stop Loss and Take Profit")-i_SLType=input(defval="Strategy Stop", title="Type Of Stop", options=["Strategy Stop", "Swing Lo/Hi", "ATR Stop"])+i_SLType=input.string(defval="Strategy Stop", title="Type Of Stop", options=["Strategy Stop", "Swing Lo/Hi", "ATR Stop"])-i_SPL=input(defval=10, title="Swing Point Lookback")+i_SPL=input.int(defval=10, title="Swing Point Lookback")-i_PercIncrement=input(defval=3, step=.1, title="Swing Point SL Perc Increment")*0.01+i_PercIncrement=input.int(defval=3, step=.1, title="Swing Point SL Perc Increment")*0.01-i_ATR = input(14, title="ATR Length")+i_ATR = input.int(14, title="ATR Length")-i_ATRMult = input(5, step=.1, title="ATR Multiple")+i_ATRMult = input.int(5, step=.1, title="ATR Multiple")-i_TPRRR = input(2.2, step=.1, title="Take Profit Risk Reward Ratio")+i_TPRRR = input.float(2.2, step=.1, title="Take Profit Risk Reward Ratio")-TS=input(false, title="Trailing Stop")+TS=input.bool(false, title="Trailing Stop")// Bought and Sold Boolean Signal⋯//Trading Inputs-DPR=input(true, "Allow Direct Position Reverse")+DPR=input.bool(true, "Allow Direct Position Reverse")-reverse=input(false, "Reverse Trades")+reverse=input.bool(false, "Reverse Trades")// Entries⋯ if not DPR- strategy.entry("long", strategy.long, when=SELL and strategy.position_size == 0)+ if SELL and strategy.position_size == 0+ strategy.entry("long", strategy.long)- strategy.entry("short", strategy.short, when=BUY and strategy.position_size == 0)+ if BUY and strategy.position_size == 0+ strategy.entry("short", strategy.short) else - strategy.entry("long", strategy.long, when=SELL)+ if SELL+ strategy.entry("long", strategy.long)- strategy.entry("short", strategy.short, when=BUY)+ if BUY+ strategy.entry("short", strategy.short)else if not DPR - strategy.entry("long", strategy.long, when=BUY and strategy.position_size == 0)+ if BUY and strategy.position_size == 0+ strategy.entry("long", strategy.long)- strategy.entry("short", strategy.short, when=SELL and strategy.position_size == 0)+ if SELL and strategy.position_size == 0+ strategy.entry("short", strategy.short) else- strategy.entry("long", strategy.long, when=BUY)+ if BUY+ strategy.entry("long", strategy.long)- strategy.entry("short", strategy.short, when=SELL)+ if SELL+ strategy.entry("short", strategy.short)if i_timedexit- strategy.close_all(when=timedexit)+ if timedexit+ strategy.close_all()SL= i_SLType == "Swing Lo/Hi" ? entry_LL_price : i_SLType == "ATR Stop" ? LongSL_ATR_price : LongStop⋯ Ststop := na-strategy.exit("TP & SL", "long", limit=TP, stop=TS? tstop : SL, when=i_SL)+if i_SL+ strategy.exit("TP & SL", "long", limit=TP, stop=TS? tstop : SL)-strategy.exit("TP & SL", "short", limit=STP, stop=TS? Ststop : SSL, when=i_SL)+if i_SL+ strategy.exit("TP & SL", "short", limit=STP, stop=TS? Ststop : SSL)/////////////////////// PLOTS //////////////////////////////////////////////////⋯Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L83:C0: a 'bool' cannot be 'na' in Pine Script v6 — declare it as true or false. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © tweakerID-//@version=5+//@version=6strategy("Money Flow Index 5 min Strategy", ⋯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 Direction | 0 | int (written input.integer) · min -1 · max 1 | line 22 |
| -----------------Strategy Inputs------------------- | true | bool | unused |
| MFI Length | 3 | int | MFI |
| Overbought Level | 100 | int | BUY |
| Oversold Level | 0 | int | SELL |
| Bar Body Size, 1=No Wicks | .5 | float · min .1 · max 1 · step .05 | shortwicksbar |
| Use MA Trend Filter | true | bool | BUY SELL |
| MA Length | 80 | int | SMA |
| Use 60 minutes exit rule | false | bool | close_all line 106 |
| Use Mirrored logic for Shorts | true | bool | line 84 |
| -----------------General Inputs------------------- | true | bool | unused |
| Use Stop Loss and Take Profit | true | bool | exit TP & SL SL SSL TP STP |
| Type Of Stop | Strategy Stop | string · options ["Strategy Stop", "Swing Lo/Hi", "ATR Stop"] | SL SSL TP STP |
| Swing Point Lookback | 10 | int | LL HH |
| Swing Point SL Perc Increment | 3 | int · step .1 | LL HH LongStop ShortStop |
| ATR Length | 14 | int | ATR |
| ATR Multiple | 5 | int · step .1 | ATR |
| Take Profit Risk Reward Ratio | 2.2 | float · step .1 | tp stp ATRtp ATRstp StratTP StratSTP |
| Trailing Stop | false | bool | exit TP & SL SL SSL |
| Allow Direct Position Reverse | true | bool | entry long entry short entry long entry short entry long entry short entry long entry short line 93 line 100 |
| Reverse Trades | false | bool | line 92 |
What it calculates
- Lowest (
lowest,ta.lowestin v5)Swing Point Lookback (10)LL · line 48 - Highest (
highest,ta.highestin v5)Swing Point Lookback (10)HH · line 49 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, LL, 0LL_price · line 50 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, HH, 0HH_price · line 51 - ATR (
atr,ta.atrin v5)ATR Length (14)ATR · line 57 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, ATRLong, 0ATRLongStop · line 60 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, ATRShort, 0ATRShortStop · line 61 - Valuewhen (
valuewhen,ta.valuewhenin v5)DayStart, low, 0LongStop · line 69 - Valuewhen (
valuewhen,ta.valuewhenin v5)DayStart, high, 0ShortStop · line 70 - MFI (
mfi,ta.mfiin v5)close, MFI Length (3)MFI · line 74 - SMA (
sma,ta.smain v5)close, MA Length (80)SMA · line 78 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, time, 0timesinceentry · line 80
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.
The script has 72 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open long long | SELL and strategy.position_size == 0 | On | 94 |
| Open short short | BUY and strategy.position_size == 0 | On | 95 |
| Open long long | SELL | On | 97 |
| Open short short | BUY | On | 98 |
| Open long long | BUY and strategy.position_size == 0 | On | 101 |
| Open short short | SELL and strategy.position_size == 0 | On | 102 |
| Open long long | BUY | On | 104 |
| Open short short | SELL | On | 105 |
| Close | timedexittimesinceentry == 60 | Off “Use 60 minutes exit rule” | 107 |
| Exit TP & SL stop and limit | i_SL | On | 133 |
| Exit TP & SL stop and limit | i_SL | On | 134 |
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.8% | +102.5% | 18 | 56% | 0.88 | 28.4% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +26.2% | +5.5% | 15 | 60% | 2.30 | 9.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +29.9% | +9.8% | 22 | 59% | 1.35 | 22.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +37.9% | +8.4% | 15 | 73% | 3.46 | 10.9% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.3% | +16.7% | 31 | 48% | 1.00 | 36.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −15.5% | +21.5% | 15 | 47% | 0.67 | 36.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −6.7% | +148.3% | 20 | 55% | 0.90 | 24.6% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +3.0% | +17.1% | 19 | 53% | 1.08 | 24.6% | 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 | 5 | 40% | −$432.20 |
| Sideways market | 8 | 75% | $1,290.75 |
| Falling market | 5 | 40% | −$1,537.49 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
35.3%
The 95th percentile of the worst drawdown when the same 18 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 17.2% to 35.3%; the tick is what actually happened, 27.7%. Order luck alone moves it this much.
All 18 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 23 Apr 2024 66,819.30 | 24 Apr 2024 63,792.80 | 1 | −$460.57 |
| long | 16 Jul 2024 64,724.10 | 21 Jul 2024 67,140.00 | 5 | $348.16 |
| long | 30 Oct 2024 72,736.40 | 8 Nov 2024 75,857.90 | 9 | $416.08 |
| long | 11 Nov 2024 80,370.00 | 12 Nov 2024 88,648.00 | 1 | $1,052.17 |
| long | 22 Nov 2024 98,317.10 | 25 Nov 2024 92,862.70 | 3 | −$638.57 |
| long | 7 Jan 2025 102,236.00 | 8 Jan 2025 93,296.40 | 1 | −$944.91 |
| short | 27 Feb 2025 84,250.10 | 2 Mar 2025 89,154.70 | 3 | −$576.72 |
| short | 10 Mar 2025 80,734.50 | 19 Mar 2025 86,542.40 | 9 | −$668.88 |
| long | 23 Apr 2025 93,443.00 | 3 May 2025 96,887.10 | 10 | $307.21 |
| long | 9 May 2025 103,262.00 | 21 May 2025 108,107.00 | 12 | $407.12 |
| long | 10 Jun 2025 110,263.00 | 11 Jul 2025 117,035.00 | 31 | $559.68 |
| long | 27 Oct 2025 114,559.00 | 3 Nov 2025 106,187.00 | 7 | −$723.52 |
| short | 15 Nov 2025 94,594.00 | 19 Nov 2025 90,068.40 | 4 | $427.02 |
| short | 2 Dec 2025 86,286.00 | 5 Jan 2026 94,564.30 | 34 | −$919.44 |
| short | 21 Jan 2026 88,427.70 | 31 Jan 2026 84,260.50 | 10 | $397.70 |
| short | 9 Mar 2026 65,971.20 | 10 Mar 2026 71,602.10 | 1 | −$773.88 |
| short | 4 Jun 2026 64,142.80 | 6 Jun 2026 60,282.70 | 2 | $487.43 |
| long | 21 Aug 2026 73,027.00 | 22 Aug 2026 78,338.00 | 1 | $624.99 |
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 |
|---|---|---|---|---|---|
| Money Flow Index 5 min Strategy this page | — | v4 strategy | −6.8% | 18 | 28.4% |
| Trend Bounce [racer8] by racer8 | highest lowest sma valuewhen | v4 strategy | +2.4% | 22 | 32.2% |
| DC Breakout Strategy - This is simplicity at its finest. by Robrecht99 | sma highest lowest atr | v5 strategy | +10.8% | 57 | 8.3% |
| Trend Following with Donchian Channels and MACD by Robrecht99 | highest lowest sma atr | v5 strategy | −1.0% | 12 | 2.7% |
| BuyHigh-SellLow Strategy by Trendoscope | sma highest lowest atr | v4 strategy | +12.4% | 4 | 6.7% |
| Positive Bars - Strategy by tweakerID | lowest highest valuewhen atr | v4 strategy | −40.3% | 7 | 54.7% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 38 ms | With a workaround 1,000 bars in 101 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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/// From "Crypto Day Trading Strategy" PDF file.// * I'm using a SMA filter to avoid buying when the price is declining. Time frame was better at 15 min according to my test.// 1 - Apply the 3 period Money Flow Index indicator to the 5 minute chart, using 0 and 100 as our oversold and overbought boundaries// 2 - Wait for the MFI to reach overbought levels, that indicates the presence of "big sharks" in the market. Price needs to hold up// the first two MFI overbought occurrences of the day to be considered as a bullish entry signal.*// 3 - We buy when the MFI = 100 and the next candle is a bullish candle with short wicks.// 4 - We place our Stop Loss below the low of the trading day and we Take Profit during the first 60 minutes after taking the trade. // The logic above can be used in a mirrored fashion to take short entries, this is a custom parameter that can be modified from// the strategy Inputs panel.// © tweakerID//@version=4strategy("Money Flow Index 5 min Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=10000, commission_value=0.04, calc_on_every_tick=false, slippage=0)direction = input(0, title = "Strategy Direction", type=input.integer, minval=-1, maxval=1)strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))/////////////////////// STRATEGY INPUTS ////////////////////////////////////////title1=input(true, "-----------------Strategy Inputs-------------------") i_MFI = input(3, title="MFI Length")OB=input(100, title="Overbought Level")OS=input(0, title="Oversold Level")barsizeThreshold=input(.5, step=.05, minval=.1, maxval=1, title="Bar Body Size, 1=No Wicks")i_MAFilter = input(true, title="Use MA Trend Filter")i_MALen = input(80, title="MA Length")i_timedexit=input(false, title="Use 60 minutes exit rule")short=input(true, title="Use Mirrored logic for Shorts")/////////////////////// BACKTESTER /////////////////////////////////////////////title2=input(true, "-----------------General Inputs-------------------") // Backtester General Inputsi_SL=input(true, title="Use Stop Loss and Take Profit")i_SLType=input(defval="Strategy Stop", title="Type Of Stop", options=["Strategy Stop", "Swing Lo/Hi", "ATR Stop"])i_SPL=input(defval=10, title="Swing Point Lookback")i_PercIncrement=input(defval=3, step=.1, title="Swing Point SL Perc Increment")*0.01i_ATR = input(14, title="ATR Length")i_ATRMult = input(5, step=.1, title="ATR Multiple")i_TPRRR = input(2.2, step=.1, title="Take Profit Risk Reward Ratio")TS=input(false, title="Trailing Stop")// Bought and Sold Boolean Signalbought = strategy.position_size > strategy.position_size[1] or strategy.position_size < strategy.position_size[1]// Price Action Stop and Take ProfitLL=(lowest(i_SPL))*(1-i_PercIncrement)HH=(highest(i_SPL))*(1+i_PercIncrement)LL_price = valuewhen(bought, LL, 0)HH_price = valuewhen(bought, HH, 0)entry_LL_price = strategy.position_size > 0 ? LL_price : na entry_HH_price = strategy.position_size < 0 ? HH_price : na tp=strategy.position_avg_price + (strategy.position_avg_price - entry_LL_price)*i_TPRRRstp=strategy.position_avg_price - (entry_HH_price - strategy.position_avg_price)*i_TPRRR// ATR StopATR=atr(i_ATR)*i_ATRMultATRLong = ohlc4 - ATRATRShort = ohlc4 + ATRATRLongStop = valuewhen(bought, ATRLong, 0)ATRShortStop = valuewhen(bought, ATRShort, 0)LongSL_ATR_price = strategy.position_size > 0 ? ATRLongStop : na ShortSL_ATR_price = strategy.position_size < 0 ? ATRShortStop : na ATRtp=strategy.position_avg_price + (strategy.position_avg_price - LongSL_ATR_price)*i_TPRRRATRstp=strategy.position_avg_price - (ShortSL_ATR_price - strategy.position_avg_price)*i_TPRRR// Strategy StopDayStart = time == timestamp("UTC", year, month, dayofmonth, 0, 0, 0)plot(DayStart ? 1e9 : na, style=plot.style_columns, color=color.silver, transp=80, title="Trade Day Start")float LongStop = valuewhen(DayStart,low,0)*(1-i_PercIncrement)float ShortStop = valuewhen(DayStart,high,0)*(1+i_PercIncrement)float StratTP = strategy.position_avg_price + (strategy.position_avg_price - LongStop)*i_TPRRRfloat StratSTP = strategy.position_avg_price - (ShortStop - strategy.position_avg_price)*i_TPRRR/////////////////////// STRATEGY LOGIC /////////////////////////////////////////MFI=mfi(close,i_MFI)barsize=high-lowbarbodysize=close>open?(open-close)*-1:(open-close)shortwicksbar=barbodysize>barsize*barsizeThresholdSMA=sma(close, i_MALen)MAFilter=close > SMAtimesinceentry=(time - valuewhen(bought, time, 0)) / 60000timedexit=timesinceentry == 60BUY = MFI[1] == OB and close > open and shortwicksbar and (i_MAFilter ? MAFilter : true)bool SELL = naif short SELL := MFI[1] == OS and close < open and shortwicksbar and (i_MAFilter ? not MAFilter : true)//Debugging Plotsplot(timesinceentry, transp=100, title="Time Since Entry")//Trading InputsDPR=input(true, "Allow Direct Position Reverse")reverse=input(false, "Reverse Trades")// Entriesif reverse if not DPR strategy.entry("long", strategy.long, when=SELL and strategy.position_size == 0) strategy.entry("short", strategy.short, when=BUY and strategy.position_size == 0) else strategy.entry("long", strategy.long, when=SELL) strategy.entry("short", strategy.short, when=BUY)else if not DPR strategy.entry("long", strategy.long, when=BUY and strategy.position_size == 0) strategy.entry("short", strategy.short, when=SELL and strategy.position_size == 0) else strategy.entry("long", strategy.long, when=BUY) strategy.entry("short", strategy.short, when=SELL)if i_timedexit strategy.close_all(when=timedexit)SL= i_SLType == "Swing Lo/Hi" ? entry_LL_price : i_SLType == "ATR Stop" ? LongSL_ATR_price : LongStopSSL= i_SLType == "Swing Lo/Hi" ? entry_HH_price : i_SLType == "ATR Stop" ? ShortSL_ATR_price : ShortStopTP= i_SLType == "Swing Lo/Hi" ? tp : i_SLType == "ATR Stop" ? ATRtp : StratTPSTP= i_SLType == "Swing Lo/Hi" ? stp : i_SLType == "ATR Stop" ? ATRstp : StratSTP//TrailingStopdif=(valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0)) -strategy.position_avg_pricetrailOffset = strategy.position_avg_price - SLvar tstop = float(na)if strategy.position_size > 0 tstop := high- trailOffset - dif if tstop<tstop[1] tstop:=tstop[1]else tstop := naStrailOffset = SSL - strategy.position_avg_pricevar Ststop = float(na)Sdif=strategy.position_avg_price-(valuewhen(strategy.position_size<0 and strategy.position_size[1]>=0, low,0))if strategy.position_size < 0 Ststop := low+ StrailOffset + Sdif if Ststop>Ststop[1] Ststop:=Ststop[1]else Ststop := nastrategy.exit("TP & SL", "long", limit=TP, stop=TS? tstop : SL, when=i_SL)strategy.exit("TP & SL", "short", limit=STP, stop=TS? Ststop : SSL, when=i_SL)/////////////////////// PLOTS //////////////////////////////////////////////////plot(i_SL and strategy.position_size > 0 and not TS ? SL : i_SL and strategy.position_size > 0 and TS ? tstop : na , title='SL', style=plot.style_cross, color=color.red)plot(i_SL and strategy.position_size < 0 and not TS ? SSL : i_SL and strategy.position_size < 0 and TS ? Ststop : na , title='SSL', style=plot.style_cross, color=color.red)plot(i_SL and strategy.position_size > 0 ? TP : na, title='TP', style=plot.style_cross, color=color.green)plot(i_SL and strategy.position_size < 0 ? STP : na, title='STP', style=plot.style_cross, color=color.green)// Draw price action setup arrowsplotshape(BUY ? 1 : na, style=shape.triangleup, location=location.belowbar, color=color.green, title="Bullish Setup", size=size.auto)plotshape(SELL ? 1 : na, style=shape.triangledown, location=location.abovebar, color=color.red, title="Bearish Setup", size=size.auto)