I want to create weighted candlesticks, based on 5 assets, in Pinescript version 5

39 views Asked by At

I am trying to track the movement of a top 5 bundle of cryptocurrencies. This is a product available on an exchange. In my script, I have requested the OHLC values of the 5 assets. Additionally, I have weighted them by multiplying them according to a certain percentage, thereby creating a weighted OHLC that summarises these 5 assets. I have used the plotcandle() function to plot the weighted OHLC as candlesticks. However, the problem is that the weighted candlesticks only show the 1st asset, which is BTC at 40% of its price. It appears that Pinescript somehow ignores the rest of the calculation. I have debugged the code with ChatGPT and Blackbox, both confirming that the code should work as intended. Anyone any idea on why it is not working? Code posted below.

    //@version=5
    indicator("Finst Top 5 Bundle", overlay=false)

    // Top 5 bundle
    frst_coin = "COINBASE:BTCEUR"
    scnd_coin = "COINBASE:ETHEUR"
    thrd_coin = "COINBASE:SOLEUR"
    frth_coin = "COINBASE:XRPEUR"
    ffth_coin = "COINBASE:ADAEUR"

    // Top 5 % inputs
    frst_coin_perc = input.float(defval = 40.87, title="1st coin %", group="Top 5 Bundle %")
    scnd_coin_perc = input.float(defval = 20.63, title="2nd coin %", group="Top 5 Bundle %")
    thrd_coin_perc = input.float(defval = 14.17, title="3rd coin %", group="Top 5 Bundle %")
    frth_coin_perc = input.float(defval = 12.32, title="4th coin %", group="Top 5 Bundle %")
    ffth_coin_perc = input.float(defval = 12.01, title="5th coin %", group="Top 5 Bundle %")

    //Getting OHLC data on daily timeframe
    frst_coin_open = request.security(frst_coin, "D", open)
    frst_coin_high = request.security(frst_coin, "D", high)
    frst_coin_low = request.security(frst_coin, "D", low)
    frst_coin_close = request.security(frst_coin, "D", close)
    frst_coin_volume = request.security(frst_coin, "D", volume)
    frst_coin_vol_weight_EUR = (frst_coin_volume * frst_coin_perc / 100) * frst_coin_close

    scnd_coin_open = request.security(scnd_coin, "D", open)
    scnd_coin_high = request.security(scnd_coin, "D", high)
    scnd_coin_low = request.security(scnd_coin, "D", low)
    scnd_coin_close = request.security(scnd_coin, "D", close)
    scnd_coin_volume = request.security(scnd_coin, "D", volume)
    scnd_coin_vol_weight_EUR = (scnd_coin_volume * scnd_coin_perc / 100) * scnd_coin_close

    thrd_coin_open = request.security(thrd_coin, "D", open)
    thrd_coin_high = request.security(thrd_coin, "D", high)
    thrd_coin_low = request.security(thrd_coin, "D", low)
    thrd_coin_close = request.security(thrd_coin, "D", close)
    thrd_coin_volume = request.security(thrd_coin, "D", volume)
    thrd_coin_vol_weight_EUR = (thrd_coin_volume * thrd_coin_perc / 100) * thrd_coin_close

    frth_coin_open = request.security(frth_coin, "D", open)
    frth_coin_high = request.security(frth_coin, "D", high)
    frth_coin_low = request.security(frth_coin, "D", low)
    frth_coin_close = request.security(frth_coin, "D", close)
    frth_coin_volume = request.security(frth_coin, "D", volume)
    frth_coin_vol_weight_EUR = (frth_coin_volume * frth_coin_perc / 100) * frth_coin_close

    ffth_coin_open = request.security(ffth_coin, "D", open)
    ffth_coin_high = request.security(ffth_coin, "D", high)
    ffth_coin_low = request.security(ffth_coin, "D", low)
    ffth_coin_close = request.security(ffth_coin, "D", close)
    ffth_coin_volume = request.security(ffth_coin, "D", volume)
    ffth_coin_vol_weight_EUR = (ffth_coin_volume * ffth_coin_perc / 100) * ffth_coin_close

    // Calculating total weighted volume
    tot_weighted_vol_EUR = frst_coin_vol_weight_EUR + scnd_coin_vol_weight_EUR +   thrd_coin_vol_weight_EUR + frth_coin_vol_weight_EUR + ffth_coin_vol_weight_EUR
    vol_plot_div = input(defval = 10000, title = "Divide volume for readability", group =    "Volume")
    disp_weight_vol_EUR = tot_weighted_vol_EUR / vol_plot_div

    // Calculate weighted OHLC values
    weighted_open = ((frst_coin_open * frst_coin_perc / 100) + (scnd_coin_open * scnd_coin_perc /   100) + (thrd_coin_open * thrd_coin_perc / 100) + (frth_coin_open * frth_coin_perc / 100) +     (ffth_coin_open * ffth_coin_perc / 100))

    weighted_high = ((frst_coin_high * frst_coin_perc / 100) + (scnd_coin_high * scnd_coin_perc /   100) + (thrd_coin_high * thrd_coin_perc / 100) + (frth_coin_high * frth_coin_perc / 100) + (ffth_coin_high * ffth_coin_perc / 100))

    weighted_low = ((frst_coin_low * frst_coin_perc / 100) + (scnd_coin_low * scnd_coin_perc / 100) + (thrd_coin_low * thrd_coin_perc / 100) + (frth_coin_low * frth_coin_perc / 100)  + (ffth_coin_low * ffth_coin_perc / 100))

    weighted_close = ((frst_coin_close * frst_coin_perc / 100) + (scnd_coin_close *   scnd_coin_perc / 100) + (thrd_coin_close * thrd_coin_perc / 100) + (frth_coin_close * frth_coin_perc / 100) + (ffth_coin_close * ffth_coin_perc / 100))

    candle_color = if(weighted_close > weighted_open)
    color.green
    else
    color.red

    wick_color = candle_color

    // Plotting weighted candlesticks
    plotcandle(open=weighted_open, high=weighted_high, low=weighted_low, close=weighted_close,   color = candle_color, wickcolor = wick_color, title="Weighted Candlesticks")

2

There are 2 answers

1
Gregory On

The border display may cause the problem. It is black by default in plotcandle(). Use bordercolor = na to hide it. Also, use wickcolor = na if you don’t need wicks to avoid dots.

0
Espen Busman On

I realised my mistake! In the code, the factor with which the asset prices are multiplied is not correct. It is visually pleasing on the chart, but it does not equalise the asset prices to create an equalised starting point from which relative changes can be observed. Sorry for the trouble and thanks very much! Best regards, Espen