如何按条件[3]找到前一个交叉口的条数?

huangapple go评论54阅读模式
英文:

How to find the number of bars of the previous intersection by the condition [3]?

问题

图片中的问题在红色标注中

我不知道如何找到前一个交叉的长度。看看上面的图片,你会立刻明白一切。是否可以通过函数 ta.barssince 找到条数?

indicator(title="OHLC", shorttitle="Close", overlay=true, max_bars_back=100)

a = 44967
plot(a, '1', color.white, 3)
var int r = 0
var x = 0
var int t = 0
var int p = 0
var int p1 = 0
var int p2 = 0

var float f = 0
var l = 0
var l1 = 0
var l2 = 0

isCond11 = ta.crossover(close, a)
isCond21 = ta.crossunder(close, a)
isCond1 = ta.cross(a, close)

x := isCond11 ? 1 : isCond21[1] ? 0 : nz(x[1])
r := isCond21 ? 1 : 0

l := ta.barssince(isCond11) + 1
l1 := ta.barssince(isCond21) + 1

p := int(math.max(1, l))
p1 := int(math.max(1, l1))

//highp = ta.highest(close, int(math.max(1, nz(p))))
p2 := l1[1] - l[1]
if isCond21
    label.new(bar_index, close, str.tostring(l[1]), color = color.green)
    label.new(bar_index, close-7000, str.tostring(p2), color = color.yellow)
英文:

The problem in the picture is indicated in red

I don't know how to find the length of the previous intersection. Look at the picture above and you will immediately understand everything. Is it possible to find the number of bars through the function ta.barssince?

//@version=5
indicator(title="OHLC", shorttitle="Close",overlay = true, max_bars_back = 100)

a = 44967
plot(a, '1', color.white, 3)
var int r = 0
var x = 0
var int t = 0
var int p = 0
var int p1 = 0
var int p2 = 0

var float f = 0
var l = 0
var l1 = 0
var l2 = 0

isCond11 = ta.crossover(close, a)
isCond21 = ta.crossunder(close, a) 
isCond1 = ta.cross(a, close) 

x := isCond11 ? 1 : isCond21[1] ? 0 : nz(x[1])
r := isCond21 ? 1 : 0

l := ta.barssince(isCond11) + 1
l1 := ta.barssince(isCond21) + 1
 
p := int(math.max(1, l))
p1 := int(math.max(1, l1))

//highp = ta.highest(close, int(math.max(1, nz(p))))
p2 := l1[1] - l[1]
if isCond21
    label.new(bar_index, close, str.tostring(l[1]), color = color.green)
    label.new(bar_index, close-7000, str.tostring(p2), color = color.yellow)

答案1

得分: 0

//@version=5
indicator("我的脚本")

ema = ta.ema(close, 50)
cond = ta.crossover(close, ema)
var int bars = 0

plot(ema)
plot(close)

bgcolor(cond ? color.green : na)

if cond
    bars := ta.valuewhen(cond, bar_index, 0) - ta.valuewhen(cond, bar_index, 1)
    label.new(bar_index, high, str.tostring(bars))
英文:

Example:

//@version=5
indicator("My script")

ema =  ta.ema(close, 50)
cond = ta.crossover(close,ema)
var int bars = 0

plot(ema)
plot(close)

bgcolor(cond? color.green:na)

if cond 
    bars := ta.valuewhen(cond, bar_index,0) - ta.valuewhen(cond, bar_index,1)
    label.new(bar_index, high, str.tostring(bars))


huangapple
  • 本文由 发表于 2023年5月24日 18:47:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定