Zeal Terminal Open in Zeal Terminal

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
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$104,269$202,527Equity from $100,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. The script plots nothing itself; the lines and the lower pane are its own calculations, drawn for reference. 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. The live re-run is not offered here: the browser bundle did not reproduce the prerendered run.
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

Differs

Trades 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

Differs

Trades 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.

SettingDefaultType and rangeFeeds
DirectionLong Onlystring · options ["Long Only", "Short Only", "Random"]line 64 line 72
Percent Chance to Enter10int · min 1 · max 100enter
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.3int · min 0 · max 100exit
Start Year2020intline 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.

enter_frequencyPercent Chance to Entersetting · 10exit_frequencyPercent Chance to Exitsetting · 3enterenterconditionexitexitconditiondirectiondirectionconditionentry:LongOpen Longstrategy.entryclose:LongClose Longstrategy.closeentry:ShortOpen Shortstrategy.entryclose:ShortClose Shortstrategy.close

Order rules

OrderFires whenBy defaultLine
Open Long longenterLong()On68
Close LongexitLong()On69
Open Short shortenterShort()On76
Close ShortexitShort()On77

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.

0/7trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+0.5%+102.5%2152%1.0041.8%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.6%+5.5%1947%0.9831.3%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+51.7%+9.8%2259%1.2063.4%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+19.2%+8.4%1963%1.5137.3%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−42.2%+16.7%1844%0.7475.9%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+21.4%+21.5%2343%1.5122.7%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+46.9%+148.3%1953%1.3445.6%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−4.7%+17.1%2832%0.8717.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 barsTradesWonP&L
Rising market540%−$11,176.40
Sideways market1258%−$9,698.82
Falling market450%$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.

0%61%
All 21 trades
SideEntryExitBarsP&L
long9 Feb 2024 45,288.7018 Mar 2024 68,393.5038$51,016.80
long21 Mar 2024 67,840.5029 Apr 2024 63,118.6039−$10,511.20
long19 May 2024 66,915.2021 Jun 2024 64,870.0033−$4,294.44
long14 Jul 2024 59,204.0015 Aug 2024 58,683.4032−$1,197.79
long26 Aug 2024 64,220.005 Sep 2024 57,970.9010−$13,137.80
long9 Sep 2024 54,869.9013 Oct 2024 63,206.2034$18,516.30
long28 Dec 2024 94,299.0015 Mar 2025 83,983.2077−$15,358.20
long26 Mar 2025 87,392.9027 Apr 2025 94,628.0032$10,351.30
long1 May 2025 94,172.006 May 2025 94,733.705$807.49
long14 May 2025 104,104.008 Jun 2025 105,552.0025$1,894.89
long13 Jun 2025 105,672.0018 Jul 2025 119,178.0035$17,648.80
long8 Aug 2025 117,472.0014 Aug 2025 123,306.006$7,734.87
long22 Aug 2025 112,500.0010 Sep 2025 111,546.0019−$1,385.68
long13 Sep 2025 116,029.0018 Sep 2025 116,448.005$584.18
long28 Sep 2025 109,636.0016 Oct 2025 110,763.0018$1,672.80
long17 Oct 2025 108,194.003 Feb 2026 78,738.60109−$44,741.90
long13 Feb 2026 66,272.202 Mar 2026 65,776.5017−$894.57
long4 Mar 2026 68,338.0020 Apr 2026 73,801.8047$9,490.83
long25 Apr 2026 77,437.108 Jul 2026 63,364.0074−$23,298.00
long22 Jul 2026 66,556.1017 Aug 2026 62,900.0026−$5,762.44
long25 Sep 2026 84,410.207 Oct 2026 85,549.9012$1,338.53

Similar scripts

No other indicator or strategy here shares enough of its calculations.

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 12 msWith a workaround 1,000 bars in 25 msNot measured
PineTSNo 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())