【问题标题】:writing a simple condition for pine script为 pine 脚本写一个简单的条件
【发布时间】:2021-01-24 09:18:28
【问题描述】:

我想为 pine 脚本写一个简单的条件,如果价格达到加号,则背景颜色将为绿色,如下图所示。但我写了它,它不是我所期望的它只为两支蜡烛制作背景,我想要它为所有接下来的蜡烛。我怎样才能做到这一点?谢谢This is the picture

//@version=4
study(title="My Indicator", overlay=true)
// Entry Point
entryPointLong = highest(high, 100)
entryPointShort = lowest(low, 100)
// Entry Point Condition
entryPointLongCond = close >= entryPointLong ? color.green : na
entryPointShortCond = close <= entryPointShort ? color.red : na
// Plot
plot(entryPointLong, title="Entry Long", style=plot.style_cross, linewidth=5, show_last=1, color=color.green)
plot(entryPointShort, title="Entry Short", style=plot.style_cross, linewidth=5, show_last=1, color=color.red)
// Background Color
bgcolor(entryPointLongCond, transp=70)
bgcolor(entryPointShortCond, transp=70)

更新代码:

//@version=4
study(title="My Indicator", overlay=true)
// Entry Point
entryPointLong = highest(high, 100)
entryPointShort = lowest(low, 100)
// Stop Loss
stopLossLongInput = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=3) * 0.01
stopLossLong = entryPointLong * (1 - stopLossLongInput)
// Entry Point Condition
entryPointLongTrue = close >= entryPointLong
if entryPointLongTrue
    entryPointLong := stopLossLong
    color.green
entryPointLongCond = close >= entryPointLong ? color.green : na
entryPointShortCond = close <= entryPointShort ? color.red : na
// Plot
plot(entryPointLong, title="Entry Long", style=plot.style_cross, linewidth=5, show_last=1, color=color.green)
plot(entryPointShort, title="Entry Short", style=plot.style_cross, linewidth=5, show_last=1, color=color.red)
plot(stopLossLong, title="Entry Short", style=plot.style_cross, linewidth=5, show_last=1, color=color.red)
// Background Color
bgcolor(entryPointLongCond, transp=70)
bgcolor(entryPointShortCond, transp=70)

【问题讨论】:

  • 我正在为 Trading View 平台编写指标。加号代表最高价的 200 支蜡烛。我想写一个条件,如果价格达到加号,背景颜色将为绿色,然后开多头头寸,但现在我只希望我的背景颜色为绿色。
  • 背景颜色只会使两根蜡烛变成绿色,但我正在尝试编写一个代码,将止损设置在进场点下方 3% 的位置,这样下一根蜡烛就会变成绿色。请帮我。谢谢

标签: function conditional-statements pine-script indicator


【解决方案1】:
//@version=4
study("My Script", overlay=true)

CO1 = "Crossover", CO2 = "Above"

priceLevel = input(1650)
condition  = input(CO1, options=[CO1, CO2])

var bool triggered = na

if condition == CO1
    triggered := crossover(close, priceLevel)
else if condition == CO2
    triggered := close > priceLevel
else
    triggered := false

bgcolor(triggered ? color.green : na)

【讨论】:

  • 谢谢,但没有帮助
  • 这个答案完全符合您在原始问题中提出的要求。但我看到你现在改变了它。一般来说,最好就它的问题提供反馈,而不是仅仅说它没有帮助。
  • 感谢您编写了代码,但这不是我想要的。我更新了我的代码,如果您将其复制并粘贴到您的 pine 编辑器中,它就可以工作。但问题是它只为一两根蜡烛制作背景,我想为下一根蜡烛制作背景,除非它达到低于入场点 3% 的止损
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2023-02-10
  • 2022-01-24
相关资源
最近更新 更多