Search code examples
pine-scriptpine-script-v5tradingview-api

Draw multiple circle instead of histogram in Pine script


I want to create a specific type of histogram in Pine script (v5). I'm trying to draw a circle instead of the columns of a histogram; For example, if the value of the histogram is 4, I want to have 4 circles piled up each other. Right now I've tried these styles in plot drawing:

plot(... , style = plot.style_circles, linewidth = 3, ... )
plot(... , style = plot.style_columns, linewidth = 3, ... )
plot(... , style = plot.style_histogram, linewidth = 3, ... )

The first style gives me a circle, but just 1 circle at the exact value. For more clarification, see the below pictures;

What I get using style_circles

What I want

So is there any trick to draw my custom style? I have to mention that the drawing has some conditions. As you can see in the pictures, I used transparent color for the plot color based on some conditions.

UPDATE: I tried to use label instead of plot, with below method:

circle_text = ""
if prev_sum_index > 0
    for i = 1 to prev_sum_index
        circle_text += "◯\n"

label_color =  greenCond ? color.green : redCond ? color.red : color.rgb(255,255,255,100)
label_yloc = greenCond ? yloc.abovebar: redCond ? yloc.belowbar: yloc.price
label_id = label.new(bar_index - 1, 0, yloc = label_yloc, textcolor = label_color, style=label.style_none, text=circle_text)

the prev_sum_index is the value of the histogram, which is an integer value. But the ylocation is not quite right; I think the y location depends on price, but my indicator's overlay is false and there is no price!


Solution

  • Since the max number of circles is unknown, your only bet here is using labels. The disadvantage is, you will have a limited number of labels on your chart. Therefore, you should add max_labels_count=500 to your indicator() or strategy() function.

    You mentioned that you are using overlay=false so you need to find a base point for your first circle. I just used 0 but you can adapt that according to your needs. The rest of the circles will be on top of the first one adjusted by \n.

    Here is a function and test code for you:

    //@version=5
    indicator("My script", max_labels_count=500)
    
    f_draw_circles(n, col) =>
        s = ""
    
        if (n > 1)
            s := "◯"
            
            for i = 0 to n - 2
                s := s + "\n◯"
        else if (n > 0)
            s := "◯"
        
        label.new(bar_index, 0, s, color=color.new(color.white, 100), textcolor=col)
    
    n = (bar_index % 10)
    label_col = (bar_index % 2) == 0 ? color.green : color.red
    
    f_draw_circles(n, label_col)
    

    enter image description here