admin管理员组

文章数量:1123791

I am trying to create a pine script that draws a horizontal ray for the current monthly high and adds the text "CMH" on the right side of the screen (at the end of the extended line) (see screenshot). CMH

For the life of me, I can't seem to find a simple way to do this as an argument of the line.new() function. And the label.new() function doesn't seem to give me the same result.

Any help is appreciated!

//@version=5
indicator(title="Current Month High", overlay=true)

// Initialize variable to hold the current month's high
var float currentMonthHigh = na
var int currentMonthBarIndex = na

// Update the current month's high and bar index
if (month(time) != month(time[1]))
    // If it's a new month, reset the high and store the current bar index
    currentMonthHigh := high
    currentMonthBarIndex := bar_index
else
    // If still in the same month, update the maximum high and bar index
    currentMonthHigh := math.max(currentMonthHigh, high)
    currentMonthBarIndex := bar_index

// Draw the high line only when we are at the last bar of the month
if (month(time) != month(time[1]) and bar_index == last_bar_index)
    // Use the current month's high directly for the line's starting point
    line.new(currentMonthBarIndex, currentMonthHigh, currentMonthBarIndex + 1, currentMonthHigh, color=color.aqua, width=1, extend=extend.right)

I am trying to create a pine script that draws a horizontal ray for the current monthly high and adds the text "CMH" on the right side of the screen (at the end of the extended line) (see screenshot). CMH

For the life of me, I can't seem to find a simple way to do this as an argument of the line.new() function. And the label.new() function doesn't seem to give me the same result.

Any help is appreciated!

//@version=5
indicator(title="Current Month High", overlay=true)

// Initialize variable to hold the current month's high
var float currentMonthHigh = na
var int currentMonthBarIndex = na

// Update the current month's high and bar index
if (month(time) != month(time[1]))
    // If it's a new month, reset the high and store the current bar index
    currentMonthHigh := high
    currentMonthBarIndex := bar_index
else
    // If still in the same month, update the maximum high and bar index
    currentMonthHigh := math.max(currentMonthHigh, high)
    currentMonthBarIndex := bar_index

// Draw the high line only when we are at the last bar of the month
if (month(time) != month(time[1]) and bar_index == last_bar_index)
    // Use the current month's high directly for the line's starting point
    line.new(currentMonthBarIndex, currentMonthHigh, currentMonthBarIndex + 1, currentMonthHigh, color=color.aqua, width=1, extend=extend.right)
Share Improve this question asked yesterday Alex MunteanuAlex Munteanu 1 New contributor Alex Munteanu is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • Your code has "hallucinations". AI-generated code is inconsistent, buggy, and unreliable. Artificial Intelligence is very useful in many aspects, but it encodes Pinescript very poorly (for now...). – Gu5tavo71 Commented 16 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

the last if is not necessary

try this:

//@version=5
indicator(title="Current Month High", overlay=true)

// Initialize variable to hold the current month's high
var float currentMonthHigh = na
var int currentMonthBarIndex = na
var line lineMonthHigh = line.new(na, na, na, na)

// Update the current month's high and bar index
if (month(time) != month(time[1]))
    // If it's a new month, reset the high and store the current bar index
    currentMonthHigh := high
    currentMonthBarIndex := bar_index
    line.delete(lineMonthHigh)
    lineMonthHigh := line.new(currentMonthBarIndex, currentMonthHigh, bar_index, currentMonthHigh, extend = extend.right)
else
    // If still in the same month, update the maximum high and bar index
    currentMonthHigh := math.max(currentMonthHigh, high)
    line.set_xy1(lineMonthHigh, currentMonthBarIndex, currentMonthHigh)
    line.set_xy2(lineMonthHigh, bar_index,            currentMonthHigh)

本文标签: tradingview apiPine Script to create horizontal rays with textStack Overflow