仅创建 select 组数字的平均值或中值
Create an average or median value of only select set of numbers
我用 tradingview 交易股票。我启动了一个简单的 Pinecode 来显示价格周期之间的百分比变化;但只有那些大于零的。我做到了那么远,但我现在想创建它们的平均值或中值。我该怎么做?
//@version=5
indicator(title="only+ROC", shorttitle="+ROC", format=format.price, precision=2, timeframe="",
timeframe_gaps=true)
length = input.int(1, minval=1)
source = input(close, "Source")
roc = 100 * (source - source[length])/source[length]
hline(0, color=#787B86, title="Zero Line")
plot(series=roc > 0 ? roc : na)
avg_len = input.int(10, title = "averaging length")
med_len = input.int(10, title = "median length")
avg = ta.sma(roc, avg_len)
med = ta.median(roc, med_len)
跟进问题:
//@version=5
indicator("ROC vals", overlay = true)
time_input_A = input.time(timestamp("20 Jul 2020 00:00 +0000"), title = "Start point", confirm = true)
time_input_B = input.time(timestamp("21 Jul 2020 00:00 +0000"), title = "End point", confirm = true)
length = input.int(1, title = "roc length")
time_A = math.min(time_input_A, time_input_B)
time_B = math.max(time_input_A, time_input_B)
in_range = time >= time_A and time <= time_B
post_bar = time > time_B and time[1] <= time_B
roc = ta.roc(close, length)
var float[] roc_vals = array.new_float()
var int start_index = na
var int end_index = na
var float start_close = na
var float end_close = na
if in_range
array.unshift(roc_vals, roc)
if na(start_index)
start_index := bar_index
if na(start_close)
start_close := close
float[] pos_roc_vals = array.new_float()
float[] neg_roc_vals = array.new_float()
if array.size(roc_vals) > 0
for i = 0 to array.size(roc_vals) - 1
roc_val_i = array.get(roc_vals, i)
if roc_val_i > 0
array.push(pos_roc_vals, roc_val_i)
if roc_val_i < 0
array.push(neg_roc_vals, roc_val_i)
if post_bar
pos_roc_avg = array.avg(pos_roc_vals)
pos_roc_med = array.median(pos_roc_vals)
neg_roc_avg = array.avg(neg_roc_vals)
neg_roc_med = array.median(neg_roc_vals)
roc_text = "+ROC\nAverage : " + str.tostring(pos_roc_avg) + "\nMedian : " + str.tostring(pos_roc_med) + "\n\n-ROC\nAverage : " + str.tostring(neg_roc_avg) + "\nMedian : " + str.tostring(neg_roc_med)
label.new(x = bar_index, y = close, style = label.style_label_left, color = color.yellow, textcolor = color.black, text = roc_text)
end_index := bar_index[1]
end_close := close[1]
box.new(left = start_index, top = math.max(start_close, end_close), right = end_index, bottom = math.min(start_close, end_close), border_color = color.gray, border_width = 2, border_style = line.style_dashed, bgcolor = na)
//@version=5
indicator(title="only+ROC", shorttitle="+ROC", format=format.price, precision=2, timeframe="",
timeframe_gaps=true)
length = input.int(1, minval=1)
source = input(close, "Source")
roc = 100 * (source - source[length])/source[length]
hline(0, color=#787B86, title="Zero Line")
plot(series=roc > 0 ? roc : na)
avg_len = input.int(10, title = "averaging length")
med_len = input.int(10, title = "median length")
avg = ta.sma(roc, avg_len)
med = ta.median(roc, med_len)
跟进问题:
//@version=5
indicator("ROC vals", overlay = true)
time_input_A = input.time(timestamp("20 Jul 2020 00:00 +0000"), title = "Start point", confirm = true)
time_input_B = input.time(timestamp("21 Jul 2020 00:00 +0000"), title = "End point", confirm = true)
length = input.int(1, title = "roc length")
time_A = math.min(time_input_A, time_input_B)
time_B = math.max(time_input_A, time_input_B)
in_range = time >= time_A and time <= time_B
post_bar = time > time_B and time[1] <= time_B
roc = ta.roc(close, length)
var float[] roc_vals = array.new_float()
var int start_index = na
var int end_index = na
var float start_close = na
var float end_close = na
if in_range
array.unshift(roc_vals, roc)
if na(start_index)
start_index := bar_index
if na(start_close)
start_close := close
float[] pos_roc_vals = array.new_float()
float[] neg_roc_vals = array.new_float()
if array.size(roc_vals) > 0
for i = 0 to array.size(roc_vals) - 1
roc_val_i = array.get(roc_vals, i)
if roc_val_i > 0
array.push(pos_roc_vals, roc_val_i)
if roc_val_i < 0
array.push(neg_roc_vals, roc_val_i)
if post_bar
pos_roc_avg = array.avg(pos_roc_vals)
pos_roc_med = array.median(pos_roc_vals)
neg_roc_avg = array.avg(neg_roc_vals)
neg_roc_med = array.median(neg_roc_vals)
roc_text = "+ROC\nAverage : " + str.tostring(pos_roc_avg) + "\nMedian : " + str.tostring(pos_roc_med) + "\n\n-ROC\nAverage : " + str.tostring(neg_roc_avg) + "\nMedian : " + str.tostring(neg_roc_med)
label.new(x = bar_index, y = close, style = label.style_label_left, color = color.yellow, textcolor = color.black, text = roc_text)
end_index := bar_index[1]
end_close := close[1]
box.new(left = start_index, top = math.max(start_close, end_close), right = end_index, bottom = math.min(start_close, end_close), border_color = color.gray, border_width = 2, border_style = line.style_dashed, bgcolor = na)