Smart Rebalance

This script is based on the portfolio rebalancing strategy. It's designed to work with cryptocurrencies, but it can work with any market.

How portfolio rebalance works?
Let's assume your initial capital is $1000, and you want to distribute it into 4 coins. This script takes the USDT as the stable coin for the initial money, so in case you want other currency, the pairs must be with that fiat as the quote.
Following our example, you would take BTC, ETH, BNB, and FTT. After selecting the coins, it's time to choose how much allocation is on each. Let's put 25% on each. This way, $250 of our capital on each coin.

After selecting the coins and their allocation, you choose the price change ratio for rebalancing. Let's use 1%. Next, you start to watch the markets. The first thing that happens, following our example, is the BTCUSDT price moving 1% up.
That amount hit the ratio of 1% for the rebalance. Hence, you sell 1% of BTC for USDT and redistribute to the other coins, buying 0.25% of each currency to rebalance the portfolio.
Next, ETHUSDT goes 1% down, time to rebalance again. This time, you need to take 0.33% of each other coin and buy ETH, so this way, it's all divided as the chosen allocation.

Why use rebalancing?
Looks easy, right? It is, but very time demanding. Demands even more if you raise the number of coins you want to distribute. Having a system to do that automatically is a must to work efficiently. Rebalancing spreads the risk among multiple currencies. This way, you earn small when it goes up, but you lose small when it goes down.

What this script helps with portfolio rebalance?
This indicator will not buy/sell for you but will help you choose the best markets for your rebalancing. Which coin will work best in that period? Do I need to have more than 8 coins? How much must be my ratio? Those questions you can answer using this indicator.

What this script has?
  • Start and End dates
    The script will work for a certain period. All calculations will be done in that period.
  • Coin Ratio %
    The amount of price movement of each asset that will be used to calculate the rebalancing
  • Initial Capital and Broker Fee
    The amount of capital to be used on the rebalancing and the broker fee you want to use the strategy. The cost will be applied on every trade, buying or selling the coins.
  • Assets, allocations, and colors
    It's possible to select from 2 to 10 assets to be used on the portfolio. Each purchase must have the allocation %. Suppose the sum of the allocations is different from 100%. In that case, a warning message will appear on the chart instead of the statistics.
  • Panel and tooltips
    There is a panel with a summary of the results
  • Set allocations automatically
    There is an option to make the indicator use the daily asset volume from the day before to determine the allocation percentage of each asset. This option is better if you are unsure how much allocation you want to use on each coin.

Use this indicator as a backtest for your rebalancing strategy. The selected market on the chart will not affect the calculation on this indicator, but the time frame will. The higher the time frame, the higher the coin ratio % must be.

About the code
The code is written to use arrays to store the values of each asset, making the calculations on each candle inside the time range. The for-loops are used to reduce the code length and make it easy to change the analysis of all assets. Finally, the script has some comments on the code.

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.


The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?