英文:
pinescript detect high and low iteratively (no lookbacks or ranges)
问题
我正在尝试通过迭代方式查找高点,而不使用内置的pivothigh/highest
,因为它们是通过指定要查找的一定范围的 bars 来工作的。相反,我正在定义起始 bar,从这个 bar 开始搜索前一个 bar 较低且下一个 bar 也较低的 bar。它应该将此 bar 检测为"Higher High"。然后,我将查看所有找到的高点之间的点差变化,并剔除那些超出某个阈值的不需要的 bars。
目前,我得到的是最高点之后的 bars(用蓝色十字标记),但我需要标有红色点的 bars。如何修复这个问题?
//@version=5
indicator("My script", overlay=true)
lineDate = input.time(timestamp("22 Feb 2023 18:00"), title="Line Location")
if barstate.islastconfirmedhistory
line.new(x1=lineDate, y1=high, x2=lineDate, y2=low, extend=extend.both,
color=color.orange, width=2, xloc=xloc.bar_time)
startbar = time == lineDate
start_bar = ta.change(startbar)
bars_since_new_index = ta.barssince(start_bar)
int hh = na
// 从起始 bar 迭代到最新 bar
for i = bars_since_new_index to 0
// 检查是否为较高点
next_high = high[i]
current_high = high[i+1]
previous_high = high[i+2]
if current_high > previous_high and next_high < current_high
hh := i+1
else
hh := na
plotshape(hh)
<details>
<summary>英文:</summary>
I am trying to find Highs iteratively, without using built in pivothigh/highest, because they are working by specifying a range of bars to look for. Instead, I am defining the starting bar, from which I search for bar that has previous bar lower, and next bar lower. It should detect this bar as Higher High. Then I will see the pips change between all found highs, and cull unwanted bars if they are outside some threshold.
currently I am getting bars after the highest (marked in blue crosses), but I need the ones marked with red dots.
[![enter image description here][1]][1]
How can I fix that?
//@version=5
indicator("My script", overlay=true)
lineDate = input.time(timestamp("22 Feb 2023 18:00"), title="Line Location")
if barstate.islastconfirmedhistory
line.new(x1=lineDate, y1=high, x2=lineDate, y2=low, extend=extend.both,
color=color.orange, width=2, xloc=xloc.bar_time)
startbar = time == lineDate
start_bar = ta.change(startbar)
bars_since_new_index = ta.barssince(start_bar)
int hh = na
// Iterate from start bar to the newest bar
for i = bars_since_new_index to 0
// Check for higher high
next_high = high[i]
current_high = high[i+1]
previous_high = high[i+2]
if current_high > previous_high and next_high < current_high
hh := i+1
else
hh := na
plotshape(hh)
[1]: https://i.stack.imgur.com/uFzLO.png
</details>
# 答案1
**得分**: 1
I think the problem is in your loop which execute on each bar and then give you the false impression of working.
I propose you to execute the loop only once all the bar are drawn and I used label.new instead of plotshape because you can use label in loop.
Here is the corrected code:
```pinescript
//@version=5
indicator("My script", overlay=true)
lineDate = input.time(timestamp("14 Mar 2023 18:00"), title="Line Location")
if barstate.islastconfirmedhistory
line.new(x1=lineDate, y1=high, x2=lineDate, y2=low, extend=extend.both,
color=color.orange, width=2, xloc=xloc.bar_time)
startbar = time == lineDate
start_bar = ta.change(startbar)
bars_since_new_index = ta.barssince(start_bar)
int hh = na
if barstate.islastconfirmedhistory
// Iterate from start bar to the newest bar
for i = bars_since_new_index to 0
// Check for higher high
next_high = high[i]
current_high = high[i+1]
previous_high = high[i+2]
if current_high > previous_high and next_high < current_high
label.new(bar_index - i - 1, close, "x", yloc=yloc.abovebar, style=label.style_none)
英文:
I think the problem is in your loop which execute on each bar and then give you the false impression of working.<br>
I propose you to execute the loop only once all the bar are drawn and I used label.new instead of plotshape because you can use label in loop.<br>
Here is the corrected code : <br>
//@version=5
indicator("My script", overlay=true)
lineDate = input.time(timestamp("14 Mar 2023 18:00"), title="Line Location")
if barstate.islastconfirmedhistory
line.new(x1=lineDate, y1=high, x2=lineDate, y2=low, extend=extend.both,
color=color.orange, width=2, xloc=xloc.bar_time)
startbar = time == lineDate
start_bar = ta.change(startbar)
bars_since_new_index = ta.barssince(start_bar)
int hh = na
if barstate.islastconfirmedhistory
// Iterate from start bar to the newest bar
for i = bars_since_new_index to 0
// Check for higher high
next_high = high[i]
current_high = high[i+1]
previous_high = high[i+2]
if current_high > previous_high and next_high < current_high
label.new(bar_index - i - 1, close, "x", yloc=yloc.abovebar, style=label.style_none)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论