简体   繁体   中英

Pine Script (TradingView) color above Highest High

I'm trying to write a code that will color all the bars that meet the following conditions:
1. their closing price is above 20-period Moving Average
AND
2. they close above Highest High price from given period (say 20 days)

This is my current code:

hp1 = highest(high, 20) //HH from given period
myMA = sma(close, 20) //my moving average to plot
plot(myMA)

myCond = close > hp1 and close > myMA 
barcolor(myCond? yellow: na) //if condition is met

The code seems to be logical, but doesn't work. Any suggestions?

I've studied your script and have identified the following issues...

  1. Your color condition is always false
  2. You are assigning the color incorrectly

Your color condition is always false

This is because your highest high logic includes the current close in question. It is impossible for the close to be higher than the highest high, because it is the highest high.

The solution is to append an index value to the highest function's response, to look at the previous period's highest high.

hp1 = highest(high, 20)[1]

or you could leave the assignment as is and append the index when you use it

myCond = close > hp1[1] and close > myMA

I invite you to refer to the series operator .

You are assigning the color incorrectly

You need to move the conditional color logic above the plot function. Additionally you need to pass it in as a parameter to the plot function. I've included the complete solution below.

Edit: Upon further review, you can alter the color, the way you originally did. You just taught me something. Cheers!

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM