Pine Script v4 · Strategy
Random Entries Work!
A Pine Script v4 strategy with no technical-analysis calculation. It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100% of equity and pays no commission. 4 settings; defaults include Direction Long Only, Percent Chance to Enter 10 and Percent Chance to Exit 3.
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 Random Entries Work! repaint?
- Yes
- In a live replay, it did not match history on 7 of 7 trades, and it produced 6 trades that history does not have.
- How did Random Entries Work! do on BTCUSDT 1D with default settings?
- +0.47%
- From 26 Jan 2024 to 21 Oct 2026, over 21 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Random Entries Work! be converted to Pine Script v5?
- No
- The automatic step from v4 to v5 changes the results, so no v5 download is offered; the edits are shown below.
- Does Random Entries Work! use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Random Entries Work! run in Zeal Terminal?
- Yes
- It compiles in 12 ms and runs 1,000 bars in 25 ms, in the browser.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +0.47%
- $474.89
- Buy and hold
- +102.5%
- same period
- Closed trades
- 21
- 11 won · 10 lost
- Profitable
- 52.4%
- of closed trades
- Profit factor
- 1.00
- gross profit ÷ gross loss
- Max drawdown
- 41.78%
- −$68,654.50
One run with the author's default settings, $100,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.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
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 68, 69, 76, 77. The upgraded version below moves each call inside an if block.
-
Note
Uses long =, which v5 renamed line 68
long = on line 68, 76. v5 writes it as direction = strategy.long or strategy.short.
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 random → math.random.
- 4 strategy calls use the when = argument (line 68, 69, 76, 77). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
DiffersTrades differ: 30 before, 25 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 1: //@version=4 → //@version=5
- Line 58: random → math.random
- Line 61: random → math.random
- Line 68: long = strategy.long → direction = strategy.long
- Line 76: long = strategy.short → direction = strategy.short
-//@version=4+//@version=5// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯// === LOGIC ===-r = random(0,100)+r = math.random(0,100)enter = enter_frequency > r[0]exit = exit_frequency > r[0]-direction = random(0,100) >= 50+direction = math.random(0,100) >= 50// === STRATEGY - LONG POSITION EXECUTION ===⋯ exit-strategy.entry(id="Long", long=strategy.long, when=enterLong())+strategy.entry(id="Long", direction = strategy.long, when=enterLong())strategy.close(id="Long", when=exitLong())⋯ exit-strategy.entry(id="Short", long=strategy.short, when=enterShort())+strategy.entry(id="Short", direction = strategy.short, when=enterShort())strategy.close(id="Short", when=exitShort())Clean v5
DiffersTrades differ: 25 before, 20 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 53: input() → input.string()
- Line 54: input() → input.int()
- Line 55: input() → input.int()
- Line 56: input() → input.int()
- Line 68: when = enterLong() → if block
- Line 69: when = exitLong() → if block
- Line 76: when = enterShort() → if block
- Line 77: when = exitShort() → if block
⋯// === GENERAL INPUTS ===-strategy = input(defval="Long Only",title="Direction",options=["Long Only", "Short Only", "Random"])+strategy = input.string(defval="Long Only",title="Direction",options=["Long Only", "Short Only", "Random"])-enter_frequency = input(defval=10,minval=1,maxval=100,title="Percent Chance to Enter")+enter_frequency = input.int(defval=10,minval=1,maxval=100,title="Percent Chance to Enter")-exit_frequency = input(defval=3, minval=0,maxval=100,title="Percent Chance to Exit",tooltip="This should be much lower than Percent Chance to Enter. Higher values decrease time in market. Lower values increase time in market.")+exit_frequency = input.int(defval=3, minval=0,maxval=100,title="Percent Chance to Exit",tooltip="This should be much lower than Percent Chance to Enter. Higher values decrease time in market. Lower values increase time in market.")-start_year = input(defval=2020, title="Start Year")+start_year = input.int(defval=2020, title="Start Year")// === LOGIC ===⋯ exit-strategy.entry(id="Long", direction = strategy.long, when=enterLong())+if enterLong()+ strategy.entry(id="Long", direction = strategy.long)-strategy.close(id="Long", when=exitLong())+if exitLong()+ strategy.close(id="Long")// === STRATEGY - SHORT POSITION EXECUTION ===⋯ exit-strategy.entry(id="Short", direction = strategy.short, when=enterShort())+if enterShort()+ strategy.entry(id="Short", direction = strategy.short)-strategy.close(id="Short", when=exitShort())+if exitShort()+ strategy.close(id="Short")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: 20 before, 20 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.
-//@version=5+//@version=6// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯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 |
|---|---|---|---|
| Direction | Long Only | string · options ["Long Only", "Short Only", "Random"] | line 64 line 72 |
| Percent Chance to Enter | 10 | int · min 1 · max 100 | enter |
| Percent Chance to ExitThis should be much lower than Percent Chance to Enter. Higher values decrease time in market. Lower values increase time in market. | 3 | int · min 0 · max 100 | exit |
| Start Year | 2020 | int | line 65 line 73 |
What it calculates
No technical-analysis calculation: the script works on prices and settings directly.
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 | enterLong() | On | 68 |
| Close Long | exitLong() | On | 69 |
| Open Short short | enterShort() | On | 76 |
| Close Short | exitShort() | On | 77 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
Yes
The live replay did not match 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.5% | +102.5% | 21 | 52% | 1.00 | 41.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.6% | +5.5% | 19 | 47% | 0.98 | 31.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +51.7% | +9.8% | 22 | 59% | 1.20 | 63.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +19.2% | +8.4% | 19 | 63% | 1.51 | 37.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −42.2% | +16.7% | 18 | 44% | 0.74 | 75.9% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +21.4% | +21.5% | 23 | 43% | 1.51 | 22.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +46.9% | +148.3% | 19 | 53% | 1.34 | 45.6% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −4.7% | +17.1% | 28 | 32% | 0.87 | 17.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 | 5 | 40% | −$11,176.40 |
| Sideways market | 12 | 58% | −$9,698.82 |
| Falling market | 4 | 50% | $21,350.10 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
53.3%
The 95th percentile of the worst drawdown when the same 21 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 29.5% to 53.3%; the tick is what actually happened, 39.7%. Order luck alone moves it this much.
All 21 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 9 Feb 2024 45,288.70 | 18 Mar 2024 68,393.50 | 38 | $51,016.80 |
| long | 21 Mar 2024 67,840.50 | 29 Apr 2024 63,118.60 | 39 | −$10,511.20 |
| long | 19 May 2024 66,915.20 | 21 Jun 2024 64,870.00 | 33 | −$4,294.44 |
| long | 14 Jul 2024 59,204.00 | 15 Aug 2024 58,683.40 | 32 | −$1,197.79 |
| long | 26 Aug 2024 64,220.00 | 5 Sep 2024 57,970.90 | 10 | −$13,137.80 |
| long | 9 Sep 2024 54,869.90 | 13 Oct 2024 63,206.20 | 34 | $18,516.30 |
| long | 28 Dec 2024 94,299.00 | 15 Mar 2025 83,983.20 | 77 | −$15,358.20 |
| long | 26 Mar 2025 87,392.90 | 27 Apr 2025 94,628.00 | 32 | $10,351.30 |
| long | 1 May 2025 94,172.00 | 6 May 2025 94,733.70 | 5 | $807.49 |
| long | 14 May 2025 104,104.00 | 8 Jun 2025 105,552.00 | 25 | $1,894.89 |
| long | 13 Jun 2025 105,672.00 | 18 Jul 2025 119,178.00 | 35 | $17,648.80 |
| long | 8 Aug 2025 117,472.00 | 14 Aug 2025 123,306.00 | 6 | $7,734.87 |
| long | 22 Aug 2025 112,500.00 | 10 Sep 2025 111,546.00 | 19 | −$1,385.68 |
| long | 13 Sep 2025 116,029.00 | 18 Sep 2025 116,448.00 | 5 | $584.18 |
| long | 28 Sep 2025 109,636.00 | 16 Oct 2025 110,763.00 | 18 | $1,672.80 |
| long | 17 Oct 2025 108,194.00 | 3 Feb 2026 78,738.60 | 109 | −$44,741.90 |
| long | 13 Feb 2026 66,272.20 | 2 Mar 2026 65,776.50 | 17 | −$894.57 |
| long | 4 Mar 2026 68,338.00 | 20 Apr 2026 73,801.80 | 47 | $9,490.83 |
| long | 25 Apr 2026 77,437.10 | 8 Jul 2026 63,364.00 | 74 | −$23,298.00 |
| long | 22 Jul 2026 66,556.10 | 17 Aug 2026 62,900.00 | 26 | −$5,762.44 |
| long | 25 Sep 2026 84,410.20 | 7 Oct 2026 85,549.90 | 12 | $1,338.53 |
Similar scripts
No other indicator or strategy here shares enough of its calculations.
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 12 ms | With a workaround 1,000 bars in 25 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = strategy.long, long = strategy.short as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 68, 76) 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. 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.
//@version=4// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © gregoirejohnb//// "tHe MaRkEtS aRe RaNdOm", say moron academics.//// The purpose of this study is to show that most markets are NOT random! Most markets show a clear bias where we can make such easy money, that a random number generator can do it.// // === HOW THE INDICATOR WORKS ===// // -The study will randomly enter the market// -The study will randomly exit the market if in a trade// -You can choose a Long Only, Short Only, or Bidirectional strategy//// === DEFAULT VALUES AND THEIR LOGIC ===// // Percent Chance to Enter Per Bar: 10%// Percent Chance to Exit Per Bar: 1%// Direction: Long Only// Commission: 0//// Each bar has a 10% chance to enter the market. Each bar has a 1% to exit the market [if in a trade]. It will only enter long.//// I included zero commission for simplication. It's a good exercise to include a commission/slippage to see just how much trading fees take from you.// // === TIPS ===//// -Increasing "Percent Chance to Exit" will shorten the time in a trade. You can see the "Avg # Bars In Trade" go down as you increase. If "Percent Chance to Exit" is too high, the study won't be in the market long enough to catch any movement, possibly exiting on the same bar most of the time.// -If you're getting the red screen, that means the strategy lost so much money it went broke. Try reducing the percent equity on the Properties tab.// -Switch the start year to avoid black swan events like the covid drop in 2020.// -// === FINDINGS ===//// Most markets lose money with a "Random" direction strategy.// Most markets lose ALL money with a "Short Only" strategy.// Most markets make money with a "Long Only" strategy.// // Try this strategy on: Bitcoin (BTCUSD) and the NASDAQ (QQQ).//// There are two popular memes right now: "Bitcoin to the moon" and "Stocks only go up". Both are seemingly true. Bitcoin was the best performing asset of the 2010's, gaining several billion percent in gains. The stock market is on a 100 year long uptrend. Why? BECAUSE FIAT CURRENCIES ALWAYS GO DOWN! This is inflation. If we measure the market in terms of others assets instead of fiat, the Long Only strategy doesn't work anymore.// Try this strategy on: Bitcoin/GLD (BTCUSD/GLD), the Eurodollar (EURUSD), and the S&P 500 measured in gold (SPY/GLD).// // Bitcoin measured in gold (BTCUSD/GLD) still works with a Long Only strategy because Bitcoin increased in value over both USD and gold.// The Eurodollar (EURUSD) generally loses money no matter what, especially if you add any commission. This makes sense as they are both fiat currencies with similar inflation schedules.// Gold and the S&P 500 have gained roughly the same amount since ~2000. Some years will show better results for a long strategy, while others will favor a short strategy. Now look at just SPY or GLD (which are both measured in USD by default!) and you'll see the same trend again: a Long Only strategy crushes even when entering and exiting randomly.//// === "JUST TELL ME WHAT TO DO, YOU NERD!" ===//// Bulls always win and Bears always lose because fiat currencies go to zero.//strategy(title="Random Entries Work", shorttitle="REW", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=100, currency=currency.USD,commission_type=strategy.commission.percent,commission_value=0)// === GENERAL INPUTS ===strategy = input(defval="Long Only",title="Direction",options=["Long Only", "Short Only", "Random"])enter_frequency = input(defval=10,minval=1,maxval=100,title="Percent Chance to Enter")exit_frequency = input(defval=3, minval=0,maxval=100,title="Percent Chance to Exit",tooltip="This should be much lower than Percent Chance to Enter. Higher values decrease time in market. Lower values increase time in market.")start_year = input(defval=2020, title="Start Year")// === LOGIC ===r = random(0,100)enter = enter_frequency > r[0]exit = exit_frequency > r[0]direction = random(0,100) >= 50// === STRATEGY - LONG POSITION EXECUTION ===enterLong() => strategy.opentrades == 0 and enter and (strategy == "Long Only" or (strategy == "Random") and direction) and time > timestamp(start_year, 01, 01, 01, 01)exitLong() => exitstrategy.entry(id="Long", long=strategy.long, when=enterLong())strategy.close(id="Long", when=exitLong())// === STRATEGY - SHORT POSITION EXECUTION ===enterShort() => strategy.opentrades == 0 and enter and (strategy == "Short Only" or (strategy == "Random" and not direction)) and time > timestamp(start_year, 01, 01, 01, 01)exitShort() => exitstrategy.entry(id="Short", long=strategy.short, when=enterShort())strategy.close(id="Short", when=exitShort())