Pine Script v5 · Strategy
How to use Leverage in PineScript
A Pine Script v5 strategy built on RSI (price, 14), Crossover (vrsi, 30) and Crossunder (vrsi, 70). It has 2 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 0.04% commission. 5 settings; defaults include Leverage 2, Length 14 and Over Sold 30.
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 How to use Leverage in PineScript 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 How to use Leverage in PineScript do on BTCUSDT 1D with default settings?
- −59.41%
- From 26 Jan 2024 to 21 Oct 2026, over 26 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does How to use Leverage in PineScript run in Pine Script v6?
- Yes
- Same 26 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does How to use Leverage in PineScript use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does How to use Leverage in PineScript run in Zeal Terminal?
- Yes
- It compiles in 10 ms and runs 1,000 bars in 67 ms, in the browser.
BTCUSDT · 1D · default settings
- Crossover
- Crossunder
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −59.41%
- −$594,070.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 26
- 6 won · 20 lost
- Profitable
- 23.1%
- of closed trades
- Profit factor
- 0.37
- gross profit ÷ gross loss
- Max drawdown
- 83.90%
- −$838,959.00
One run with the author's default settings, $1,000,000 starting capital, commission as the script sets it. A short 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
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
3 settings have no label
In the settings dialog length (line 13), overSold (line 14), overBought (line 15) show an empty name. The labels on this page come from the variable names.
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 |
|---|---|---|---|
| Leverage | 2 | int · min 1 · max 100 · step 1 | Lev |
| Length (no label in the script) | 14 | int | vrsi |
| Over Sold (no label in the script) | 30 | int | co |
| Over Bought (no label in the script) | 70 | int | cu |
| Precision | 1 | int | Lev |
What it calculates
- RSIprice, Length (14)
vrsi · line 17 - Crossovervrsi, Over Sold (30)
co · line 18 - Crossundervrsi, Over Bought (70)
cu · line 19
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 RsiLE long | (co) | On | 30 |
| Open RsiSE short | (cu) | On | 32 |
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 | −59.4% | +102.5% | 26 | 23% | 0.37 | 83.9% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −39.6% | +5.5% | 15 | 27% | 0.46 | 52.8% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −95.2% | +9.8% | 32 | 9% | 0.40 | 97.2% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −59.3% | +8.4% | 14 | 21% | 0.38 | 72.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −79.6% | +16.7% | 19 | 21% | 0.24 | 96.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +6.1% | +21.5% | 18 | 39% | 1.05 | 57.6% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −95.2% | +148.3% | 26 | 8% | 0.06 | 96.6% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −17.1% | +17.1% | 10 | 30% | 0.77 | 56.4% | 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 | 6 | 0% | −$507,826.00 |
| Sideways market | 17 | 24% | −$158,291.00 |
| Falling market | 3 | 67% | $72,279.60 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
71.1%
The 95th percentile of the worst drawdown when the same 26 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 46.6% to 71.1%; the tick is what actually happened, 67.1%. Order luck alone moves it this much.
All 26 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 24 Feb 2024 50,744.20 | 26 Feb 2024 54,910.00 | 2 | −$16,832.40 |
| short | 24 Feb 2024 50,744.20 | 27 Feb 2024 57,588.20 | 3 | −$82,648.00 |
| short | 24 Feb 2024 50,744.20 | 28 Feb 2024 64,000.00 | 4 | −$13,301.70 |
| short | 24 Feb 2024 50,744.20 | 4 Mar 2024 68,499.00 | 9 | −$284,841.00 |
| short | 24 Feb 2024 50,744.20 | 26 Jun 2024 61,806.00 | 123 | −$71,084.00 |
| long | 26 Jun 2024 61,806.00 | 5 Aug 2024 49,000.00 | 40 | −$51,401.30 |
| long | 26 Jun 2024 61,806.00 | 22 Oct 2024 67,377.50 | 118 | $72,861.60 |
| short | 22 Oct 2024 67,377.50 | 29 Oct 2024 73,620.10 | 7 | −$25,196.10 |
| short | 22 Oct 2024 67,377.50 | 10 Nov 2024 81,500.00 | 19 | −$56,728.20 |
| short | 22 Oct 2024 67,377.50 | 11 Nov 2024 89,530.50 | 20 | −$22,215.80 |
| short | 22 Oct 2024 67,377.50 | 13 Nov 2024 93,265.60 | 22 | −$25,952.40 |
| short | 22 Oct 2024 67,377.50 | 21 Nov 2024 98,988.00 | 30 | −$31,677.00 |
| short | 22 Oct 2024 67,377.50 | 5 Dec 2024 104,088.00 | 44 | −$36,779.10 |
| short | 22 Oct 2024 67,377.50 | 16 Dec 2024 107,793.00 | 55 | −$40,485.60 |
| short | 22 Oct 2024 67,377.50 | 2 Mar 2025 86,064.50 | 131 | −$63,744.60 |
| long | 2 Mar 2025 86,064.50 | 4 May 2025 95,856.40 | 63 | $56,370.80 |
| short | 4 May 2025 95,856.40 | 8 May 2025 104,146.00 | 4 | −$8,369.34 |
| short | 4 May 2025 95,856.40 | 21 May 2025 110,797.00 | 17 | −$15,023.60 |
| short | 4 May 2025 95,856.40 | 10 Jul 2025 116,868.00 | 67 | −$21,096.70 |
| short | 4 May 2025 95,856.40 | 13 Aug 2025 123,668.00 | 101 | −$27,899.20 |
| short | 4 May 2025 95,856.40 | 19 Nov 2025 92,960.80 | 199 | $6,768.15 |
| long | 19 Nov 2025 92,960.80 | 16 Jan 2026 95,604.80 | 58 | $13,356.50 |
| short | 16 Jan 2026 95,604.80 | 7 Feb 2026 70,580.30 | 22 | $132,278.00 |
| long | 7 Feb 2026 70,580.30 | 5 Jun 2026 59,130.90 | 118 | −$11,501.20 |
| long | 7 Feb 2026 70,580.30 | 31 Aug 2026 77,682.00 | 205 | $70,424.40 |
| short | 31 Aug 2026 77,682.00 | 21 Sep 2026 87,395.70 | 21 | −$39,118.80 |
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 |
|---|---|---|---|---|---|
| How to use Leverage in PineScript this page | — | v5 strategy | −59.4% | 26 | 83.9% |
| Triple RSI strategy by Trade_by_DB | rsi | v5 strategy | +3.2% | 2 | 3.3% |
| Hobbiecode - RSI + Close previous day by hobbiecode | rsi | v5 strategy | +1.2% | 63 | 4.2% |
| dirt cheap yet elegant RSI Strategy by SparkyFlary | rsi | v4 strategy | −11.4% | 8 | 20.5% |
| RSI Classic Strategy (by Coinrule) by Coinrule | rsi | v4 strategy | +7.5% | 6 | 7.2% |
| RSITrendStrategy by email_analysts | rsi | v4 strategy | +69.2% | 22 | 38.6% |
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.
- 3 settings use the untyped input() (line 13, 14, 15). 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 26 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 13: input() → input.int()
- Line 14: input() → input.int()
- Line 15: input() → input.int()
⋯//////////////////////////////////////////-length = input( 14 )+length = input.int( 14 )-overSold = input( 30 )+overSold = input.int( 30 )-overBought = input( 70 )+overBought = input.int( 70 )price = close⋯Does it run in Pine Script v6?
Yes
Same 26 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © RingsCherrY-//@version=5+//@version=6leverage = input.int(2,title='Leverage',minval = 1, maxval = 100 ,step = 1)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 10 ms | Yes 1,000 bars in 67 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/// © RingsCherrY//@version=5leverage = input.int(2,title='Leverage',minval = 1, maxval = 100 ,step = 1)// As the margin here must be a constant, can not be a variable// so in adjusting the leverage at the same time you need to adjust the margin in the settings or source code at the same time// initially set to 80% margin rate that burst// 1./2*80 2 == leverage, 80 == margin rate that burststrategy("How to use Leverage in PineScript", overlay=true, pyramiding=1, initial_capital=1000000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, calc_on_order_fills=false, slippage=0, commission_type=strategy.commission.percent, commission_value=0.04,margin_long = 1./2*80,margin_short = 1./2*80)//////////////////////////////////////////////////////////Indicators//////////////////////////////////////////////////////////length = input( 14 )overSold = input( 30 )overBought = input( 70 )price = closevrsi = ta.rsi(price, length)co = ta.crossover(vrsi, overSold)cu = ta.crossunder(vrsi, overBought)///////////////////////////////////////////////////////////Leverage/////////////////////////////////////////////////////////////The number of decimal places for each position opening, i.e., the accuracyprecision = input.int(1,title='Precision')//Leverage, the default is 1, here is not recommended to open a high leverage//Calculate the number of open contracts, here using equity for calculation, considering that everyone compound interest is used for trading equityLev = math.max(math.round(strategy.equity * leverage / close , precision), 0)if (not na(vrsi)) if (co) strategy.entry("RsiLE", strategy.long,qty = Lev, comment="RsiLE") if (cu) strategy.entry("RsiSE", strategy.short,qty = Lev, comment="RsiSE")//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)