Portfolio Performance - Effects of Rebalancing
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Skywalking2874
//@version=5

strategy("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 < exittime
exitcondition = time >= exittime
strategy.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.00
portfolio_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.00
low_threshold1 = input.float(defval=1, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold1", group="threshold 1")/100.00

high_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.00
low_threshold2 = input.float(defval=2.5, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold2", group="threshold 2")/100.00

high_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.00
low_threshold3 = input.float(defval=5, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold3", group="threshold 3")/100.00

high_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.00
low_threshold4 = input.float(defval=10, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold4", group="threshold 4")/100.00

high_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.00
low_threshold5 = input.float(defval=15, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold5", group="threshold 5")/100.00

high_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.00
low_threshold6 = input.float(defval=20, title="Low (%)", minval=0.0, maxval=1000.00, step=0.1, inline="threshold6", group="threshold 6")/100.00

high_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.00
low_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) : 2524608000000
bar_count = ta.barssince(time <= trade_open_time)

//threshold group 1
threshold_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_holdings
threshold_group_1_asset2_current_holdings = threshold_group_1_asset2_initial_holdings
threshold_group_1_asset1_current_holdings := bar_count > 1 ? threshold_group_1_asset1_current_holdings[1] : threshold_group_1_asset1_initial_holdings
threshold_group_1_asset2_current_holdings := bar_count > 1 ? threshold_group_1_asset2_current_holdings[1] : threshold_group_1_asset2_initial_holdings

threshold_group_1_asset1_value = threshold_group_1_asset1_current_holdings*close
threshold_group_1_asset2_value = threshold_group_1_asset2_current_holdings*second_symbol

threshold_group_1_portfolio_value = threshold_group_1_asset1_value + threshold_group_1_asset2_value

threshold_group_1_asset1_weight = threshold_group_1_asset1_value/threshold_group_1_portfolio_value
threshold_group_1_asset2_weight = threshold_group_1_asset2_value/threshold_group_1_portfolio_value

threshold_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_move2
else 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_move2
else 
    threshold_group_1_asset1_current_holdings:= threshold_group_1_asset1_current_holdings
    threshold_group_1_asset2_current_holdings:= threshold_group_1_asset2_current_holdings

threshold_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 2
threshold_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_holdings
threshold_group_2_asset2_current_holdings = threshold_group_2_asset2_initial_holdings
threshold_group_2_asset1_current_holdings := bar_count > 1 ? threshold_group_2_asset1_current_holdings[1] : threshold_group_2_asset1_initial_holdings
threshold_group_2_asset2_current_holdings := bar_count > 1 ? threshold_group_2_asset2_current_holdings[1] : threshold_group_2_asset2_initial_holdings

threshold_group_2_asset1_value = threshold_group_2_asset1_current_holdings*close
threshold_group_2_asset2_value = threshold_group_2_asset2_current_holdings*second_symbol

threshold_group_2_portfolio_value = threshold_group_2_asset1_value + threshold_group_2_asset2_value

threshold_group_2_asset1_weight = threshold_group_2_asset1_value/threshold_group_2_portfolio_value
threshold_group_2_asset2_weight = threshold_group_2_asset2_value/threshold_group_2_portfolio_value

threshold_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_move2
else 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_move2
else 
    threshold_group_2_asset1_current_holdings:= threshold_group_2_asset1_current_holdings
    threshold_group_2_asset2_current_holdings:= threshold_group_2_asset2_current_holdings

threshold_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 3
threshold_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_holdings
threshold_group_3_asset2_current_holdings = threshold_group_3_asset2_initial_holdings
threshold_group_3_asset1_current_holdings := bar_count > 1 ? threshold_group_3_asset1_current_holdings[1] : threshold_group_3_asset1_initial_holdings
threshold_group_3_asset2_current_holdings := bar_count > 1 ? threshold_group_3_asset2_current_holdings[1] : threshold_group_3_asset2_initial_holdings

threshold_group_3_asset1_value = threshold_group_3_asset1_current_holdings*close
threshold_group_3_asset2_value = threshold_group_3_asset2_current_holdings*second_symbol

threshold_group_3_portfolio_value = threshold_group_3_asset1_value + threshold_group_3_asset2_value

threshold_group_3_asset1_weight = threshold_group_3_asset1_value/threshold_group_3_portfolio_value
threshold_group_3_asset2_weight = threshold_group_3_asset2_value/threshold_group_3_portfolio_value

threshold_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_move2
else 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_move2
else 
    threshold_group_3_asset1_current_holdings:= threshold_group_3_asset1_current_holdings
    threshold_group_3_asset2_current_holdings:= threshold_group_3_asset2_current_holdings

threshold_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 4
threshold_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_holdings
threshold_group_4_asset2_current_holdings = threshold_group_4_asset2_initial_holdings
threshold_group_4_asset1_current_holdings := bar_count > 1 ? threshold_group_4_asset1_current_holdings[1] : threshold_group_4_asset1_initial_holdings
threshold_group_4_asset2_current_holdings := bar_count > 1 ? threshold_group_4_asset2_current_holdings[1] : threshold_group_4_asset2_initial_holdings

threshold_group_4_asset1_value = threshold_group_4_asset1_current_holdings*close
threshold_group_4_asset2_value = threshold_group_4_asset2_current_holdings*second_symbol

threshold_group_4_portfolio_value = threshold_group_4_asset1_value + threshold_group_4_asset2_value

threshold_group_4_asset1_weight = threshold_group_4_asset1_value/threshold_group_4_portfolio_value
threshold_group_4_asset2_weight = threshold_group_4_asset2_value/threshold_group_4_portfolio_value

threshold_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_move2
else 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_move2
else 
    threshold_group_4_asset1_current_holdings:= threshold_group_4_asset1_current_holdings
    threshold_group_4_asset2_current_holdings:= threshold_group_4_asset2_current_holdings

threshold_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 5
threshold_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_holdings
threshold_group_5_asset2_current_holdings = threshold_group_5_asset2_initial_holdings
threshold_group_5_asset1_current_holdings := bar_count > 1 ? threshold_group_5_asset1_current_holdings[1] : threshold_group_5_asset1_initial_holdings
threshold_group_5_asset2_current_holdings := bar_count > 1 ? threshold_group_5_asset2_current_holdings[1] : threshold_group_5_asset2_initial_holdings

threshold_group_5_asset1_value = threshold_group_5_asset1_current_holdings*close
threshold_group_5_asset2_value = threshold_group_5_asset2_current_holdings*second_symbol

threshold_group_5_portfolio_value = threshold_group_5_asset1_value + threshold_group_5_asset2_value

threshold_group_5_asset1_weight = threshold_group_5_asset1_value/threshold_group_5_portfolio_value
threshold_group_5_asset2_weight = threshold_group_5_asset2_value/threshold_group_5_portfolio_value

threshold_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_move2
else 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_move2
else 
    threshold_group_5_asset1_current_holdings:= threshold_group_5_asset1_current_holdings
    threshold_group_5_asset2_current_holdings:= threshold_group_5_asset2_current_holdings

threshold_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 6
threshold_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_holdings
threshold_group_6_asset2_current_holdings = threshold_group_6_asset2_initial_holdings
threshold_group_6_asset1_current_holdings := bar_count > 1 ? threshold_group_6_asset1_current_holdings[1] : threshold_group_6_asset1_initial_holdings
threshold_group_6_asset2_current_holdings := bar_count > 1 ? threshold_group_6_asset2_current_holdings[1] : threshold_group_6_asset2_initial_holdings

threshold_group_6_asset1_value = threshold_group_6_asset1_current_holdings*close
threshold_group_6_asset2_value = threshold_group_6_asset2_current_holdings*second_symbol

threshold_group_6_portfolio_value = threshold_group_6_asset1_value + threshold_group_6_asset2_value

threshold_group_6_asset1_weight = threshold_group_6_asset1_value/threshold_group_6_portfolio_value
threshold_group_6_asset2_weight = threshold_group_6_asset2_value/threshold_group_6_portfolio_value

threshold_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_move2
else 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_move2
else 
    threshold_group_6_asset1_current_holdings:= threshold_group_6_asset1_current_holdings
    threshold_group_6_asset2_current_holdings:= threshold_group_6_asset2_current_holdings

threshold_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 7
threshold_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_holdings
threshold_group_7_asset2_current_holdings = threshold_group_7_asset2_initial_holdings
threshold_group_7_asset1_current_holdings := bar_count > 1 ? threshold_group_7_asset1_current_holdings[1] : threshold_group_7_asset1_initial_holdings
threshold_group_7_asset2_current_holdings := bar_count > 1 ? threshold_group_7_asset2_current_holdings[1] : threshold_group_7_asset2_initial_holdings

threshold_group_7_asset1_value = threshold_group_7_asset1_current_holdings*close
threshold_group_7_asset2_value = threshold_group_7_asset2_current_holdings*second_symbol

threshold_group_7_portfolio_value = threshold_group_7_asset1_value + threshold_group_7_asset2_value

threshold_group_7_asset1_weight = threshold_group_7_asset1_value/threshold_group_7_portfolio_value
threshold_group_7_asset2_weight = threshold_group_7_asset2_value/threshold_group_7_portfolio_value

threshold_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_move2
else 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_move2
else 
    threshold_group_7_asset1_current_holdings:= threshold_group_7_asset1_current_holdings
    threshold_group_7_asset2_current_holdings:= threshold_group_7_asset2_current_holdings

threshold_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 Asset
percent_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) : 2524608000000

transparant = color.rgb(0, 0, 0, 100)

//Color Palette 
base_color = time >= trade_open_time ? color.new(#001eff, 0) : transparant

threshold_color1 = time >= trade_open_time ? color.new(#10B22D, 0) : transparant
threshold_color2 = time >= trade_open_time ? color.new(#00FF1E, 0) : transparant
threshold_color3 = time >= trade_open_time ? color.new(#FFFB00, 0) : transparant
threshold_color4 = time >= trade_open_time ? color.new(#ffa900, 0) : transparant
threshold_color5 = time >= trade_open_time ?color.new(#FF6600, 0) : transparant
threshold_color6 = time >= trade_open_time ? color.new(#FF0000, 0) : transparant
threshold_color7 = time >= trade_open_time ? color.new(#9C0000, 0) : transparant

plot(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)


