简体   繁体   English

Pine:如何根据条件设置背景颜色

[英]Pine: How to set background color based on a condition

I'm trying to have the entire background colored on a condition (eg green) if the last close is above the close 50 bars back and red if it's below.如果最后收盘价高于收盘价 50 条,我试图将整个背景着色为某种条件(例如绿色),如果低于收盘价则为红色。
I've tried different approaches and I can change the color, but it doesn't end up coloring the entire background, but only certain areas.我尝试了不同的方法,我可以改变颜色,但它最终不会为整个背景着色,而只是为某些区域着色。

My script:我的脚本:

bgcolor(close[50] >= open[1] ? color.red : color.green, transp=70)

split colors in background在后台拆分 colors

In this case the entire background should be red, since the last close is below the close 50 bars back (indicated by the yellow label).在这种情况下,整个背景应该是红色的,因为最后收盘价低于收盘价后 50 根柱线(由黄色标签指示)。
Any ideas on what I need to change?关于我需要改变什么的任何想法?

This will color the background on your condition.这将根据您的情况为背景着色。 It uses a very wide line to do it, and because of that the indicator is occupying all the background, so some chart functions like the measuring tool cannot be used with Shift-Click, but it will work if you select its tool explicitly.它使用很宽的线来做,因为指标占据了所有的背景,所以像测量工具这样的图表功能不能与 Shift-Click 一起使用,但是如果你明确地使用它的工具 select 它将起作用。

You can play with the position and width of the background if you don't want it to cover the whole chart.如果您不希望它覆盖整个图表,您可以使用 position 和背景宽度。

The background is very light.背景非常浅。 If you want to change its brightness, you'll need to play with the transparency in the two color.new() calls, as it cannot be controlled from an input:如果你想改变它的亮度,你需要在两个color.new()调用中使用透明度,因为它不能通过输入来控制:

//@version=4
study("", "", true)

offsetCalc  = input(50,     "Close lookback", minval = 2)
offstBg     = input(100,    "Background: Horizontal Offset to its Center", minval = 0, step = 5)
lineWidth   = input(10000,  "Background: Width", minval = 0, step = 100)

condUp      = barstate.islast and close[1] > close[offsetCalc]
condDn      = barstate.islast and close[1] < close[offsetCalc]
c_lineColor = condUp ? color.new(color.green, 97) : condDn ? color.new(color.maroon, 97) : na

if barstate.islast
    var line bg = na
    line.delete(bg)
    bg := line.new(bar_index[offstBg], low - tr, bar_index[offstBg], high + tr, color = c_lineColor, extend = extend.both, width = lineWidth)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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