Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (5)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOctRSI$353,257$2,025,273Equity from $1,000,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.
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.

SettingDefaultType and rangeFeeds
Leverage2int · min 1 · max 100 · step 1Lev
Length (no label in the script)14intvrsi
Over Sold (no label in the script)30intco
Over Bought (no label in the script)70intcu
Precision1intLev

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.

lengthLengthsetting · 14overSoldOver Soldsetting · 30overBoughtOver Boughtsetting · 70closecloseprice dataleverageLeveragesetting · 2precisionPrecisionsetting · 1vrsiRSI→ vrsicoCrossover→ cocuCrossunder→ cupricepricevalueLevLevvalueentry:RsiLEOpen RsiLEstrategy.entryentry:RsiSEOpen RsiSEstrategy.entry

Order rules

OrderFires whenBy defaultLine
Open RsiLE long(co)On30
Open RsiSE short(cu)On32

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.

4/4trades 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−59.4%+102.5%2623%0.3783.9%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−39.6%+5.5%1527%0.4652.8%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−95.2%+9.8%329%0.4097.2%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−59.3%+8.4%1421%0.3872.1%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−79.6%+16.7%1921%0.2496.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+6.1%+21.5%1839%1.0557.6%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−95.2%+148.3%268%0.0696.6%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−17.1%+17.1%1030%0.7756.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 barsTradesWonP&L
Rising market60%−$507,826.00
Sideways market1724%−$158,291.00
Falling market367%$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.

0%82%
All 26 trades
SideEntryExitBarsP&L
short24 Feb 2024 50,744.2026 Feb 2024 54,910.002−$16,832.40
short24 Feb 2024 50,744.2027 Feb 2024 57,588.203−$82,648.00
short24 Feb 2024 50,744.2028 Feb 2024 64,000.004−$13,301.70
short24 Feb 2024 50,744.204 Mar 2024 68,499.009−$284,841.00
short24 Feb 2024 50,744.2026 Jun 2024 61,806.00123−$71,084.00
long26 Jun 2024 61,806.005 Aug 2024 49,000.0040−$51,401.30
long26 Jun 2024 61,806.0022 Oct 2024 67,377.50118$72,861.60
short22 Oct 2024 67,377.5029 Oct 2024 73,620.107−$25,196.10
short22 Oct 2024 67,377.5010 Nov 2024 81,500.0019−$56,728.20
short22 Oct 2024 67,377.5011 Nov 2024 89,530.5020−$22,215.80
short22 Oct 2024 67,377.5013 Nov 2024 93,265.6022−$25,952.40
short22 Oct 2024 67,377.5021 Nov 2024 98,988.0030−$31,677.00
short22 Oct 2024 67,377.505 Dec 2024 104,088.0044−$36,779.10
short22 Oct 2024 67,377.5016 Dec 2024 107,793.0055−$40,485.60
short22 Oct 2024 67,377.502 Mar 2025 86,064.50131−$63,744.60
long2 Mar 2025 86,064.504 May 2025 95,856.4063$56,370.80
short4 May 2025 95,856.408 May 2025 104,146.004−$8,369.34
short4 May 2025 95,856.4021 May 2025 110,797.0017−$15,023.60
short4 May 2025 95,856.4010 Jul 2025 116,868.0067−$21,096.70
short4 May 2025 95,856.4013 Aug 2025 123,668.00101−$27,899.20
short4 May 2025 95,856.4019 Nov 2025 92,960.80199$6,768.15
long19 Nov 2025 92,960.8016 Jan 2026 95,604.8058$13,356.50
short16 Jan 2026 95,604.807 Feb 2026 70,580.3022$132,278.00
long7 Feb 2026 70,580.305 Jun 2026 59,130.90118−$11,501.20
long7 Feb 2026 70,580.3031 Aug 2026 77,682.00205$70,424.40
short31 Aug 2026 77,682.0021 Sep 2026 87,395.7021−$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.

ScriptSharesTypeNetTradesMax drawdown
How to use Leverage in PineScript this page—v5 strategy−59.4%2683.9%
Triple RSI strategy by Trade_by_DBrsiv5 strategy+3.2%23.3%
Hobbiecode - RSI + Close previous day by hobbiecodersiv5 strategy+1.2%634.2%
dirt cheap yet elegant RSI Strategy by SparkyFlaryrsiv4 strategy−11.4%820.5%
RSI Classic Strategy (by Coinrule) by Coinrulersiv4 strategy+7.5%67.2%
RSITrendStrategy by email_analystsrsiv4 strategy+69.2%2238.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.

Clean v5

VerifiedDownload the v5 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.

Download the v6 script

⋯// © RingsCherrY-//@version=5+//@version=6leverage = input.int(2,title='Leverage',minval = 1, maxval = 100 ,step = 1)⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 10 msYes 1,000 bars in 67 msNot measured
PineTSNot testedNot testedNot 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)