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

how to draw horizontal lines and show price tag for each line?


I have my own calculation method to calculate some pressure and support prices, and I want to draw the horizontal lines of these pressure and support prices, as well as showing the price for each line. Below is what i want(it is tradingview built-in Fibonacci retracement lines): what I want is showing lines with price tags for each line

and my own code is like this:

//@version=5
indicator(title="testing-pressure and support", overlay=true)

//中心价和 K 线振幅
float mid_price = (high + low) / 2
float diff      = high - low 

//support price
float support1  = mid_price - 0.618 * diff
float support2  = mid_price - 1 * diff

//压力位
float pressure1 = mid_price + 0.618 * diff
float pressure2 = mid_price + 1 * diff

//是否显示支撑位和压力位
show_support = input.bool(title="显示支撑位", defval=true) 
show_pressure = input.bool(title="显示压力位", defval=true) 
show_mid_price = input.bool(title="显示中心价", defval=true) 

//绘制支撑位和压力位
line_length = input.int(defval=10, title="支撑/压力线的长度")

if barstate.islast
    line.new(bar_index-1, support1[1], bar_index+line_length,support1[1], color = show_support ? color.green : na, width=1)
    line.new(bar_index-1, support2[1], bar_index+line_length,support2[1], color = show_support ? color.green : na, width=1)
    
    line.new(bar_index-1, pressure1[1], bar_index+line_length,pressure1[1], color = show_support ? color.red : na, width=1)
    line.new(bar_index-1, pressure2[1], bar_index+line_length,pressure2[1], color = show_support ? color.red : na, width=1)
    
    line.new(bar_index-1, mid_price[1], bar_index+line_length, mid_price[1], color = show_mid_price ? color.yellow : na, width = 1)

The code works like below: display result of my own code

The lines are drawn and displayed successfully but I don't know how to put a price tag for each line like the above mentioned picture showing. So can you help me on this?

Apart from above question, I also have another demand:let me select a bar's low point to another bar's high point, it will be a period which includes maybe 5 bars or 8 bars or 20 bars... in this period, there is also high/low/open/close. I want to draw some lines base on the period high/period low/period open/period close. How to achieve it? Thank you!


Solution

  • I have added numbers to be on top of your lines:

    //@version=5
    indicator(title="Testing", overlay=true)
    
    //中心价和 K 线振幅
    float mid_price = (high + low) / 2
    float diff      = high - low 
    
    //support price
    float support1  = mid_price - 0.618 * diff
    float support2  = mid_price - 1 * diff
    
    //压力位
    float pressure1 = mid_price + 0.618 * diff
    float pressure2 = mid_price + 1 * diff
    
    //是否显示支撑位和压力位
    show_support = input.bool(title="显示支撑位", defval=true) 
    show_pressure = input.bool(title="显示压力位", defval=true) 
    show_mid_price = input.bool(title="显示中心价", defval=true)
    
    //绘制支撑位和压力位
    line_length = input.int(defval=10, title="支撑/压力线的长度")
    
    if barstate.islast
        if show_support
            line.new(bar_index-1, support1[1], bar_index+line_length, support1[1], color=color.green, width=1)
            label.new(bar_index+line_length, support1[1], text=str.tostring(support1[1]), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.green, textcolor=color.green, size=size.small)
    
            line.new(bar_index-1, support2[1], bar_index+line_length, support2[1], color=color.green, width=1)
            label.new(bar_index+line_length, support2[1], text=str.tostring(support2[1]), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.green, textcolor=color.green, size=size.small)
    
        if show_pressure
            line.new(bar_index-1, pressure1[1], bar_index+line_length, pressure1[1], color=color.red, width=1)
            label.new(bar_index+line_length, pressure1[1], text=str.tostring(pressure1[1]), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.red, textcolor=color.red, size=size.small)
    
            line.new(bar_index-1, pressure2[1], bar_index+line_length, pressure2[1], color=color.red, width=1)
            label.new(bar_index+line_length, pressure2[1], text=str.tostring(pressure2[1]), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.red, textcolor=color.red, size=size.small)
        
        if show_mid_price
            line.new(bar_index-1, mid_price[1], bar_index+line_length, mid_price[1], color=color.yellow, width=1)
            label.new(bar_index+line_length, mid_price[1], text=str.tostring(mid_price[1]), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.yellow, textcolor=color.yellow, size=size.small)
    

    Which produces:

    enter image description here

    If you wish, you can add different styles of labels to the lines.

    As for drawing lines between the high and low (and other points), I put this in your last question - will it not suffice? You could apply the logic to draw lines between any two points:

    //@version=5
    indicator(title="Testing", overlay=true)
    
    if barstate.islast and bar_index >= 3
        line.new(bar_index - 4, high[4], bar_index, low, width=1, color=color.blue)
    

    Which produces:

    enter image description here