Pine Script v5 · Strategy
Portfolio Performance - Effects of Rebalancing
A Pine Script v5 strategy with no technical-analysis calculation. It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 28 settings; defaults include Prefix BATS, Ticker AGG and Initial Investment ($) 100000.
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 Portfolio Performance - Effects of Rebalancing repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Portfolio Performance - Effects of Rebalancing do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its code limits trading to a date window, which can leave this period out. It closed no trades on the other markets either. A backtest, not investment advice.
- Does Portfolio Performance - Effects of Rebalancing run in Pine Script v6?
- Not tested
- Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download.
- Does Portfolio Performance - Effects of Rebalancing use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Portfolio Performance - Effects of Rebalancing run in Zeal Terminal?
- Yes
- It compiles in 111 ms and runs 1,000 bars in 150 ms, in the browser.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
Needs BATS:AGG D data, not available: not a Binance spot market Values that depend on it are empty on this chart.
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
One run with the author's default settings, $1,000,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
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.
-
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 10, 11. The upgraded version below moves each call inside an if block.
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 |
|---|---|---|---|
| Time Interval | |||
| Entry TimeEnter the starting date | timestamp("2021-12-16") | time | entrycondition |
| Exit TimeEnter the ending date | timestamp("2021-12-17") | time | entrycondition exitcondition |
| Second Asset | |||
| Prefix | BATS | string | second_ticker |
| TickerInput the prefix and ticker of the second asset that makes up the portfolio | AGG | string | second_ticker |
| Portfolio | |||
| Initial Investment ($)Input the dollar value of the inital investment | 100000 | float · min 0 · step 0.1 | threshold_group_1_asset1_initial_holdings threshold_group_1_asset2_initial_holdings threshold_group_2_asset1_initial_holdings threshold_group_2_asset2_initial_holdings threshold_group_3_asset1_initial_holdings threshold_group_3_asset2_initial_holdings threshold_group_4_asset1_initial_holdings threshold_group_4_asset2_initial_holdings threshold_group_5_asset1_initial_holdings threshold_group_5_asset2_initial_holdings threshold_group_6_asset1_initial_holdings threshold_group_6_asset2_initial_holdings threshold_group_7_asset1_initial_holdings threshold_group_7_asset2_initial_holdings |
| First Weight (%) | 80.00 | float · min 0 · max 100 · step 0.1 | threshold_group_1_asset1_initial_holdings threshold_group_1_delta1 threshold_group_2_asset1_initial_holdings threshold_group_2_delta1 threshold_group_3_asset1_initial_holdings threshold_group_3_delta1 threshold_group_4_asset1_initial_holdings threshold_group_4_delta1 threshold_group_5_asset1_initial_holdings threshold_group_5_delta1 threshold_group_6_asset1_initial_holdings threshold_group_6_delta1 threshold_group_7_asset1_initial_holdings threshold_group_7_delta1 line 55 line 58 line 81 line 84 line 107 line 110 line 133 line 136 line 159 line 162 line 185 line 188 line 211 line 214 |
| Second Weight (%)Input the weight of each asset relative to the portfolio | 20.00 | float · min 0 · max 100 · step 0.1 | threshold_group_1_asset2_initial_holdings threshold_group_1_delta2 threshold_group_2_asset2_initial_holdings threshold_group_2_delta2 threshold_group_3_asset2_initial_holdings threshold_group_3_delta2 threshold_group_4_asset2_initial_holdings threshold_group_4_delta2 threshold_group_5_asset2_initial_holdings threshold_group_5_delta2 threshold_group_6_asset2_initial_holdings threshold_group_6_delta2 threshold_group_7_asset2_initial_holdings threshold_group_7_delta2 |
| threshold 1 | |||
| High (%)Input the low and high rebalancing thresholds | 1 | float · min 0.0 · max 1000.00 · step 0.1 | line 55 |
| Low (%) | 1 | float · min 0.0 · max 1000.00 · step 0.1 | line 58 |
| Plot Threshold 1 Return | true | bool | Threshold 1 Percent Return |
| threshold 2 | |||
| High (%)Input the low and high rebalancing thresholds | 2.5 | float · min 0.0 · max 1000.00 · step 0.1 | line 81 |
| Low (%) | 2.5 | float · min 0.0 · max 1000.00 · step 0.1 | line 84 |
| Plot Threshold 2 Return | true | bool | Threshold 2 Percent Return |
| threshold 3 | |||
| High (%)Input the low and high rebalancing thresholds | 5 | float · min 0.0 · max 1000.00 · step 0.1 | line 107 |
| Low (%) | 5 | float · min 0.0 · max 1000.00 · step 0.1 | line 110 |
| Plot Threshold 3 Return | true | bool | Threshold 3 Percent Return |
| threshold 4 | |||
| High (%)Input the low and high rebalancing thresholds | 10 | float · min 0.0 · max 1000.00 · step 0.1 | line 133 |
| Low (%) | 10 | float · min 0.0 · max 1000.00 · step 0.1 | line 136 |
| Plot Threshold 4 Return | true | bool | Threshold 4 Percent Return |
| threshold 5 | |||
| High (%)Input the low and high rebalancing thresholds | 15 | float · min 0.0 · max 1000.00 · step 0.1 | line 159 |
| Low (%) | 15 | float · min 0.0 · max 1000.00 · step 0.1 | line 162 |
| Plot Threshold 5 Return | true | bool | Threshold 5 Percent Return |
| threshold 6 | |||
| High (%)Input the low and high rebalancing thresholds | 20 | float · min 0.0 · max 1000.00 · step 0.1 | line 185 |
| Low (%) | 20 | float · min 0.0 · max 1000.00 · step 0.1 | line 188 |
| Plot Threshold 6 Return | true | bool | Threshold 6 Percent Return |
| threshold 7 | |||
| High (%)Input the low and high rebalancing thresholds | 30 | float · min 0.0 · max 1000.00 · step 0.1 | line 211 |
| Low (%) | 30 | float · min 0.0 · max 1000.00 · step 0.1 | line 214 |
| Plot Threshold 7 Return | true | bool | Threshold 7 Percent Return |
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.
The script has 113 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 Buy long | entryconditiontime >= entrytime and time < exittime | On | 10 |
| Close | exitconditiontime >= exittime | On | 11 |
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.
- ClearHigher-timeframe data1 request.security call(s), none with lookahead on.
- 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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
Not run on other markets: the script requests BATS:AGG D, and that data is not available here (not a Binance spot market). Its results would rest on empty values.
Similar scripts
No other indicator or strategy here shares enough of its calculations.
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.
- 2 strategy calls use the when = argument (line 10, 11). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 10: when = entrycondition → if block
- Line 11: when = exitcondition → if block
⋯exitcondition = time >= exittime-strategy.entry("Buy", strategy.long, qty=100000/close, when=entrycondition)+if entrycondition+ strategy.entry("Buy", strategy.long, qty=100000/close)-strategy.close_all(when = exitcondition)+if exitcondition+ strategy.close_all()//****************************************************************** Define the second asset ******************************************************************//⋯Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © Skywalking2874-//@version=5+//@version=6strategy("Portfolio Performance - Effects of Rebalancing", overlay=false, margin_long=100, margin_short=100, process_orders_on_close=true, max_bars_back=5000)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 111 ms | With a workaround 1,000 bars in 150 ms | Not measured |
| PineTS | Not tested | Not tested | 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.
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/// © Skywalking2874//@version=5strategy("Portfolio Performance - Effects of Rebalancing", overlay=false, margin_long=100, margin_short=100, process_orders_on_close=true, max_bars_back=5000)//****************************************************************** Define the time interval ******************************************************************//entrytime = input.time(defval=timestamp("2021-12-16"), title="Entry Time", tooltip="Enter the starting date", confirm=true, group="Time Interval")exittime = input.time(defval=timestamp("2021-12-17"), title="Exit Time", tooltip="Enter the ending date", confirm=true, group="Time Interval")entrycondition = time >= entrytime and time < exittimeexitcondition = time >= exittimestrategy.entry("Buy", strategy.long, qty=100000/close, when=entrycondition)strategy.close_all(when = exitcondition)//****************************************************************** Define the second asset ******************************************************************//second_asset_prefix = input.string(defval = "BATS", title = "Prefix", group="Second Asset", inline="Asset2")second_asset_ticker = input.string(defval = "AGG", title="Ticker", group="Second Asset", inline="Asset2", tooltip="Input the prefix and ticker of the second asset that makes up the portfolio")second_ticker = ticker.new(prefix = second_asset_prefix, ticker = second_asset_ticker, session = session.regular, adjustment = adjustment.dividends)second_symbol = request.security(second_ticker, "D", close)//****************************************************************** Define the portfolio weights ******************************************************************//initial_investment = input.float(defval=100000, title="Initial Investment ($)", minval=0, step=0.1, tooltip="Input the dollar value of the inital investment", group="Portfolio")portfolio_weight1 = input.float(defval=80.00, title="First Weight (%)", minval=0, maxval=100, step=0.1, inline="Asset1", group="Portfolio")/100.00portfolio_weight2 = input.float(defval=20.00, title="Second Weight (%)", minval=0, maxval=100, step=0.1, tooltip="Input the weight of each asset relative to the portfolio", inline="Asset1", group="Portfolio")/100.00//****************************************************************** Define the rebalancing thresholds ******************************************************************************//high_threshold1 = input.float(defval=1, title="High (%)", minval=0.0, maxval=1000.00, step=0.1, tooltip="Input the low and high rebalancing thresholds", inline="threshold1", group="threshold 1")/100.00low_threshold1 = input.float(defval=1, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold1", group="threshold 1")/100.00high_threshold2 = input.float(defval=2.5, title="High (%)", minval=0.0, maxval=1000.00, step=0.1, tooltip="Input the low and high rebalancing thresholds", inline="threshold2", group="threshold 2")/100.00low_threshold2 = input.float(defval=2.5, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold2", group="threshold 2")/100.00high_threshold3 = input.float(defval=5, title="High (%)", minval=0.0, maxval=1000.00, step=0.1, tooltip="Input the low and high rebalancing thresholds", inline="threshold3", group="threshold 3")/100.00low_threshold3 = input.float(defval=5, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold3", group="threshold 3")/100.00high_threshold4 = input.float(defval=10, title="High (%)", minval=0.0, maxval=1000.00, step=0.1, tooltip="Input the low and high rebalancing thresholds", inline="threshold4", group="threshold 4")/100.00low_threshold4 = input.float(defval=10, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold4", group="threshold 4")/100.00high_threshold5 = input.float(defval=15, title="High (%)", minval=0.0, maxval=1000.00, step=0.1, tooltip="Input the low and high rebalancing thresholds", inline="threshold5", group="threshold 5")/100.00low_threshold5 = input.float(defval=15, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold5", group="threshold 5")/100.00high_threshold6 = input.float(defval=20, title="High (%)", minval=0.0, maxval=1000.00, step=0.1, tooltip="Input the low and high rebalancing thresholds", inline="threshold6", group="threshold 6")/100.00low_threshold6 = input.float(defval=20, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold6", group="threshold 6")/100.00high_threshold7 = input.float(defval=30, title="High (%)", minval=0.0, maxval=1000.00, step=0.1, tooltip="Input the low and high rebalancing thresholds", inline="threshold7", group="threshold 7")/100.00low_threshold7 = input.float(defval=30, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold7", group="threshold 7")/100.00//****************************************************************** Calculate %Return ******************************************************************//trade_open_time = time >= strategy.opentrades.entry_time(strategy.opentrades-1) ? strategy.opentrades.entry_time(strategy.opentrades-1) : 2524608000000bar_count = ta.barssince(time <= trade_open_time)//threshold group 1threshold_group_1_asset1_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight1/close[bar_count]) :int(initial_investment*portfolio_weight1/close)threshold_group_1_asset2_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight2/second_symbol[bar_count]) : int(initial_investment*portfolio_weight2/second_symbol)threshold_group_1_asset1_current_holdings = threshold_group_1_asset1_initial_holdingsthreshold_group_1_asset2_current_holdings = threshold_group_1_asset2_initial_holdingsthreshold_group_1_asset1_current_holdings := bar_count > 1 ? threshold_group_1_asset1_current_holdings[1] : threshold_group_1_asset1_initial_holdingsthreshold_group_1_asset2_current_holdings := bar_count > 1 ? threshold_group_1_asset2_current_holdings[1] : threshold_group_1_asset2_initial_holdingsthreshold_group_1_asset1_value = threshold_group_1_asset1_current_holdings*closethreshold_group_1_asset2_value = threshold_group_1_asset2_current_holdings*second_symbolthreshold_group_1_portfolio_value = threshold_group_1_asset1_value + threshold_group_1_asset2_valuethreshold_group_1_asset1_weight = threshold_group_1_asset1_value/threshold_group_1_portfolio_valuethreshold_group_1_asset2_weight = threshold_group_1_asset2_value/threshold_group_1_portfolio_valuethreshold_group_1_delta1 = math.abs(threshold_group_1_asset1_value - (threshold_group_1_portfolio_value * portfolio_weight1))threshold_group_1_delta2 = math.abs(threshold_group_1_asset2_value - (threshold_group_1_portfolio_value * portfolio_weight2))threshold_group_1_number_of_assets_to_move1 = int(threshold_group_1_delta1/close)threshold_group_1_number_of_assets_to_move2 = int(threshold_group_1_delta2/second_symbol)if threshold_group_1_asset1_weight >= (portfolio_weight1+high_threshold1) threshold_group_1_asset1_current_holdings:= threshold_group_1_asset1_current_holdings - threshold_group_1_number_of_assets_to_move1 threshold_group_1_asset2_current_holdings:= threshold_group_1_asset2_current_holdings + threshold_group_1_number_of_assets_to_move2else if threshold_group_1_asset1_weight <= (portfolio_weight1-low_threshold1) threshold_group_1_asset1_current_holdings:= threshold_group_1_asset1_current_holdings + threshold_group_1_number_of_assets_to_move1 threshold_group_1_asset2_current_holdings:= threshold_group_1_asset2_current_holdings - threshold_group_1_number_of_assets_to_move2else threshold_group_1_asset1_current_holdings:= threshold_group_1_asset1_current_holdings threshold_group_1_asset2_current_holdings:= threshold_group_1_asset2_current_holdingsthreshold_group_1_portfolio_percent_return = ((threshold_group_1_portfolio_value - threshold_group_1_portfolio_value[bar_count])/threshold_group_1_portfolio_value[bar_count])*100//threshold group 2threshold_group_2_asset1_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight1/close[bar_count]) :int(initial_investment*portfolio_weight1/close)threshold_group_2_asset2_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight2/second_symbol[bar_count]) : int(initial_investment*portfolio_weight2/second_symbol)threshold_group_2_asset1_current_holdings = threshold_group_2_asset1_initial_holdingsthreshold_group_2_asset2_current_holdings = threshold_group_2_asset2_initial_holdingsthreshold_group_2_asset1_current_holdings := bar_count > 1 ? threshold_group_2_asset1_current_holdings[1] : threshold_group_2_asset1_initial_holdingsthreshold_group_2_asset2_current_holdings := bar_count > 1 ? threshold_group_2_asset2_current_holdings[1] : threshold_group_2_asset2_initial_holdingsthreshold_group_2_asset1_value = threshold_group_2_asset1_current_holdings*closethreshold_group_2_asset2_value = threshold_group_2_asset2_current_holdings*second_symbolthreshold_group_2_portfolio_value = threshold_group_2_asset1_value + threshold_group_2_asset2_valuethreshold_group_2_asset1_weight = threshold_group_2_asset1_value/threshold_group_2_portfolio_valuethreshold_group_2_asset2_weight = threshold_group_2_asset2_value/threshold_group_2_portfolio_valuethreshold_group_2_delta1 = math.abs(threshold_group_2_asset1_value - (threshold_group_2_portfolio_value * portfolio_weight1))threshold_group_2_delta2 = math.abs(threshold_group_2_asset2_value - (threshold_group_2_portfolio_value * portfolio_weight2))threshold_group_2_number_of_assets_to_move1 = int(threshold_group_2_delta1/close)threshold_group_2_number_of_assets_to_move2 = int(threshold_group_2_delta2/second_symbol)if threshold_group_2_asset1_weight >= (portfolio_weight1+high_threshold2) threshold_group_2_asset1_current_holdings:= threshold_group_2_asset1_current_holdings - threshold_group_2_number_of_assets_to_move1 threshold_group_2_asset2_current_holdings:= threshold_group_2_asset2_current_holdings + threshold_group_2_number_of_assets_to_move2else if threshold_group_2_asset1_weight <= (portfolio_weight1-low_threshold2) threshold_group_2_asset1_current_holdings:= threshold_group_2_asset1_current_holdings + threshold_group_2_number_of_assets_to_move1 threshold_group_2_asset2_current_holdings:= threshold_group_2_asset2_current_holdings - threshold_group_2_number_of_assets_to_move2else threshold_group_2_asset1_current_holdings:= threshold_group_2_asset1_current_holdings threshold_group_2_asset2_current_holdings:= threshold_group_2_asset2_current_holdingsthreshold_group_2_portfolio_percent_return = ((threshold_group_2_portfolio_value - threshold_group_2_portfolio_value[bar_count])/threshold_group_2_portfolio_value[bar_count])*100//threshold group 3threshold_group_3_asset1_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight1/close[bar_count]) :int(initial_investment*portfolio_weight1/close)threshold_group_3_asset2_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight2/second_symbol[bar_count]) : int(initial_investment*portfolio_weight2/second_symbol)threshold_group_3_asset1_current_holdings = threshold_group_3_asset1_initial_holdingsthreshold_group_3_asset2_current_holdings = threshold_group_3_asset2_initial_holdingsthreshold_group_3_asset1_current_holdings := bar_count > 1 ? threshold_group_3_asset1_current_holdings[1] : threshold_group_3_asset1_initial_holdingsthreshold_group_3_asset2_current_holdings := bar_count > 1 ? threshold_group_3_asset2_current_holdings[1] : threshold_group_3_asset2_initial_holdingsthreshold_group_3_asset1_value = threshold_group_3_asset1_current_holdings*closethreshold_group_3_asset2_value = threshold_group_3_asset2_current_holdings*second_symbolthreshold_group_3_portfolio_value = threshold_group_3_asset1_value + threshold_group_3_asset2_valuethreshold_group_3_asset1_weight = threshold_group_3_asset1_value/threshold_group_3_portfolio_valuethreshold_group_3_asset2_weight = threshold_group_3_asset2_value/threshold_group_3_portfolio_valuethreshold_group_3_delta1 = math.abs(threshold_group_3_asset1_value - (threshold_group_3_portfolio_value * portfolio_weight1))threshold_group_3_delta2 = math.abs(threshold_group_3_asset2_value - (threshold_group_3_portfolio_value * portfolio_weight2))threshold_group_3_number_of_assets_to_move1 = int(threshold_group_3_delta1/close)threshold_group_3_number_of_assets_to_move2 = int(threshold_group_3_delta2/second_symbol)if threshold_group_3_asset1_weight >= (portfolio_weight1+high_threshold3) threshold_group_3_asset1_current_holdings:= threshold_group_3_asset1_current_holdings - threshold_group_3_number_of_assets_to_move1 threshold_group_3_asset2_current_holdings:= threshold_group_3_asset2_current_holdings + threshold_group_3_number_of_assets_to_move2else if threshold_group_3_asset1_weight <= (portfolio_weight1-low_threshold3) threshold_group_3_asset1_current_holdings:= threshold_group_3_asset1_current_holdings + threshold_group_3_number_of_assets_to_move1 threshold_group_3_asset2_current_holdings:= threshold_group_3_asset2_current_holdings - threshold_group_3_number_of_assets_to_move2else threshold_group_3_asset1_current_holdings:= threshold_group_3_asset1_current_holdings threshold_group_3_asset2_current_holdings:= threshold_group_3_asset2_current_holdingsthreshold_group_3_portfolio_percent_return = ((threshold_group_3_portfolio_value - threshold_group_3_portfolio_value[bar_count])/threshold_group_3_portfolio_value[bar_count])*100//threshold group 4threshold_group_4_asset1_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight1/close[bar_count]) :int(initial_investment*portfolio_weight1/close)threshold_group_4_asset2_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight2/second_symbol[bar_count]) : int(initial_investment*portfolio_weight2/second_symbol)threshold_group_4_asset1_current_holdings = threshold_group_4_asset1_initial_holdingsthreshold_group_4_asset2_current_holdings = threshold_group_4_asset2_initial_holdingsthreshold_group_4_asset1_current_holdings := bar_count > 1 ? threshold_group_4_asset1_current_holdings[1] : threshold_group_4_asset1_initial_holdingsthreshold_group_4_asset2_current_holdings := bar_count > 1 ? threshold_group_4_asset2_current_holdings[1] : threshold_group_4_asset2_initial_holdingsthreshold_group_4_asset1_value = threshold_group_4_asset1_current_holdings*closethreshold_group_4_asset2_value = threshold_group_4_asset2_current_holdings*second_symbolthreshold_group_4_portfolio_value = threshold_group_4_asset1_value + threshold_group_4_asset2_valuethreshold_group_4_asset1_weight = threshold_group_4_asset1_value/threshold_group_4_portfolio_valuethreshold_group_4_asset2_weight = threshold_group_4_asset2_value/threshold_group_4_portfolio_valuethreshold_group_4_delta1 = math.abs(threshold_group_4_asset1_value - (threshold_group_4_portfolio_value * portfolio_weight1))threshold_group_4_delta2 = math.abs(threshold_group_4_asset2_value - (threshold_group_4_portfolio_value * portfolio_weight2))threshold_group_4_number_of_assets_to_move1 = int(threshold_group_4_delta1/close)threshold_group_4_number_of_assets_to_move2 = int(threshold_group_4_delta2/second_symbol)if threshold_group_4_asset1_weight >= (portfolio_weight1+high_threshold4) threshold_group_4_asset1_current_holdings:= threshold_group_4_asset1_current_holdings - threshold_group_4_number_of_assets_to_move1 threshold_group_4_asset2_current_holdings:= threshold_group_4_asset2_current_holdings + threshold_group_4_number_of_assets_to_move2else if threshold_group_4_asset1_weight <= (portfolio_weight1-low_threshold4) threshold_group_4_asset1_current_holdings:= threshold_group_4_asset1_current_holdings + threshold_group_4_number_of_assets_to_move1 threshold_group_4_asset2_current_holdings:= threshold_group_4_asset2_current_holdings - threshold_group_4_number_of_assets_to_move2else threshold_group_4_asset1_current_holdings:= threshold_group_4_asset1_current_holdings threshold_group_4_asset2_current_holdings:= threshold_group_4_asset2_current_holdingsthreshold_group_4_portfolio_percent_return = ((threshold_group_4_portfolio_value - threshold_group_4_portfolio_value[bar_count])/threshold_group_4_portfolio_value[bar_count])*100//threshold group 5threshold_group_5_asset1_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight1/close[bar_count]) :int(initial_investment*portfolio_weight1/close)threshold_group_5_asset2_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight2/second_symbol[bar_count]) : int(initial_investment*portfolio_weight2/second_symbol)threshold_group_5_asset1_current_holdings = threshold_group_5_asset1_initial_holdingsthreshold_group_5_asset2_current_holdings = threshold_group_5_asset2_initial_holdingsthreshold_group_5_asset1_current_holdings := bar_count > 1 ? threshold_group_5_asset1_current_holdings[1] : threshold_group_5_asset1_initial_holdingsthreshold_group_5_asset2_current_holdings := bar_count > 1 ? threshold_group_5_asset2_current_holdings[1] : threshold_group_5_asset2_initial_holdingsthreshold_group_5_asset1_value = threshold_group_5_asset1_current_holdings*closethreshold_group_5_asset2_value = threshold_group_5_asset2_current_holdings*second_symbolthreshold_group_5_portfolio_value = threshold_group_5_asset1_value + threshold_group_5_asset2_valuethreshold_group_5_asset1_weight = threshold_group_5_asset1_value/threshold_group_5_portfolio_valuethreshold_group_5_asset2_weight = threshold_group_5_asset2_value/threshold_group_5_portfolio_valuethreshold_group_5_delta1 = math.abs(threshold_group_5_asset1_value - (threshold_group_5_portfolio_value * portfolio_weight1))threshold_group_5_delta2 = math.abs(threshold_group_5_asset2_value - (threshold_group_5_portfolio_value * portfolio_weight2))threshold_group_5_number_of_assets_to_move1 = int(threshold_group_5_delta1/close)threshold_group_5_number_of_assets_to_move2 = int(threshold_group_5_delta2/second_symbol)if threshold_group_5_asset1_weight >= (portfolio_weight1+high_threshold5) threshold_group_5_asset1_current_holdings:= threshold_group_5_asset1_current_holdings - threshold_group_5_number_of_assets_to_move1 threshold_group_5_asset2_current_holdings:= threshold_group_5_asset2_current_holdings + threshold_group_5_number_of_assets_to_move2else if threshold_group_5_asset1_weight <= (portfolio_weight1-low_threshold5) threshold_group_5_asset1_current_holdings:= threshold_group_5_asset1_current_holdings + threshold_group_5_number_of_assets_to_move1 threshold_group_5_asset2_current_holdings:= threshold_group_5_asset2_current_holdings - threshold_group_5_number_of_assets_to_move2else threshold_group_5_asset1_current_holdings:= threshold_group_5_asset1_current_holdings threshold_group_5_asset2_current_holdings:= threshold_group_5_asset2_current_holdingsthreshold_group_5_portfolio_percent_return = ((threshold_group_5_portfolio_value - threshold_group_5_portfolio_value[bar_count])/threshold_group_5_portfolio_value[bar_count])*100//threshold group 6threshold_group_6_asset1_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight1/close[bar_count]) :int(initial_investment*portfolio_weight1/close)threshold_group_6_asset2_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight2/second_symbol[bar_count]) : int(initial_investment*portfolio_weight2/second_symbol)threshold_group_6_asset1_current_holdings = threshold_group_6_asset1_initial_holdingsthreshold_group_6_asset2_current_holdings = threshold_group_6_asset2_initial_holdingsthreshold_group_6_asset1_current_holdings := bar_count > 1 ? threshold_group_6_asset1_current_holdings[1] : threshold_group_6_asset1_initial_holdingsthreshold_group_6_asset2_current_holdings := bar_count > 1 ? threshold_group_6_asset2_current_holdings[1] : threshold_group_6_asset2_initial_holdingsthreshold_group_6_asset1_value = threshold_group_6_asset1_current_holdings*closethreshold_group_6_asset2_value = threshold_group_6_asset2_current_holdings*second_symbolthreshold_group_6_portfolio_value = threshold_group_6_asset1_value + threshold_group_6_asset2_valuethreshold_group_6_asset1_weight = threshold_group_6_asset1_value/threshold_group_6_portfolio_valuethreshold_group_6_asset2_weight = threshold_group_6_asset2_value/threshold_group_6_portfolio_valuethreshold_group_6_delta1 = math.abs(threshold_group_6_asset1_value - (threshold_group_6_portfolio_value * portfolio_weight1))threshold_group_6_delta2 = math.abs(threshold_group_6_asset2_value - (threshold_group_6_portfolio_value * portfolio_weight2))threshold_group_6_number_of_assets_to_move1 = int(threshold_group_6_delta1/close)threshold_group_6_number_of_assets_to_move2 = int(threshold_group_6_delta2/second_symbol)if threshold_group_6_asset1_weight >= (portfolio_weight1+high_threshold6) threshold_group_6_asset1_current_holdings:= threshold_group_6_asset1_current_holdings - threshold_group_6_number_of_assets_to_move1 threshold_group_6_asset2_current_holdings:= threshold_group_6_asset2_current_holdings + threshold_group_6_number_of_assets_to_move2else if threshold_group_6_asset1_weight <= (portfolio_weight1-low_threshold6) threshold_group_6_asset1_current_holdings:= threshold_group_6_asset1_current_holdings + threshold_group_6_number_of_assets_to_move1 threshold_group_6_asset2_current_holdings:= threshold_group_6_asset2_current_holdings - threshold_group_6_number_of_assets_to_move2else threshold_group_6_asset1_current_holdings:= threshold_group_6_asset1_current_holdings threshold_group_6_asset2_current_holdings:= threshold_group_6_asset2_current_holdingsthreshold_group_6_portfolio_percent_return = ((threshold_group_6_portfolio_value - threshold_group_6_portfolio_value[bar_count])/threshold_group_6_portfolio_value[bar_count])*100//threshold group 7threshold_group_7_asset1_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight1/close[bar_count]) :int(initial_investment*portfolio_weight1/close)threshold_group_7_asset2_initial_holdings = bar_count > 0 ? int(initial_investment*portfolio_weight2/second_symbol[bar_count]) : int(initial_investment*portfolio_weight2/second_symbol)threshold_group_7_asset1_current_holdings = threshold_group_7_asset1_initial_holdingsthreshold_group_7_asset2_current_holdings = threshold_group_7_asset2_initial_holdingsthreshold_group_7_asset1_current_holdings := bar_count > 1 ? threshold_group_7_asset1_current_holdings[1] : threshold_group_7_asset1_initial_holdingsthreshold_group_7_asset2_current_holdings := bar_count > 1 ? threshold_group_7_asset2_current_holdings[1] : threshold_group_7_asset2_initial_holdingsthreshold_group_7_asset1_value = threshold_group_7_asset1_current_holdings*closethreshold_group_7_asset2_value = threshold_group_7_asset2_current_holdings*second_symbolthreshold_group_7_portfolio_value = threshold_group_7_asset1_value + threshold_group_7_asset2_valuethreshold_group_7_asset1_weight = threshold_group_7_asset1_value/threshold_group_7_portfolio_valuethreshold_group_7_asset2_weight = threshold_group_7_asset2_value/threshold_group_7_portfolio_valuethreshold_group_7_delta1 = math.abs(threshold_group_7_asset1_value - (threshold_group_7_portfolio_value * portfolio_weight1))threshold_group_7_delta2 = math.abs(threshold_group_7_asset2_value - (threshold_group_7_portfolio_value * portfolio_weight2))threshold_group_7_number_of_assets_to_move1 = int(threshold_group_7_delta1/close)threshold_group_7_number_of_assets_to_move2 = int(threshold_group_7_delta2/second_symbol)if threshold_group_7_asset1_weight >= (portfolio_weight1+high_threshold7) threshold_group_7_asset1_current_holdings:= threshold_group_7_asset1_current_holdings - threshold_group_7_number_of_assets_to_move1 threshold_group_7_asset2_current_holdings:= threshold_group_7_asset2_current_holdings + threshold_group_7_number_of_assets_to_move2else if threshold_group_7_asset1_weight <= (portfolio_weight1-low_threshold7) threshold_group_7_asset1_current_holdings:= threshold_group_7_asset1_current_holdings + threshold_group_7_number_of_assets_to_move1 threshold_group_7_asset2_current_holdings:= threshold_group_7_asset2_current_holdings - threshold_group_7_number_of_assets_to_move2else threshold_group_7_asset1_current_holdings:= threshold_group_7_asset1_current_holdings threshold_group_7_asset2_current_holdings:= threshold_group_7_asset2_current_holdingsthreshold_group_7_portfolio_percent_return = ((threshold_group_7_portfolio_value - threshold_group_7_portfolio_value[bar_count])/threshold_group_7_portfolio_value[bar_count])*100//Base Assetpercent_return_base_asset = ((close - close[bar_count])/close[bar_count])*100//****************************************************************** Plot ******************************************************************//threshold1_return = input.bool(defval=true, title="Plot Threshold 1 Return", group="threshold 1")threshold2_return = input.bool(defval=true, title="Plot Threshold 2 Return", group="threshold 2")threshold3_return = input.bool(defval=true, title="Plot Threshold 3 Return", group="threshold 3")threshold4_return = input.bool(defval=true, title="Plot Threshold 4 Return", group="threshold 4")threshold5_return = input.bool(defval=true, title="Plot Threshold 5 Return", group="threshold 5")threshold6_return = input.bool(defval=true, title="Plot Threshold 6 Return", group="threshold 6")threshold7_return = input.bool(defval=true, title="Plot Threshold 7 Return", group="threshold 7")trade_close_time = time >= strategy.closedtrades.entry_time(strategy.closedtrades-1) ? strategy.closedtrades.entry_time(strategy.closedtrades-1) : 2524608000000transparant = color.rgb(0, 0, 0, 100)//Color Palette base_color = time >= trade_open_time ? color.new(#001eff, 0) : transparantthreshold_color1 = time >= trade_open_time ? color.new(#10B22D, 0) : transparantthreshold_color2 = time >= trade_open_time ? color.new(#00FF1E, 0) : transparantthreshold_color3 = time >= trade_open_time ? color.new(#FFFB00, 0) : transparantthreshold_color4 = time >= trade_open_time ? color.new(#ffa900, 0) : transparantthreshold_color5 = time >= trade_open_time ?color.new(#FF6600, 0) : transparantthreshold_color6 = time >= trade_open_time ? color.new(#FF0000, 0) : transparantthreshold_color7 = time >= trade_open_time ? color.new(#9C0000, 0) : transparantplot(percent_return_base_asset, title="Return of Base Asset", color=base_color)plot(threshold_group_1_portfolio_percent_return, title="Threshold 1 Percent Return", color=threshold1_return == true ? threshold_color1 : transparant)plot(threshold_group_2_portfolio_percent_return, title="Threshold 2 Percent Return", color=threshold2_return == true ? threshold_color2 : transparant)plot(threshold_group_3_portfolio_percent_return, title="Threshold 3 Percent Return", color=threshold3_return == true ? threshold_color3 : transparant)plot(threshold_group_4_portfolio_percent_return, title="Threshold 4 Percent Return", color=threshold4_return == true ? threshold_color4 : transparant)plot(threshold_group_5_portfolio_percent_return, title="Threshold 5 Percent Return", color=threshold5_return == true ? threshold_color5 : transparant)plot(threshold_group_6_portfolio_percent_return, title="Threshold 6 Percent Return", color=threshold6_return == true ? threshold_color6 : transparant)plot(threshold_group_7_portfolio_percent_return, title="Threshold 7 Percent Return", color=threshold7_return == true ? threshold_color7 : transparant)