英文:
count the number of green candles after each red candle untill we reach to the next green candle?
问题
我正在尝试找出每个红蜡烛之后连续出现的绿蜡烛数量,然后将该数量显示在相应红蜡烛的顶部。
例如,如果我有RGGRRGGGGRG
表示红(R)和绿(G)蜡烛,我希望在第一个红蜡烛上显示2
,在第二个红蜡烛上显示0
,在第三个红蜡烛上显示4
,在最后一个红蜡烛上显示1
。
如果有人能告诉我如何实现这个,我将不胜感激。
英文:
I am trying to find the number of consecutive green candles that happen after each red candle, then show that number on the top of the corresponding red candle.
For example, if I have RGGRRGGGGRG
for red (R) and green (G) candles, I am hoping to show 2
on the first red candles, 0
on the 2nd red candle, 4
on the 3rd red candle and 1
on the last red candle.
I appreciate it if someone can help me how I can do this.
答案1
得分: 1
Sure, here's the translated code:
你只需要两个 var
变量。一个用来跟踪连续的绿色条的数量,另一个用来跟踪红色条的索引,以便你可以将标签放在那里。
//@version=5
indicator("我的脚本", overlay=true, max_labels_count=500)
var green_cnt = 0
var red_candle_idx = bar_index
is_green_candle = (close >= open)
is_red_candle = not is_green_candle
if (is_red_candle)
label.new(red_candle_idx, high, str.tostring(green_cnt), yloc=yloc.abovebar, color=na, textcolor=color.white)
green_cnt := 0
red_candle_idx := bar_index
else
green_cnt := green_cnt + 1
英文:
All you need is two var
variables. One to keep track of the number of consecutive green bars, and one to keep track of the bar index of the red bar so you can place your label there.
//@version=5
indicator("My script", overlay=true,max_labels_count=500)
var green_cnt = 0
var red_candle_idx = bar_index
is_green_candle = (close >= open)
is_red_candle = not is_green_candle
if (is_red_candle)
label.new(red_candle_idx, high, str.tostring(green_cnt), yloc=yloc.abovebar, color=na, textcolor=color.white)
green_cnt := 0
red_candle_idx := bar_index
else
green_cnt := green_cnt + 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论