如何通过Pine中的手动布尔输入切换来更改背景颜色



我是Pine Script的新手,尝试根据布尔值更改图表的背景色,我可以通过输入设置手动设置为true/false(实时(。背景颜色应该只针对布尔值为true的条形图进行更改(参见示例(。

bool dummy_toggle = input(title="dummy_toggle", type=input.bool, defval=false)
BgBool = false
if dummy_toggle == true
BgBool := true
bgcolor(color=BgBool ? color.lime : na, transp=85)

我可能犯的错误是,我没有将bool值指定给特定的bar(series(。我不知道怎么解决这个问题。有人能帮我吗?

您正朝着正确的方向前进。您案例中的脚本应该是这样的。

bool dummy_toggle = input(title="dummy_toggle", type=input.bool, defval=false)
BgBool = false
if condition_for_background_color == true
BgBool := true
bgcolor(color=(dummy_toggle and BgBool)? color.lime : na, transp=85)

新年快乐!

这是我的示例代码:

//@version=4
study("Test_Trade-ON-OFF-switch_bgcolor", overlay = true)
//GENERAL VARIABLES
//switch to manually turn the active trading ON and OFF
bool trade_on_off_switch = input(title="dummy_trade_on_off_switch", type=input.bool, defval=false)
//color background green for bars where active trading is ON (i.e. trade_on_off_switch == true)
color plotColor = color.white
BgBool = false
if trade_on_off_switch == true
BgBool := true
bgcolor(color=(trade_on_off_switch and BgBool)? color.green : na, transp=85)

此脚本有效。

//@version=4
study("Test_Trade-ON-OFF-switch_bgcolor", overlay = true)
//GENERAL VARIABLES
//switch to manually turn the active trading ON and OFF
bool trade_on_off_switch = input(title="dummy_trade_on_off_switch", type=input.bool, defval=true)
//color background green for bars where active trading is ON (i.e. trade_on_off_switch == true)
color plotColor = color.white
p = stoch(close, high, low, 14)
BgBool = false
if cross(p, 50) // e.g. crossing stochastic with level 50 
BgBool := true
hline(50,color=color.black)
bgcolor(color=(trade_on_off_switch and BgBool)? color.green : na, transp=50)
plot(p, color=color.blue)

最新更新